public class TailStream
extends java.io.FilterInputStream
A specialized input stream implementation which records the last portion read from an underlying stream.
This stream implementation is useful to deal with information which is known to be located at the end of a stream (e.g. ID3 v1 tags). While reading bytes from the underlying stream, a given number of bytes is kept in an internal buffer. This buffer can then be queried after the whole stream was read. It contains the last bytes read from the original input stream.
Modifier and Type | Field and Description |
---|---|
private long |
bytesRead
The number of bytes that have been read so far.
|
private int |
currentIndex
The current index into the tail buffer.
|
private byte[] |
markBuffer
A copy of the internal tail buffer used for mark() operations.
|
private long |
markBytesRead
The number of bytes read at the last mark() operation.
|
private int |
markIndex
A copy of the current index used for mark() operations.
|
private static int |
SKIP_SIZE
Constant for the default skip buffer size.
|
private byte[] |
tailBuffer
The buffer in which the tail data is stored.
|
private int |
tailSize
The size of the internal tail buffer.
|
Constructor and Description |
---|
TailStream(java.io.InputStream in,
int size)
Creates a new instance of
TailStream . |
Modifier and Type | Method and Description |
---|---|
private void |
appendBuf(byte[] buf,
int ofs,
int length)
Adds the content of the given buffer to the internal tail buffer.
|
private void |
appendByte(byte b)
Adds the given byte to the internal tail buffer.
|
private void |
copyToTailBuffer(byte[] buf,
int ofs,
int length)
Copies the given buffer into the internal tail buffer at the current
position.
|
byte[] |
getTail()
Returns an array with the last data read from the underlying stream.
|
void |
mark(int limit)
This implementation saves the internal state including the
content of the tail buffer so that it can be restored when ''reset()'' is
called later.
|
int |
read()
This implementation adds the read byte to the internal tail
buffer.
|
int |
read(byte[] buf)
This implementation delegates to the underlying stream and
then adds the correct portion of the read buffer to the internal tail
buffer.
|
int |
read(byte[] buf,
int ofs,
int length)
This implementation delegates to the underlying stream and
then adds the correct portion of the read buffer to the internal tail
buffer.
|
private void |
replaceTailBuffer(byte[] buf,
int ofs,
int length)
Replaces the content of the internal tail buffer by the last portion of
the given buffer.
|
void |
reset()
This implementation restores this stream's state to the
state when ''mark()'' was called the last time.
|
long |
skip(long n)
This implementation delegates to the
read() method
to ensure that the tail buffer is also filled if data is skipped. |
private static final int SKIP_SIZE
private final byte[] tailBuffer
private final int tailSize
private byte[] markBuffer
private long bytesRead
private long markBytesRead
private int currentIndex
private int markIndex
public TailStream(java.io.InputStream in, int size)
TailStream
.in
- the underlying input streamsize
- the size of the tail bufferpublic int read() throws java.io.IOException
read
in class java.io.FilterInputStream
java.io.IOException
public int read(byte[] buf) throws java.io.IOException
read
in class java.io.FilterInputStream
java.io.IOException
public int read(byte[] buf, int ofs, int length) throws java.io.IOException
read
in class java.io.FilterInputStream
java.io.IOException
public long skip(long n) throws java.io.IOException
read()
method
to ensure that the tail buffer is also filled if data is skipped.skip
in class java.io.FilterInputStream
java.io.IOException
public void mark(int limit)
mark
in class java.io.FilterInputStream
public void reset()
reset
in class java.io.FilterInputStream
public byte[] getTail()
private void appendByte(byte b)
b
- the byte to be addedprivate void appendBuf(byte[] buf, int ofs, int length)
buf
- the bufferofs
- the start offset in the bufferlength
- the number of bytes to be copiedprivate void replaceTailBuffer(byte[] buf, int ofs, int length)
buf
- the bufferofs
- the start offset in the bufferlength
- the number of bytes to be copiedprivate void copyToTailBuffer(byte[] buf, int ofs, int length)
buf
- the bufferofs
- the start offset in the bufferlength
- the number of bytes to be copied