An API that I’m implementing deals with InputStreams containing hierarchically structured data i.e. nested blocks, including a number of images in the leaf blocks. (If you must know, it is CBEFF data that I’m parsing.) Each block of data is prefixed with a header containing some meta-data about that block.
1st level 1 header
1st level 2 header
1st level 2 data block
2nd level 2 header
2nd level 2 data block
2nd level 1 header
3rd level 2 header
3rd level 2 data block
The original InputStream is an argument to the constructor of my API classes and is passed around down the hierarchy.
Currently I’m reading the images into byte arrays in the constructor of my API classes, so each constructor blocks while reading the complete data that that class is responsible for and later on when clients call the relevant getter method of that API class they will get the image data served from memory. I’d much rather offer the contained images in the form of some kind of lazy InputStreams to clients of my API, so that the image bytes are only read from the original InputStream as a result of clients reading bytes of the resulting InputStream delivered by the getter. This makes it possible, for example, to do progressive rendering, which is useful as the original InputStream is slow.
Is there an elegant way to solve this with InputStreams?
InputStreamisn’t suitable for random access. So reading parts of it isn’t going to work in most cases, even though you can achieve similar effects on some input streams using a combination ofresetandskip. But not all streams supportreset, and skipping bytes is often as expensive as reading bytes.So I suggest you try some alternate approach. Either you buffer the whole stream to some random-access buffer, like a temporary file, which still means reading all the bytes off the stream in the first place. Or you find a way to get random access to the original source. You didn’t specify what kind of source you’re dealing with, but e.g. for a HTTP connection you can download parts using a range request. Similar solutions might work for other sources.
No matter how you implement the random access (and seeing your comment, you’ll likely do so using an
InputStreamwithresetandskip), you can create your own class to represent a part of that stream. You can let that class itself be an instance ofInputStreamby subclassingFilterInputStream.You’d have to ensure that any operation using one of these streams calls
resetfirst. If you need to enforce a proper end-of-stream treatment, you’ll have to override mostreadimplementations. If concurrent access might be possible, then you’ll want to synchronize operations on the underlying stream. So the code using this class could look something like this: