chemaxon.marvin.io
Class PositionedInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by java.io.BufferedInputStream
              extended by chemaxon.marvin.io.PositionedInputStream
All Implemented Interfaces:
java.io.Closeable
Direct Known Subclasses:
MolInputStream

public class PositionedInputStream
extends java.io.BufferedInputStream

Positioned input stream that has the ability to set / get the stream position as well as put back some parts already read.

Since:
Marvin 4.0, 02/25/2005
Version:
5.2, 10/18/2008
Author:
Nora Mate, Peter Csizmadia, Jozsef Kovacs

Field Summary
protected  chemaxon.marvin.io.Encoding encoding
          The encoding.
protected  java.nio.ByteBuffer lineByteBuffer
          Lines are read into this buffer.
protected  int lineCurrentColumn
          Column position of the next character to read.
 
Fields inherited from class java.io.BufferedInputStream
buf, count, marklimit, markpos, pos
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
  PositionedInputStream(java.io.InputStream is)
          Constructor.
  PositionedInputStream(java.io.InputStream is, int size)
          Constructor.
  PositionedInputStream(java.io.InputStream is, int size, java.lang.String enc)
          Constructor.
protected PositionedInputStream(java.io.InputStream is, int size, java.lang.String enc, int initialChunkSize)
          Constructor.
  PositionedInputStream(java.io.InputStream is, java.lang.String enc)
          Constructor.
 
Method Summary
protected  int bufincRead()
          Reads a character and writes into the buffer.
 java.lang.String endGrabLines()
          Ends grabbing lines.
 chemaxon.marvin.io.Encoding getEncoding()
          Gets the encoding.
 long getFilePointer()
          Get the file pointer.
 java.lang.String getGrabbedLines()
          Gets the grabbed lines.
 int getLineCount()
          Gets the current line number.
protected  int getWord(java.nio.ByteBuffer bb, int i)
           
 boolean isSeekable()
          Tests whether the stream is seekable.
 long length()
          Gets the file length.
 void putBackLine()
          Puts back the last line into the stream.
 void putBackLine(int col)
          Puts back the last line into the stream.
 int read()
          Reads a character.
 int read(byte[] b, int off, int len)
          Reads a byte array.
 java.lang.String readLine()
          Reads a line.
 void reset()
          Repositions this stream to the position at the time the mark method was last called on this input stream.
protected  void resetFilePointer()
          Resets file pointer to zero.
 void seek(long p, int lcount)
          Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
protected  void setByteOrder(java.nio.ByteBuffer bb)
           
 void setDesiredBufferSize(int size)
          Sets desired buffer size.
 void setEncoding(chemaxon.marvin.io.Encoding enc)
          Sets the encoding.
 void setEncoding(java.lang.String enc)
          Sets the encoding.
protected  void setWord(java.nio.ByteBuffer bb, int i, int c)
           
 long skip(long n)
          Skips over and discards n bytes of data.
 boolean skipLine()
          Skips the next line.
 void startGrabLines()
          Starts grabbing lines.
 
Methods inherited from class java.io.BufferedInputStream
available, close, mark, markSupported
 
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
 

Field Detail

lineByteBuffer

protected java.nio.ByteBuffer lineByteBuffer
Lines are read into this buffer.

See Also:
readLine()

lineCurrentColumn

protected int lineCurrentColumn
Column position of the next character to read.


encoding

protected chemaxon.marvin.io.Encoding encoding
The encoding.

Since:
Marvin 5.0, 11/10/2007
Constructor Detail

PositionedInputStream

public PositionedInputStream(java.io.InputStream is)
                      throws java.io.IOException
Constructor. It tries to read 1024 bytes into the buffer and to recognize the character encoding.

Parameters:
is - the original input stream
Throws:
java.io.IOException

PositionedInputStream

public PositionedInputStream(java.io.InputStream is,
                             int size)
                      throws java.io.IOException
Constructor. It tries to read 1024 bytes into the buffer and to recognize the character encoding.

Parameters:
is - the original input stream
size - the buffer size
Throws:
java.io.IOException

PositionedInputStream

public PositionedInputStream(java.io.InputStream is,
                             java.lang.String enc)
                      throws java.io.IOException
Constructor. It tries to read 1024 bytes into the buffer and to recognize the character encoding if it is not specified (null).

Parameters:
is - the original input stream
enc - the character encoding or null
Throws:
java.io.IOException
Since:
Marvin 3.5.5

PositionedInputStream

public PositionedInputStream(java.io.InputStream is,
                             int size,
                             java.lang.String enc)
                      throws java.io.IOException
Constructor. It tries to read 1024 bytes into the buffer and to recognize the character encoding if it is not specified (null).

Parameters:
is - the original input stream
size - the buffer size
enc - the character encoding or null
Throws:
java.io.IOException
Since:
Marvin 3.5.5

PositionedInputStream

protected PositionedInputStream(java.io.InputStream is,
                                int size,
                                java.lang.String enc,
                                int initialChunkSize)
                         throws java.io.IOException
Constructor.

Parameters:
is - the original input stream
size - the buffer size
enc - the character encoding or null
initialChunkSize - number of bytes to read in advance for encoding recognition
Throws:
java.io.IOException
Since:
Marvin 5.0, 11/10/2007
Method Detail

