org.avis.util
Class Streams

java.lang.Object
  extended by org.avis.util.Streams

public final class Streams
extends Object

General utilities for messing with I/O streams.

Author:
Matthew Phillips

Method Summary
static BufferedReader bufferedReaderFor(Reader reader)
          Generate a buffered reader wrapper for a reader, if it is not already one.
static byte[] bytesFrom(InputStream in)
          Read all the bytes from a stream and then close it.
static boolean close(Closeable in)
          Close an input stream.
static boolean eof(Reader in)
          Reader's and InputStream's (almost unbelievably) do not have a way to tell when the stream is at eof without modifying it.
static InputStream fileStream(File file)
          Open an input stream on a file.
static InputStream fileStream(String filename)
          Open an input stream on a file.
static Properties propertiesFrom(InputStream in)
          Load a set of java.util.Properties from an input stream and close it.
static String readLine(Reader in)
          Read a line from a Reader.
static InputStream resourceStream(String resource)
          Open an input stream on a resource.
static String stringFrom(InputStream stream)
          Read the entirety of a UTF-8 encoded input stream into a string.
static String stringFrom(Reader reader)
          Read the entirety of stream into a string.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

bufferedReaderFor

public static BufferedReader bufferedReaderFor(Reader reader)
Generate a buffered reader wrapper for a reader, if it is not already one.


bytesFrom

public static byte[] bytesFrom(InputStream in)
                        throws IOException
Read all the bytes from a stream and then close it.

Parameters:
in - The input stream to read.
Returns:
The bytes read.
Throws:
IOException - if an error occurs reading stream.
See Also:
stringFrom(InputStream)

eof

public static boolean eof(Reader in)
                   throws IOException
Reader's and InputStream's (almost unbelievably) do not have a way to tell when the stream is at eof without modifying it. This uses mark () and read () to non-destructively test for eof. The stream must support mark ().

Throws:
IOException

fileStream

public static InputStream fileStream(File file)
                              throws FileNotFoundException
Open an input stream on a file.

Throws:
FileNotFoundException

fileStream

public static InputStream fileStream(String filename)
                              throws FileNotFoundException
Open an input stream on a file.

Throws:
FileNotFoundException

propertiesFrom

public static Properties propertiesFrom(InputStream in)
                                 throws IOException
Load a set of java.util.Properties from an input stream and close it.

Throws:
IOException

readLine

public static String readLine(Reader in)
                       throws IOException
Read a line from a Reader. The reader must support mark () and reset (). Lines may be terminated by NL, CRLF/NL or NL/CRLF.

Parameters:
in - The reader.
Returns:
The line read, or null if at EOF.
Throws:
IOException - if an error occurs reading data.

resourceStream

public static InputStream resourceStream(String resource)
                                  throws FileNotFoundException
Open an input stream on a resource.

Throws:
FileNotFoundException

stringFrom

public static String stringFrom(InputStream stream)
                         throws IOException
Read the entirety of a UTF-8 encoded input stream into a string.

Throws:
IOException

stringFrom

public static String stringFrom(Reader reader)
                         throws IOException
Read the entirety of stream into a string.

Throws:
IOException
See Also:
bytesFrom(InputStream)

close

public static boolean close(Closeable in)
Close an input stream. Eats the (pointless, because there's nothing we can do) IO exception and returns true/false instead.



Copyright © 2008 Matthew Phillips. All Rights Reserved.