com.gc.iotools.stream.utils
Class StreamUtils

java.lang.Object
  extended by com.gc.iotools.stream.utils.StreamUtils

public final class StreamUtils
extends Object

General utilities for handling streams.

Since:
1.0.9
Author:
dvd.smnt

Method Summary
static byte[] read(InputStream source, int size)
           Read a specified amount of bytes from the source InputStream and place them into the returned byte array.
static int tryReadFully(InputStream source, byte[] buffer, int offset, int len)
           Read bytes from the source InputStream into the buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

read

public static byte[] read(InputStream source,
                          int size)
                   throws IOException

Read a specified amount of bytes from the source InputStream and place them into the returned byte array.

This utility ensures that either size bytes are read or the end of the stream has been reached.

Parameters:
source - Stream from which the data is read.
size - The maximum length of the data read.
Returns:
byte[] containing the data read from the stream. null if the End Of File has been reached.
Throws:
IOException - If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs.
Since:
1.0.9

tryReadFully

public static int tryReadFully(InputStream source,
                               byte[] buffer,
                               int offset,
                               int len)
                        throws IOException

Read bytes from the source InputStream into the buffer.

This utility ensures that either len bytes are read or the end of the stream has been reached.

Parameters:
source - Stream from which the data is read.
buffer - the buffer into which the data is read.
offset - the start offset in array buffer at which the data is written.
len - maximum length of the bytes read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
IOException - If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs.
NullPointerException - If b is null.
IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than b.length - off
Since:
1.0.8
See Also:
InputStream.read(byte[] buf,int off, int len)


Copyright © 2008-2009. All Rights Reserved.