diff --git a/docs/articles/core/binary/datastream.md b/docs/articles/core/binary/datastream.md index a55ac1db..85ee7565 100644 --- a/docs/articles/core/binary/datastream.md +++ b/docs/articles/core/binary/datastream.md @@ -38,6 +38,14 @@ Another use case is reading a binary format with sections. By creating a modular and safe. It would prevent reading data outside the range of the section. +We can create a _sub-stream_ from the `DataStream` constructor: + +[!code-csharp[SubStreamConstructor](./../../../../src/Yarhl.Examples/IO/DataStreamExamples.cs?name=SubStreamConstructor)] + +or from the `Slice` API: + +[!code-csharp[SubStreamConstructor](./../../../../src/Yarhl.Examples/IO/DataStreamExamples.cs?name=SubStreamSlice)] + ## Factory The constructors of `DataStream` takes a `Stream` with optional offset and diff --git a/src/Yarhl.Examples/IO/DataStreamExamples.cs b/src/Yarhl.Examples/IO/DataStreamExamples.cs new file mode 100644 index 00000000..32a684e0 --- /dev/null +++ b/src/Yarhl.Examples/IO/DataStreamExamples.cs @@ -0,0 +1,44 @@ +// Copyright (c) 2023 SceneGate + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +namespace Yarhl.Examples.IO; + +using Yarhl.IO; + +public static class DataStreamExamples +{ + public static void CreateSubStreamConstructor() + { + #region SubStreamConstructor + var baseStream = new FileStream("container.bin", FileMode.Open); + using var file1Stream = new DataStream(baseStream, 0x100, 0x2C0); + using var file2Stream = new DataStream(baseStream, 0x3C0, 0x80); + #endregion + } + + public static void CreateSubStreamSlice() + { + #region SubStreamSlice + DataStream containerStream = DataStreamFactory.FromFile("container.bin", FileOpenMode.Read); + using DataStream file1Stream = containerStream.Slice(0x100, 0x2C0); + using DataStream file2Stream = containerStream.Slice(0x3C0, 0x80); + using DataStream lastFileStream = containerStream.Slice(0x8400); + #endregion + } +} diff --git a/src/Yarhl.UnitTests/IO/DataStreamTests.cs b/src/Yarhl.UnitTests/IO/DataStreamTests.cs index 651671c6..b77b3bae 100644 --- a/src/Yarhl.UnitTests/IO/DataStreamTests.cs +++ b/src/Yarhl.UnitTests/IO/DataStreamTests.cs @@ -23,10 +23,8 @@ namespace Yarhl.UnitTests.IO using System.Diagnostics.CodeAnalysis; using System.IO; using System.Threading.Tasks; - using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.Interfaces; using Moq; using NUnit.Framework; - using NUnit.Framework.Internal; using Yarhl.FileFormat; using Yarhl.IO; using Yarhl.IO.StreamFormat;