com.levigo.jadice.document.io
Class SeekableInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.levigo.jadice.document.io.SeekableInputStream
All Implemented Interfaces:
Closeable, DataInput, ImageInputStream
Direct Known Subclasses:
CarbonCopyStream, ConcatenatedInputStream, FileCacheInputStream, HTTPRangeRequestStream, MemoryInputStream, RandomAccessFileInputStream, SeekableLookaheadStream, SubfileSeekableInputStream, UncloseableSeekableInputStream

public abstract class SeekableInputStream
extends InputStream
implements ImageInputStream

This class defines an extended InputStream which is seekable.


Field Summary
protected  int bitOffset
          The current bit offset within the stream.
protected  ByteOrder byteOrder
          The byte order of the stream as an instance of the enumeration class java.nio.ByteOrder, where ByteOrder.BIG_ENDIAN indicates network byte order and ByteOrder.LITTLE_ENDIAN indicates the reverse order.
protected  long flushedPos
          The position prior to which data may be discarded.
 
Constructor Summary
SeekableInputStream()
           
 
Method Summary
protected  void checkClosed()
          Throws an IOException if the stream has been closed.
 void close()
          Closes this seekable input stream.
protected  void finalize()
          Finalizes this object prior to garbage collection.
 void flush()
           
 void flushBefore(long pos)
           
 int getBitOffset()
           
 ByteOrder getByteOrder()
           
 long getFlushedPosition()
           
abstract  long getSizeEstimate()
          A rough estimated size for this stream instance, which is used for internal cache management.
 boolean isCached()
          Default implementation returns false.
 boolean isCachedFile()
          Default implementation returns false.
 boolean isCachedMemory()
          Default implementation returns false.
abstract  long length()
          Returns the length of this stream contents, if available.
 void mark()
          Pushes the current stream position onto a stack of marked positions.
 void mark(int readlimit)
           
abstract  int read()
          Reads a single byte from the stream and returns it as an int between 0 and 255.
 int read(byte[] b)
          A convenience method that calls read(b, 0, b.length).
abstract  int read(byte[] b, int off, int len)
          Reads up to len bytes from the stream, and stores them into b starting at index off.
 int readBit()
           
 long readBits(int numBits)
           
 boolean readBoolean()
           
 byte readByte()
           
 void readBytes(IIOByteBuffer buf, int len)
           
 char readChar()
           
 double readDouble()
           
 float readFloat()
           
 void readFully(byte[] b)
           
 void readFully(byte[] b, int off, int len)
           
 void readFully(char[] c, int off, int len)
           
 void readFully(double[] d, int off, int len)
           
 void readFully(float[] f, int off, int len)
           
 void readFully(int[] i, int off, int len)
           
 void readFully(long[] l, int off, int len)
           
 void readFully(short[] s, int off, int len)
           
 int readInt()
           
 int readInt(int appliedBytes)
          Reads appliedBytes bytes from the stream, and (conceptually) concatenates them according to the current byte order and returns the result as an int.
 String readLine()
           
 long readLong()
           
 short readShort()
           
 int readUnsignedByte()
           
 long readUnsignedInt()
           
 long readUnsignedInt(int appliedBytes)
          Reads appliedBytes bytes from the stream, and (conceptually) concatenates them according to the current byte order, converts the result to a long, masks it with 0xffffffffL in order to strip off any sign-extension bits, and returns the result as an unsigned long value.
 int readUnsignedShort()
           
 String readUTF()
           
 void reset()
          Resets the current stream byte and bit positions from the stack of marked positions.
 void resetToInitialState()
          Resets the current stream byte, bit positions from the stack of marked positions, the flushed position and seeks to 0.
abstract  void seek(long pos)
          Sets the current stream position, measured from the beginning of this data stream, at which the next read occurs.
 void setBitOffset(int bitOffset)
           
 void setByteOrder(ByteOrder byteOrder)
           
 long skip(long n)
           
 int skipBytes(int n)
          Advances the current stream position by calling seek(getStreamPosition() + n).
 long skipBytes(long n)
          Advances the current stream position by calling seek(getStreamPosition() + n).
 
Methods inherited from class java.io.InputStream
available, markSupported
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.imageio.stream.ImageInputStream
getStreamPosition
 

Field Detail

