-
Notifications
You must be signed in to change notification settings - Fork 127
ICryptoolStreamUsage
Nils Kopal edited this page Feb 15, 2022
·
4 revisions
ICryptoolStream is a replacement for the now obsolete CryptoolStream class. With ICryptoolStream a plugin can pass data as byte stream to other plugins.
Features:
- Supports 0..n concurrent readers on one ICryptoolStream
- Clean separation between writer and reader -- reading plugins can't disturb the writing plugin or other reading instances
- Does not forget data -- new readers can be created at any time and support random seeking
- Uses internal byte array as underlying buffer and switches automagically to a temporary file, if data exceeds a certain size
- Supports chunked streaming -- receivers can read and process data even if the sender has not finished writing
- https://github.com/CrypToolProject/CrypTool-2/blob/main/CrypPluginBase/IO/ICryptoolStream.cs
- https://github.com/CrypToolProject/CrypTool-2/blob/main/CrypPluginBase/IO/CStreamWriter.cs
- https://github.com/CrypToolProject/CrypTool-2/blob/main/CrypPluginBase/IO/CStreamReader.cs
- Create an output property (e.g. called "OutputStream") with type ICryptoolStream.
- In Execute() create a new CStreamWriter.
- Set the CStreamWriter as OutputStream property (will be implicitly downcasted).
- Announce the OutputStream update with OnPropertyChanged("OutputStream").
- Pass data to the stream via CStreamWriter.Write().
- Call CStreamWriter.Close() when finished.
- Best practice is to not care about disposal of writer.
- Create an input property (e.g. called "InputStream") with type ICryptoolStream.
- In Execute() call CreateReader() on that ICryptoolStream to get a new CStreamReader.
- Retrieve data with CStreamReader.Read() in a loop.
- End loop when read attempt returns 0 bytes.
- Beware of evaluating CStreamReader.Length: writer may not have finished writing to the stream, so the length may grow.
- Best practice is to not care about disposal of reader.
- You may use CStreamReader.WaitEof() to wait until the writer has closed the stream. When the method returns, the writer has finished and CStreamReader.Length will remain constant.
- If you don't like the property of CStreamReader.Read(byte[]), that it may return less bytes than requested, you can use CStreamReader.ReadFully(byte[]). Still, when you reach the end of a closed stream it will return less bytes.
- If you're superlazy, CStreamReader.ReadFully() (without parameters) will do all the stream reading and waiting work and deliver everything as a single byte[]. However, it is terribly inefficient and you may get memory problems when processing large data amounts. Try to avoid it.