com.lizardtech.djvu
Class DataPool

java.lang.Object
  extended by com.lizardtech.djvu.DjVuObject
      extended by com.lizardtech.djvu.DataPool
All Implemented Interfaces:
DjVuInterface, java.lang.Cloneable

public class DataPool
extends DjVuObject
implements java.lang.Cloneable

This class impliments a random access view of buffered data.

Version:
$Revision: 1.10 $
Author:
Bill C. Riemers

Nested Class Summary
 class DataPool.CachedInputStream
          This class is an input stream which spans the accessable data in a DataPool object.
 
Field Summary
static java.util.Hashtable cache
          Object for caching raw data.
 
Fields inherited from class com.lizardtech.djvu.DjVuObject
hasReferences
 
Constructor Summary
DataPool()
          Creates a new DataPool object.
 
Method Summary
 java.lang.Object clone()
          Create a copy of this data pool which will refere to the original as a parent to read data.
 ByteVector createByteVector()
          Create a new ByteVector.
static DataPool createDataPool(DjVuInterface ref)
          Creates an instance of DataPool with the options interherited from the specified reference.
static DataPool createDataPool(DjVuInterface ref, java.net.URL url)
          Called to convert a URL to a DataPool.
 DataPool.CachedInputStream createInputStream()
          Created a DataPool.CachedInputStream to read the data from.
protected  ByteVector getBuffer()
          Query the buffer.
 ByteVector getData(int targetSize)
          Read the data of the underlying DataPool up until the targetSize or EOF.
 int getEndOffset()
          Query the end position of this pool.
 DataPool getParent()
          Query the parent pool.
 int getPosition()
          Query the end position of buffered data.
 int getStartOffset()
          Query the start offset of this pool.
 java.net.URL getUrl()
          Queries the URL used to create this stream.
 DataPool init(DataPool parent, int startOffset, int endOffset)
          Initialize this pool to read data from a parent using the specified range.
 DataPool init(java.io.InputStream input)
          Initialize the DataPool with the specified input stream
 DataPool init(java.net.URL url, boolean prefetch)
          Initialize the DataPool with the specified url.
 void prefetch()
          Prefetched datain a separate thread.
 java.lang.String readUTF()
          Convert the accessable data into a string.
protected  void setBuffer(ByteVector buffer)
          Set a new buffer.
 void setEndOffset(int offset)
          Adjust the endOffset of the DataPool.
 void setStartOffset(int offset)
          Adjust the startOffset of the DataPool.
 byte[] toSizedByteArray()
          Convert the accessable data into an array of bytes, with the first two bytes equal to the array length.
 
Methods inherited from class com.lizardtech.djvu.DjVuObject
checkLockTime, create, create, createSoftReference, createWeakReference, getDjVuOptions, getFromReference, invoke, setDjVuOptions
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cache

public static java.util.Hashtable cache
Object for caching raw data. !

Constructor Detail

DataPool

public DataPool()
Creates a new DataPool object.

Method Detail

setEndOffset

public void setEndOffset(int offset)
Adjust the endOffset of the DataPool. This may be decreased, but never increased.

Parameters:
offset - DOCUMENT ME!

getEndOffset

public int getEndOffset()
Query the end position of this pool.

Returns:
the end position

getParent

public DataPool getParent()
Query the parent pool.

Returns:
the parent pool

setStartOffset

public void setStartOffset(int offset)
Adjust the startOffset of the DataPool. This may be increased, but never decreased.

Parameters:
offset - the now start offset of this DataPool

getStartOffset

public int getStartOffset()
Query the start offset of this pool.

Returns:
the start offset

getUrl

public java.net.URL getUrl()
Queries the URL used to create this stream.

Returns:
url used to create this stream or null if created from a non-URL data source

createDataPool

public static DataPool createDataPool(DjVuInterface ref)
Creates an instance of DataPool with the options interherited from the specified reference.

Parameters:
ref - Object to interherit DjVuOptions from.
Returns:
a new instance of DataPool.

createDataPool

public static DataPool createDataPool(DjVuInterface ref,
                                      java.net.URL url)
Called to convert a URL to a DataPool. A simple static caching is used to limit repeated downloads.

Parameters:
ref - Object to interherit DjVuOptions from.
url - The URL to open.
Returns:
The cached DataPool.

getData

public ByteVector getData(int targetSize)
                   throws java.io.IOException
Read the data of the underlying DataPool up until the targetSize or EOF. Even if this is a duplicate DataPool, all data will be present in the ByteVector. Ideally, this method should be protected or private, but some Java 1.1 implimentations require public access to this method.

Parameters:
targetSize - the maximum amount of data to read.
Returns:
a ByteVector containing the data read.
Throws:
java.io.IOException - if an io error occurs.

clone

public java.lang.Object clone()
Create a copy of this data pool which will refere to the original as a parent to read data.

Overrides:
clone in class java.lang.Object
Returns:
the newly created copy

createByteVector

public ByteVector createByteVector()
Create a new ByteVector.

Returns:
the new ByteVector.

createInputStream

public DataPool.CachedInputStream createInputStream()
Created a DataPool.CachedInputStream to read the data from. Each CachedInputStream will be logically separate from eachother, with the exception that they will both share a synchronization lock when reading new data.

Returns:
the new CachedInputStream.

init

public DataPool init(DataPool parent,
                     int startOffset,
                     int endOffset)
Initialize this pool to read data from a parent using the specified range.

Parameters:
parent - pool containing the data source
startOffset - start position to read from
endOffset - end position to read to
Returns:
the initialized pool

init

public DataPool init(java.io.InputStream input)
Initialize the DataPool with the specified input stream

Parameters:
input - stream to read
Returns:
this

init

public DataPool init(java.net.URL url,
                     boolean prefetch)
Initialize the DataPool with the specified url. Data may be prefetched in a separate thread.

Parameters:
url - to read
prefetch - true if data should be prefetched in a separate thread
Returns:
this

prefetch

public void prefetch()
Prefetched datain a separate thread.


readUTF

public java.lang.String readUTF()
                         throws java.io.IOException
Convert the accessable data into a string. First a java modified UTF-8 translation will be tried. If that fails, the data will be converted in the current locale encoding.

Returns:
the newly created string
Throws:
java.io.IOException - if an error occurs

toSizedByteArray

public byte[] toSizedByteArray()
                        throws java.io.IOException
Convert the accessable data into an array of bytes, with the first two bytes equal to the array length.

Returns:
the newly created array
Throws:
java.io.IOException - if an error occurs

setBuffer

protected void setBuffer(ByteVector buffer)
Set a new buffer.

Parameters:
buffer - the new ByteVector to use

getBuffer

protected ByteVector getBuffer()
Query the buffer.

Returns:
the ByteVector used to buffer data

getPosition

public int getPosition()
Query the end position of buffered data.

Returns:
the end position of buffered data.