org.jagatoo.util.streams
Class StreamUtils

java.lang.Object
  extended by org.jagatoo.util.streams.StreamUtils

public class StreamUtils
extends java.lang.Object

Contains static utility methods for Stream.

Author:
Marvin Froehlich (aka Qudus)

Field Summary
static int TRANSFER_BUFFER_SIZE
           
 
Method Summary
static byte[] buildByteArray(java.io.InputStream in)
          This calls buildByteArray(InputStream, int) with an initialSize of in.available().
static byte[] buildByteArray(java.io.InputStream in, int initialSize)
          Builds a byte-array from the given InputStream.
static java.lang.Boolean closeReader(java.io.Reader r)
          Closes the passed reader if non null ignoring theoretically irrelevant IOException.
static java.lang.Boolean closeStream(java.io.InputStream s)
          Closes the passed stream if non null ignoring theoretically irrelevant IOException.
static java.lang.Boolean closeStream(java.io.OutputStream s)
          Closes the passed stream if non null ignoring theoretically irrelevant IOException.
static java.lang.Boolean closeWriter(java.io.Writer w)
          Closes the passed writer if non null ignoring theoretically irrelevant IOException.
static java.lang.Boolean flushStream(java.io.OutputStream s)
          Flushes the given stream, if it is non null and ignores a rarely possible IOException.
static java.lang.Boolean flushWriter(java.io.Writer w)
          Flushes the given writer, if it is non null and ignores a rarely possible IOException.
static byte[] getBytesFromStream(java.io.InputStream in)
          Copies all bytes from the given InputStream into a new byte array.
static byte[] getBytesFromStream(java.io.InputStream in, boolean closeIn)
          Copies all bytes from the given InputStream into a new byte array.
static java.lang.String getStringFromStream(java.io.InputStream in)
          Copies all bytes from the given InputStream into a new String.
static java.lang.String getStringFromStream(java.io.InputStream in, boolean closeIn)
          Copies all bytes from the given InputStream into a new String.
static byte readByte(java.io.BufferedInputStream in)
          Reads one byte from the InputStream.
static byte readByte(java.io.InputStream in)
          Reads one byte from the InputStream.
static void readBytes(java.io.BufferedInputStream in, int bytesToRead, byte[] buffer, int bufferOffset)
          Reads bytesToRead bytes from the stream.
static void readBytes(java.io.InputStream in, int bytesToRead, byte[] buffer, int bufferOffset)
          Reads bytesToRead bytes from the stream.
static java.lang.String readCString(java.io.InputStream in, int maxLength, boolean alwaysReadMaxLength)
          Reads a String from the InputStream.
static int readInt(java.io.BufferedInputStream in)
          Reads one (signed) int from the stream.
static int readInt(java.io.InputStream in)
          Reads one (signed) int from the stream.
static short readShort(java.io.BufferedInputStream in)
          Reads two bytes from the InputStream stored in a short-value.
static short readShort(java.io.InputStream in)
          Reads two bytes from the InputStream stored in a short-value.
static int readSwappedInt(java.io.BufferedInputStream in)
          Reads one (signed) int from the stream.
static int readSwappedInt(java.io.InputStream in)
          Reads one (signed) int from the stream.
static short readSwappedShort(java.io.BufferedInputStream in)
          Reads two bytes from the InputStream stored in a short-value.
static short readSwappedShort(java.io.InputStream in)
          Reads two bytes from the InputStream stored in a short-value.
static int readSwappedUnsignedShort(java.io.BufferedInputStream in)
          Reads two bytes from the InputStream, convertes them to a short and stores them to an int to preserve the sign.
static int readSwappedUnsignedShort(java.io.InputStream in)
          Reads two bytes from the InputStream, convertes them to a short and stores them to an int to preserve the sign.
static short readUnsignedByte(java.io.BufferedInputStream in)
          Reads one unsigned byte from the InputStream stored in a short-value to preserve the sign.
static short readUnsignedByte(java.io.InputStream in)
          Reads one unsigned byte from the InputStream stored in a short-value to preserve the sign.
