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 int
BF_BUFFER_SIZE
protected byte[]
buffer
static int
START_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 int
addToBuffer(int bytesToAdd)
Reads next portion of data from the underlying stream and appends it to the end of data, contained in internal buffer.byte
bufferPop()
Returns the character pointed by buffer begin marker and advances it.int
bufferPopArray(byte[] buffer, int read)
Reads data from internal buffer into passed byte array and advances begin marker.int
bufferSize()
void
closeResource()
Closes stream resource.static byte[]
concatenate(byte[] one, int lengthOne, byte[] two, int lengthTwo)
int
feedBuffer(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.int
getBufferBegin()
int
getBufferCapacity()
int
getBufferEnd()
int
getReadCounter()
ASInputStream
getStream(int length)
Gets a stream that is a piece of this stream.ASInputStream
getStreamUntilToken(byte[] token)
Gets a stream that is a piece of this stream.void
initialize()
This method should be called before using ASBufferedInFilter as buffered stream.boolean
isEOF()
byte
peek()
byte
peek(int i)
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.int
read(byte[] buffer)
int
read(byte[] buffer, int size)
byte
readByte()
void
reset()
void
resetReadCounter()
int
skip(int size)
Skips given number of decoded bytes in stream.void
unread()
void
unread(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 IOException
This method should be called before using ASBufferedInFilter as buffered stream.- Throws:
IOException
-
read
public int read(byte[] buffer, int size) throws IOException
Description copied from class:ASInFilter
- Overrides:
read
in classASInFilter
- Throws:
IOException
-
read
public int read(byte[] buffer) throws IOException
Description copied from class:ASInFilter
- Overrides:
read
in 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 IOException
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.- Parameters:
bytesToRead
- amount of bytes to read.- Returns:
- amount of bytes actually placed into buffer.
- Throws:
IOException
-
addToBuffer
public int addToBuffer(int bytesToAdd) throws IOException
Reads 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 IOException
Reads 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 IOException
Closes 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:
closeResource
in classASInFilter
- Throws:
IOException
-
reset
public void reset() throws IOException
- Overrides:
reset
in classASInFilter
- Throws:
IOException
-
concatenate
public static byte[] concatenate(byte[] one, int lengthOne, byte[] two, int lengthTwo)
-
skip
public int skip(int size) throws IOException
Skips given number of decoded bytes in stream.- Overrides:
skip
in 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
-
-