com.lizardtech.djvu
Class CachedInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.lizardtech.djvu.CachedInputStream
All Implemented Interfaces:
DjVuInterface, java.io.Closeable, java.lang.Cloneable

public class CachedInputStream
extends java.io.InputStream
implements DjVuInterface, java.lang.Cloneable

This class is an InputStream which spans some of the accessable data in a DataPool object.

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

Constructor Summary
CachedInputStream()
          Creates a new CachedInputStream object.
 
Method Summary
 int available()
          Query how much data is available without blocking.
 java.lang.Object clone()
          Create a copy of this stream which will referes to the same DataPool
static CachedInputStream createCachedInputStream(DjVuInterface ref)
          Creates an instance of CachedInputStream with the options interherited from the specified reference.
 CachedInputStream createCachedInputStream(int size)
          Create an a new CachedInputStream.
 DjVuOptions getDjVuOptions()
          Query the DjVuOptions used by this object.
 int getEndOffset()
          Query the end position.
 java.util.Enumeration getIFFChunks()
          Query the enumeration of IFF chunks.
 java.lang.String getName()
          Query the name of this stream.
 CachedInputStream init(DataPool buffer, int startOffset, int endOffset)
          Initialize the stream with a data source, startOffset, and endOffset.
 CachedInputStream init(java.io.InputStream input)
          Initialize the stream by copying the supplied input.
 CachedInputStream init(java.net.URL url, boolean prefetch)
          Initialize the stream with a data source, startOffset, and endOffset.
 boolean isDjVuFile()
          Test if the underlying file has a DjVu octet signature.
 void mark(int readLimit)
          Marks the current location for a reset() later.
 boolean markSuppoted()
          Query if mark is supported.
 void prefetchWait()
          Prefetch data in the same thread.
 int read()
          Read the next byte of data ranged 0 to 255.
 int read(byte[] b)
          Read data into an array of bytes.
 int read(byte[] b, int off, int len)
          Read data into an array of bytes.
 int read16()
          Read the next two bytes as a posative integer.
 int read24()
          Read the next three bytes as a posative integer.
 java.lang.String readFullyUTF()
          Convert the accessable data into a string.
 java.lang.String readSizedUTF(int textsize)
          Convert the accessable data into a string.
 void reset()
          Restore the marked position
 void setDjVuOptions(DjVuOptions options)
          Set the DjVuOptions used by this object.
 void setName(java.lang.String name)
          Set the name of this stream.
 void setSize(int size)
          Set the maximum number of bytes left in this stream.
 long skip(long n)
          Skip bytes without reading.
 
Methods inherited from class java.io.InputStream
close, markSupported
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CachedInputStream

public CachedInputStream()
Creates a new CachedInputStream object.

Method Detail

createCachedInputStream

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

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

setDjVuOptions

public void setDjVuOptions(DjVuOptions options)
Set the DjVuOptions used by this object.

Specified by:
setDjVuOptions in interface DjVuInterface
Parameters:
options - The DjVuOptions used by this object.

getDjVuOptions

public DjVuOptions getDjVuOptions()
Query the DjVuOptions used by this object.

Specified by:
getDjVuOptions in interface DjVuInterface
Returns:
the DjVuOptions used by this object.

createCachedInputStream

public CachedInputStream createCachedInputStream(int size)
Create an a new CachedInputStream.

Parameters:
size - the maximum amount of data to read from this stream
Returns:
the newly created CachedInputStream

init

public CachedInputStream init(DataPool buffer,
                              int startOffset,
                              int endOffset)
Initialize the stream with a data source, startOffset, and endOffset.

Parameters:
buffer - DataPool containing data.
startOffset - Starting position of this stream.
endOffset - Ending position of this stream.
Returns:
the initialized stream

init

public CachedInputStream init(java.net.URL url,
                              boolean prefetch)
Initialize the stream with a data source, startOffset, and endOffset.

Parameters:
url - URL to read.
prefetch - True if data should be prefetched. (Temporarily broken)
Returns:
the initialized stream

init

public CachedInputStream init(java.io.InputStream input)
Initialize the stream by copying the supplied input.

Parameters:
input - InputStream to copy
Returns:
the initialized stream

clone

public java.lang.Object clone()
Create a copy of this stream which will referes to the same DataPool

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

getEndOffset

public int getEndOffset()
Query the end position. This value may only be reduced, never increased.

Returns:
an offset past the last byte of this stream

setSize

public void setSize(int size)
Set the maximum number of bytes left in this stream. Size may only be reduced, never increased.

Parameters:
size - the new size

available

public int available()
Query how much data is available without blocking.

Overrides:
available in class java.io.InputStream
Returns:
number of buffered bytes

mark

public void mark(int readLimit)
Marks the current location for a reset() later.

Overrides:
mark in class java.io.InputStream
Parameters:
readLimit - ignored

markSuppoted

public boolean markSuppoted()
Query if mark is supported.

Returns:
true

read

public int read()
Read the next byte of data ranged 0 to 255. Returns -1 if an EOF has been read.

Specified by:
read in class java.io.InputStream
Returns:
next byte

read

public int read(byte[] b,
                int off,
                int len)
Read data into an array of bytes.

Overrides:
read in class java.io.InputStream
Parameters:
b - byte array to copy data to
off - byte array offset to start copying to
len - maximum number of bytes to copy
Returns:
number of bytes read

read

public int read(byte[] b)
Read data into an array of bytes. The number of bytes read is the array length unless an EOF is read.

Overrides:
read in class java.io.InputStream
Parameters:
b - byte array to copy data to
Returns:
number of bytes read

read16

public int read16()
Read the next two bytes as a posative integer. If EOF is reached, then return -1.

Returns:
the value read

read24

public int read24()
Read the next three bytes as a posative integer. If EOF is reached, then return -1.

Returns:
the value read

reset

public void reset()
           throws java.io.IOException
Restore the marked position

Overrides:
reset in class java.io.InputStream
Throws:
java.io.IOException - if an error occurs

skip

public long skip(long n)
Skip bytes without reading.

Overrides:
skip in class java.io.InputStream
Parameters:
n - number of bytes to skip
Returns:
number of bytes actually skipped

prefetchWait

public void prefetchWait()
Prefetch data in the same thread.


readSizedUTF

public java.lang.String readSizedUTF(int textsize)
                              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. Unlike ImageInputStream.readUTF the first two bytes are not a length.

Parameters:
textsize - maximum amount of data to read
Returns:
the newly created string
Throws:
java.io.IOException - if an error occurs

readFullyUTF

public java.lang.String readFullyUTF()
                              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. Unlike an ImageInputStream.readUTY the first two bytes are not a length.

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

getName

public java.lang.String getName()
Query the name of this stream.

Returns:
the stream name

setName

public void setName(java.lang.String name)
Set the name of this stream.

Parameters:
name - the stream name

isDjVuFile

public boolean isDjVuFile()
Test if the underlying file has a DjVu octet signature.

Returns:
true if this is a valid DjVu file.

getIFFChunks

public java.util.Enumeration getIFFChunks()
Query the enumeration of IFF chunks. Returns null, if the name is four characters long.

Returns:
an Enumeration of CachedInputStream or null.