borisbrodski / sevenzipjbinding

7-Zip-JBinding
http://sevenzipjbind.sourceforge.net/
Other
176 stars 50 forks source link

when exactly ISequentialInStream implementation requires thread safe measures #37

Closed gonearewe closed 3 years ago

gonearewe commented 3 years ago

I've read #15 and #21 , and I'm planning to make a converter from java.io.InputStream to ISequentialInStream. But I just found out the Note for ISequentialInStream in version '16.02-2.01'

depending on the archive format and the data size this method may be called from different threads. Synchronized implementation may be required.

But I'm not sure when exactly this will be called from different threads, can I control the multithreading with Java API, or should I make the converter always thread safe anyway (at a cost, of course).

borisbrodski commented 3 years ago

Hey, thank you very much for the offer. If you plan to use your code with all possible archive formats, especially RAR and 7z, you will need a thread safe implementation. If you just interested in extracting TAR (for example), your will get away with a non thread safe implementation.

To convert "push" to "pull" you normally use additional "producer-consumer" thread with a small buffer. In this case you will need some kind of synchronization anyway, so implementing everything thread safe shouldn't add to much complexity.

You can also employ an optimization for small streams, (e.g. under 4k) where you read the entire stream into the memory avoiding overhead of starting new thread.

But there are my thoughts. You are free to do whatever you find cool for you :)

PS In case you want to contribute: I would be happy to add an universal converter to the code base. In case of a short non universal solution, I could add it to the documentation as an example.

gonearewe commented 3 years ago

Thanks for your reply, it helps a lot.