Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement DataStream inheritance with Stream and support of Readers and Writers with Stream #161

Merged
merged 2 commits into from Dec 26, 2020

Conversation

pleonex
Copy link
Member

@pleonex pleonex commented Dec 25, 2020

Description

As explained in #158, in order to improve the compatibility of Yarhl with other frameworks, it would be great if DataStream can be treated as a regular Stream, the standard base type for streams in .NET. Also, make other reader and writer class to work with it.

This PR obsolete some methods, these methods are marked with the Obsolete attribute and they are not guarantee to work (no unit test or code coverage). They can be removed in any future release.

Stream has a CopyTo(Stream) method but I've kept the WriteTo(Stream). The reason is that CopyTo does not copy from the start but from the current position and it doesn't preserve this position. Also it doesn't detect well cases where the streams are disposed.

BinaryFormat keeps working only with DataStream as it facilitates the usage inside the Yarhl framework to make easier other substreams, WriteTo would affect Node too, but now this DataStream is a regular Stream too.

Also improving guards from several methods, especially DataStream.WriteSegmentTo and DataReader.ReadBytes.

Breaking changes

  • The property Stream of DataReader, DataWriter, TextReader and TextWriter now returns Stream instead of DataStream. This due to the fact that these classes now works entirely with Stream.

    • In order to use DataStream specific features like the property EndOfStream or methods like WriteTo, you could keep a reference to your original DataReader. If you know the reader/writer is working with a DataStream you could cast it.
    • Access to EndOfStream property from a reader could be replaced with reader.Stream.Position >= reader.Stream.Length.
  • DataStream does NOT throw EndOfStreamException when reading bytes. This change is required to match the expected behavior of Stream.

    • ReadByte returns -1 when it reaches the end.
    • ReadBytes reads as many bytes as possible given in the count argument. It will read nothing when it reaches the end. It returns how many bytes it has read (0 in end of stream).
    • DataReader keeps the behavior and it WILL throw EndOfStreamException when reading any data type, including ReadByte and ReadBytes.
  • DataStream.ReadByte now returns int instead of byte. This is required to overload Stream function. It returns always byte values except when reaches the end that it returns -1.

  • IStream now requires to implement the Flush method. DataStream.Flush will call it.

  • Move DataStream.Length setter to its own method SetLength(long). This is a required changed by Stream.

  • Obsolete SeekMode and any method having this type as argument. Instead use the BCL type System.IO.SeekOrigin.

  • DataReader and DataWriter now throw NotSupportedException when using an invalid endianness. Before it returned -1 or write in big endianness mode.

Example

You can now using a DataStream instead with any other framework that works with Stream, like xdelta-sharp or compression libraries.

You can also use the classes DataReader, DataWriter, TextReader and TextWriter with regular Stream too.

This closes #158

@pleonex pleonex added this to the vNext milestone Dec 25, 2020
@pleonex pleonex self-assigned this Dec 25, 2020
@pleonex pleonex merged commit d944b00 into develop Dec 26, 2020
@pleonex pleonex deleted the feature/datastream-stream branch December 26, 2020 06:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support System.IO.Stream in Yarhl.IO classes
1 participant