com.metamatrix.common.util
Class ByteArrayHelper

java.lang.Object
  extended by com.metamatrix.common.util.ByteArrayHelper

public class ByteArrayHelper
extends java.lang.Object


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

CHUNK_SIZE

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.

See Also:
Constant Field Values
Constructor Detail

ByteArrayHelper

public ByteArrayHelper()
Method Detail

toByteArray

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.

Parameters:
file - data to be converted to a byte array
Returns:
byte array exactly filled with data; no leftover space
Throws:
java.io.IOException - if there is an Exception reading from the InputStream

toByteArray

public 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).

Parameters:
stream - data to be converted to a byte array
Returns:
byte array exactly filled with data; no leftover space
Throws:
java.io.IOException - if there is an Exception reading from the InputStream

toByteArray

public 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.

Parameters:
stream - data to be converted to a byte array
chunkSize - size of intermediate byte array(s) to buffer data
Returns:
byte array exactly filled with data; no leftover space
Throws:
java.io.IOException - if there is an Exception reading from the InputStream

toInputStream

public static java.io.InputStream toInputStream(byte[] data)
                                         throws java.lang.Exception
converts the byte array to an input stream

Throws:
java.lang.Exception


Copyright © 2009. All Rights Reserved.