ewe.io
Class StreamUtils

java.lang.Object
  extended byewe.io.StreamUtils

public final class StreamUtils
extends Object


Field Summary
static int CLOSE_INPUT
          An option for transfer().
static int CLOSE_OUTPUT
          An option for transfer().
static int DONT_CLOSE_IF_ABORTED
          An option for transfer().
static int DONT_STOP_AFTER_KNOWN_SIZE
          An option for transfer().
 
Method Summary
static ByteArray readAllBytes(Handle h, InputStream in, ByteArray dest)
          Read all the bytes from an InputStream, placing them in a ByteArray.
static ByteArray readAllBytes(Handle h, InputStream in, ByteArray dest, int knownSize, int optionsAndBufferSize)
          Read all the bytes from an InputStream, placing them in a ByteArray.
static IOHandle readAllBytes(InputStream in, ByteArray dest)
          Read all the bytes in a separate thread.
static IOHandle readAllBytes(InputStream in, ByteArray dest, int knownSize, int optionsAndBufferSize)
          Read all the bytes in a separate thread.
static void readFully(InputStream in, byte[] dest)
          Reads in all the bytes of an array.
static void readFully(InputStream in, byte[] dest, int start, int count)
          Reads in exactly a certain number of bytes.
static int transfer(Handle h, InputStream in, OutputStream out)
          Transfer data from an InputStream to an OutputStream.
static int transfer(Handle h, InputStream in, OutputStream out, int knownSize, int optionsAndBufferSize)
          Transfer data from an InputStream to an OutputStream.
static int transfer(Handle h, Reader in, Writer out, int knownSize, int optionsAndBufferSize)
          Transfer data from a Reader to a Writer.
static IOHandle transfer(InputStream in, OutputStream out)
          Transfer data from an InputStream to an OutputStream in a separate thread.
static IOHandle transfer(InputStream in, OutputStream out, int knownSize, int optionsAndBufferSize)
          Transfer data from an InputStream to an OutputStream in a separate thread.
static IOHandle transfer(Reader in, Writer out)
          Transfer data from a Reader to a Writer in a separate thread.
static IOHandle transfer(Reader in, Writer out, int knownSize, int optionsAndBufferSize)
          Transfer data from a Reader to a Writer in a separate thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Field Detail

CLOSE_INPUT

public static final int CLOSE_INPUT
An option for transfer().

See Also:
Constant Field Values

CLOSE_OUTPUT

public static final int CLOSE_OUTPUT
An option for transfer().

See Also:
Constant Field Values

DONT_STOP_AFTER_KNOWN_SIZE

public static final int DONT_STOP_AFTER_KNOWN_SIZE
An option for transfer().

See Also:
Constant Field Values

DONT_CLOSE_IF_ABORTED

public static final int DONT_CLOSE_IF_ABORTED
An option for transfer().

See Also:
Constant Field Values
Method Detail

readFully

public static void readFully(InputStream in,
                             byte[] dest,
                             int start,
                             int count)
                      throws IOException,
                             EOFException
Reads in exactly a certain number of bytes. An EOF error is thrown if the Stream ends before the bytes are read.

Parameters:
in - The InputStream
dest - The destination for the input data bytes.
start - The offset in the dest to read from.
count - The number of bytes to read.
Throws:
IOException - If an I/O error occurs.
EOFException - If the stream ends before all the bytes are read.

readFully

public static void readFully(InputStream in,
                             byte[] dest)
                      throws IOException,
                             EOFException
Reads in all the bytes of an array. An EOF error is thrown if the Stream ends before the bytes are read.

Parameters:
in - The InputStream
dest - The destination for the input data bytes.
Returns:
Throws:
IOException
EOFException

transfer

public static int transfer(Handle h,
                           InputStream in,
                           OutputStream out,
                           int knownSize,
                           int optionsAndBufferSize)
                    throws IOException
Transfer data from an InputStream to an OutputStream. This method runs in the current thread and does not return until it is complete. However if the stop() method of the optional Handle parameter is called, the transfer will be aborted.

Parameters:
h - an optional handle that can be used by another Thread to monitor and cancel the transfer.
in - the InputStream.
out - the OutputStream.
knownSize - If the number of bytes is known, set this to be that value. Otherwise set it to -1.
optionsAndBufferSize - any of CLOSE_INPUT|CLOSE_OUTPUT|DONT_STOP_AFTER_KNOWN_SIZE OR'ed with an optional buffer size specified in Kilobytes. e.g. CLOSE_INPUT|CLOSE_OUTPUT|10
Returns:
the actual number of bytes transferred, or -1 if the handle was stopped.
Throws:
IOException - if an IOException occurs.

transfer

public static int transfer(Handle h,
                           InputStream in,
                           OutputStream out)
                    throws IOException
Transfer data from an InputStream to an OutputStream. This method runs in the current thread and does not return until it is complete. However if the stop() method of the optional Handle parameter is called, the transfer will be aborted.

Parameters:
h - an optional handle that can be used by another Thread to monitor and cancel the transfer.
in - the InputStream.
out - the OutputStream.
Returns:
the actual number of bytes transferred, or -1 if the handle was stopped.
Throws:
IOException - if an IOException occurs.

transfer

public static IOHandle transfer(InputStream in,
                                OutputStream out,
                                int knownSize,
                                int optionsAndBufferSize)
Transfer data from an InputStream to an OutputStream in a separate thread.

Parameters:
in - the InputStream.
out - the OutputStream.
knownSize - If the number of bytes is known, set this to be that value. Otherwise set it to -1.
optionsAndBufferSize - any of CLOSE_INPUT|CLOSE_OUTPUT|DONT_STOP_AFTER_KNOWN_SIZE OR'ed with an optional buffer size specified in Kilobytes. e.g. CLOSE_INPUT|CLOSE_OUTPUT|10
Returns:
an IOHandle that can be used to monitor and possibly abort the transfer. When the IOHandle indicates success, the number of bytes transferred will be given by the bytesTransferred value of the IOHandle.

