Skip to content

Commit

Permalink
ByteSerializer now implements IDisposable (fixes premature closing of…
Browse files Browse the repository at this point in the history
… streams)
  • Loading branch information
akopetsch committed Jun 4, 2024
1 parent 948cfe4 commit 17a1982
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
35 changes: 35 additions & 0 deletions ByteSerialization.Tests/Integration/ClosedStreamTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: MIT

using ByteSerialization.Attributes;
using ByteSerialization.IO;
using Xunit;

namespace ByteSerialization.Tests.Integration
{
public class ClosedStreamTest
{
public class Struct1
{
[Order(0)]
public byte Byte0 { get; set; }

[Order(1)]
public byte Byte1 { get; set; }
}

[Fact]
public void Test()
{
byte[] bytes = [0x01, 0x02, 0x03];
using var ms = new MemoryStream(bytes);

using var ser = new ByteSerializer();
var struct1 = ser.Deserialize<Struct1>(ms, Endianness.LittleEndian);
byte byte2 = Convert.ToByte(ms.ReadByte());

Assert.Equal(bytes[0], struct1.Byte0);
Assert.Equal(bytes[1], struct1.Byte1);
Assert.Equal(bytes[2], byte2);
}
}
}
2 changes: 1 addition & 1 deletion ByteSerialization/ByteSerialization.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>disable</Nullable>
<Authors>Alexander Kopetsch</Authors>
<Version>0.0.1.0</Version>
<PackageVersion>0.0.1-alpha.4</PackageVersion>
<PackageVersion>0.0.1-alpha.5</PackageVersion>
<PackageProjectUrl>https://github.com/akopetsch/ByteSerialization</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
25 changes: 20 additions & 5 deletions ByteSerialization/ByteSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,26 @@

namespace ByteSerialization
{
public class ByteSerializer
public class ByteSerializer : IDisposable
{
#region Fields

private readonly Dictionary<Type, ICustomSerializer> _customSerializers =
new Dictionary<Type, ICustomSerializer>();

private EndianBinaryReader _reader;
private EndianBinaryWriter _writer;

#endregion

#region Methods (: IDisposable)

public void Dispose()
{
_reader?.Dispose();
_writer?.Dispose();
}

#endregion

#region Methods (ICustomSerializer)
Expand Down Expand Up @@ -48,8 +61,9 @@ public T Deserialize<T>(Stream stream, Endianness endianness) =>

public T Deserialize<T>(Stream stream, Endianness endianness, out ByteSerializerContext context)
{
using var r = new EndianBinaryReader(stream, endianness);
var n = Node.CreateRoot(this, r, typeof(T));
_reader?.Dispose();
_reader = new EndianBinaryReader(stream, endianness);
var n = Node.CreateRoot(this, _reader, typeof(T));
n.Deserialize();
context = n.Context;
return (T)n.Value;
Expand All @@ -74,8 +88,9 @@ public void Serialize(Stream stream, object value, Endianness endianness) =>

public void Serialize(Stream stream, object value, Endianness endianness, out ByteSerializerContext context)
{
using var w = new EndianBinaryWriter(stream, endianness);
var n = Node.CreateRoot(this, w, value);
_writer?.Dispose();
_writer = new EndianBinaryWriter(stream, endianness);
var n = Node.CreateRoot(this, _writer, value);
n.Serialize();
context = n.Context;
}
Expand Down

0 comments on commit 17a1982

Please sign in to comment.