|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.metamatrix.common.util.ByteArrayHelper
public class ByteArrayHelper
Field Summary | |
---|---|
static int |
CHUNK_SIZE
The default size of each temporary byte array that is instantiated to buffer data from an InputStream, in the toByteArray(InputStream) method. |
Constructor Summary | |
---|---|
ByteArrayHelper()
|
Method Summary | |
---|---|
static byte[] |
toByteArray(java.io.File file)
Reads data from the file and returns it as a byte array. |
static byte[] |
toByteArray(java.io.InputStream stream)
Reads binary data from the InputStream and returns it as a byte array. |
static byte[] |
toByteArray(java.io.InputStream stream,
int chunkSize)
Reads binary data from the InputStream and returns it as a byte array. |
static java.io.InputStream |
toInputStream(byte[] data)
converts the byte array to an input stream |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int CHUNK_SIZE
The default size of each temporary byte array that is instantiated
to buffer data from an InputStream, in the
toByteArray(InputStream)
method.
Ideally, this number should be big enough that only one byte array is needed, but small enough that wasted memory isn't allocated. If the first temp array is filled, then a second one of this size will be created, and so on until all of the stream is read.
Constructor Detail |
---|
public ByteArrayHelper()
Method Detail |
---|
public static byte[] toByteArray(java.io.File file) throws java.io.IOException
Reads data from the file and returns it as a byte array. The returned byte array is exactly filled with the data from the InputStream, with no space left over.
file
- data to be converted to a byte array
java.io.IOException
- if there is an Exception reading from the InputStreampublic static byte[] toByteArray(java.io.InputStream stream) throws java.io.IOException
Reads binary data from the InputStream and returns it as a byte array. The InputStream is not closed in this method. The returned byte array is exactly filled with the data from the InputStream, with no space left over.
If the amount of data in the input stream is known, use
toByteArray(InputStream, int)
.
stream
- data to be converted to a byte array
java.io.IOException
- if there is an Exception reading from the InputStreampublic static byte[] toByteArray(java.io.InputStream stream, int chunkSize) throws java.io.IOException
Reads binary data from the InputStream and returns it as a byte array. The InputStream is not closed in this method. The returned byte array is exactly filled with the data from the InputStream, with no space left over.
The chunkSize parameter controls the size of intermediate byte array(s) that are used to buffer the stream data. Ideally, this number should be big enough that only one byte array is needed, but small enough that wasted memory isn't allocated. If the first temp array is filled, then a second one of this size will be created, and so on until all of the stream is read. Then, data will be copied into the final, correctly-size byte array which is returned form this method.
If the size of the input stream is known beforehand (for example, if the size of a file represented by a FileInputStream is known), then that size plus one should be passed in as the chunkSize.
Implementation notes: If more than one intermediate byte array is needed, an ArrayList is instantiated to hold the intermediate byte arrays until all data is read from the stream. Afterward, the ArrayList is iterated through; the intermediate array(s) are copied using System.arrayCopy into the final byte array.
stream
- data to be converted to a byte arraychunkSize
- size of intermediate byte array(s) to buffer data
java.io.IOException
- if there is an Exception reading from the InputStreampublic static java.io.InputStream toInputStream(byte[] data) throws java.lang.Exception
java.lang.Exception
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |