com.sun.jimi.core.util
Class SeekInputStream

java.lang.Object
  |
  +--java.io.InputStream
        |
        +--java.io.FilterInputStream
              |
              +--com.sun.jimi.core.util.SeekInputStream

public class SeekInputStream
extends java.io.FilterInputStream
implements java.io.DataInput

Minimimum Buffering Seekable Linear processed Data Input Stream Problem TIFF file format IFD can be anywhere and so can image data no gaurantee on order This is annoying as could have to seek around back/forward. Solution So create class which returns correct stream - [file header class] This class reads in and buffers chunks of file as necessary if it needs to skip forward through file - then when user requests input stream at location 'X' it can either hand back underlying stream or returns inputstream made from byte[] array buffered. It assumes that any file section will only ever be read once so that if the underlying stream returned it is not buffered and that does not matter in the slightest. This design to minimise the amount of extra memory needed to buffer the file to allow proper decoding. In most cases this class should not be buffering anything I expect for TIFF as generally IFD will preceed image data blocks. Could do all this as a transparent InputStream class which implements DataInput and provides one extra methods seek(long pos). Then this class just acts as a proxy and selects the appropriatte input stream internally whenever seek() is called and the individual methods do appropriatte things. This buffers as little as possible and will release any buffered pieces the minute those pieces of data have been pumped out the InputStream. As it assumes that anything is read only once. - currently doesnt release anything till whole object gone - As a rule if skip() is used to go to a buffered memory area then the data will not be exhausted in the buffered area by client because of the way the data is only buffered on a skip to an area that wants to be read and it is assumed that each byte in file is only passed back to client once. As i need to have DataInput() interface to this stream and skip() only exists on this object stream this has to be the implementer of DataInput. This class makes use of DataInputStream and LEDataInputStream to provide the functionality dependent on the constructor parameter be. Example of a key assumption this class makes. Start reading file at locatin 0 read 20 bytes.then seek to location 200 and read 20 bytes then seek to location 20 and read 180 bytes. This all causes no problem but it is totall requiered that a seek() occur after reading the 180 bytes as it is invalid to read any more data.


Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
SeekInputStream(boolean be, java.io.InputStream in, int consumed)
           
 
Method Summary
 int available()
           
 void close()
          close underlying input stream and release a few handles on objects
protected  java.io.InputStream getBufIS(int location)
           
 void mark(int readlimit)
          this does nothing
 boolean markSupported()
           
 int read()
           
 int read(byte[] b, int off, int len)
           
 boolean readBoolean()
           
 byte readByte()
           
 char readChar()
           
 double readDouble()
           
 float readFloat()
           
 void readFully(byte[] b)
           
 void readFully(byte[] b, int off, int len)
           
protected static int readFully(java.io.InputStream is, byte[] b, int off, int len)
          read as much as possible from inputstream as possible
 int readInt()
           
 java.lang.String readLine()
          this method should not be called in this class
 long readLong()
           
 short readShort()
           
 int readUnsignedByte()
           
 int readUnsignedShort()
           
 java.lang.String readUTF()
          this method should not be called in this class
 java.lang.String readUTF(java.io.DataInput in)
          this method should not be called in this class
 void reset()
          this does nothing
 void seek(int pos)
          Additional method to allow seeking to byte location in file.
protected  void setInputStream(int location)
           
 long skip(long n)
           
 int skipBytes(int n)
           
 
Methods inherited from class java.io.FilterInputStream
read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SeekInputStream

public SeekInputStream(boolean be,
                       java.io.InputStream in,
                       int consumed)
                throws java.io.IOException
Parameters:
be - flag set indicates big endian input stream
in - input stream to read data from. This input stream is assumed to allready have been Buffered.
consumed - the number of bytes of data that has allready been consumed from the input stream so that any requests for an input stream at a location 'X' in the file relative to the start of the file can be catered for appropriately.
Method Detail

seek

public void seek(int pos)
          throws java.io.IOException
Additional method to allow seeking to byte location in file.
Parameters:
pos - the byte offset in the file that is desired to seek to

setInputStream

protected void setInputStream(int location)
                       throws java.io.IOException
Parameters:
location - the absolute byte location relative to the start of the file that the input stream returned should be set at.
Returns:
InputStream which could be the underling input stream or maybe a ByteArrayInputStream based on a previously buffered file section.
Throws:
java.io.IOException - thrown if error encountered either buffering data when getting to location or cannot find buffered data

getBufIS

protected java.io.InputStream getBufIS(int location)
                                throws java.io.IOException
Returns:
ByteArrayInputStream based on buffered input data if its available.
Throws:
java.io.IOException - cannot find buffered data for this location

readFully

protected static int readFully(java.io.InputStream is,
                               byte[] b,
                               int off,
                               int len)
                        throws java.io.IOException
read as much as possible from inputstream as possible
Returns:
how much data read from input stream. value returned is negative if end of file encountered. the value should be negated to figure out how much data was read upto end of file.

read

public final int read()
               throws java.io.IOException
Overrides:
read in class java.io.FilterInputStream

read

public final int read(byte[] b,
                      int off,
                      int len)
               throws java.io.IOException
Overrides:
read in class java.io.FilterInputStream

close

public void close()
           throws java.io.IOException
close underlying input stream and release a few handles on objects
Overrides:
close in class java.io.FilterInputStream

skip

public long skip(long n)
          throws java.io.IOException
Overrides:
skip in class java.io.FilterInputStream

available

public int available()
              throws java.io.IOException
Overrides:
available in class java.io.FilterInputStream

mark

public void mark(int readlimit)
this does nothing
Overrides:
mark in class java.io.FilterInputStream

markSupported

public boolean markSupported()
Returns:
false always for this class
Overrides:
markSupported in class java.io.FilterInputStream

reset

public void reset()
           throws java.io.IOException
this does nothing
Overrides:
reset in class java.io.FilterInputStream

readFully

public final void readFully(byte[] b)
                     throws java.io.IOException
Specified by:
readFully in interface java.io.DataInput

readFully

public final void readFully(byte[] b,
                            int off,
                            int len)
                     throws java.io.IOException
Specified by:
readFully in interface java.io.DataInput

skipBytes

public final int skipBytes(int n)
                    throws java.io.IOException
Specified by:
skipBytes in interface java.io.DataInput

readBoolean

public final boolean readBoolean()
                          throws java.io.IOException
Specified by:
readBoolean in interface java.io.DataInput

readByte

public final byte readByte()
                    throws java.io.IOException
Specified by:
readByte in interface java.io.DataInput

readUnsignedByte

public final int readUnsignedByte()
                           throws java.io.IOException
Specified by:
readUnsignedByte in interface java.io.DataInput

readShort

public final short readShort()
                      throws java.io.IOException
Specified by:
readShort in interface java.io.DataInput

readUnsignedShort

public final int readUnsignedShort()
                            throws java.io.IOException
Specified by:
readUnsignedShort in interface java.io.DataInput

readChar

public final char readChar()
                    throws java.io.IOException
Specified by:
readChar in interface java.io.DataInput

readInt

public final int readInt()
                  throws java.io.IOException
Specified by:
readInt in interface java.io.DataInput

readLong

public final long readLong()
                    throws java.io.IOException
Specified by:
readLong in interface java.io.DataInput

readFloat

public final float readFloat()
                      throws java.io.IOException
Specified by:
readFloat in interface java.io.DataInput

readDouble

public final double readDouble()
                        throws java.io.IOException
Specified by:
readDouble in interface java.io.DataInput

readLine

public final java.lang.String readLine()
                                throws java.io.IOException
this method should not be called in this class
Specified by:
readLine in interface java.io.DataInput

readUTF

public final java.lang.String readUTF()
                               throws java.io.IOException
this method should not be called in this class
Specified by:
readUTF in interface java.io.DataInput

readUTF

public final java.lang.String readUTF(java.io.DataInput in)
                               throws java.io.IOException
this method should not be called in this class