com.aliasi.util
Class Streams

java.lang.Object
  extended by com.aliasi.util.Streams

public class Streams
extends Object

Static utility methods for processing input and output streams, readers and writers.

Since:
LingPipe1.0
Version:
3.8.3
Author:
Bob Carpenter

Method Summary
static void closeInputSource(InputSource in)
          Close the specified input source.
static void closeInputStream(InputStream in)
          Close an input stream.
static void closeOutputStream(OutputStream out)
          Close an output stream.
static void closeReader(Reader reader)
          Close a reader.
static void closeWriter(Writer writer)
          Close a writer.
static void copy(InputStream in, OutputStream out)
          Copies the content of the input stream into the output stream.
static void copy(Reader reader, Writer writer)
          Copies the content of the reader into the writer.
static String getDefaultJavaCharset()
          Returns Java's canonical name of the default character set for the system's current default locale.
static byte[] toByteArray(InputStream in)
          Returns all the bytes read from the specified input stream as an array.
static char[] toCharArray(InputSource in)
          Reads the character content from the specified input source and returns it as a character array.
static char[] toCharArray(InputStream in, String charset)
          Reads the full contents of the specified input stream and returns the characters determined by the specified character set.
static char[] toCharArray(Reader reader)
          Reads the full contents of the specified reader and returns it as a character array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getDefaultJavaCharset

public static String getDefaultJavaCharset()
Returns Java's canonical name of the default character set for the system's current default locale. Note that this is returned as a Java charset name, not an official mime name.

Note: This method is available in the J2EE version of Java as javax.mail.internet.getDefaultJavaCharset().

Note 2: For example, the standard English install of Sun's J2SE 1.4.2 on windows sets the default character set to "Cp1252", the Windows variant of Latin1.

Returns:
The default charset for the current platform.

toByteArray

public static byte[] toByteArray(InputStream in)
                          throws IOException
Returns all the bytes read from the specified input stream as an array. This method does not close the input stream after it is done reading.

This method will block waiting for input.

Parameters:
in - Input stream from which to read bytes.
Returns:
Array of bytes read from stream.
Throws:
IOException - If the underlying stream throws an exception while reading.

toCharArray

public static char[] toCharArray(Reader reader)
                          throws IOException
Reads the full contents of the specified reader and returns it as a character array. This method does not close the reader.

Parameters:
reader - Reader from which to get characters.
Returns:
The characters read from the reader.
Throws:
IOException - If there is an underlying I/O exception.

toCharArray

public static char[] toCharArray(InputStream in,
                                 String charset)
                          throws IOException
Reads the full contents of the specified input stream and returns the characters determined by the specified character set.

Parameters:
in - The input stream from which to read the bytes.
charset - Character set used to convert bytes to characters.
Returns:
The characters read from the input stream.
Throws:
IOException - If there is an underlying I/O exception.

toCharArray

public static char[] toCharArray(InputSource in)
                          throws IOException
Reads the character content from the specified input source and returns it as a character array. If the input source has a specified character stream (a Reader), then that is used. If it has a specified byte stream (an InputStream), then that is used. If it has neither a character nor a byte stream, a byte stream is created from the system identifier (URL). For both specified and URL-constructed byte streams, the input source's specified character set will be used if it is specified.

I/O errors will arise from errors reading from a specified stream, from the character set conversion on a byte stream, or from errors forming or opening a URL specified as a system identifier.

Note that this method does not close the streams within the input source. See closeInputSource(InputSource).

Parameters:
in - Input source from which to read.
Returns:
Array of characters read from input source.
Throws:
IOException - If there is an I/O error reading.

copy

public static void copy(Reader reader,
                        Writer writer)
                 throws IOException
Copies the content of the reader into the writer. Blocks if the reader or writer block. Does not close the reader or writer when finished.

Parameters:
reader - Reader to copy from.
writer - Writer to copy to.
Throws:
IOException - If there is an underlying I/O exception.

copy

public static void copy(InputStream in,
                        OutputStream out)
                 throws IOException
Copies the content of the input stream into the output stream. Blocks if the input or output streams block. Does not close the streams.

Parameters:
in - Input stream to copy from.
out - Output stream to copy to.
Throws:
IOException - If there is an underlying I/O exception.

closeInputSource

public static void closeInputSource(InputSource in)
Close the specified input source. Any I/O exceptions will be caught and ignored. The input source may be null, as may its byte stream and/or character stream without throwing an exception.

Parameters:
in - Input source to close.

closeInputStream

public static void closeInputStream(InputStream in)
Close an input stream. Any I/O exceptions will be caught and ignored. Input stream may be null without generating an exception.

Parameters:
in - Input stream to close.

closeOutputStream

public static void closeOutputStream(OutputStream out)
Close an output stream. Any IO exceptions will be caught and logged as warnings. Output stream may be null without generating an exception.

Parameters:
out - Output stream to close.

closeReader

public static void closeReader(Reader reader)
Close a reader. Any IO exceptions will be caught and logged as warnings. Reader may be null without generating an exception.

Parameters:
reader - Reader to close.

closeWriter

public static void closeWriter(Writer writer)
Close a writer. Any IO exceptions will be caught and logged as warnings. Writer may be null without generating an exception.

Parameters:
writer - Writer to close.