byteOrder

protected ByteOrder byteOrder
The byte order of the stream as an instance of the enumeration class java.nio.ByteOrder, where ByteOrder.BIG_ENDIAN indicates network byte order and ByteOrder.LITTLE_ENDIAN indicates the reverse order. By default, the value is ByteOrder.BIG_ENDIAN.


bitOffset

protected int bitOffset
The current bit offset within the stream. Subclasses are responsible for keeping this value current from any method they override that alters the bit offset.


flushedPos

protected long flushedPos
The position prior to which data may be discarded. Seeking to a smaller position is not allowed. flushedPos will always be >= 0.

Constructor Detail

SeekableInputStream

public SeekableInputStream()
Method Detail

checkClosed

protected final void checkClosed()
                          throws IOException
Throws an IOException if the stream has been closed. Subclasses may call this method from any of their methods that require the stream not to be closed.

Throws:
IOException - if the stream is closed.

close

public void close()
           throws IOException
Closes this seekable input stream. Further attempts to read from it will throw IOExceptions. Additional attempts to close(), however, have no effect.

Specified by:
close in interface Closeable
Specified by:
close in interface ImageInputStream
Overrides:
close in class InputStream
Throws:
IOException
See Also:
InputStream.close()

finalize

protected void finalize()
                 throws Throwable
Finalizes this object prior to garbage collection. The close method is called to close any open input source. This method should not be called from application code.

Overrides:
finalize in class Object
Throws:
Throwable - if an error occurs during superclass finalization.

flush

public void flush()
           throws IOException
Specified by:
flush in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.flush()

flushBefore

public void flushBefore(long pos)
                 throws IOException
Specified by:
flushBefore in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.flushBefore(long)

getBitOffset

public int getBitOffset()
                 throws IOException
Specified by:
getBitOffset in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.getBitOffset()

getByteOrder

public ByteOrder getByteOrder()
Specified by:
getByteOrder in interface ImageInputStream
See Also:
ImageInputStream.getByteOrder()

getFlushedPosition

public long getFlushedPosition()
Specified by:
getFlushedPosition in interface ImageInputStream
See Also:
ImageInputStream.getFlushedPosition()

getSizeEstimate

public abstract long getSizeEstimate()
A rough estimated size for this stream instance, which is used for internal cache management.

Returns:
an estimated size

isCached

public boolean isCached()
Default implementation returns false. Subclasses should override this if they cache data.

Specified by:
isCached in interface ImageInputStream
See Also:
ImageInputStream.isCached()

isCachedFile

public boolean isCachedFile()
Default implementation returns false. Subclasses should override this if they cache data in a temporary file.

Specified by:
isCachedFile in interface ImageInputStream
See Also:
ImageInputStream.isCachedFile()

isCachedMemory

public boolean isCachedMemory()
Default implementation returns false. Subclasses should override this if they cache data in main memory.

Specified by:
isCachedMemory in interface ImageInputStream
See Also:
ImageInputStream.isCachedMemory()

length

public abstract long length()
                     throws IOException
Returns the length of this stream contents, if available. Otherwise -1L will be returned.

Specified by:
length in interface ImageInputStream
Returns:
the length, measured in bytes or -1L if unknown.
Throws:
IOException - if an I/O error occurs.

mark

public void mark()
Pushes the current stream position onto a stack of marked positions.

Specified by:
mark in interface ImageInputStream

mark

public void mark(int readlimit)
Overrides:
mark in class InputStream

read

public abstract int read()
                  throws IOException
Reads a single byte from the stream and returns it as an int between 0 and 255. If EOF is reached, -1 is returned.

Subclasses must provide an implementation for this method. The subclass implementation should update the stream position before exiting.

The bit offset within the stream must be reset to zero before the read occurs.

Specified by:
read in interface ImageInputStream
Specified by:
read in class InputStream
Returns:
the value of the next byte in the stream, or -1 if EOF is reached.
Throws:
IOException - if the stream has been closed.

read

public int read(byte[] b)
         throws IOException
A convenience method that calls read(b, 0, b.length).

The bit offset within the stream is reset to zero before the read occurs.

Specified by:
read in interface ImageInputStream
Overrides:
read in class InputStream
Parameters:
b - the byte buffer to fill up
Returns:
the number of bytes actually read, or -1 to indicate EOF.
Throws:
NullPointerException - if b is null.
IOException - if an I/O error occurs.

