Java – good design: how to pass inputstreams as parameters?

I have a large file and I am opening a FileInputStream This file contains files, each with an offset from the beginning and size In addition, I have a parser that should evaluate such an included 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 don't think it's a good habit Is there a better way to do this, perhaps using buffering?

In addition, I think the skip () method will greatly slow down the reading process

Solution

It sounds like what you really want is a "partial" input stream - a bit like zipinputstream, where you have a stream

You can write it yourself, proxy all InputStream methods to the original input stream, adjust the offset appropriately, and check the contents at the end of the read sub file

Is that what you're talking about?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>