org.entityfs.support.io
Class StreamUtil

java.lang.Object
  extended by org.entityfs.support.io.StreamUtil

public final class StreamUtil
extends Object

This class contains static utility methods for working with streams.

Since:
1.0
Author:
Karl Gustafsson
In_jar:
entityfs-core

Method Summary
static long copyChannels(ReadableByteChannel rbc, WritableByteChannel wbc, int bufSize)
          Copy binary data read from the readable byte channel to the writable byte channel.
static long copyChannels(ReadableByteChannel rbc, WritableByteChannel wbc, int bufSize, long noToCopy)
          Copy binary data read from the readable byte channel to the writable byte channel.
static long copyStreams(InputStream is, OutputStream os, int bufSize)
          Copy data read from the input stream to the output stream.
static long copyStreams(InputStream is, OutputStream os, int bufSize, long noToCopy)
          Copy data read from the input stream to the output stream.
static long copyStreams(Reader r, Writer w, int bufSize)
          Copy character data from the reader to the writer.
static long copyStreams(Reader r, Writer w, int bufSize, long noToCopy)
          Copy data read from the character reader to the character writer.
static long getSizeOfDataInStream(InputStream is, int bufSize)
          Calculate the size of the data that can be read from the stream.
static byte[] readChannelFully(ReadableByteChannel c, int bufSize)
          Read the contents of the channel into a byte array.
static byte[] readStreamFully(InputStream is, int bufSize)
          Read the contents of the stream into a byte array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

copyStreams

public static long copyStreams(InputStream is,
                               OutputStream os,
                               int bufSize,
                               long noToCopy)
                        throws WrappedIOException
Copy data read from the input stream to the output stream. The data is first read to a temporary buffer that is bufSize bytes long. No more than noToCopy bytes are copied, even if there are more bytes available in the InputStream.

Parameters:
is - The input stream. Not closed after copying.
os - The output stream. Not closed after copying.
bufSize - The size of the data buffer. If writing to a file, it is recommended that its file system's buffer size is used.
noToCopy - The maximum amount of bytes to copy. Set this to -1 (or call copyStreams(InputStream, OutputStream, int)) to copy all available data.
Returns:
The total number of bytes copied.
Throws:
WrappedIOException - On errors.
See Also:
copyStreams(InputStream, OutputStream, int), copyStreams(Reader, Writer, int, long)

copyStreams

public static long copyStreams(InputStream is,
                               OutputStream os,
                               int bufSize)
                        throws WrappedIOException
Copy data read from the input stream to the output stream. The data is read to a buffer with the given size.

Parameters:
is - The input stream. Not closed after copying.
os - The output stream. Not closed after copying.
bufSize - The size of the data buffer. If writing to a file, it is recommended that its file system's buffer size is used.
Returns:
The total number of bytes copied.
Throws:
WrappedIOException - On errors
See Also:
copyStreams(InputStream, OutputStream, int, long), copyStreams(Reader, Writer, int)

copyStreams

public static long copyStreams(Reader r,
                               Writer w,
                               int bufSize,
                               long noToCopy)
                        throws WrappedIOException
Copy data read from the character reader to the character writer. The data is first read to a temporary buffer that is bufSize characters long. No more than noToCopy characters are copied, even if there are more characters available in r

Parameters:
r - The reader to read character data from. Not closed after copying.
w - The writer to write character data to. Not closed after copying.
bufSize - The number of characters in the temporary data buffer. If writing to a file, it is recommended that its file system's buffer size is used.
noToCopy - The maximum number of characters to copy. If this is set to -1 (or if copyStreams(Reader, Writer, int) is called), all available characters in r are copied.
Returns:
The total number of characters copied.
Throws:
WrappedIOException - On errors
See Also:
copyStreams(Reader, Writer, int), copyStreams(InputStream, OutputStream, int, long)

copyStreams

public static long copyStreams(Reader r,
                               Writer w,
                               int bufSize)
                        throws WrappedIOException
Copy character data from the reader to the writer. The data is first read to a temporary buffer with the given size.

Parameters:
r - The reader to read character data from. Not closed after copying.
w - The writer to write character data from. Not closed after copying.
bufSize - The size of the temporary data buffer. If writing to a file, it is recommended that its file system's buffer size is used.
Returns:
The total number of characters copied.
Throws:
WrappedIOException - On errors

copyChannels

public static long copyChannels(ReadableByteChannel rbc,
                                WritableByteChannel wbc,
                                int bufSize,
                                long noToCopy)
                         throws WrappedIOException
Copy binary data read from the readable byte channel to the writable byte channel. The data is temporarily read to a buffer that can store bufSize bytes. No more than noToCopy bytes are copied, even if there are more bytes available in rbc.

Parameters:
rbc - The readable byte channel to read data from. The channel is not closed by this method.
wbc - The writable byte channel to write data to. The channel is not closed by this method.
bufSize - The size of the temporary data buffer. If writing to a file, it is recommended that its file system's buffer size is used.
noToCopy - The maximum number of bytes to copy. If this is set to -1 (or if copyChannels(ReadableByteChannel, WritableByteChannel, int) is called), all available data in rbc is copied.
Returns:
The total number of bytes copied.
Throws:
WrappedIOException - On errors

copyChannels

public static long copyChannels(ReadableByteChannel rbc,
                                WritableByteChannel wbc,
                                int bufSize)
                         throws WrappedIOException
Copy binary data read from the readable byte channel to the writable byte channel. The data is temporarily read to a buffer that can store bufSize bytes.

Parameters:
rbc - The readable byte channel to read data from. The channel is not closed by this method.
wbc - The writable byte channel to write data to. The channel is not closed by this method.
bufSize - The size of the temporary data buffer. If writing to a file, it is recommended that its file system's buffer size is used.
Throws:
WrappedIOException - On errors
See Also:
copyChannels(ReadableByteChannel, WritableByteChannel, int, long)

getSizeOfDataInStream

public static long getSizeOfDataInStream(InputStream is,
                                         int bufSize)
                                  throws WrappedIOException
Calculate the size of the data that can be read from the stream.

Parameters:
is - The input stream. Not closed by this method.
bufSize - The size of temporary data buffers. If reading from a file, it is recommended that the file's file system's buffer size is used.
Returns:
The number of bytes that could be read from the stream.
Throws:
WrappedIOException - On errors.

readStreamFully

public static byte[] readStreamFully(InputStream is,
                                     int bufSize)
                              throws WrappedIOException,
                                     IllegalArgumentException
Read the contents of the stream into a byte array.

Parameters:
is - The stream to read from. Not closed by this method.
bufSize - The size of temporary buffers. If reading from a file, it is recommended that its file system's buffer size is used.
Returns:
The contents of the stream.
Throws:
WrappedIOException - On I/O errors.
IllegalArgumentException - If the buffer size is 0 or less.
See Also:
readChannelFully(ReadableByteChannel, int)

readChannelFully

public static byte[] readChannelFully(ReadableByteChannel c,
                                      int bufSize)
                               throws WrappedIOException,
                                      IllegalArgumentException
Read the contents of the channel into a byte array.

Parameters:
c - The channel to read from. Not closed by this method.
bufSize - The size of temporary buffers. If reading from a file, it is recommended that its file system's buffer size is used.
Returns:
The contents of the channel.
Throws:
WrappedIOException - On I/O errors.
IllegalArgumentException - If the buffer size is 0 or less.
Since:
1.1
See Also:
readStreamFully(InputStream, int)