read

public abstract int read(byte[] b,
                         int off,
                         int len)
                  throws IOException
Reads up to len bytes from the stream, and stores them into b starting at index off. If no bytes can be read because the end of the stream has been reached, -1 is returned.

The bit offset within the stream must be reset to zero before the read occurs.

Subclasses must provide an implementation for this method. The subclass implementation should update the stream position before exiting.

Specified by:
read in interface ImageInputStream
Overrides:
read in class InputStream
Parameters:
b - an array of bytes to be written to.
off - the starting position within b to write to.
len - the maximum number of bytes to read.
Returns:
the number of bytes actually read, or -1 to indicate EOF.
Throws:
IndexOutOfBoundsException - if off is negative, len is negative, or off + len is greater than b.length.
NullPointerException - if b is null.
IOException - if an I/O error occurs.

readBit

public int readBit()
            throws IOException
Specified by:
readBit in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readBit()

readBits

public long readBits(int numBits)
              throws IOException
Specified by:
readBits in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readBits(int)

readBoolean

public boolean readBoolean()
                    throws IOException
Specified by:
readBoolean in interface DataInput
Specified by:
readBoolean in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readBoolean()

readByte

public byte readByte()
              throws IOException
Specified by:
readByte in interface DataInput
Specified by:
readByte in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readByte()

readBytes

public void readBytes(IIOByteBuffer buf,
                      int len)
               throws IOException
Specified by:
readBytes in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readBytes(javax.imageio.stream.IIOByteBuffer, int)

readChar

public char readChar()
              throws IOException
Specified by:
readChar in interface DataInput
Specified by:
readChar in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readChar()

readDouble

public double readDouble()
                  throws IOException
Specified by:
readDouble in interface DataInput
Specified by:
readDouble in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readDouble()

readFloat

public float readFloat()
                throws IOException
Specified by:
readFloat in interface DataInput
Specified by:
readFloat in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readFloat()

readFully

public void readFully(byte[] b)
               throws IOException
Specified by:
readFully in interface DataInput
Specified by:
readFully in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readFully(byte[])

readFully

public void readFully(byte[] b,
                      int off,
                      int len)
               throws IOException
Specified by:
readFully in interface DataInput
Specified by:
readFully in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readFully(byte[], int, int)

readFully

public void readFully(char[] c,
                      int off,
                      int len)
               throws IOException
Specified by:
readFully in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readFully(char[], int, int)

readFully

public void readFully(double[] d,
                      int off,
                      int len)
               throws IOException
Specified by:
readFully in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readFully(double[], int, int)

readFully

public void readFully(float[] f,
                      int off,
                      int len)
               throws IOException
Specified by:
readFully in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readFully(float[], int, int)

readFully

public void readFully(int[] i,
                      int off,
                      int len)
               throws IOException
Specified by:
readFully in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readFully(int[], int, int)

readFully

public void readFully(long[] l,
                      int off,
                      int len)
               throws IOException
Specified by:
readFully in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readFully(long[], int, int)

readFully

public void readFully(short[] s,
                      int off,
                      int len)
               throws IOException
Specified by:
readFully in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readFully(short[], int, int)

readInt

public int readInt()
            throws IOException
Specified by:
readInt in interface DataInput
Specified by:
readInt in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readInt()

readInt

public int readInt(int appliedBytes)
            throws IOException
Reads appliedBytes bytes from the stream, and (conceptually) concatenates them according to the current byte order and returns the result as an int. As consequence, appliedBytes have to be greater than 0 and smaller or equals than 4. All other values of appliedBytes will cause an IllegalArgumentException.

The bit offset within the stream is ignored and treated as though it were zero.

Parameters:
appliedBytes -
Returns:
a signed int value from the stream.
Throws:
EOFException - if the stream reaches the end before reading all the bytes.
IOException - if an I/O error occurs.
See Also:
getByteOrder()

readLine

public String readLine()
                throws IOException
Specified by:
readLine in interface DataInput
Specified by:
readLine in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readLine()

readLong

public long readLong()
              throws IOException
Specified by:
readLong in interface DataInput
Specified by:
readLong in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readLong()

readShort

