Package chemaxon.marvin.io
Class PositionedInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- java.io.BufferedInputStream
-
- chemaxon.marvin.io.PositionedInputStream
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Direct Known Subclasses:
MolInputStream
@PublicAPI public class PositionedInputStream extends 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
-
-
Field Summary
Fields Modifier and Type Field Description protected chemaxon.marvin.io.Encoding
encoding
The encoding.protected boolean
keeporiginalLineEnding
protected 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.FilterInputStream
in
-
-
Constructor Summary
Constructors Modifier Constructor Description PositionedInputStream(InputStream is)
Constructor.PositionedInputStream(InputStream is, int size)
Constructor.PositionedInputStream(InputStream is, int size, String enc)
Constructor.protected
PositionedInputStream(InputStream is, int size, String enc, int initialChunkSize)
Constructor.PositionedInputStream(InputStream is, String enc)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected int
bufincRead()
Reads a character and writes into the buffer.String
endGrabLines()
Ends grabbing lines.chemaxon.marvin.io.Encoding
getEncoding()
Gets the encoding.long
getFilePointer()
Get the file pointer.String
getGrabbedLines()
Gets the grabbed lines.String
getLastEOL()
int
getLineCount()
Gets the current line number.protected int
getWord(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.String
readLine()
Reads a line.void
reset()
Repositions this stream to the position at the time themark
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(ByteBuffer bb)
void
setDesiredBufferSize(int size)
Sets desired buffer size.void
setEncoding(chemaxon.marvin.io.Encoding enc)
Sets the encoding.void
setEncoding(String enc)
Sets the encoding.protected void
setWord(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.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
-
-
-
-
Field Detail
-
lineByteBuffer
protected ByteBuffer lineByteBuffer
Lines are read into this buffer.- See Also:
readLine()
-
lineCurrentColumn
protected int lineCurrentColumn
Column position of the next character to read.
-
keeporiginalLineEnding
protected boolean keeporiginalLineEnding
-
encoding
protected chemaxon.marvin.io.Encoding encoding
The encoding.- Since:
- Marvin 5.0, 11/10/2007
-
-
Constructor Detail
-
PositionedInputStream
public PositionedInputStream(InputStream is) throws IOException
Constructor. It tries to read 1024 bytes into the buffer and to recognize the character encoding.- Parameters:
is
- the original input stream- Throws:
IOException
-
PositionedInputStream
public PositionedInputStream(InputStream is, int size) throws IOException
Constructor. It tries to read 1024 bytes into the buffer and to recognize the character encoding.- Parameters:
is
- the original input streamsize
- the buffer size- Throws:
IOException
-
PositionedInputStream
public PositionedInputStream(InputStream is, String enc) throws 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 streamenc
- the character encoding ornull
- Throws:
IOException
- Since:
- Marvin 3.5.5
-
PositionedInputStream
public PositionedInputStream(InputStream is, int size, String enc) throws 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 streamsize
- the buffer sizeenc
- the character encoding ornull
- Throws:
IOException
- Since:
- Marvin 3.5.5
-
PositionedInputStream
protected PositionedInputStream(InputStream is, int size, String enc, int initialChunkSize) throws IOException
Constructor.- Parameters:
is
- the original input streamsize
- the buffer sizeenc
- the character encoding or nullinitialChunkSize
- number of bytes to read in advance for encoding recognition- Throws:
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 IOException
Reads a character. Warning! This method does not update the line number.- Overrides:
read
in classBufferedInputStream
- Returns:
- the character
- Throws:
IOException
- read error occurred
-
bufincRead
protected final int bufincRead() throws IOException
Reads a character and writes into the buffer. Increases the buffer size if it becomes full.- Returns:
- the character
- Throws:
IOException
- in case of read error
-
read
public int read(byte[] b, int off, int len) throws IOException
Reads a byte array. Warning! This method does not update the line number.- Overrides:
read
in classBufferedInputStream
- Parameters:
b
- the buffer into which the data is read.off
- the start offset of the datalen
- 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:
IOException
- read error occurred
-
skip
public long skip(long n) throws IOException
Skips over and discards n bytes of data. Warning! This method does not update the line number.- Overrides:
skip
in classBufferedInputStream
- Parameters:
n
- the number of bytes- Returns:
- the number of skipped bytes
- Throws:
IOException
- read error occurred
-
reset
public void reset() throws IOException
Repositions this stream to the position at the time themark
method was last called on this input stream.- Overrides:
reset
in classBufferedInputStream
- Throws:
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 String endGrabLines()
Ends grabbing lines.- Returns:
- the grabbed lines
- Since:
- Marvin 4.0, 01/05/2005
-
getGrabbedLines
public final String getGrabbedLines()
Gets the grabbed lines.- Returns:
- the grabbed lines
- Since:
- Marvin 4.0, 01/05/2005
-
readLine
public String readLine() throws IOException
Reads a line.- Returns:
- the line without the line separator character(s)
- Throws:
IOException
- If an I/O error has occurred.
-
skipLine
public boolean skipLine() throws 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:
IOException
- If an I/O error has occurred.- Since:
- Marvin 5.1.3, 10/18/2008
-
getWord
protected int getWord(ByteBuffer bb, int i)
-
setWord
protected void setWord(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 IOException
Puts back the last line into the stream. Also repositions the file pointer.- Throws:
IOException
- If an I/O error has occurred.- See Also:
readLine()
,getFilePointer()
-
putBackLine
public final void putBackLine(int col) throws 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:
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 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 positionlcount
- the line count at the specified position- Throws:
IOException
- ifpos
is less than0
or if an I/O error occurs.- Since:
- Marvin 4.1, 04/13/2006
-
length
public long length() throws IOException
Gets the file length.- Returns:
- the file length
- Throws:
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(String enc)
Sets the encoding.- Parameters:
enc
- the encoding- Throws:
IllegalCharsetNameException
- if the encoding is illegalUnsupportedCharsetException
- if the encoding is unsupported
-
setByteOrder
protected void setByteOrder(ByteBuffer bb)
-
getLastEOL
public String getLastEOL()
-
-