public class ChunkSeqReader extends Object implements IBytesConsumer
This has little intelligence, it's quite low-level and general (it could even be used for a MNG stream, for example). It supports signature recognition and idat deflate
Modifier and Type | Field and Description |
---|---|
protected static int |
SIGNATURE_LEN |
protected boolean |
withSignature |
Constructor and Description |
---|
ChunkSeqReader()
Creates a ChunkSeqReader (with signature)
|
ChunkSeqReader(boolean withSignature) |
Modifier and Type | Method and Description |
---|---|
protected void |
checkSignature(byte[] buf)
Throws PngjInputException if bad signature
|
void |
close()
Closes this object and release resources.
|
int |
consume(byte[] buffer,
int offset,
int len)
Consumes (in general, partially) a number of bytes.
|
protected ChunkReader |
createChunkReaderForNewChunk(String id,
int len,
long offset,
boolean skip)
This will be called for all chunks (even skipped), except for IDAT-like non-skiped chunks
The default behaviour is to create a ChunkReader in BUFFER mode (or SKIP if skip==true) that calls
postProcessChunk(ChunkReader) (always) when done. |
protected DeflatedChunksSet |
createIdatSet(String id)
DeflatedChunksSet factory.
|
protected String |
endChunkId()
Which should be the id of the last chunk
|
boolean |
feedAll(byte[] buf,
int off,
int len)
Trys to feeds exactly len bytes, calling
consume(byte[], int, int) retrying if necessary. |
void |
feedFromFile(File f)
Reads all content from a file.
|
void |
feedFromInputStream(InputStream is) |
void |
feedFromInputStream(InputStream is,
boolean closeStream)
Reads all content from an input stream.
|
protected String |
firstChunkId()
Which should be the id of the first chunk
|
long |
getBytesCount()
total of bytes read (buffered or not)
|
int |
getChunkCount() |
ChunkReader |
getCurChunkReader()
Currently reading chunk, or just ended reading
|
DeflatedChunksSet |
getCurReaderDeflatedSet()
The latest deflated set (typically IDAT chunks) reader.
|
long |
getIdatBytes()
Helper method, reports amount of bytes inside IDAT chunks.
|
boolean |
isAtChunkBoundary()
Returns true if we are not in middle of a chunk: we have just ended reading past chunk , or we are at the start, or
end of signature, or we are done
|
boolean |
isDone()
If true, we either have processe the IEND chunk, or close() has been called, or a fatal error has happened
|
protected boolean |
isIdatKind(String id)
Decides if this Chunk is of "IDAT" kind (in concrete: if it is, and if it's not to be skiped, a DeflatedChunksSet
will be created to deflate it and process+ the deflated data)
This implementation always returns always false
|
boolean |
isSignatureDone()
If false, we are still reading the signature
|
protected void |
postProcessChunk(ChunkReader chunkR)
This is called after a chunk is read, in all modes
This implementation only chenks the id of the first chunk, and process the IEND chunk (sets done=true)
Further processing should be overriden (call this first!)
|
protected boolean |
shouldCheckCrc(int len,
String id) |
protected boolean |
shouldSkipContent(int len,
String id)
Chunks can be skipped depending on id and/or length.
|
protected void |
startNewChunk(int len,
String id,
long offset)
Called for all chunks when a chunk start has been read (id and length), before the chunk data itself is read.
|
protected static final int SIGNATURE_LEN
protected final boolean withSignature
public ChunkSeqReader()
public ChunkSeqReader(boolean withSignature)
withSignature
- If true, the stream is assumed be prepended by 8 bit signaturepublic int consume(byte[] buffer, int offset, int len)
startNewChunk(int, String, long)
When data from a chunk is being read, it delegates to ChunkReader.feedBytes(byte[], int, int)
The caller might want to call this method more than once in succesion
This should rarely be overridenconsume
in interface IBytesConsumer
buffer
- offset
- Offset in bufferlen
- Valid bytes that can be consumedpublic boolean feedAll(byte[] buf, int off, int len)
consume(byte[], int, int)
retrying if necessary.
This should only be used in callback modeprotected void startNewChunk(int len, String id, long offset)
getCurChunkReader()
) in the corresponding mode, and
eventually a curReaderDeflatedSet.(field accesible via getCurReaderDeflatedSet()
)
To decide the mode and options, it calls shouldCheckCrc(int, String)
,
shouldSkipContent(int, String)
, isIdatKind(String)
. Those methods should be overriden in
preference to this; if overriden, this should be called first.
The respective ChunkReader.chunkDone()
method is directed to this postProcessChunk(ChunkReader)
.
Instead of overriding this, see also createChunkReaderForNewChunk(String, int, long, boolean)
protected ChunkReader createChunkReaderForNewChunk(String id, int len, long offset, boolean skip)
postProcessChunk(ChunkReader)
(always) when done.id
- Chunk idlen
- Chunk lengthoffset
- offset inside PNG stream , merely informativeskip
- flag: is true, the content will not be buffered (nor processed)protected void postProcessChunk(ChunkReader chunkR)
protected DeflatedChunksSet createIdatSet(String id)
protected boolean isIdatKind(String id)
id
- protected boolean shouldSkipContent(int len, String id)
len
- id
- protected boolean shouldCheckCrc(int len, String id)
protected void checkSignature(byte[] buf)
buf
- Signature. Should be of length 8public boolean isSignatureDone()
public boolean isDone()
public long getBytesCount()
public int getChunkCount()
public ChunkReader getCurChunkReader()
public DeflatedChunksSet getCurReaderDeflatedSet()
public void close()
public boolean isAtChunkBoundary()
protected String firstChunkId()
public long getIdatBytes()
protected String endChunkId()
public void feedFromFile(File f)
public void feedFromInputStream(InputStream is, boolean closeStream)
is
- closeStream
- Closes the input stream when done (or if error)public void feedFromInputStream(InputStream is)
Copyright © 2014. All rights reserved.