Reputation: 8936
There is private InputStream inputStream;
. Now, I would like to split the inputStream into 10 chunks, which return another 10 InputStream. 10 threads will read the 10 InputStream at the same time. How to do this?
Does the following piece of codes work?
BoundedInputStream stream = new BoundedInputStream(inputStream, chunkSize);
Upvotes: 0
Views: 2664
Reputation: 718758
That code does not do what you think. A BoundedInputStream
is a stream that limits the number of bytes that can be read from the underlying stream. But it doesn't say which bytes will be returned ... if other threads are also reading from the underlying stream.
For an input stream that is reading from a non-file source, the only way to divide the stream into chunks is to read the whole stream, write it to something that can be chunked, then open separate input streams on the chunks.
If the source is a file, you could open multiple independent input streams, use seek
or equivalent to skip to the required position and read. For example (based on source):
public InputStream chunkInputStream(File file, int pos, int count) {
RandomAccessFile raf = new RandomAccessFile(file, "r");
BoundedInputStream res = new BoundedInputStream(
Channels.newInputStream(raf.getChannel().position(pos)),
count);
// res.setPropagateClose(false) ; // INCORRECT!
return res ;
}
In fact, you can avoid the RandomAccessFile
and do this:
public InputStream chunkInputStream(File file, int pos, int count) {
FileChannel channel = FileChannel.open(file, READ);
return new BoundedInputStream(
Channels.newInputStream(channel.position(pos)),
count);
}
... but the differences are purely cosmetic.
Upvotes: 4