Commit a67ae663 authored by Ray Koopa's avatar Ray Koopa

Organize in subfolders, get Serialization to compile again

parent fdcedef0
......@@ -9,7 +9,7 @@ namespace Syroot.BinaryData
/// The array has a prefix of a 7-bit encoded integer of variable size determining the number of elements out
/// of which the array consists.
/// </summary>
DynamicCount,
VariableCount,
/// <summary>
/// The array has a prefix of 1 byte determining the number of elements out of which the array consists.
......
using System;
namespace Syroot.BinaryData
{
/// <summary>
/// Represents a collection of extension methods for the <see cref="BinaryDataReader"/> class.
/// </summary>
public static class BinaryDataReaderExtensions
{
// ---- Object ----
/// <summary>
/// Reads an object of type <typeparamref name="T"/> from the current stream.
/// </summary>
/// <typeparam name="T">The type of the object to load.</typeparam>
/// <param name="self">The extended <see cref="BinaryDataReader"/> instance.</param>
/// <returns>The object read from the current stream.</returns>
public static T ReadObject<T>(this BinaryDataReader self)
=> self.BaseStream.ReadObject<T>(self.ByteConverter);
/// <summary>
/// Reads the specified number of objects of type <typeparamref name="T"/> from the current stream.
/// </summary>
/// <typeparam name="T">The type of the objects to load.</typeparam>
/// <param name="self">The extended <see cref="BinaryDataReader"/> instance.</param>
/// <param name="count">The number of objects to read.</param>
/// <returns>The objects array read from the current stream.</returns>
public static T[] ReadObjects<T>(this BinaryDataReader self, int count)
=> self.BaseStream.ReadObjects<T>(count, self.ByteConverter);
/// <summary>
/// Reads an object of the given <paramref name="type"/> from the current stream.
/// </summary>
/// <param name="self">The extended <see cref="BinaryDataReader"/> instance.</param>
/// <param name="type">The type of the object to load.</param>
/// <returns>The object read from the current stream.</returns>
public static object ReadObject(this BinaryDataReader self, Type type)
=> self.BaseStream.ReadObject(type, self.ByteConverter);
/// <summary>
/// Reads the specified number of objects of the given <paramref name="type"/> from the current stream.
/// </summary>
/// <param name="self">The extended <see cref="BinaryDataReader"/> instance.</param>
/// <param name="type">The type of the object to load.</param>
/// <param name="count">The number of objects to read.</param>
/// <returns>The objects array read from the current stream.</returns>
public static object[] ReadObjects(this BinaryDataReader self, Type type, int count)
=> self.BaseStream.ReadObjects(type, count, self.ByteConverter);
}
}
namespace Syroot.BinaryData
{
/// <summary>
/// Represents a collection of extension methods for the <see cref="BinaryDataWriter"/> class.
/// </summary>
public static class BinaryDataWriterExtensions
{
// ---- Object ----
/// <summary>
/// Writes an object or enumerable of objects to this stream.
/// </summary>
/// <param name="self">The extended <see cref="BinaryDataWriter"/> instance.</param>
/// <param name="value">The object or enumerable of objects to write.</param>
public static void WriteObject(this BinaryDataWriter self, object value)
=> self.BaseStream.WriteObject(value, self.ByteConverter);
}
}
......@@ -216,8 +216,8 @@ namespace Syroot.BinaryData
? byteConverter : ByteConverter.GetConverter(ArrayCountCodingEndian);
switch (ArrayCountCoding)
{
case ArrayLengthCoding.DynamicCount:
return stream.ReadDynamicInt32();
case ArrayLengthCoding.VariableCount:
return stream.Read7BitInt32();
case ArrayLengthCoding.ByteCount:
return stream.ReadByte();
case ArrayLengthCoding.Int16Count:
......
......@@ -221,7 +221,7 @@ namespace Syroot.BinaryData
=> stream.ReadDateTime(memberData.DateTimeCoding, byteConverter);
private static object ReadDecimal(Stream stream, ByteConverter byteConverter, MemberData memberData)
=> stream.ReadDecimal(byteConverter);
=> stream.ReadDecimal();
private static object ReadDouble(Stream stream, ByteConverter byteConverter, MemberData memberData)
=> stream.ReadDouble(byteConverter);
......
using System.IO;
namespace Syroot.BinaryData
{
public static partial class StreamExtensions
{
// ---- METHODS (PUBLIC) ---------------------------------------------------------------------------------------
// ---- Object ----
/// <summary>
/// Writes an object or enumerable of objects to the <paramref name="stream"/>.
/// </summary>
/// <param name="stream">The extended <see cref="Stream"/> instance.</param>
/// <param name="value">The object or enumerable of objects to write.</param>
/// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param>
public static void WriteObject(this Stream stream, object value, ByteConverter converter = null)
=> _serializer.WriteObject(stream, value, converter);
}
}
using System;
using System.IO;
namespace Syroot.BinaryData
{
/// <summary>
/// Represents static extension methods for read and write operations on <see cref="Stream"/> instances.
/// </summary>
public static partial class StreamExtensions
{
// ---- FIELDS -------------------------------------------------------------------------------------------------
[ThreadStatic] private static byte[] _buffer;
private static Serializer _serializer = new Serializer();
// ---- PROPERTIES ---------------------------------------------------------------------------------------------
private static byte[] Buffer
{
get
{
if (_buffer == null)
_buffer = new byte[16];
return _buffer;
}
}
}
}
......@@ -3,10 +3,33 @@ using System.IO;
namespace Syroot.BinaryData
{
/// <summary>
/// Represents static extension methods for read and write operations on <see cref="Stream"/> instances.
/// </summary>
public static partial class StreamExtensions
{
// ---- FIELDS -------------------------------------------------------------------------------------------------
[ThreadStatic]
private static byte[] _buffer;
private static Serializer _serializer = new Serializer();
// ---- PROPERTIES ---------------------------------------------------------------------------------------------
private static byte[] Buffer
{
get
{
if (_buffer == null)
_buffer = new byte[16];
return _buffer;
}
}
// ---- METHODS (PUBLIC) ---------------------------------------------------------------------------------------
// ---- Read ----
/// <summary>
/// Returns an object of type <typeparamref name="T"/> read from the <paramref name="stream"/>.
/// </summary>
......@@ -71,6 +94,17 @@ namespace Syroot.BinaryData
return values;
}
// ---- Write ----
/// <summary>
/// Writes an object or enumerable of objects to the <paramref name="stream"/>.
/// </summary>
/// <param name="stream">The extended <see cref="Stream"/> instance.</param>
/// <param name="value">The object or enumerable of objects to write.</param>
/// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param>
public static void WriteObject(this Stream stream, object value, ByteConverter converter = null)
=> _serializer.WriteObject(stream, value, converter);
// ---- METHODS (PRIVATE) --------------------------------------------------------------------------------------
private static void FillBuffer(Stream stream, int length)
......
......@@ -16,20 +16,17 @@
<PackageTags>binary;data;io;binaryreader;binarywriter;serialization</PackageTags>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/Syroot/BinaryData</RepositoryUrl>
<TargetFrameworks>net452;netstandard2.0</TargetFrameworks>
<VersionPrefix>5.0.0-alpha5</VersionPrefix>
<TargetFrameworks>net452;netstandard20</TargetFrameworks>
<VersionPrefix>5.0.0-alpha1</VersionPrefix>
</PropertyGroup>
<ItemGroup Condition="'$(Configuration)' != 'Release (Signed)'">
<PackageReference Include="Syroot.BinaryData" Version="5.0.0-alpha0" />
<ItemGroup>
<ProjectReference Include="..\Syroot.BinaryData\Syroot.BinaryData.csproj" />
</ItemGroup>
<!-- Release (Signed) -->
<PropertyGroup Condition="'$(Configuration)' == 'Release (Signed)'">
<AssemblyOriginatorKeyFile>Syroot.BinaryData.Serialization.pfx</AssemblyOriginatorKeyFile>
<PackageId>Syroot.BinaryData.Serialization.Signed</PackageId>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<ItemGroup Condition="'$(Configuration)' == 'Release (Signed)'">
<PackageReference Include="Syroot.BinaryData.Signed" Version="5.0.0-alpha0" />
</ItemGroup>
</Project>
......@@ -46,7 +46,7 @@ namespace Syroot.BinaryData.UnitTest
CollectionAssert.AreEqual(bitConvert, byteConvert);
Array.Reverse(bitConvert);
GetReversedConverter().GetBytes(value, byteConvert);
TestTools.ReverseByteConverter.GetBytes(value, byteConvert);
CollectionAssert.AreEqual(bitConvert, byteConvert);
}
}
......@@ -63,7 +63,7 @@ namespace Syroot.BinaryData.UnitTest
CollectionAssert.AreEqual(bitConvert, byteConvert);
Array.Reverse(bitConvert);
GetReversedConverter().GetBytes(value, byteConvert);
TestTools.ReverseByteConverter.GetBytes(value, byteConvert);
CollectionAssert.AreEqual(bitConvert, byteConvert);
}
}
......@@ -81,7 +81,7 @@ namespace Syroot.BinaryData.UnitTest
CollectionAssert.AreEqual(bitConvert, byteConvert);
Array.Reverse(bitConvert);
GetReversedConverter().GetBytes(value, byteConvert);
TestTools.ReverseByteConverter.GetBytes(value, byteConvert);
CollectionAssert.AreEqual(bitConvert, byteConvert);
}
}
......@@ -99,7 +99,7 @@ namespace Syroot.BinaryData.UnitTest
CollectionAssert.AreEqual(bitConvert, byteConvert);
Array.Reverse(bitConvert);
GetReversedConverter().GetBytes(value, byteConvert);
TestTools.ReverseByteConverter.GetBytes(value, byteConvert);
CollectionAssert.AreEqual(bitConvert, byteConvert);
}
}
......@@ -117,7 +117,7 @@ namespace Syroot.BinaryData.UnitTest
CollectionAssert.AreEqual(bitConvert, byteConvert);
Array.Reverse(bitConvert);
GetReversedConverter().GetBytes(value, byteConvert);
TestTools.ReverseByteConverter.GetBytes(value, byteConvert);
CollectionAssert.AreEqual(bitConvert, byteConvert);
}
}
......@@ -134,7 +134,7 @@ namespace Syroot.BinaryData.UnitTest
CollectionAssert.AreEqual(bitConvert, byteConvert);
Array.Reverse(bitConvert);
GetReversedConverter().GetBytes(value, byteConvert);
TestTools.ReverseByteConverter.GetBytes(value, byteConvert);
CollectionAssert.AreEqual(bitConvert, byteConvert);
}
}
......@@ -151,7 +151,7 @@ namespace Syroot.BinaryData.UnitTest
CollectionAssert.AreEqual(bitConvert, byteConvert);
Array.Reverse(bitConvert);
GetReversedConverter().GetBytes(value, byteConvert);
TestTools.ReverseByteConverter.GetBytes(value, byteConvert);
CollectionAssert.AreEqual(bitConvert, byteConvert);
}
}
......@@ -169,7 +169,7 @@ namespace Syroot.BinaryData.UnitTest
CollectionAssert.AreEqual(bitConvert, byteConvert);
Array.Reverse(bitConvert);
GetReversedConverter().GetBytes(value, byteConvert);
TestTools.ReverseByteConverter.GetBytes(value, byteConvert);
CollectionAssert.AreEqual(bitConvert, byteConvert);
}
}
......@@ -201,7 +201,7 @@ namespace Syroot.BinaryData.UnitTest
Assert.AreEqual(value, parsedValue);
Array.Reverse(bytes);
parsedValue = GetReversedConverter().ToDouble(bytes);
parsedValue = TestTools.ReverseByteConverter.ToDouble(bytes);
Assert.AreEqual(value, parsedValue);
}
}
......@@ -217,7 +217,7 @@ namespace Syroot.BinaryData.UnitTest
Assert.AreEqual(value, parsedValue);
Array.Reverse(bytes);
parsedValue = GetReversedConverter().ToInt16(bytes);
parsedValue = TestTools.ReverseByteConverter.ToInt16(bytes);
Assert.AreEqual(value, parsedValue);
}
}
......@@ -234,7 +234,7 @@ namespace Syroot.BinaryData.UnitTest
Assert.AreEqual(value, parsedValue);
Array.Reverse(bytes);
parsedValue = GetReversedConverter().ToInt32(bytes);
parsedValue = TestTools.ReverseByteConverter.ToInt32(bytes);
Assert.AreEqual(value, parsedValue);
}
}
......@@ -251,7 +251,7 @@ namespace Syroot.BinaryData.UnitTest
Assert.AreEqual(value, parsedValue);
Array.Reverse(bytes);
parsedValue = GetReversedConverter().ToInt64(bytes);
parsedValue = TestTools.ReverseByteConverter.ToInt64(bytes);
Assert.AreEqual(value, parsedValue);
}
}
......@@ -268,7 +268,7 @@ namespace Syroot.BinaryData.UnitTest
Assert.AreEqual(value, parsedValue);
Array.Reverse(bytes);
parsedValue = GetReversedConverter().ToSingle(bytes);
parsedValue = TestTools.ReverseByteConverter.ToSingle(bytes);
Assert.AreEqual(value, parsedValue);
}
}
......@@ -284,7 +284,7 @@ namespace Syroot.BinaryData.UnitTest
Assert.AreEqual(value, parsedValue);
Array.Reverse(bytes);
parsedValue = GetReversedConverter().ToUInt16(bytes);
parsedValue = TestTools.ReverseByteConverter.ToUInt16(bytes);
Assert.AreEqual(value, parsedValue);
}
}
......@@ -300,7 +300,7 @@ namespace Syroot.BinaryData.UnitTest
Assert.AreEqual(value, parsedValue);
Array.Reverse(bytes);
parsedValue = GetReversedConverter().ToUInt32(bytes);
parsedValue = TestTools.ReverseByteConverter.ToUInt32(bytes);
Assert.AreEqual(value, parsedValue);
}
}
......@@ -317,16 +317,9 @@ namespace Syroot.BinaryData.UnitTest
Assert.AreEqual(value, parsedValue);
Array.Reverse(bytes);
parsedValue = GetReversedConverter().ToUInt64(bytes);
parsedValue = TestTools.ReverseByteConverter.ToUInt64(bytes);
Assert.AreEqual(value, parsedValue);
}
}
// ---- METHODS (PRIVATE) --------------------------------------------------------------------------------------
private ByteConverter GetReversedConverter()
{
return ByteConverter.System.Endian == Endian.Little ? ByteConverter.Big : ByteConverter.Little;
}
}
}
......@@ -5,15 +5,13 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Syroot.BinaryData.UnitTest
{
[TestClass]
public class Performance
public class PerformanceTests
{
// ---- CONSTANTS ----------------------------------------------------------------------------------------------
private const int _writeCount = 10_000_000;
private static readonly Random _random = new Random();
private static readonly ByteConverter _nonSystemConverter
= BitConverter.IsLittleEndian ? ByteConverter.Big : ByteConverter.Little;
private static readonly byte[] _buffer = new byte[sizeof(Int32)];
// ---- FIELDS -------------------------------------------------------------------------------------------------
......@@ -79,7 +77,7 @@ namespace Syroot.BinaryData.UnitTest
{
for (int i = 0; i < _writeCount; i++)
{
_nonSystemConverter.GetBytes(_random.Next(Int32.MaxValue), _buffer);
TestTools.ReverseByteConverter.GetBytes(_random.Next(Int32.MaxValue), _buffer);
writer.Write(_buffer);
}
}
......@@ -89,7 +87,7 @@ namespace Syroot.BinaryData.UnitTest
public void Writing_NonSystem_StreamExtension()
{
for (int i = 0; i < _writeCount; i++)
_stream.Write(_random.Next(Int32.MaxValue), _nonSystemConverter);
_stream.Write(_random.Next(Int32.MaxValue), TestTools.ReverseByteConverter);
}
}
}
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Syroot.BinaryData.UnitTest
{
[TestClass]
public class StreamExtensionTestsInt16
{
// ---- METHODS (PUBLIC) ---------------------------------------------------------------------------------------
// ---- Read ----
[TestMethod]
public void ReadInt16()
{
Int16[] values = new Int16[] { 12345, -12345, 1, 0, 25125, Int16.MinValue, Int16.MaxValue };
using (MemoryStream stream = new MemoryStream())
{
// Prepare test data.
foreach (Int16 value in values)
TestTools.WriteInt16(stream, value);
foreach (Int16 value in values)
TestTools.WriteInt16(stream, value, true);
// Read test data.
stream.Position = 0;
foreach (Int16 value in values)
Assert.AreEqual(value, stream.ReadInt16());
foreach (Int16 value in values)
Assert.AreEqual(value, stream.ReadInt16(TestTools.ReverseByteConverter));
// Read test data all at once.
stream.Position = 0;
CollectionAssert.AreEqual(values, stream.ReadInt16s(values.Length));
CollectionAssert.AreEqual(values, stream.ReadInt16s(values.Length, TestTools.ReverseByteConverter));
}
}
}
}
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Syroot.BinaryData.UnitTest
{
[TestClass]
public class StreamExtensionTestsInt32
{
// ---- METHODS (PUBLIC) ---------------------------------------------------------------------------------------
// ---- Read ----
[TestMethod]
public void ReadInt32()
{
Int32[] values = new Int32[] { 1234567890, -1234567890, 1, 0, 251258109, Int32.MinValue, Int32.MaxValue };
using (MemoryStream stream = new MemoryStream())
{
// Prepare test data.
foreach (Int32 value in values)
TestTools.WriteInt32(stream, value);
foreach (Int32 value in values)
TestTools.WriteInt32(stream, value, true);
// Read test data.
stream.Position = 0;
foreach (Int32 value in values)
Assert.AreEqual(value, stream.ReadInt32());
foreach (Int32 value in values)
Assert.AreEqual(value, stream.ReadInt32(TestTools.ReverseByteConverter));
// Read test data all at once.
stream.Position = 0;
CollectionAssert.AreEqual(values, stream.ReadInt32s(values.Length));
CollectionAssert.AreEqual(values, stream.ReadInt32s(values.Length, TestTools.ReverseByteConverter));
}
}
}
}
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Syroot.BinaryData.UnitTest
{
[TestClass]
public class StreamExtensionTestsInt64
{
// ---- METHODS (PUBLIC) ---------------------------------------------------------------------------------------
// ---- Read ----
[TestMethod]
public void ReadInt64()
{
Int64[] values = new Int64[] { 1234567890123456789, -1234567890123456789, 1, 0, 2512581093475885631,
Int64.MinValue, Int64.MaxValue };
using (MemoryStream stream = new MemoryStream())
{
// Prepare test data.
foreach (Int64 value in values)
TestTools.WriteInt64(stream, value);
foreach (Int64 value in values)
TestTools.WriteInt64(stream, value, true);
// Read test data.
stream.Position = 0;
foreach (Int64 value in values)
Assert.AreEqual(value, stream.ReadInt64());
foreach (Int64 value in values)
Assert.AreEqual(value, stream.ReadInt64(TestTools.ReverseByteConverter));
// Read test data all at once.
stream.Position = 0;
CollectionAssert.AreEqual(values, stream.ReadInt64s(values.Length));
CollectionAssert.AreEqual(values, stream.ReadInt64s(values.Length, TestTools.ReverseByteConverter));
}
}
}
}
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Syroot.BinaryData.UnitTest
{
[TestClass]
public class StreamExtensionTestsUInt16
{
// ---- METHODS (PUBLIC) ---------------------------------------------------------------------------------------
// ---- Read ----
[TestMethod]
public void ReadUInt16()
{
UInt16[] values = new UInt16[] { 12345, 1, 0, 25125, UInt16.MinValue, UInt16.MaxValue };
using (MemoryStream stream = new MemoryStream())
{
// Prepare test data.
foreach (UInt16 value in values)
TestTools.WriteUInt16(stream, value);
foreach (UInt16 value in values)
TestTools.WriteUInt16(stream, value, true);
// Read test data.
stream.Position = 0;
foreach (UInt16 value in values)
Assert.AreEqual(value, stream.ReadUInt16());
foreach (UInt16 value in values)
Assert.AreEqual(value, stream.ReadUInt16(TestTools.ReverseByteConverter));
// Read test data all at once.
stream.Position = 0;
CollectionAssert.AreEqual(values, stream.ReadUInt16s(values.Length));
CollectionAssert.AreEqual(values, stream.ReadUInt16s(values.Length, TestTools.ReverseByteConverter));
}
}
}
}
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Syroot.BinaryData.UnitTest
{
[TestClass]
public class StreamExtensionTestsUInt32
{
// ---- METHODS (PUBLIC) ---------------------------------------------------------------------------------------
// ---- Read ----
[TestMethod]
public void ReadUInt32()
{
UInt32[] values = new UInt32[] { 1234567890, 1, 0, 251258109, UInt32.MinValue, UInt32.MaxValue };
using (MemoryStream stream = new MemoryStream())
{
// Prepare test data.
foreach (UInt32 value in values)
TestTools.WriteUInt32(stream, value);
foreach (UInt32 value in values)
TestTools.WriteUInt32(stream, value, true);
// Read test data.
stream.Position = 0;
foreach (UInt32 value in values)
Assert.AreEqual(value, stream.ReadUInt32());
foreach (UInt32 value in values)
Assert.AreEqual(value, stream.ReadUInt32(TestTools.ReverseByteConverter));
// Read test data all at once.
stream.Position = 0;
CollectionAssert.AreEqual(values, stream.ReadUInt32s(values.Length));
CollectionAssert.AreEqual(values, stream.ReadUInt32s(values.Length, TestTools.ReverseByteConverter));
}
}
}
}
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Syroot.BinaryData.UnitTest
{
[TestClass]
public partial class StreamExtensionTestsUInt64
{
// ---- METHODS (PUBLIC) ---------------------------------------------------------------------------------------
// ---- Read ----
[TestMethod]
public void ReadUInt64()
{
UInt64[] values = new UInt64[] { 1234567890123456789, 1, 0, 2512581093475885631, UInt64.MinValue,
UInt64.MaxValue };
using (MemoryStream stream = new MemoryStream())
{
// Prepare test data.
foreach (UInt64 value in values)
TestTools.WriteUInt64(stream, value);
foreach (UInt64 value in values)
TestTools.WriteUInt64(stream, value, true);
// Read test data.
stream.Position = 0;
foreach (UInt64 value in values)
Assert.AreEqual(value, stream.ReadUInt64());
foreach (UInt64 value in values)
Assert.AreEqual(value, stream.ReadUInt64(TestTools.ReverseByteConverter));
// Read test data all at once.
stream.Position = 0;
CollectionAssert.AreEqual(values, stream.ReadUInt64s(values.Length));
CollectionAssert.AreEqual(values, stream.ReadUInt64s(values.Length, TestTools.ReverseByteConverter));
}
}
}
}
using System;
using System.IO;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Syroot.BinaryData.UnitTest
{
[TestClass]