info.aduna.io
Class IOUtil

java.lang.Object
  extended by info.aduna.io.IOUtil

public class IOUtil
extends Object

Utility methods for I/O working with Readers, Writers, InputStreams and OutputStreams.


Constructor Summary
IOUtil()
           
 
Method Summary
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)
           
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 file.
static String readString(InputStream in)
           
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)
           
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)
           
static void writeBytes(byte[] data, File file)
           
static void writeBytes(byte[] data, OutputStream out)
           
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)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IOUtil

public IOUtil()
Method Detail

readString

public static String readString(File file)
                         throws IOException
Read the contents as a string from the given file.

Throws:
IOException

readString

public static String readString(URL url)
                         throws IOException
Throws:
IOException

readString

public static String readString(InputStream in)
                         throws IOException
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
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 than Integer.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 from
defaults - 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 write
file - the file to write to
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 write
out - the output stream to write to
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
Throws:
IOException

writeBytes

public static void writeBytes(byte[] data,
                              File file)
                       throws IOException
Throws:
IOException

writeBytes

public static void writeBytes(byte[] data,
                              OutputStream out)
                       throws IOException
Throws:
IOException

urlToReader

public static Reader urlToReader(URL url)
                          throws IOException
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 occured 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 occured while trying to write the data to the file.
See Also:
FileWriter


Copyright © 2011 Aduna. All Rights Reserved.