public short readShort()
                throws IOException
Specified by:
readShort in interface DataInput
Specified by:
readShort in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readShort()

readUnsignedByte

public int readUnsignedByte()
                     throws IOException
Specified by:
readUnsignedByte in interface DataInput
Specified by:
readUnsignedByte in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readUnsignedByte()

readUnsignedInt

public long readUnsignedInt()
                     throws IOException
Specified by:
readUnsignedInt in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readUnsignedInt()

readUnsignedInt

public long readUnsignedInt(int appliedBytes)
                     throws IOException
Reads appliedBytes bytes from the stream, and (conceptually) concatenates them according to the current byte order, converts the result to a long, masks it with 0xffffffffL in order to strip off any sign-extension bits, and returns the result as an unsigned long value. As consequence, appliedBytes have to be greater than 0 and smaller or equals than 4. All other values of appliedBytes will cause an IllegalArgumentException.

The bit offset within the stream is reset to zero before the read occurs.

Parameters:
appliedBytes -
Returns:
an unsigned int value from the stream, as a long.
Throws:
EOFException - if the stream reaches the end before reading all the bytes.
IOException - if an I/O error occurs.
See Also:
getByteOrder()

readUnsignedShort

public int readUnsignedShort()
                      throws IOException
Specified by:
readUnsignedShort in interface DataInput
Specified by:
readUnsignedShort in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readUnsignedShort()

readUTF

public String readUTF()
               throws IOException
Specified by:
readUTF in interface DataInput
Specified by:
readUTF in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.readUTF()

reset

public void reset()
           throws IOException
Resets the current stream byte and bit positions from the stack of marked positions.

An IOException will be thrown if the previous marked position lies in the discarded portion of the stream.

Specified by:
reset in interface ImageInputStream
Overrides:
reset in class InputStream
Throws:
IOException - if an I/O error occurs.

resetToInitialState

public void resetToInitialState()
                         throws IOException
Resets the current stream byte, bit positions from the stack of marked positions, the flushed position and seeks to 0.

Throws:
IOException - if an I/O error occurs.

seek

public abstract void seek(long pos)
                   throws IOException
Sets the current stream position, measured from the beginning of this data stream, at which the next read occurs. The offset may be set beyond the end of this data stream. Setting the offset beyond the end does not change the data length, an EOFException will be thrown only if a read is performed. The bit offset is set to 0.

An IndexOutOfBoundsException will be thrown if pos is smaller than the flushed position (as returned by getflushedPosition).

It is legal to seek past the end of the file; an EOFException will be thrown only if a read is performed.

Specified by:
seek in interface ImageInputStream
Parameters:
pos - a long containing the desired file pointer position.
Throws:
IndexOutOfBoundsException - if pos is smaller than the flushed position.
IOException - if any other I/O error occurs.

setBitOffset

public void setBitOffset(int bitOffset)
                  throws IOException
Specified by:
setBitOffset in interface ImageInputStream
Throws:
IOException
See Also:
ImageInputStream.setBitOffset(int)

setByteOrder

public void setByteOrder(ByteOrder byteOrder)
Specified by:
setByteOrder in interface ImageInputStream
See Also:
ImageInputStream.setByteOrder(java.nio.ByteOrder)

skip

public long skip(long n)
          throws IOException
Overrides:
skip in class InputStream
Throws:
IOException

skipBytes

public int skipBytes(int n)
              throws IOException
Advances the current stream position by calling seek(getStreamPosition() + n).

The bit offset is reset to zero.

Specified by:
skipBytes in interface DataInput
Specified by:
skipBytes in interface ImageInputStream
Parameters:
n - the number of bytes to seek forward.
Returns:
an int representing the number of bytes skipped.
Throws:
IOException - if getStreamPosition throws an IOException when computing either the starting or ending position.

skipBytes

public long skipBytes(long n)
               throws IOException
Advances the current stream position by calling seek(getStreamPosition() + n).

The bit offset is reset to zero.

Specified by:
skipBytes in interface ImageInputStream
Parameters:
n - the number of bytes to seek forward.
Returns:
a long representing the number of bytes skipped.
Throws:
IOException - if getStreamPosition throws an IOException when computing either the starting or ending position.


Copyright © 1995-2020 levigo holding gmbh. All Rights Reserved.