org.semanticdesktop.aperture.util
Class IOUtil

java.lang.Object
  extended by org.semanticdesktop.aperture.util.IOUtil

public class IOUtil
extends Object

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


Constructor Summary
IOUtil()
           
 
Method Summary
static int fillByteArray(InputStream in, byte[] byteArray)
          Fills the supplied byte array with bytes read from the specified InputStream.
static int fillCharArray(Reader r, char[] charArray)
          Fills the supplied character array with characters read from the specified Reader.
static byte[] readBytes(InputStream in)
          Reads all bytes from the supplied input stream and returns them as a byte array.
static byte[] readBytes(InputStream in, int maxBytes)
          Reads bytes from the supplied input stream up until a maximum number of bytes has been reached and returns them as a byte array.
static char[] readChars(Reader r)
          Reads all characters from the supplied reader and returns them as an array
static String readString(File file)
          Reads the contents from the given file as a String.
static String readString(InputStream in)
          Read the contents of the given stream as a String, using the default Charset.
static String readString(InputStream in, Charset charset)
          Read the contents of the given stream as a String, using the given Charset.
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)
          Reads the contents from the given URL as a String.
static String rollingHash(InputStream stream)
           
static Reader urlToReader(URL url)
          Creates a Reader accessing the contents of the specified URL.
static void writeStream(InputStream in, File file)
          Writes all data that can be read from the supplied InputStream to the specified file.
static void writeStream(InputStream in, OutputStream out)
          Writes all data that can be read from the supplied InputStream to the supplied OutputStream.
static void writeString(String contents, File file)
          Writes the contents of the specified String to the specified File.
static void writeString(String contents, String filename)
          Write the contents of the specified contents String to a file with the specified file name.
 
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

readChars

public static char[] readChars(Reader r)
                        throws IOException
Reads all characters from the supplied reader and returns them as an array

Throws:
IOException

readString

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

Throws:
IOException

readString

public static String readString(URL url)
                         throws IOException
Reads the contents from the given URL as a String.

Throws:
IOException

readString

public static String readString(InputStream in)
                         throws IOException
Read the contents of the given stream as a String, using the default Charset.

Throws:
IOException

readString

public static String readString(InputStream in,
                                Charset charset)
                         throws IOException
Read the contents of the given stream as a String, using the given Charset.

Throws:
IOException

readString

public static String readString(Reader r)
                         throws IOException
Reads all characters from the supplied reader and returns them as a String.

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

fillCharArray

public static int fillCharArray(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 when 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(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 bytes from the supplied input stream up until a maximum number of bytes has been reached 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 array containing all available bytes from the stream otherwise.
Throws:
IOException

rollingHash

public static String rollingHash(InputStream stream)
                          throws IOException
Throws:
IOException

fillByteArray

public static int fillByteArray(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 when 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

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.

Throws:
IOException

writeStream

public static void writeStream(InputStream in,
                               OutputStream out)
                        throws IOException
Writes all data that can be read from the supplied InputStream to the supplied OutputStream.

Throws:
IOException

writeString

public static void writeString(String contents,
                               File file)
                        throws IOException
Writes the contents of the specified String to the specified File.

Throws:
IOException

writeString

public static void writeString(String contents,
                               String filename)
                        throws IOException
Write the contents of the specified contents String to a file with the specified file name.

Throws:
IOException

urlToReader

public static Reader urlToReader(URL url)
                          throws IOException
Creates a Reader accessing the contents of the specified URL.

Throws:
IOException


Copyright © 2010 Aperture Development Team. All Rights Reserved.