I’ve got a big file on which I’m opening a FileInputStream. This file contains some files each having an offset from the beginning and a size. Furthermore, I’ve got a parser that should evaluate such a contained file.
File file = ...; // the big file long offset = 1734; // a contained file's offset long size = 256; // a contained file's size FileInputStream fis = new FileInputStream(file ); fis.skip(offset); parse(fis, size); public void parse(InputStream is, long size) { // parse stream data and insure we don't read more than size bytes is.close(); }
I feel like this is no good practice. Is there a better way to do this, maybe using buffering?
Furthermore, I feel like the skip() method slows the reading process a lot.
It sounds like what you really want is a sort of ‘partial’ input stream – one a bit like the ZipInputStream, where you’ve got a stream within a stream.
You could write this yourself, proxying all InputStream methods to the original input stream making suitable adjustments for offset and checking for reading past the end of the subfile.
Is that the sort of thing you’re talking about?