com.ibm.commons.util.io
Class ByteStreamCache

java.lang.Object
  extended by com.ibm.commons.util.io.ByteStreamCache

public class ByteStreamCache
extends java.lang.Object

Byte buffer.

This class is used to store bytes and provides both an OutputStream for storing the bytes and an InputStream for reading them. It stores the bytes in memory in a highly optimized way, with a possibility of using a backing file when the content is bigger than a predefined threshold.


Nested Class Summary
protected  class ByteStreamCache.InternalInputStream
           
protected  class ByteStreamCache.InternalOutputStream
           
 
Field Summary
static int DEFAULT_BLOCKSIZE
           
static int DEFAULT_THRESHOLD
           
 
Constructor Summary
ByteStreamCache()
           
ByteStreamCache(int blockSize)
           
ByteStreamCache(int blockSize, int threshold)
           
 
Method Summary
 void clear()
          Clear the data inside the cache.
 void copyFrom(java.io.InputStream is)
          Init the data from an existing InputStream.
 void copyFrom(java.io.InputStream is, int maxRead)
          Init the data from an existing InputStream with a maximum bytes to be read.
 void copyFrom(java.io.ObjectInput is)
          Init the data from an existing ObjectInput.
 void copyFrom(java.io.ObjectInput is, int maxRead)
          Init the data from an existing ObjectInput with a maximum bytes to be read.
 void copyTo(java.io.File f)
          Copy the stream to a file.
 void copyTo(java.io.ObjectOutput os)
          Copy the stream to another stream.
 void copyTo(java.io.OutputStream os)
          Copy the stream to another stream.
 byte getByte(int pos)
          Get an indexed byte.
 java.io.InputStream getInputStream()
          Get an input stream on the stored data.
 long getLength()
          Get the number of stored bytes.
 java.io.OutputStream getOutputStream()
          Get an output stream to store the data.
 boolean isEqual(ByteStreamCache other)
           
 byte[] toByteArray()
          Convert to a byte array.
 void write(byte[] b, int off, int len)
           
 void write(int b)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BLOCKSIZE

public static final int DEFAULT_BLOCKSIZE
See Also:
Constant Field Values

DEFAULT_THRESHOLD

public static final int DEFAULT_THRESHOLD
See Also:
Constant Field Values
Constructor Detail

ByteStreamCache

public ByteStreamCache()

ByteStreamCache

public ByteStreamCache(int blockSize)

ByteStreamCache

public ByteStreamCache(int blockSize,
                       int threshold)
Method Detail

isEqual

public boolean isEqual(ByteStreamCache other)

getLength

public long getLength()
Get the number of stored bytes.


getByte

public byte getByte(int pos)
Get an indexed byte.


clear

public void clear()
Clear the data inside the cache.


getInputStream

public java.io.InputStream getInputStream()
Get an input stream on the stored data.


getOutputStream

public java.io.OutputStream getOutputStream()
Get an output stream to store the data.


copyFrom

public void copyFrom(java.io.InputStream is)
              throws java.io.IOException
Init the data from an existing InputStream.

Throws:
java.io.IOException

copyFrom

public void copyFrom(java.io.InputStream is,
                     int maxRead)
              throws java.io.IOException
Init the data from an existing InputStream with a maximum bytes to be read.

Throws:
java.io.IOException

copyFrom

public void copyFrom(java.io.ObjectInput is)
              throws java.io.IOException
Init the data from an existing ObjectInput.

Throws:
java.io.IOException

copyFrom

public void copyFrom(java.io.ObjectInput is,
                     int maxRead)
              throws java.io.IOException
Init the data from an existing ObjectInput with a maximum bytes to be read.

Throws:
java.io.IOException

copyTo

public void copyTo(java.io.OutputStream os)
            throws java.io.IOException
Copy the stream to another stream.

Throws:
java.io.IOException

copyTo

public void copyTo(java.io.ObjectOutput os)
            throws java.io.IOException
Copy the stream to another stream.

Throws:
java.io.IOException

copyTo

public void copyTo(java.io.File f)
            throws java.io.IOException
Copy the stream to a file.

Throws:
java.io.IOException

toByteArray

public byte[] toByteArray()
Convert to a byte array. WARN: this fct is *not* really optimized!


write

public final void write(int b)
                 throws java.io.IOException
Throws:
java.io.IOException

write

public final void write(byte[] b,
                        int off,
                        int len)
                 throws java.io.IOException
Throws:
java.io.IOException