Skip to content
This repository has been archived by the owner on Nov 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #43 from lukasz-pyrzyk/develop
Browse files Browse the repository at this point in the history
Version 1.2.0
  • Loading branch information
lukasz-pyrzyk authored Jul 9, 2017
2 parents ab6fd41 + abfdcea commit c836d7b
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 63 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1.1.0.{build}
version: 1.2.0.{build}
image: Visual Studio 2017

environment:
Expand Down
2 changes: 1 addition & 1 deletion src/BinaryFormatter/BinaryFormatter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Description>BinaryFormatter - easy and light binary serializer</Description>
<Copyright>Łukasz Pyrzyk</Copyright>
<VersionPrefix>1.1.0</VersionPrefix>
<VersionPrefix>1.2.0</VersionPrefix>
<Authors>Łukasz Pyrzyk</Authors>
<TargetFrameworks>netstandard1.1</TargetFrameworks>
<AssemblyName>BinaryFormatter</AssemblyName>
Expand Down
4 changes: 1 addition & 3 deletions src/BinaryFormatter/TypeConverter/ByteArrayConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ internal class ByteArrayConverter : BaseTypeConverter<byte[]>
protected override void WriteObjectToStream(byte[] obj, Stream stream)
{
Size = obj.Length;
byte[] lengthBytes = BitConverter.GetBytes(Size);

stream.Write(lengthBytes);
stream.Write(obj);
stream.WriteWithLengthPrefix(obj);
}

protected override byte[] ProcessDeserialize(byte[] stream, ref int offset)
Expand Down
25 changes: 4 additions & 21 deletions src/BinaryFormatter/TypeConverter/IEnumerableConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ protected override void WriteObjectToStream(object obj, Stream stream)
if (objAsIEnumerable != null)
{
BinaryConverter converter = new BinaryConverter();
List<byte> listAsArray = new List<byte>();

foreach (var sourceElementValue in objAsIEnumerable)
{
if (sourceElementValue == null)
Expand All @@ -43,28 +41,13 @@ protected override void WriteObjectToStream(object obj, Stream stream)

Type elementType = elementValue.GetType();
byte[] typeInfo = Encoding.UTF8.GetBytes(elementType.AssemblyQualifiedName);
byte[] sizeTypeInfo = BitConverter.GetBytes(typeInfo.Length);
stream.WriteWithLengthPrefix(typeInfo);

byte[] data = converter.Serialize(elementValue);
byte[] sizeData = BitConverter.GetBytes(data.Length);
int elementAsBytesLength =
sizeTypeInfo.Length +
typeInfo.Length +
sizeData.Length +
data.Length;

byte[] elementAsBytes = new byte[elementAsBytesLength];
Array.Copy(sizeTypeInfo, 0, elementAsBytes, 0, sizeTypeInfo.Length);
Array.Copy(typeInfo, 0, elementAsBytes, sizeTypeInfo.Length, typeInfo.Length);
Array.Copy(sizeData, 0, elementAsBytes, sizeTypeInfo.Length + typeInfo.Length, sizeData.Length);
Array.Copy(data, 0, elementAsBytes, sizeTypeInfo.Length + typeInfo.Length + sizeData.Length, data.Length);

listAsArray.AddRange(elementAsBytes);
}
stream.WriteWithLengthPrefix(data);

byte[] collectionAsByteArray = listAsArray.ToArray();
Size = collectionAsByteArray.Length;
stream.Write(collectionAsByteArray);
Size += data.Length;
}
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/BinaryFormatter/TypeConverter/StringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ internal class StringConverter : BaseTypeConverter<string>
protected override void WriteObjectToStream(string obj, Stream stream)
{
byte[] objBytes = Encoding.UTF8.GetBytes(obj);
byte[] sizeBytes = BitConverter.GetBytes(objBytes.Length);
Size = objBytes.Length;

stream.Write(sizeBytes);
stream.Write(objBytes);
stream.WriteWithLengthPrefix(objBytes);
}

protected override string ProcessDeserialize(byte[] stream, ref int offset)
Expand Down
10 changes: 9 additions & 1 deletion src/BinaryFormatter/Utils/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;

namespace BinaryFormatter.Utils
{
Expand All @@ -8,5 +9,12 @@ public static void Write(this Stream stream, byte[] buffer)
{
stream.Write(buffer, 0, buffer.Length);
}

public static void WriteWithLengthPrefix(this Stream stream, byte[] buffer)
{
byte[] sizePrefix = BitConverter.GetBytes(buffer.Length);
stream.Write(sizePrefix);
stream.Write(buffer);
}
}
}
8 changes: 0 additions & 8 deletions tests/BinaryFormatterTests/BinaryFormatterTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<AssemblyName>BinaryFormatterTests</AssemblyName>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
</PropertyGroup>

<ItemGroup>
Expand All @@ -27,5 +20,4 @@
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

</Project>
23 changes: 0 additions & 23 deletions tests/BinaryFormatterTests/Properties/AssemblyInfo.cs

This file was deleted.

29 changes: 27 additions & 2 deletions tests/BinaryFormatterTests/Utils/StreamExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using BinaryFormatter.Utils;
using System;
using BinaryFormatter.Utils;
using System.IO;
using System.Linq;
using System.Text;
using Xunit;

namespace BinaryFormatterTests.Utils
Expand All @@ -10,12 +13,34 @@ public class StreamExtensionsTests
public void Write_WritesAllElementsToTheStream()
{
var stream = new MemoryStream();
byte[] data = new byte[10000];
var data = new byte[100];
stream.Write(data);

byte[] dataFromStream = stream.ToArray();

Assert.Equal(data, dataFromStream);
}

[Fact]
public void WriteWithLengthPrefix_WritesAllElementsToTheStream()
{
// Arrange
var stream = new MemoryStream();
var data = Encoding.UTF8.GetBytes("Hello world");
var expectedBytesHeader = BitConverter.GetBytes(data.Length);

// Act
stream.WriteWithLengthPrefix(data);

// Assert
byte[] dataFromStream = stream.ToArray();
Assert.Equal(dataFromStream.Length, data.Length + sizeof(int));

byte[] lengthPrefix = dataFromStream.Take(sizeof(int)).ToArray();
Assert.Equal(expectedBytesHeader, lengthPrefix);

byte[] dataBytes = dataFromStream.Skip(sizeof(int)).ToArray();
Assert.Equal(data, dataBytes);
}
}
}

0 comments on commit c836d7b

Please sign in to comment.