From 7206b6e1919f3397d254c039efc731cf2205cf4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pyrzyk?= Date: Fri, 7 Jul 2017 21:47:05 +0200 Subject: [PATCH] All primitive converters with serialization based on stream --- .../TypeConverter/BaseTypeConverter.cs | 7 +++-- .../TypeConverter/BoolConverter.cs | 7 +++-- .../TypeConverter/ByteArrayConverter.cs | 11 ++++---- .../TypeConverter/ByteConverter.cs | 7 +++-- .../TypeConverter/CharConverter.cs | 7 +++-- .../TypeConverter/DatetimeConverter.cs | 7 +++-- .../TypeConverter/DecimalConverter.cs | 10 +++---- .../TypeConverter/DoubleConverter.cs | 7 +++-- .../TypeConverter/FloatConverter.cs | 7 +++-- .../TypeConverter/IEnumerableConverter.cs | 26 ++++++++++--------- .../TypeConverter/IntConverter.cs | 7 +++-- .../TypeConverter/LongConverter.cs | 7 +++-- .../TypeConverter/SByteConverter.cs | 7 +++-- .../TypeConverter/ShortConverter.cs | 7 +++-- .../TypeConverter/StringConverter.cs | 12 ++++----- .../TypeConverter/UIntConverter.cs | 7 +++-- .../TypeConverter/ULongConverter.cs | 7 +++-- .../TypeConverter/UshortConverter.cs | 7 +++-- .../TypeConverter/BaseTypeConverterTests.cs | 6 +++-- 19 files changed, 99 insertions(+), 64 deletions(-) diff --git a/src/BinaryFormatter/TypeConverter/BaseTypeConverter.cs b/src/BinaryFormatter/TypeConverter/BaseTypeConverter.cs index 9bafa5a..8db3ebd 100644 --- a/src/BinaryFormatter/TypeConverter/BaseTypeConverter.cs +++ b/src/BinaryFormatter/TypeConverter/BaseTypeConverter.cs @@ -11,11 +11,10 @@ public void Serialize(T obj, Stream stream) { if (obj == null) throw new ArgumentNullException(nameof(obj)); - byte[] objectBytes = ProcessSerialize(obj); byte[] objectType = BitConverter.GetBytes((ushort)Type); - stream.Write(objectType); - stream.Write(objectBytes); + + WriteObjectToStream(obj, stream); } public override void Serialize(object obj, Stream stream) @@ -49,7 +48,7 @@ public override object DeserializeToObject(byte[] stream, ref int offset) } protected abstract int GetTypeSize(); - protected abstract byte[] ProcessSerialize(T obj); + protected abstract void WriteObjectToStream(T obj, Stream stream); protected abstract T ProcessDeserialize(byte[] stream, ref int offset); protected virtual SerializedType GetPackageType(byte[] stream, ref int offset) diff --git a/src/BinaryFormatter/TypeConverter/BoolConverter.cs b/src/BinaryFormatter/TypeConverter/BoolConverter.cs index 7a6abe0..5da57a3 100644 --- a/src/BinaryFormatter/TypeConverter/BoolConverter.cs +++ b/src/BinaryFormatter/TypeConverter/BoolConverter.cs @@ -1,13 +1,16 @@ using System; +using System.IO; using BinaryFormatter.Types; +using BinaryFormatter.Utils; namespace BinaryFormatter.TypeConverter { internal class BoolConverter : BaseTypeConverter { - protected override byte[] ProcessSerialize(bool obj) + protected override void WriteObjectToStream(bool obj, Stream stream) { - return BitConverter.GetBytes(obj); + byte[] data = BitConverter.GetBytes(obj); + stream.Write(data); } protected override bool ProcessDeserialize(byte[] stream, ref int offset) diff --git a/src/BinaryFormatter/TypeConverter/ByteArrayConverter.cs b/src/BinaryFormatter/TypeConverter/ByteArrayConverter.cs index 0af3525..95ee0db 100644 --- a/src/BinaryFormatter/TypeConverter/ByteArrayConverter.cs +++ b/src/BinaryFormatter/TypeConverter/ByteArrayConverter.cs @@ -1,5 +1,7 @@ using System; +using System.IO; using BinaryFormatter.Types; +using BinaryFormatter.Utils; namespace BinaryFormatter.TypeConverter { @@ -7,16 +9,13 @@ internal class ByteArrayConverter : BaseTypeConverter { private int Size { get; set; } - protected override byte[] ProcessSerialize(byte[] obj) + protected override void WriteObjectToStream(byte[] obj, Stream stream) { Size = obj.Length; byte[] lengthBytes = BitConverter.GetBytes(Size); - byte[] serializedStringWithSize = new byte[sizeof(int) + Size]; - Array.Copy(lengthBytes, 0, serializedStringWithSize, 0, lengthBytes.Length); - Array.Copy(obj, 0, serializedStringWithSize, lengthBytes.Length, obj.Length); - - return serializedStringWithSize; + stream.Write(lengthBytes); + stream.Write(obj); } protected override byte[] ProcessDeserialize(byte[] stream, ref int offset) diff --git a/src/BinaryFormatter/TypeConverter/ByteConverter.cs b/src/BinaryFormatter/TypeConverter/ByteConverter.cs index 757beb5..e9f324e 100644 --- a/src/BinaryFormatter/TypeConverter/ByteConverter.cs +++ b/src/BinaryFormatter/TypeConverter/ByteConverter.cs @@ -1,13 +1,16 @@ using System; +using System.IO; using BinaryFormatter.Types; +using BinaryFormatter.Utils; namespace BinaryFormatter.TypeConverter { internal class ByteConverter : BaseTypeConverter { - protected override byte[] ProcessSerialize(byte obj) + protected override void WriteObjectToStream(byte obj, Stream stream) { - return BitConverter.GetBytes(obj); + byte[] data = BitConverter.GetBytes(obj); + stream.Write(data); } protected override byte ProcessDeserialize(byte[] stream, ref int offset) diff --git a/src/BinaryFormatter/TypeConverter/CharConverter.cs b/src/BinaryFormatter/TypeConverter/CharConverter.cs index 22f30fa..a027ca7 100644 --- a/src/BinaryFormatter/TypeConverter/CharConverter.cs +++ b/src/BinaryFormatter/TypeConverter/CharConverter.cs @@ -1,13 +1,16 @@ using System; +using System.IO; using BinaryFormatter.Types; +using BinaryFormatter.Utils; namespace BinaryFormatter.TypeConverter { internal class CharConverter : BaseTypeConverter { - protected override byte[] ProcessSerialize(char obj) + protected override void WriteObjectToStream(char obj, Stream stream) { - return BitConverter.GetBytes(obj); + byte[] data = BitConverter.GetBytes(obj); + stream.Write(data); } protected override char ProcessDeserialize(byte[] stream, ref int offset) diff --git a/src/BinaryFormatter/TypeConverter/DatetimeConverter.cs b/src/BinaryFormatter/TypeConverter/DatetimeConverter.cs index f4d968c..304f594 100644 --- a/src/BinaryFormatter/TypeConverter/DatetimeConverter.cs +++ b/src/BinaryFormatter/TypeConverter/DatetimeConverter.cs @@ -1,13 +1,16 @@ using System; +using System.IO; using BinaryFormatter.Types; +using BinaryFormatter.Utils; namespace BinaryFormatter.TypeConverter { internal class DatetimeConverter : BaseTypeConverter { - protected override byte[] ProcessSerialize(DateTime obj) + protected override void WriteObjectToStream(DateTime obj, Stream stream) { - return BitConverter.GetBytes(obj.Ticks); + byte[] data = BitConverter.GetBytes(obj.Ticks); + stream.Write(data); } protected override DateTime ProcessDeserialize(byte[] stream, ref int offset) diff --git a/src/BinaryFormatter/TypeConverter/DecimalConverter.cs b/src/BinaryFormatter/TypeConverter/DecimalConverter.cs index 95c33bf..21e4963 100644 --- a/src/BinaryFormatter/TypeConverter/DecimalConverter.cs +++ b/src/BinaryFormatter/TypeConverter/DecimalConverter.cs @@ -1,7 +1,5 @@ -using System; -using System.IO; +using System.IO; using System.Linq; -using System.Text; using BinaryFormatter.Types; namespace BinaryFormatter.TypeConverter @@ -10,14 +8,12 @@ internal class DecimalConverter : BaseTypeConverter { private int Size { get; set; } = 0; - protected override byte[] ProcessSerialize(decimal obj) + protected override void WriteObjectToStream(decimal obj, Stream stream) { string sdecimal = obj.ToString("F"); Size = sdecimal.Length; - var ms = new MemoryStream(); - new StringConverter().Serialize(sdecimal, ms); - return ms.ToArray(); + new StringConverter().Serialize(sdecimal, stream); } protected override decimal ProcessDeserialize(byte[] stream, ref int offset) diff --git a/src/BinaryFormatter/TypeConverter/DoubleConverter.cs b/src/BinaryFormatter/TypeConverter/DoubleConverter.cs index 599f2f1..901b11e 100644 --- a/src/BinaryFormatter/TypeConverter/DoubleConverter.cs +++ b/src/BinaryFormatter/TypeConverter/DoubleConverter.cs @@ -1,13 +1,16 @@ using System; +using System.IO; using BinaryFormatter.Types; +using BinaryFormatter.Utils; namespace BinaryFormatter.TypeConverter { internal class DoubleConverter : BaseTypeConverter { - protected override byte[] ProcessSerialize(double obj) + protected override void WriteObjectToStream(double obj, Stream stream) { - return BitConverter.GetBytes(obj); + byte[] data = BitConverter.GetBytes(obj); + stream.Write(data); } protected override double ProcessDeserialize(byte[] stream, ref int offset) diff --git a/src/BinaryFormatter/TypeConverter/FloatConverter.cs b/src/BinaryFormatter/TypeConverter/FloatConverter.cs index 149f90c..fa72230 100644 --- a/src/BinaryFormatter/TypeConverter/FloatConverter.cs +++ b/src/BinaryFormatter/TypeConverter/FloatConverter.cs @@ -1,13 +1,16 @@ using System; +using System.IO; using BinaryFormatter.Types; +using BinaryFormatter.Utils; namespace BinaryFormatter.TypeConverter { internal class FloatConverter : BaseTypeConverter { - protected override byte[] ProcessSerialize(float obj) + protected override void WriteObjectToStream(float obj, Stream stream) { - return BitConverter.GetBytes(obj); + byte[] data = BitConverter.GetBytes(obj); + stream.Write(data); } protected override float ProcessDeserialize(byte[] stream, ref int offset) diff --git a/src/BinaryFormatter/TypeConverter/IEnumerableConverter.cs b/src/BinaryFormatter/TypeConverter/IEnumerableConverter.cs index c083711..c5a8808 100644 --- a/src/BinaryFormatter/TypeConverter/IEnumerableConverter.cs +++ b/src/BinaryFormatter/TypeConverter/IEnumerableConverter.cs @@ -1,8 +1,10 @@ using System; using BinaryFormatter.Types; using System.Collections.Generic; +using System.IO; using System.Text; using System.Reflection; +using BinaryFormatter.Utils; namespace BinaryFormatter.TypeConverter { @@ -10,13 +12,12 @@ internal class IEnumerableConverter : BaseTypeConverter { private int Size { get; set; } - protected override byte[] ProcessSerialize(object obj) + protected override void WriteObjectToStream(object obj, Stream stream) { - byte[] collectionAsByteArray = null; - var objAsIEnumerable = (obj as IEnumerable); + var objAsIEnumerable = obj as IEnumerable; if (objAsIEnumerable != null) { - BinaryConverter converter = new BinaryConverter(); + BinaryConverter converter = new BinaryConverter(); List listAsArray = new List(); foreach (var sourceElementValue in objAsIEnumerable) @@ -25,17 +26,19 @@ protected override byte[] ProcessSerialize(object obj) continue; object elementValue = (sourceElementValue as IEnumerable); - if(elementValue == null) + if (elementValue == null) { elementValue = sourceElementValue; - } else + } + else { List collectionOfObjects = new List(); - foreach (var item in (elementValue as IEnumerable)) + foreach (var item in (IEnumerable)elementValue) { collectionOfObjects.Add(item); } - elementValue = collectionOfObjects as IEnumerable; + + elementValue = collectionOfObjects; } Type elementType = elementValue.GetType(); @@ -59,11 +62,10 @@ protected override byte[] ProcessSerialize(object obj) listAsArray.AddRange(elementAsBytes); } - collectionAsByteArray = listAsArray.ToArray(); + byte[] collectionAsByteArray = listAsArray.ToArray(); Size = collectionAsByteArray.Length; + stream.Write(collectionAsByteArray); } - - return collectionAsByteArray; } protected override object ProcessDeserialize(byte[] stream, ref int offset) @@ -79,7 +81,7 @@ protected override object ProcessDeserialize(byte[] stream, ref int offset) { int sizeTypeInfo = BitConverter.ToInt32(stream, offset); offset += sizeof(int); - if(sizeTypeInfo == 0) + if (sizeTypeInfo == 0) { continue; } diff --git a/src/BinaryFormatter/TypeConverter/IntConverter.cs b/src/BinaryFormatter/TypeConverter/IntConverter.cs index 8aca697..91b350f 100644 --- a/src/BinaryFormatter/TypeConverter/IntConverter.cs +++ b/src/BinaryFormatter/TypeConverter/IntConverter.cs @@ -1,13 +1,16 @@ using System; +using System.IO; using BinaryFormatter.Types; +using BinaryFormatter.Utils; namespace BinaryFormatter.TypeConverter { internal class IntConverter : BaseTypeConverter { - protected override byte[] ProcessSerialize(int obj) + protected override void WriteObjectToStream(int obj, Stream stream) { - return BitConverter.GetBytes(obj); + byte[] data = BitConverter.GetBytes(obj); + stream.Write(data); } protected override int ProcessDeserialize(byte[] stream, ref int offset) diff --git a/src/BinaryFormatter/TypeConverter/LongConverter.cs b/src/BinaryFormatter/TypeConverter/LongConverter.cs index cc17a6a..b68ada4 100644 --- a/src/BinaryFormatter/TypeConverter/LongConverter.cs +++ b/src/BinaryFormatter/TypeConverter/LongConverter.cs @@ -1,13 +1,16 @@ using System; +using System.IO; using BinaryFormatter.Types; +using BinaryFormatter.Utils; namespace BinaryFormatter.TypeConverter { internal class LongConverter : BaseTypeConverter { - protected override byte[] ProcessSerialize(long obj) + protected override void WriteObjectToStream(long obj, Stream stream) { - return BitConverter.GetBytes(obj); + byte[] data = BitConverter.GetBytes(obj); + stream.Write(data); } protected override long ProcessDeserialize(byte[] stream, ref int offset) diff --git a/src/BinaryFormatter/TypeConverter/SByteConverter.cs b/src/BinaryFormatter/TypeConverter/SByteConverter.cs index 718efa2..2344c95 100644 --- a/src/BinaryFormatter/TypeConverter/SByteConverter.cs +++ b/src/BinaryFormatter/TypeConverter/SByteConverter.cs @@ -1,13 +1,16 @@ using System; +using System.IO; using BinaryFormatter.Types; +using BinaryFormatter.Utils; namespace BinaryFormatter.TypeConverter { internal class SByteConverter : BaseTypeConverter { - protected override byte[] ProcessSerialize(sbyte obj) + protected override void WriteObjectToStream(sbyte obj, Stream stream) { - return BitConverter.GetBytes(obj); + byte[] data = BitConverter.GetBytes(obj); + stream.Write(data); } protected override sbyte ProcessDeserialize(byte[] stream, ref int offset) diff --git a/src/BinaryFormatter/TypeConverter/ShortConverter.cs b/src/BinaryFormatter/TypeConverter/ShortConverter.cs index f0af78b..38bb19c 100644 --- a/src/BinaryFormatter/TypeConverter/ShortConverter.cs +++ b/src/BinaryFormatter/TypeConverter/ShortConverter.cs @@ -1,13 +1,16 @@ using System; +using System.IO; using BinaryFormatter.Types; +using BinaryFormatter.Utils; namespace BinaryFormatter.TypeConverter { internal class ShortConverter : BaseTypeConverter { - protected override byte[] ProcessSerialize(short obj) + protected override void WriteObjectToStream(short obj, Stream stream) { - return BitConverter.GetBytes(obj); + byte[] data = BitConverter.GetBytes(obj); + stream.Write(data); } protected override short ProcessDeserialize(byte[] stream, ref int offset) diff --git a/src/BinaryFormatter/TypeConverter/StringConverter.cs b/src/BinaryFormatter/TypeConverter/StringConverter.cs index 076091e..69157d3 100644 --- a/src/BinaryFormatter/TypeConverter/StringConverter.cs +++ b/src/BinaryFormatter/TypeConverter/StringConverter.cs @@ -1,6 +1,8 @@ using System; +using System.IO; using System.Text; using BinaryFormatter.Types; +using BinaryFormatter.Utils; namespace BinaryFormatter.TypeConverter { @@ -8,18 +10,14 @@ internal class StringConverter : BaseTypeConverter { private int Size { get; set; } - protected override byte[] ProcessSerialize(string obj) + protected override void WriteObjectToStream(string obj, Stream stream) { byte[] objBytes = Encoding.UTF8.GetBytes(obj); byte[] sizeBytes = BitConverter.GetBytes(objBytes.Length); Size = objBytes.Length; - byte[] serializedStringWithSize = new byte[sizeof(int) + objBytes.Length]; - - Array.ConstrainedCopy(sizeBytes, 0, serializedStringWithSize, 0, sizeBytes.Length); - Array.ConstrainedCopy(objBytes, 0, serializedStringWithSize, sizeBytes.Length, objBytes.Length); - - return serializedStringWithSize; + stream.Write(sizeBytes); + stream.Write(objBytes); } protected override string ProcessDeserialize(byte[] stream, ref int offset) diff --git a/src/BinaryFormatter/TypeConverter/UIntConverter.cs b/src/BinaryFormatter/TypeConverter/UIntConverter.cs index c47a182..e55a8a9 100644 --- a/src/BinaryFormatter/TypeConverter/UIntConverter.cs +++ b/src/BinaryFormatter/TypeConverter/UIntConverter.cs @@ -1,13 +1,16 @@ using System; +using System.IO; using BinaryFormatter.Types; +using BinaryFormatter.Utils; namespace BinaryFormatter.TypeConverter { internal class UIntConverter : BaseTypeConverter { - protected override byte[] ProcessSerialize(uint obj) + protected override void WriteObjectToStream(uint obj, Stream stream) { - return BitConverter.GetBytes(obj); + byte[] data = BitConverter.GetBytes(obj); + stream.Write(data); } protected override uint ProcessDeserialize(byte[] stream, ref int offset) diff --git a/src/BinaryFormatter/TypeConverter/ULongConverter.cs b/src/BinaryFormatter/TypeConverter/ULongConverter.cs index dbed65b..9de96f9 100644 --- a/src/BinaryFormatter/TypeConverter/ULongConverter.cs +++ b/src/BinaryFormatter/TypeConverter/ULongConverter.cs @@ -1,13 +1,16 @@ using System; +using System.IO; using BinaryFormatter.Types; +using BinaryFormatter.Utils; namespace BinaryFormatter.TypeConverter { internal class ULongConverter : BaseTypeConverter { - protected override byte[] ProcessSerialize(ulong obj) + protected override void WriteObjectToStream(ulong obj, Stream stream) { - return BitConverter.GetBytes(obj); + byte[] data = BitConverter.GetBytes(obj); + stream.Write(data); } protected override ulong ProcessDeserialize(byte[] stream, ref int offset) diff --git a/src/BinaryFormatter/TypeConverter/UshortConverter.cs b/src/BinaryFormatter/TypeConverter/UshortConverter.cs index f09824a..b6b84a6 100644 --- a/src/BinaryFormatter/TypeConverter/UshortConverter.cs +++ b/src/BinaryFormatter/TypeConverter/UshortConverter.cs @@ -1,13 +1,16 @@ using System; +using System.IO; using BinaryFormatter.Types; +using BinaryFormatter.Utils; namespace BinaryFormatter.TypeConverter { internal class UShortConverter : BaseTypeConverter { - protected override byte[] ProcessSerialize(ushort obj) + protected override void WriteObjectToStream(ushort obj, Stream stream) { - return BitConverter.GetBytes(obj); + byte[] data = BitConverter.GetBytes(obj); + stream.Write(data); } protected override ushort ProcessDeserialize(byte[] stream, ref int offset) diff --git a/tests/BinaryFormatterTests/TypeConverter/BaseTypeConverterTests.cs b/tests/BinaryFormatterTests/TypeConverter/BaseTypeConverterTests.cs index 6f942a2..6533e78 100644 --- a/tests/BinaryFormatterTests/TypeConverter/BaseTypeConverterTests.cs +++ b/tests/BinaryFormatterTests/TypeConverter/BaseTypeConverterTests.cs @@ -3,6 +3,7 @@ using System.Text; using BinaryFormatter.TypeConverter; using BinaryFormatter.Types; +using BinaryFormatter.Utils; using Xunit; namespace BinaryFormatterTests.TypeConverter @@ -34,9 +35,10 @@ protected override int GetTypeSize() return Message.Length; } - protected override byte[] ProcessSerialize(string obj) + protected override void WriteObjectToStream(string obj, Stream stream) { - return Encoding.UTF8.GetBytes(obj); + var data = Encoding.UTF8.GetBytes(obj); + stream.Write(data); } protected override string ProcessDeserialize(byte[] stream, ref int offset)