transfer

public static IOHandle transfer(InputStream in,
                                OutputStream out)
Transfer data from an InputStream to an OutputStream in a separate thread.

Parameters:
in - the InputStream.
out - the OutputStream.
Returns:
an IOHandle that can be used to monitor and possibly abort the transfer. When the IOHandle indicates success, the number of bytes transferred will be given by the bytesTransferred value of the IOHandle.

readAllBytes

public static ByteArray readAllBytes(Handle h,
                                     InputStream in,
                                     ByteArray dest,
                                     int knownSize,
                                     int optionsAndBufferSize)
                              throws IOException
Read all the bytes from an InputStream, placing them in a ByteArray.

Parameters:
h - an optional handle that can be used by another Thread to monitor and cancel the transfer.
in - The input stream.
dest - An optional destination ByteArray.
knownSize - If the size is known then set this to be that size. If it is not known then set it to -1.
optionsAndBufferSize - any of CLOSE_INPUT|CLOSE_OUTPUT|DONT_STOP_AFTER_KNOWN_SIZE OR'ed with an optional buffer size specified in Kilobytes. e.g. CLOSE_INPUT|CLOSE_OUTPUT|10
Returns:
the dest ByteArray or a new ByteArray containing the data read in. If the transfer was aborted because stop() was called on the h Handle, then the method will return null.
Throws:
IOException - if an IOException occurs.

readAllBytes

public static ByteArray readAllBytes(Handle h,
                                     InputStream in,
                                     ByteArray dest)
                              throws IOException
Read all the bytes from an InputStream, placing them in a ByteArray.

Parameters:
h - an optional handle that can be used by another Thread to monitor and cancel the transfer.
in - The input stream.
dest - An optional destination ByteArray.
Returns:
the dest ByteArray or a new ByteArray containing the data read in. If the transfer was aborted because stop() was called on the h Handle, then the method will return null.
Throws:
IOException - if an IOException occurs.

readAllBytes

public static IOHandle readAllBytes(InputStream in,
                                    ByteArray dest,
                                    int knownSize,
                                    int optionsAndBufferSize)
Read all the bytes in a separate thread. When finished the read bytes are placed in the destination ByteArray (or a new one if dest is null) and then the returnValue member of the returned IOHandle will be that ByteArray.

Parameters:
in - The input stream.
dest - An optional destination ByteArray.
knownSize - If the size is known then set this to be that size. If it is not known then set it to -1.
optionsAndBufferSize - any of CLOSE_INPUT|CLOSE_OUTPUT|DONT_STOP_AFTER_KNOWN_SIZE OR'ed with an optional buffer size specified in Kilobytes. e.g. CLOSE_INPUT|CLOSE_OUTPUT|10
Returns:
an IOHandle that can be used to monitor and stop the process. When it reports success then its returnValue will hold the destination ByteArray containing the data.

readAllBytes

public static IOHandle readAllBytes(InputStream in,
                                    ByteArray dest)
Read all the bytes in a separate thread. When finished the read bytes are placed in the destination ByteArray (or a new one if dest is null) and then the returnValue member of the returned IOHandle will be that ByteArray.

Parameters:
in - The input stream.
dest - An optional destination ByteArray.
Returns:
an IOHandle that can be used to monitor and stop the process. When it reports success then its returnValue will hold the destination ByteArray containing the data.

transfer

public static int transfer(Handle h,
                           Reader in,
                           Writer out,
                           int knownSize,
                           int optionsAndBufferSize)
                    throws IOException
Transfer data from a Reader to a Writer. This method runs in the current thread and does not return until it is complete. However if the stop() method of the optional Handle parameter is called, the transfer will be aborted.

Parameters:
h - an optional handle that can be used by another Thread to monitor and cancel the transfer.
in - the Reader.
out - the Writer.
knownSize - If the number of bytes is known, set this to be that value. Otherwise set it to -1.
optionsAndBufferSize - any of CLOSE_INPUT|CLOSE_OUTPUT|DONT_STOP_AFTER_KNOWN_SIZE OR'ed with an optional buffer size specified in Kilobytes. e.g. CLOSE_INPUT|CLOSE_OUTPUT|10
Returns:
the actual number of bytes transferred, or -1 if the handle was stopped.
Throws:
IOException - if an IOException occurs.

transfer

public static IOHandle transfer(Reader in,
                                Writer out,
                                int knownSize,
                                int optionsAndBufferSize)
Transfer data from a Reader to a Writer in a separate thread.

Parameters:
in - the Reader.
out - the Writer.
knownSize - If the number of bytes is known, set this to be that value. Otherwise set it to -1.
optionsAndBufferSize - any of CLOSE_INPUT|CLOSE_OUTPUT|DONT_STOP_AFTER_KNOWN_SIZE OR'ed with an optional buffer size specified in Kilobytes. e.g. CLOSE_INPUT|CLOSE_OUTPUT|10
Returns:
an IOHandle that can be used to monitor and possibly abort the transfer. When the IOHandle indicates success, the number of chars transferred will be given by the bytesTransferred value of the IOHandle.

transfer

public static IOHandle transfer(Reader in,
                                Writer out)
Transfer data from a Reader to a Writer in a separate thread.

Parameters:
in - the Reader.
out - the Writer.
Returns:
an IOHandle that can be used to monitor and possibly abort the transfer. When the IOHandle indicates success, the number of chars transferred will be given by the bytesTransferred value of the IOHandle.