static int readUnsignedShort(java.io.BufferedInputStream in)
          Reads two bytes from the InputStream, convertes them to a short and stores them to an int to preserve the sign.
static int readUnsignedShort(java.io.InputStream in)
          Reads two bytes from the InputStream, convertes them to a short and stores them to an int to preserve the sign.
static void skipBytes(java.io.BufferedInputStream in, long toSkip)
          Skips and discards the given number of bytes from the given stream.
static void skipBytes(java.io.InputStream in, long toSkip)
          Skips and discards the given number of bytes from the given stream.
static long transferBytes(java.io.InputStream in, byte[] buffer, java.io.OutputStream out, boolean closeIn, boolean closeOut)
          Reads all bytes from the provided InputStream and writes them to the provided OutputStream.
static long transferBytes(java.io.InputStream in, java.io.OutputStream out)
          Reads all bytes from the provided InputStream and writes them to the provided OutputStream.
static long transferBytes(java.io.InputStream in, java.io.OutputStream out, boolean closeIn, boolean closeOut)
          Reads all bytes from the provided InputStream and writes them to the provided OutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TRANSFER_BUFFER_SIZE

public static final int TRANSFER_BUFFER_SIZE
See Also:
Constant Field Values
Method Detail

skipBytes

public static final void skipBytes(java.io.InputStream in,
                                   long toSkip)
                            throws java.io.IOException
Skips and discards the given number of bytes from the given stream.

Parameters:
in -
toSkip -
Throws:
java.io.IOException

skipBytes

public static final void skipBytes(java.io.BufferedInputStream in,
                                   long toSkip)
                            throws java.io.IOException
Skips and discards the given number of bytes from the given stream.

Parameters:
in -
toSkip -
Throws:
java.io.IOException

readByte

public static final byte readByte(java.io.InputStream in)
                           throws java.io.IOException
Reads one byte from the InputStream.

Parameters:
in -
Returns:
the read byte.
Throws:
java.io.IOException

readByte

public static final byte readByte(java.io.BufferedInputStream in)
                           throws java.io.IOException
Reads one byte from the InputStream.

Parameters:
in -
Returns:
the read byte.
Throws:
java.io.IOException

readUnsignedByte

public static final short readUnsignedByte(java.io.InputStream in)
                                    throws java.io.IOException
Reads one unsigned byte from the InputStream stored in a short-value to preserve the sign.

Parameters:
in -
Returns:
the read unsigned byte as a short.
Throws:
java.io.IOException

readUnsignedByte

public static final short readUnsignedByte(java.io.BufferedInputStream in)
                                    throws java.io.IOException
Reads one unsigned byte from the InputStream stored in a short-value to preserve the sign.

Parameters:
in -
Returns:
the read unsigned byte as a short.
Throws:
java.io.IOException

readShort

public static final short readShort(java.io.InputStream in)
                             throws java.io.IOException
Reads two bytes from the InputStream stored in a short-value.

Parameters:
in -
Returns:
the read short.
Throws:
java.io.IOException

readShort

public static final short readShort(java.io.BufferedInputStream in)
                             throws java.io.IOException
Reads two bytes from the InputStream stored in a short-value.

Parameters:
in -
Returns:
the read short.
Throws:
java.io.IOException

readUnsignedShort

public static final int readUnsignedShort(java.io.InputStream in)
                                   throws java.io.IOException
Reads two bytes from the InputStream, convertes them to a short and stores them to an int to preserve the sign.

Parameters:
in -
Returns:
the read unsigned short (as an int).
Throws:
java.io.IOException

readUnsignedShort

public static final int readUnsignedShort(java.io.BufferedInputStream in)
                                   throws java.io.IOException
Reads two bytes from the InputStream, convertes them to a short and stores them to an int to preserve the sign.

Parameters:
in -
Returns:
the read unsigned short (as an int).
Throws:
java.io.IOException

readSwappedShort

public static final short readSwappedShort(java.io.InputStream in)
                                    throws java.io.IOException
Reads two bytes from the InputStream stored in a short-value.

Parameters:
in -
Returns:
the read short.
Throws:
java.io.IOException

