Package org.verapdf.as.filters.io
Class ASBufferedInFilter
- java.lang.Object
-
- java.io.InputStream
-
- org.verapdf.as.io.ASInputStream
-
- org.verapdf.as.filters.ASInFilter
-
- org.verapdf.as.filters.io.ASBufferedInFilter
-
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
COSFilterAESDecryptionDefault,COSFilterASCII85Decode,COSFilterASCIIHexDecode,COSFilterFlateDecode,COSFilterLZWDecode,COSFilterRC4DecryptionDefault,COSPredictorDecode,EexecFilterDecode
public class ASBufferedInFilter extends ASInFilter
Class provides buffered input from input stream. It has two uses. If the ASBufferedInFilter object is used as a buffered stream (e. g. in unseekable parsers) then the buffer holds DECODED bytes read from inlaying stream. Before using the ASBufferedInFilter in this make sure to call initialize() method. In filter classes that are inherited from ASBufferedInFilter the buffer holds ENCODED bytes that are processed into decoded bytes on read() method calls.- Author:
- Sergey Shemyakov
-
-
Field Summary
Fields Modifier and Type Field Description static intBF_BUFFER_SIZEprotected byte[]bufferstatic intSTART_BUFFER_SIZE-
Fields inherited from class org.verapdf.as.io.ASInputStream
isClosed, isSourceClosed, resourceUsers
-
-
Constructor Summary
Constructors Constructor Description ASBufferedInFilter(ASInputStream stream)ASBufferedInFilter(ASInputStream stream, int buffCapacity)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intaddToBuffer(int bytesToAdd)Reads next portion of data from the underlying stream and appends it to the end of data, contained in internal buffer.bytebufferPop()Returns the character pointed by buffer begin marker and advances it.intbufferPopArray(byte[] buffer, int read)Reads data from internal buffer into passed byte array and advances begin marker.intbufferSize()voidcloseResource()Closes stream resource.static byte[]concatenate(byte[] one, int lengthOne, byte[] two, int lengthTwo)intfeedBuffer(int bytesToRead)Reads next portion of data from the underlying stream to the internal buffer, updates begin and end pointers and returns number of bytes actually placed in buffer.intgetBufferBegin()intgetBufferCapacity()intgetBufferEnd()intgetReadCounter()ASInputStreamgetStream(int length)Gets a stream that is a piece of this stream.ASInputStreamgetStreamUntilToken(byte[] token)Gets a stream that is a piece of this stream.voidinitialize()This method should be called before using ASBufferedInFilter as buffered stream.booleanisEOF()bytepeek()bytepeek(int i)intprocessBuffer(int bytesToProcess)Shifts begin marker by up to bytesToProceed bytes to the right of to the end of the buffer if bytesToProceed is too big.intread(byte[] buffer)intread(byte[] buffer, int size)bytereadByte()voidreset()voidresetReadCounter()intskip(int size)Skips given number of decoded bytes in stream.voidunread()voidunread(int i)-
Methods inherited from class org.verapdf.as.filters.ASInFilter
close, decrementResourceUsers, getInputStream, incrementResourceUsers, read, setInputStream
-
Methods inherited from class org.verapdf.as.io.ASInputStream
createStreamFromStream
-
Methods inherited from class java.io.InputStream
available, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, skip, transferTo
-
-
-
-
Field Detail
-
START_BUFFER_SIZE
public static final int START_BUFFER_SIZE
- See Also:
- Constant Field Values
-
BF_BUFFER_SIZE
public static final int BF_BUFFER_SIZE
- See Also:
- Constant Field Values
-
buffer
protected byte[] buffer
-
-
Constructor Detail
-
ASBufferedInFilter
public ASBufferedInFilter(ASInputStream stream) throws IOException
- Throws:
IOException
-
ASBufferedInFilter
public ASBufferedInFilter(ASInputStream stream, int buffCapacity)
-
-
Method Detail
-
initialize
public void initialize() throws IOExceptionThis method should be called before using ASBufferedInFilter as buffered stream.- Throws:
IOException
-
read
public int read(byte[] buffer, int size) throws IOExceptionDescription copied from class:ASInFilter- Overrides:
readin classASInFilter- Throws:
IOException
-
read
public int read(byte[] buffer) throws IOExceptionDescription copied from class:ASInFilter- Overrides:
readin classASInFilter- Throws:
IOException
-
readByte
public byte readByte() throws IOException- Throws:
IOException
-
processBuffer
public int processBuffer(int bytesToProcess)
Shifts begin marker by up to bytesToProceed bytes to the right of to the end of the buffer if bytesToProceed is too big.- Parameters:
bytesToProcess- amount of bytes to shift.- Returns:
- amount of bytes actually processed.
-
feedBuffer
public int feedBuffer(int bytesToRead) throws IOExceptionReads next portion of data from the underlying stream to the internal buffer, updates begin and end pointers and returns number of bytes actually placed in buffer.- Parameters:
bytesToRead- amount of bytes to read.- Returns:
- amount of bytes actually placed into buffer.
- Throws:
IOException
-
addToBuffer
public int addToBuffer(int bytesToAdd) throws IOExceptionReads next portion of data from the underlying stream and appends it to the end of data, contained in internal buffer.- Parameters:
bytesToAdd- amount of bytes to read.- Returns:
- amount of bytes actually appended to buffer.
- Throws:
IOException
-
getBufferBegin
public int getBufferBegin()
- Returns:
- beginning index of unread data in buffer.
-
getBufferEnd
public int getBufferEnd()
- Returns:
- index of the end of of valid unread data in buffer.
-
bufferPop
public byte bufferPop()
Returns the character pointed by buffer begin marker and advances it.- Returns:
- character, pointed by buffer begin marker.
-
bufferPopArray
public int bufferPopArray(byte[] buffer, int read) throws IOExceptionReads data from internal buffer into passed byte array and advances begin marker.- Parameters:
buffer- is byte array where data will be read.read- maximal amount of bytes to read.- Returns:
- amount of actually read bytes.
- Throws:
IOException- if passed buffer is too small to contain necessary amount of bytes.
-
bufferSize
public int bufferSize()
- Returns:
- the number of bytes currently available in the buffer.
-
getBufferCapacity
public int getBufferCapacity()
- Returns:
- the total capacity of buffer.
-
closeResource
public void closeResource() throws IOExceptionCloses stream resource. There is a difference between closing stream and closing it's resource. Several streams may have the same resource (e. g. the same file stream) and resource should be closed only after all streams using it are closed.- Overrides:
closeResourcein classASInFilter- Throws:
IOException
-
reset
public void reset() throws IOException- Overrides:
resetin classASInFilter- Throws:
IOException
-
concatenate
public static byte[] concatenate(byte[] one, int lengthOne, byte[] two, int lengthTwo)
-
skip
public int skip(int size) throws IOExceptionSkips given number of decoded bytes in stream.- Overrides:
skipin classASInFilter- Parameters:
size- is amount of bytes to skip.- Returns:
- amount of actually skipped bytes.
- Throws:
IOException- if stream-reading error occurs.
-
peek
public byte peek() throws IOException- Throws:
IOException
-
peek
public byte peek(int i) throws IOException- Throws:
IOException
-
unread
public void unread() throws IOException- Throws:
IOException
-
unread
public void unread(int i) throws IOException- Throws:
IOException
-
isEOF
public boolean isEOF()
-
resetReadCounter
public void resetReadCounter()
-
getReadCounter
public int getReadCounter()
-
getStream
public ASInputStream getStream(int length) throws IOException
Gets a stream that is a piece of this stream. The next length bytes will be the data in the new stream.- Parameters:
length- is the length of new stream.- Returns:
- new stream.
- Throws:
IOException
-
getStreamUntilToken
public ASInputStream getStreamUntilToken(byte[] token) throws IOException
Gets a stream that is a piece of this stream. The data is taken from the current buffer position until the given token is not found.- Parameters:
token- is the byte array that means the end of stream.- Returns:
- new stream.
- Throws:
IOException
-
-