I'm writing an InputStream
that processes data streams containing "trailer" data. That is, the final n bytes of the stream is a piece of data that must be handled separately and shouldn't be returned from the read
methods.
My class will maintain an internal byte array of length n and read data into this byte array from the underlying stream. Data will then be popped from the front of the buffer to service the requests to read
.
Implementing the int read()
method is simple enough - I just call the same method on the underlying stream, shift my byte array by 1 and pop the result to the caller.
Implementing int read(byte[] b)
is more challenging as it requires shifting a large number of bytes through my n-byte buffer, whilst popping and collating the bytes from the front. Essentially a "sliding window" buffer, always containing the last n bytes from my underlying stream.
I get the feeling I'm re-inventing the wheel if I start coding this from scratch. Can anyone suggest if I've overlooked an existing InputStream
or Queue
implementation that would be well suited to this task?