readSwappedShort

public static final short readSwappedShort(java.io.BufferedInputStream in)
                                    throws java.io.IOException
Reads two bytes from the InputStream stored in a short-value.

Parameters:
in -
Returns:
the read short.
Throws:
java.io.IOException

readSwappedUnsignedShort

public static final int readSwappedUnsignedShort(java.io.InputStream in)
                                          throws java.io.IOException
Reads two bytes from the InputStream, convertes them to a short and stores them to an int to preserve the sign.

Parameters:
in -
Returns:
the read unsigned short (as an int).
Throws:
java.io.IOException

readSwappedUnsignedShort

public static final int readSwappedUnsignedShort(java.io.BufferedInputStream in)
                                          throws java.io.IOException
Reads two bytes from the InputStream, convertes them to a short and stores them to an int to preserve the sign.

Parameters:
in -
Returns:
the read unsigned short (as an int).
Throws:
java.io.IOException

readInt

public static final int readInt(java.io.InputStream in)
                         throws java.io.IOException
Reads one (signed) int from the stream.

Parameters:
in -
Returns:
the read int.
Throws:
java.io.IOException

readInt

public static final int readInt(java.io.BufferedInputStream in)
                         throws java.io.IOException
Reads one (signed) int from the stream.

Parameters:
in -
Returns:
the read int.
Throws:
java.io.IOException

readSwappedInt

public static final int readSwappedInt(java.io.InputStream in)
                                throws java.io.IOException
Reads one (signed) int from the stream.

Parameters:
in -
Returns:
the read int.
Throws:
java.io.IOException

readSwappedInt

public static final int readSwappedInt(java.io.BufferedInputStream in)
                                throws java.io.IOException
Reads one (signed) int from the stream.

Parameters:
in -
Returns:
the read int.
Throws:
java.io.IOException

readBytes

public static final void readBytes(java.io.InputStream in,
                                   int bytesToRead,
                                   byte[] buffer,
                                   int bufferOffset)
                            throws java.io.IOException
Reads bytesToRead bytes from the stream.

Parameters:
in -
bytesToRead -
buffer -
bufferOffset -
Throws:
java.io.IOException

readBytes

public static final void readBytes(java.io.BufferedInputStream in,
                                   int bytesToRead,
                                   byte[] buffer,
                                   int bufferOffset)
                            throws java.io.IOException
Reads bytesToRead bytes from the stream.

Parameters:
in -
bytesToRead -
buffer -
bufferOffset -
Throws:
java.io.IOException

readCString

public static final java.lang.String readCString(java.io.InputStream in,
                                                 int maxLength,
                                                 boolean alwaysReadMaxLength)
                                          throws java.io.IOException
Reads a String from the InputStream. The string is expected to be 0-terminated.

Parameters:
in -
maxLength -
alwaysReadMaxLength -
Returns:
the read String.
Throws:
java.io.IOException

buildByteArray

public static final byte[] buildByteArray(java.io.InputStream in,
                                          int initialSize)
                                   throws java.io.IOException
Builds a byte-array from the given InputStream.
The byte-array is created with a size of initialSize and is enlarged on demand.
The InputStream is NOT closed at the end.

Parameters:
in - the InputStream to get data from
initialSize - the initial size of the output byte-array
Returns:
the filled and correctly sized byte-array
Throws:
java.io.IOException

buildByteArray

public static final byte[] buildByteArray(java.io.InputStream in)
                                   throws java.io.IOException
This calls buildByteArray(InputStream, int) with an initialSize of in.available().

Parameters:
in - the InputStream to get data from
Returns:
the filled and correctly sized byte-array
Throws:
java.io.IOException

closeStream

public static java.lang.Boolean closeStream(java.io.InputStream s)
Closes the passed stream if non null ignoring theoretically irrelevant IOException.

Parameters:
s - the stream to be closed. If null, this is noop
Returns:
null, if the stream is null, true, if the closing was successful, false otherwise.

closeStream

public static java.lang.Boolean closeStream(java.io.OutputStream s)
Closes the passed stream if non null ignoring theoretically irrelevant IOException. Make sure, the stream is flushed before.

