Package org.eclipse.rdf4j.common.io
Class IOUtil
- java.lang.Object
-
- org.eclipse.rdf4j.common.io.IOUtil
-
public class IOUtil extends Object
Utility 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 byte[]
readBytes(File file)
Reads all bytes from the specified file and returns them as a byte array.static byte[]
readBytes(InputStream in)
Reads all bytes from the supplied input stream and returns them as a byte array.static int
readBytes(InputStream in, byte[] byteArray)
Fills the supplied byte array with bytes read from the specified InputStream.static byte[]
readBytes(InputStream in, int maxBytes)
Reads at most maxBytes bytes from the supplied input stream and returns them as a byte array.static char[]
readChars(Reader r)
Reads all characters from the supplied reader and returns them.static int
readChars(Reader r, char[] charArray)
Fills the supplied character array with characters read from the specified Reader.static char[]
readChars(URL url)
Read the contents of a (unbuffered) resource into a character array.static Properties
readProperties(File propsFile)
Read properties from the specified file.static Properties
readProperties(File propsFile, Properties defaults)
Read properties from the specified file.static Properties
readProperties(InputStream in)
Read properties from the specified InputStream.static Properties
readProperties(InputStream in, Properties defaults)
Read properties from the specified InputStream.static String
readString(File file)
Read the contents as a string from the given (unbuffered) file.static String
readString(InputStream in)
Read the contents of an (unbuffered) input stream into a single string.static String
readString(Reader r)
Reads all characters from the supplied reader and returns them as a string.static String
readString(Reader r, int maxChars)
Reads a string of at most length maxChars from the supplied Reader.static String
readString(URL url)
Read the contents of a (unbuffered) resource into one single string.static long
transfer(InputStream in, File file)
Writes all bytes from an InputStream to a file.static long
transfer(InputStream in, OutputStream out)
Transfers all bytes that can be read from in to out.static long
transfer(Reader reader, File file)
Writes all characters from a Reader to a file using the default character encoding.static long
transfer(Reader in, Writer out)
Transfers all characters that can be read from in to out .static Reader
urlToReader(URL url)
Read the contents of a resource into a reader.static void
writeBytes(byte[] data, File file)
Write the contents of a byte array (unbuffered) to a file.static void
writeBytes(byte[] data, OutputStream out)
Write he contents of a byte array (unbuffered) to an output stream.static void
writeProperties(Properties props, File file, boolean includeDefaults)
Write the specified properties to the specified file.static void
writeProperties(Properties props, OutputStream out, boolean includeDefaults)
Write the specified properties to the specified output stream.static void
writeStream(InputStream in, File file)
Writes all data that can be read from the supplied InputStream to the specified file.static void
writeString(String contents, File file)
Write the contents of a string (unbuffered) to a file
-
-
-
Method Detail
-
readString
public static String readString(File file) throws IOException
Read the contents as a string from the given (unbuffered) file.- Parameters:
file
- file to read- Returns:
- content as one single string
- Throws:
IOException
-
readString
public static String readString(URL url) throws IOException
Read the contents of a (unbuffered) resource into one single string.- Parameters:
url
- url to get the data from- Returns:
- string
- Throws:
IOException
-
readString
public static String readString(InputStream in) throws IOException
Read the contents of an (unbuffered) input stream into a single string.- Parameters:
in
- input stream- Returns:
- string
- Throws:
IOException
-
readString
public static String readString(Reader r) throws IOException
Reads 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:
IOException
-
readString
public static String readString(Reader r, int maxChars) throws IOException
Reads 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:
IOException
-
readChars
public static char[] readChars(URL url) throws IOException
Read the contents of a (unbuffered) resource into a character array.- Parameters:
url
- url to get the data from- Returns:
- character array
- Throws:
IOException
-
readChars
public static char[] readChars(Reader r) throws IOException
Reads 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:
IOException
-
readChars
public static int readChars(Reader r, char[] charArray) throws IOException
Fills 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:
IOException
-
readBytes
public static byte[] readBytes(File file) throws IOException
Reads 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:
IOException
- If an I/O error occurred while reading from the file.IllegalArgumentException
- If the file size exceeds the maximum array length (larger thanInteger.MAX_VALUE
.
-
readBytes
public static byte[] readBytes(InputStream in) throws IOException
Reads 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:
IOException
-
readBytes
public static byte[] readBytes(InputStream in, int maxBytes) throws IOException
Reads 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:
IOException
-
readBytes
public static int readBytes(InputStream in, byte[] byteArray) throws IOException
Fills 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:
IOException
-
readProperties
public static Properties readProperties(File propsFile) throws IOException
Read properties from the specified file.- Parameters:
propsFile
- the file to read from- Returns:
- Properties loaded from the specified file
- Throws:
IOException
- when the file could not be read properly
-
readProperties
public static Properties readProperties(File propsFile, Properties defaults) throws IOException
Read 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:
IOException
- when the file could not be read properly
-
readProperties
public static Properties readProperties(InputStream in) throws IOException
Read 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:
IOException
- when the stream could not be read properly
-
readProperties
public static Properties readProperties(InputStream in, Properties defaults) throws IOException
Read 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:
IOException
- when the stream could not be read properly
-
writeProperties
public static void writeProperties(Properties props, File file, boolean includeDefaults) throws IOException
Write 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:
IOException
- when the properties could not be written to the file properly
-
writeProperties
public static void writeProperties(Properties props, OutputStream out, boolean includeDefaults) throws IOException
Write 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:
IOException
- when the properties could not be written to the output stream properly
-
writeStream
public static void writeStream(InputStream in, File file) throws IOException
Writes 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:
IOException
- If an I/O error occurred.
-
writeString
public static void writeString(String contents, File file) throws IOException
Write the contents of a string (unbuffered) to a file- Parameters:
contents
- string contents to writefile
- file to write to- Throws:
IOException
-
writeBytes
public static void writeBytes(byte[] data, File file) throws IOException
Write the contents of a byte array (unbuffered) to a file.- Parameters:
data
- data to writefile
- file- Throws:
IOException
-
writeBytes
public static void writeBytes(byte[] data, OutputStream out) throws IOException
Write he contents of a byte array (unbuffered) to an output stream.- Parameters:
data
- data to writeout
- file- Throws:
IOException
-
urlToReader
public static Reader urlToReader(URL url) throws IOException
Read the contents of a resource into a reader. Currently ignores HTTP Content-Encoding response header.- Parameters:
url
- url- Returns:
- reader
- Throws:
IOException
-
transfer
public static final long transfer(InputStream in, OutputStream out) throws IOException
Transfers 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:
IOException
-
transfer
public static final long transfer(InputStream in, File file) throws IOException
Writes 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:
IOException
- If an I/O error occurred while trying to write the data to the file.
-
transfer
public static final long transfer(Reader in, Writer out) throws IOException
Transfers 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:
IOException
-
transfer
public static final long transfer(Reader reader, File file) throws IOException
Writes 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:
IOException
- If an I/O error occurred while trying to write the data to the file.- See Also:
FileWriter
-
-