Package org.eclipse.rdf4j.common.io
Class IOUtil
- java.lang.Object
-
- org.eclipse.rdf4j.common.io.IOUtil
-
public class IOUtil extends java.lang.ObjectUtility methods for I/O working with Readers, Writers, InputStreams and OutputStreams.
-
-
Constructor Summary
Constructors Constructor Description IOUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidmain(java.lang.String[] args)private static bytereadByte(java.io.InputStream in)static byte[]readBytes(java.io.File file)Reads all bytes from the specified file and returns them as a byte array.static byte[]readBytes(java.io.InputStream in)Reads all bytes from the supplied input stream and returns them as a byte array.static intreadBytes(java.io.InputStream in, byte[] byteArray)Fills the supplied byte array with bytes read from the specified InputStream.static byte[]readBytes(java.io.InputStream in, int maxBytes)Reads at most maxBytes bytes from the supplied input stream and returns them as a byte array.static char[]readChars(java.io.Reader r)Reads all characters from the supplied reader and returns them.static intreadChars(java.io.Reader r, char[] charArray)Fills the supplied character array with characters read from the specified Reader.static char[]readChars(java.net.URL url)Read the contents of a (unbuffered) resource into a character array.private static java.io.CharArrayWriterreadFully(java.io.Reader r)Read into a character array writer.static java.util.PropertiesreadProperties(java.io.File propsFile)Read properties from the specified file.static java.util.PropertiesreadProperties(java.io.File propsFile, java.util.Properties defaults)Read properties from the specified file.static java.util.PropertiesreadProperties(java.io.InputStream in)Read properties from the specified InputStream.static java.util.PropertiesreadProperties(java.io.InputStream in, java.util.Properties defaults)Read properties from the specified InputStream.static java.lang.StringreadString(java.io.File file)Read the contents as a string from the given (unbuffered) file.static java.lang.StringreadString(java.io.InputStream in)Read the contents of an (unbuffered) input stream into a single string.static java.lang.StringreadString(java.io.Reader r)Reads all characters from the supplied reader and returns them as a string.static java.lang.StringreadString(java.io.Reader r, int maxChars)Reads a string of at most length maxChars from the supplied Reader.static java.lang.StringreadString(java.net.URL url)Read the contents of a (unbuffered) resource into one single string.static intreadVarInt(java.io.InputStream in)Read an variable length integer.static longtransfer(java.io.InputStream in, java.io.File file)Writes all bytes from an InputStream to a file.static longtransfer(java.io.InputStream in, java.io.OutputStream out)Transfers all bytes that can be read from in to out.static longtransfer(java.io.Reader reader, java.io.File file)Writes all characters from a Reader to a file using the default character encoding.static longtransfer(java.io.Reader in, java.io.Writer out)Transfers all characters that can be read from in to out .static java.io.ReaderurlToReader(java.net.URL url)Read the contents of a resource into a reader.static voidwriteBytes(byte[] data, java.io.File file)Write the contents of a byte array (unbuffered) to a file.static voidwriteBytes(byte[] data, java.io.OutputStream out)Write he contents of a byte array (unbuffered) to an output stream.static voidwriteProperties(java.util.Properties props, java.io.File file, boolean includeDefaults)Write the specified properties to the specified file.static voidwriteProperties(java.util.Properties props, java.io.OutputStream out, boolean includeDefaults)Write the specified properties to the specified output stream.static voidwriteStream(java.io.InputStream in, java.io.File file)Writes all data that can be read from the supplied InputStream to the specified file.static voidwriteString(java.lang.String contents, java.io.File file)Write the contents of a string (unbuffered) to a filestatic voidwriteVarInt(java.io.OutputStream out, int value)Write an variable length (non-negative) integer.
-
-
-
Method Detail
-
readString
public static java.lang.String readString(java.io.File file) throws java.io.IOExceptionRead the contents as a string from the given (unbuffered) file.- Parameters:
file- file to read- Returns:
- content as one single string
- Throws:
java.io.IOException
-
readString
public static java.lang.String readString(java.net.URL url) throws java.io.IOExceptionRead the contents of a (unbuffered) resource into one single string.- Parameters:
url- url to get the data from- Returns:
- string
- Throws:
java.io.IOException
-
readString
public static java.lang.String readString(java.io.InputStream in) throws java.io.IOExceptionRead the contents of an (unbuffered) input stream into a single string.- Parameters:
in- input stream- Returns:
- string
- Throws:
java.io.IOException
-
readString
public static java.lang.String readString(java.io.Reader r) throws java.io.IOExceptionReads all characters from the supplied reader and returns them as a string.- Parameters:
r- The Reader supplying the characters- Returns:
- A String containing all characters from the supplied reader.
- Throws:
java.io.IOException
-
readString
public static java.lang.String readString(java.io.Reader r, int maxChars) throws java.io.IOExceptionReads a string of at most length maxChars from the supplied Reader.- Parameters:
r- The Reader to read the string from.maxChars- The maximum number of characters to read.- Returns:
- A String of length maxChars, or less if the supplied Reader did not contain that much characters.
- Throws:
java.io.IOException
-
readChars
public static char[] readChars(java.net.URL url) throws java.io.IOExceptionRead the contents of a (unbuffered) resource into a character array.- Parameters:
url- url to get the data from- Returns:
- character array
- Throws:
java.io.IOException
-
readChars
public static char[] readChars(java.io.Reader r) throws java.io.IOExceptionReads all characters from the supplied reader and returns them.- Parameters:
r- The Reader supplying the characters- Returns:
- A character array containing all characters from the supplied reader.
- Throws:
java.io.IOException
-
readChars
public static int readChars(java.io.Reader r, char[] charArray) throws java.io.IOExceptionFills the supplied character array with characters read from the specified Reader. This method will only stop reading when the character array has been filled completely, or the end of the stream has been reached.- Parameters:
r- The Reader to read the characters from.charArray- The character array to fill with characters.- Returns:
- The number of characters written to the character array.
- Throws:
java.io.IOException
-
readBytes
public static byte[] readBytes(java.io.File file) throws java.io.IOExceptionReads all bytes from the specified file and returns them as a byte array.- Parameters:
file- The file to read.- Returns:
- A byte array containing all bytes from the specified file.
- Throws:
java.io.IOException- If an I/O error occurred while reading from the file.java.lang.IllegalArgumentException- If the file size exceeds the maximum array length (larger thanInteger.MAX_VALUE.
-
readBytes
public static byte[] readBytes(java.io.InputStream in) throws java.io.IOExceptionReads all bytes from the supplied input stream and returns them as a byte array.- Parameters:
in- The InputStream supplying the bytes.- Returns:
- A byte array containing all bytes from the supplied input stream.
- Throws:
java.io.IOException
-
readBytes
public static byte[] readBytes(java.io.InputStream in, int maxBytes) throws java.io.IOExceptionReads at most maxBytes bytes from the supplied input stream and returns them as a byte array.- Parameters:
in- The InputStream supplying the bytes.maxBytes- The maximum number of bytes to read from the input stream.- Returns:
- A byte array of size maxBytes if the input stream can produce that amount of bytes, or a smaller byte array containing all available bytes from the stream otherwise.
- Throws:
java.io.IOException
-
readBytes
public static int readBytes(java.io.InputStream in, byte[] byteArray) throws java.io.IOExceptionFills the supplied byte array with bytes read from the specified InputStream. This method will only stop reading when the byte array has been filled completely, or the end of the stream has been reached.- Parameters:
in- The InputStream to read the bytes from.byteArray- The byte array to fill with bytes.- Returns:
- The number of bytes written to the byte array.
- Throws:
java.io.IOException
-
readProperties
public static java.util.Properties readProperties(java.io.File propsFile) throws java.io.IOExceptionRead properties from the specified file.- Parameters:
propsFile- the file to read from- Returns:
- Properties loaded from the specified file
- Throws:
java.io.IOException- when the file could not be read properly
-
readProperties
public static java.util.Properties readProperties(java.io.File propsFile, java.util.Properties defaults) throws java.io.IOExceptionRead properties from the specified file.- Parameters:
propsFile- the file to read fromdefaults- the default properties to use- Returns:
- Properties loaded from the specified file
- Throws:
java.io.IOException- when the file could not be read properly
-
readProperties
public static java.util.Properties readProperties(java.io.InputStream in) throws java.io.IOExceptionRead properties from the specified InputStream.- Parameters:
in- the stream to read from. The stream will be closed by this method.- Returns:
- Properties loaded from the specified stream. The stream will be closed by this method.
- Throws:
java.io.IOException- when the stream could not be read properly
-
readProperties
public static java.util.Properties readProperties(java.io.InputStream in, java.util.Properties defaults) throws java.io.IOExceptionRead properties from the specified InputStream.- Parameters:
in- the stream to read from. The stream will be closed by this method.defaults- the default properties- Returns:
- Properties loaded from the specified stream. The stream will be closed by this method.
- Throws:
java.io.IOException- when the stream could not be read properly
-
writeProperties
public static void writeProperties(java.util.Properties props, java.io.File file, boolean includeDefaults) throws java.io.IOExceptionWrite the specified properties to the specified file.- Parameters:
props- the properties to writefile- the file to write toincludeDefaults- true when default values need to be included- Throws:
java.io.IOException- when the properties could not be written to the file properly
-
writeProperties
public static void writeProperties(java.util.Properties props, java.io.OutputStream out, boolean includeDefaults) throws java.io.IOExceptionWrite the specified properties to the specified output stream.- Parameters:
props- the properties to writeout- the output stream to write toincludeDefaults- true if default values need to be included- Throws:
java.io.IOException- when the properties could not be written to the output stream properly
-
writeStream
public static void writeStream(java.io.InputStream in, java.io.File file) throws java.io.IOExceptionWrites all data that can be read from the supplied InputStream to the specified file.- Parameters:
in- An InputStream.file- The file to write the data to.- Throws:
java.io.IOException- If an I/O error occurred.
-
writeString
public static void writeString(java.lang.String contents, java.io.File file) throws java.io.IOExceptionWrite the contents of a string (unbuffered) to a file- Parameters:
contents- string contents to writefile- file to write to- Throws:
java.io.IOException
-
writeBytes
public static void writeBytes(byte[] data, java.io.File file) throws java.io.IOExceptionWrite the contents of a byte array (unbuffered) to a file.- Parameters:
data- data to writefile- file- Throws:
java.io.IOException
-
writeBytes
public static void writeBytes(byte[] data, java.io.OutputStream out) throws java.io.IOExceptionWrite he contents of a byte array (unbuffered) to an output stream.- Parameters:
data- data to writeout- file- Throws:
java.io.IOException
-
urlToReader
public static java.io.Reader urlToReader(java.net.URL url) throws java.io.IOExceptionRead the contents of a resource into a reader. Currently ignores HTTP Content-Encoding response header.- Parameters:
url- url- Returns:
- reader
- Throws:
java.io.IOException
-
readFully
private static java.io.CharArrayWriter readFully(java.io.Reader r) throws java.io.IOExceptionRead into a character array writer.- Parameters:
r- input reader- Returns:
- character array writer
- Throws:
java.io.IOException
-
transfer
public static final long transfer(java.io.InputStream in, java.io.OutputStream out) throws java.io.IOExceptionTransfers all bytes that can be read from in to out.- Parameters:
in- The InputStream to read data from.out- The OutputStream to write data to.- Returns:
- The total number of bytes transfered.
- Throws:
java.io.IOException
-
transfer
public static final long transfer(java.io.InputStream in, java.io.File file) throws java.io.IOExceptionWrites all bytes from an InputStream to a file.- Parameters:
in- The InputStream containing the data to write to the file.file- The file to write the data to.- Returns:
- The total number of bytes written.
- Throws:
java.io.IOException- If an I/O error occurred while trying to write the data to the file.
-
transfer
public static final long transfer(java.io.Reader in, java.io.Writer out) throws java.io.IOExceptionTransfers all characters that can be read from in to out .- Parameters:
in- The Reader to read characters from.out- The Writer to write characters to.- Returns:
- The total number of characters transfered.
- Throws:
java.io.IOException
-
transfer
public static final long transfer(java.io.Reader reader, java.io.File file) throws java.io.IOExceptionWrites all characters from a Reader to a file using the default character encoding.- Parameters:
reader- The Reader containing the data to write to the file.file- The file to write the data to.- Returns:
- The total number of characters written.
- Throws:
java.io.IOException- If an I/O error occurred while trying to write the data to the file.- See Also:
FileWriter
-
writeVarInt
public static void writeVarInt(java.io.OutputStream out, int value) throws java.io.IOExceptionWrite an variable length (non-negative) integer.
The variable length integer encoding (also know as varint and vint) writes non-negative / unsigned integers in a minimal number of bytes (binary octets). The encoding uses the most significant bit is used to indicate whether another varint byte follows. The remaining 7 bits of every octet are used to store the binary representation of the actual integer value. Note that the least significant bytes of the integer value are stored before more significate bytes.
The table below shows a few examples of decimals encoded both as a 32-bit integer and as variable length binary:
decimal | 32-bit integer | variable length binary 0 | 00000000 00000000 00000000 00000000 | 00000000 (1 byte) 1 | 00000000 00000000 00000000 00000001 | 00000001 (1 byte) 42 | 00000000 00000000 00000000 00101010 | 00101010 (1 byte) 421 | 00000000 00000000 00000001 10100101 | 10100101 00000011 (2 bytes) 100000 | 00000000 00000001 10000110 10100000 | 10100000 10001101 00000110 (3 bytes)- Parameters:
out- TheOutputStreamto write to.value- Theintvalue to write.- Throws:
java.io.IOException- If an error occurred while writing the integer.
-
main
public static void main(java.lang.String[] args) throws java.io.IOException- Throws:
java.io.IOException
-
readVarInt
public static int readVarInt(java.io.InputStream in) throws java.io.IOExceptionRead an variable length integer. SeewriteVarInt(OutputStream, int)for encoding details.- Parameters:
in- TheInputStreamto read from.- Returns:
- The integer read.
- Throws:
java.io.IOException- If an error occurred while reading the integer.
-
readByte
private static byte readByte(java.io.InputStream in) throws java.io.IOException- Throws:
java.io.IOException
-
-