Parameters:
s - the stream to be closed. If null, this is noop
Returns:
null, if the stream is null, true, if the closing was successful, false otherwise.

flushStream

public static java.lang.Boolean flushStream(java.io.OutputStream s)
Flushes the given stream, if it is non null and ignores a rarely possible IOException.

Parameters:
s - the strema to flush
Returns:
null, if the stream is null, true, if the closing was successful, false otherwise.

closeReader

public static java.lang.Boolean closeReader(java.io.Reader r)
Closes the passed reader if non null ignoring theoretically irrelevant IOException.

Parameters:
r - the reader to be closed. If null, this is noop
Returns:
null, if the reader is null, true, if the closing was successful, false otherwise.

closeWriter

public static java.lang.Boolean closeWriter(java.io.Writer w)
Closes the passed writer if non null ignoring theoretically irrelevant IOException. Make sure, the stream is flushed before.

Parameters:
w - the writer to be closed. If null, this is noop
Returns:
null, if the writer is null, true, if the closing was successful, false otherwise.

flushWriter

public static java.lang.Boolean flushWriter(java.io.Writer w)
Flushes the given writer, if it is non null and ignores a rarely possible IOException.

Parameters:
w - the writer to flush
Returns:
null, if the writer is null, true, if the closing was successful, false otherwise.

transferBytes

public static long transferBytes(java.io.InputStream in,
                                 byte[] buffer,
                                 java.io.OutputStream out,
                                 boolean closeIn,
                                 boolean closeOut)
                          throws java.io.IOException
Reads all bytes from the provided InputStream and writes them to the provided OutputStream.

Parameters:
in -
buffer -
out -
closeIn - if true the provided InputStream is closed after all.
closeOut - if true the provided OutputStream is closed after all.
Returns:
the number of bytes transfered.
Throws:
java.io.IOException

transferBytes

public static long transferBytes(java.io.InputStream in,
                                 java.io.OutputStream out,
                                 boolean closeIn,
                                 boolean closeOut)
                          throws java.io.IOException
Reads all bytes from the provided InputStream and writes them to the provided OutputStream.

Parameters:
in -
out -
closeIn - if true the provided InputStream is closed after all.
closeOut - if true the provided OutputStream is closed after all.
Returns:
the number of bytes transfered.
Throws:
java.io.IOException

transferBytes

public static long transferBytes(java.io.InputStream in,
                                 java.io.OutputStream out)
                          throws java.io.IOException
Reads all bytes from the provided InputStream and writes them to the provided OutputStream. The provided streams are closed after all.

Parameters:
in -
out -
Returns:
the number of bytes transfered.
Throws:
java.io.IOException

getBytesFromStream

public static byte[] getBytesFromStream(java.io.InputStream in,
                                        boolean closeIn)
                                 throws java.io.IOException
Copies all bytes from the given InputStream into a new byte array.

Parameters:
in -
closeIn - if true the provided InputStream is closed after all.
Returns:
a byte array containing all the bytes from the given InputStream.
Throws:
java.io.IOException

getBytesFromStream

public static byte[] getBytesFromStream(java.io.InputStream in)
                                 throws java.io.IOException
Copies all bytes from the given InputStream into a new byte array. The provided stream is closed after all.

Parameters:
in -
Returns:
a byte array containing all the bytes from the given InputStream.
Throws:
java.io.IOException

getStringFromStream

public static java.lang.String getStringFromStream(java.io.InputStream in,
                                                   boolean closeIn)
                                            throws java.io.IOException
Copies all bytes from the given InputStream into a new String.

Parameters:
in -
closeIn - if true the provided InputStream is closed after all.
Returns:
a String containing all the bytes from the given InputStream.
Throws:
java.io.IOException

getStringFromStream

public static java.lang.String getStringFromStream(java.io.InputStream in)
                                            throws java.io.IOException
Copies all bytes from the given InputStream into a new String. The provided stream is closed after all.

Parameters:
in -
Returns:
a String containing all the bytes from the given InputStream.
Throws:
java.io.IOException