Can I search for a file from different streams regardless of FileChannel?

I created a web application that works with FLV files.

This application uses the library that I created to analyze content from FLV files. This library uses the FileChannel to search for a file.

Now I am experiencing strange behavior when I search for the same flv file from different streams. Let's say that Thread_1 and Thread_2 are simultaneously looking for movie.flv (my question comes after the example).

Thread_1

// Thread_1 moves to position 200 to read something FileChannel chan1 = new FileInputStream("movie.flv").getFileChannel(); chan1.position(200); 

Thread_2 (executed immediately after Thread_1 )

 // Thread_2 moves to position 600 to read something else FileChannel chan2 = new FileInputStream("movie.flv").getFileChannel(); chan2.position(600); 

Finally Thread_1 does:

 ByteBuffer bb = ByteBuffer.allocate(40); chan1.read(bb); 

Is Thread_1 reading 40 bytes from position 200 or from position 600? More precisely, are chan1 and chan2 independent (= can search independently) for channels or not?

I read from the documentation that FileChannel is unique , so my bet (unfortunately) is that in the example Thread_1 will be read from position 600: \

In the case, can you suggest a different approach for finding a file regardless of different streams?

thanks!

+4
source share
3 answers

I think that everything is in order, since you are creating a new FileInputStream for both streams. Related documentation claims that the FileChannel returned from FileInputStream.getChannel () is unique to this input stream . The FileChannel documentation also assumes that different FileChannels created from different sources (for example, different instances of FileInputStream) will not share state.

0
source

I wrote a library that supports writing and reading from the same file in different threads or different processes. It only works with a file (if a stream is used by only one stream)

You can find a memory mapping that a file is an easier way to share data, since the whole file is essentially in memory. You can create multiple .splice() MappedByteBuffer to access them in different places at the same time (one per stream). It will also be 10 times faster than a system call.

+3
source

The FileChannel instance is unique to the instance of the source stream. In your example, there are two different instances of FileInputStream. Therefore, I think that their channels are independent of each other.

+1
source

Source: https://habr.com/ru/post/1394640/


All Articles