Implement DataStream inheritance with Stream and support of Readers and Writers with Stream #161
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 regularStream
, 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 aCopyTo(Stream)
method but I've kept theWriteTo(Stream)
. The reason is thatCopyTo
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 withDataStream
as it facilitates the usage inside the Yarhl framework to make easier other substreams,WriteTo
would affectNode
too, but now thisDataStream
is a regularStream
too.Also improving guards from several methods, especially
DataStream.WriteSegmentTo
andDataReader.ReadBytes
.Breaking changes
The property
Stream
ofDataReader
,DataWriter
,TextReader
andTextWriter
now returnsStream
instead ofDataStream
. This due to the fact that these classes now works entirely withStream
.DataStream
specific features like the propertyEndOfStream
or methods likeWriteTo
, you could keep a reference to your originalDataReader
. If you know the reader/writer is working with aDataStream
you could cast it.EndOfStream
property from a reader could be replaced withreader.Stream.Position >= reader.Stream.Length
.DataStream
does NOT throwEndOfStreamException
when reading bytes. This change is required to match the expected behavior ofStream
.ReadByte
returns-1
when it reaches the end.ReadBytes
reads as many bytes as possible given in thecount
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 throwEndOfStreamException
when reading any data type, includingReadByte
andReadBytes
.DataStream.ReadByte
now returnsint
instead ofbyte
. This is required to overloadStream
function. It returns always byte values except when reaches the end that it returns -1.IStream
now requires to implement theFlush
method.DataStream.Flush
will call it.Move
DataStream.Length
setter to its own methodSetLength(long)
. This is a required changed byStream
.Obsolete
SeekMode
and any method having this type as argument. Instead use the BCL typeSystem.IO.SeekOrigin
.DataReader
andDataWriter
now throwNotSupportedException
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 withStream
, like xdelta-sharp or compression libraries.You can also use the classes
DataReader
,DataWriter
,TextReader
andTextWriter
with regularStream
too.This closes #158