Skip to content

Commit

Permalink
馃摉 Document Slice API
Browse files Browse the repository at this point in the history
  • Loading branch information
pleonex committed Nov 28, 2023
1 parent 203f430 commit 74711de
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
8 changes: 8 additions & 0 deletions docs/articles/core/binary/datastream.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions src/Yarhl.Examples/IO/DataStreamExamples.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
2 changes: 0 additions & 2 deletions src/Yarhl.UnitTests/IO/DataStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 74711de

Please sign in to comment.