setDesiredBufferSize

public void setDesiredBufferSize(int size)
Sets desired buffer size.

Parameters:
size - the desired buffer size

read

public int read()
         throws java.io.IOException
Reads a character. Warning! This method does not update the line number.

Overrides:
read in class java.io.BufferedInputStream
Returns:
the character
Throws:
java.io.IOException - read error occured

bufincRead

protected final int bufincRead()
                        throws java.io.IOException
Reads a character and writes into the buffer. Increases the buffer size if it becomes full.

Returns:
the character
Throws:
java.io.IOException - in case of read error

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Reads a byte array. Warning! This method does not update the line number.

Overrides:
read in class java.io.BufferedInputStream
Parameters:
b - the buffer into which the data is read.
off - the start offset of the data
len - maximum number of bytes to read
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
java.io.IOException - read error occured

skip

public long skip(long n)
          throws java.io.IOException
Skips over and discards n bytes of data. Warning! This method does not update the line number.

Overrides:
skip in class java.io.BufferedInputStream
Parameters:
n - the number of bytes
Returns:
the number of skipped bytes
Throws:
java.io.IOException - read error occured

reset

public void reset()
           throws java.io.IOException
Repositions this stream to the position at the time the mark method was last called on this input stream.

Overrides:
reset in class java.io.BufferedInputStream
Throws:
java.io.IOException - if this stream has not been marked or if the mark has been invalidated.

startGrabLines

public final void startGrabLines()
Starts grabbing lines. Lines will be grabbed while reading them at each readLine call. The resulting text will be separated by \n characters (UNIX line separator).

Since:
Marvin 4.0, 01/05/2005
See Also:
endGrabLines(), getGrabbedLines()

endGrabLines

public final java.lang.String endGrabLines()
Ends grabbing lines.

Returns:
the grabbed lines
Since:
Marvin 4.0, 01/05/2005

getGrabbedLines

public final java.lang.String getGrabbedLines()
Gets the grabbed lines.

Returns:
the grabbed lines
Since:
Marvin 4.0, 01/05/2005

readLine

public java.lang.String readLine()
                          throws java.io.IOException
Reads a line.

Returns:
the line without the line separator character(s)
Throws:
java.io.IOException - If an I/O error has occurred.

skipLine

public boolean skipLine()
                 throws java.io.IOException
Skips the next line. The skipped line is not stored and cannot be put back into the stream. Moreover, line grabbing is disabled.

Returns:
true if a line was skipped successfully, false at end of file
Throws:
java.io.IOException - If an I/O error has occurred.
Since:
Marvin 5.1.3, 10/18/2008

getWord

protected int getWord(java.nio.ByteBuffer bb,
                      int i)

setWord

protected void setWord(java.nio.ByteBuffer bb,
                       int i,
                       int c)

getLineCount

public final int getLineCount()
Gets the current line number. Only lines read with readLine() are counted.

Returns:
the line number

putBackLine

public final void putBackLine()
                       throws java.io.IOException
Puts back the last line into the stream. Also repositions the file pointer.

Throws:
java.io.IOException - If an I/O error has occurred.
See Also:
readLine(), getFilePointer()

putBackLine

public final void putBackLine(int col)
                       throws java.io.IOException
Puts back the last line into the stream. Also repositions the file pointer.

Parameters:
col - put back the substring starting at this column
Throws:
java.io.IOException - If an I/O error has occurred.
Since:
Marvin 5.0.1, 01/12/2008
See Also:
readLine(), getFilePointer()

resetFilePointer

protected final void resetFilePointer()
Resets file pointer to zero.


getFilePointer

public final long getFilePointer()
Get the file pointer.

Returns:
the file pointer

isSeekable

public boolean isSeekable()
Tests whether the stream is seekable.

Returns:
true if the stream is seekable, false otherwise
Since:
Marvin 4.1, 04/13/2006

seek

public void seek(long p,
                 int lcount)
          throws java.io.IOException
Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.

Parameters:
p - the offset position
lcount - the line count at the specified position
Throws:
java.io.IOException - if pos is less than 0 or if an I/O error occurs.
Since:
Marvin 4.1, 04/13/2006

length

public long length()
            throws java.io.IOException
Gets the file length.

Returns:
the file length
Throws:
java.io.IOException - if the file length cannot be determined
Since:
Marvin 4.1, 04/18/2006

getEncoding

public final chemaxon.marvin.io.Encoding getEncoding()
Gets the encoding.

Returns:
the encoding or null
Since:
Marvin 5.0, 11/20/2007

setEncoding

public final void setEncoding(chemaxon.marvin.io.Encoding enc)
Sets the encoding.

Parameters:
enc - the encoding
Since:
Marvin 5.0, 11/20/2007

setEncoding

public final void setEncoding(java.lang.String enc)
Sets the encoding.

Parameters:
enc - the encoding
Throws:
java.nio.charset.IllegalCharsetNameException - if the encoding is illegal
java.nio.charset.UnsupportedCharsetException - if the encoding is unsupported

setByteOrder

protected void setByteOrder(java.nio.ByteBuffer bb)