org.icepdf.core.util
Class ByteCache

java.lang.Object
  extended by org.icepdf.core.util.ByteCache

public class ByteCache
extends java.lang.Object

The ByteCache class is designed to cache a byte array input to file when a specified size has been reached (Cached). This cached byte stream can be read from file when the byte array is needed by a program.

The ByteCache creates a temporary file in the users temporary directory and the file is deleted when the application is closed or when the deleteFileCache is called.

File caching can be forced by calling the forceByteCaching() method, but caching is normally done automatically when the byteArray reaches a specified length. The length is specified by the system property org.icepdf.core.util.byteFileCachingSize where the associated value is byte array length in which the byte array will be cached to a temporary file.

Every time a new bye cache is created the full path to the file is recored in the CacheManager class which allows the CacheManager to delete a documents cached bytes when the document is closed.

Since:
1.1

Constructor Summary
ByteCache(byte[] bytes, Library library)
          Creates a new instance of ByteCache object.
ByteCache(int numNewBytes, Library library)
          Creates a new instance of ByteCache object.
 
Method Summary
 void close()
          Closes this file input stream and releases any system resources associated with the stream.
 void deleteFileCache()
          Dispose of temp file and bytes array.
 void dispose(boolean cache)
          Dispose this objects internal references and free up any available memory.
 void forceByteCaching()
          Forces the caching of byte array to a temp file.
 int getLength()
          Gets the length of the byte array stored in this byte cache.
 boolean inMemory()
           
 boolean isCached()
          Return if this byte cache has stored its byte data in a a temp file.
 int readByte()
          Reads a byte of data from this input stream.
 int readBytes(byte[] bytes)
          Reads up to bytes.length bytes of data from this input stream into an array of bytes.
 int readBytes(byte[] bytes, int offset, int length)
          Reads up to length bytes of data from this byte cache into an array of bytes.
 void reset()
          Resets the buffer to the marked position.
 byte[] toByteArray()
          Creates a newly allocated byte array.
 void writeBytes(byte[] bytes)
          Writes bytes.length bytes from the specified byte array to this byte cache..
 void writeBytes(byte[] bytes, int offset, int numNewBytes)
          Writes length bytes from the specified byte array starting at offset offset to this byte cache.
 void writeBytes(java.io.InputStream in, int numNewBytes)
          Writes length bytes from the specified byte array starting at offset offset to this byte cache.
 void writeBytes(int bytes)
          Writes the specified byte to this byte cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteCache

public ByteCache(byte[] bytes,
                 Library library)
Creates a new instance of ByteCache object.

Parameters:
bytes - bytes that will be stored in ByteCache .

ByteCache

public ByteCache(int numNewBytes,
                 Library library)
Creates a new instance of ByteCache object. This is an empty place holder for a byte stream. The length value will be used to pre determine if a temp file will be created even though no data has been cached.

Parameters:
numNewBytes - estimated length of a byte array that will be read at a later instnace.
Method Detail

writeBytes

public void writeBytes(java.io.InputStream in,
                       int numNewBytes)
Writes length bytes from the specified byte array starting at offset offset to this byte cache.

Parameters:
in - InputStream which holds the data.
numNewBytes - the number of bytes to write.

writeBytes

public void writeBytes(byte[] bytes,
                       int offset,
                       int numNewBytes)
Writes length bytes from the specified byte array starting at offset offset to this byte cache.

Parameters:
bytes - the data.
offset - the start offset in the data.
numNewBytes - the number of bytes to write.

writeBytes

public void writeBytes(byte[] bytes)
Writes bytes.length bytes from the specified byte array to this byte cache..

Parameters:
bytes - the data.

writeBytes

public void writeBytes(int bytes)
Writes the specified byte to this byte cache.

Parameters:
bytes - the byte to be written.

readBytes

public int readBytes(byte[] bytes,
                     int offset,
                     int length)
Reads up to length bytes of data from this byte cache into an array of bytes. This method blocks until some input is available.

Parameters:
bytes - the buffer into which the data is read.
offset - the start offset of the data.
length - the maximum number of 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 file has been reached.

readBytes

public int readBytes(byte[] bytes)
Reads up to bytes.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available.

Parameters:
bytes - the buffer into which the data is read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.

readByte

public int readByte()
Reads a byte of data from this input stream. This method blocks if no input is yet available.

Returns:
the next byte of data, or -1 if the end of the file is reached.

forceByteCaching

public void forceByteCaching()
Forces the caching of byte array to a temp file. If the system property corg.icepdf.core.util.bytecache.caching is set to false this method will not write to file.


dispose

public void dispose(boolean cache)
Dispose this objects internal references and free up any available memory.

Parameters:
cache - Whether to make it so we can continue to use this object later

toByteArray

public byte[] toByteArray()
Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.

Returns:
the current contents of this byte cache, as a byte array.

deleteFileCache

public void deleteFileCache()
Dispose of temp file and bytes array.


close

public void close()
Closes this file input stream and releases any system resources associated with the stream.


reset

public void reset()
Resets the buffer to the marked position. The marked position is 0 unless another position was marked.


isCached

public boolean isCached()
Return if this byte cache has stored its byte data in a a temp file.

Returns:
true if the byte data has been cached to file; false otherwise.

getLength

public int getLength()
Gets the length of the byte array stored in this byte cache.

Returns:

inMemory

public boolean inMemory()
Returns:
true if all our bytes are in memory, so we won't have to go to disk for them