public void ByteArrayWithoutLengthPrefixCanBeReaded() { // Arrange var data = Encoding.UTF8.GetBytes("hello world"); // Act var stream = new DeserializationStream(data); var result = stream.ReadBytes(data.Length); // Assert result.Should().Equal(data); stream.Offset.Should().Be(data.Length); }
protected override object DeserializeInternal(DeserializationStream stream, Type sourceType) { Type collectionType = sourceType; if (collectionType == typeof(object)) { collectionType = typeof(List <object>); } var deserializedCollection = Activator.CreateInstance(collectionType); bool isDictionary = TypeHelper.IsDictionary(deserializedCollection); bool isLinkedList = TypeHelper.IsLinkedList(deserializedCollection); IList deserializedCollectionAsList = null; IDictionary deserializedCollectionAsDictionary = null; if (isDictionary) { deserializedCollectionAsDictionary = (IDictionary)deserializedCollection; } else if (isLinkedList) { Type listType = typeof(List <>).MakeGenericType(sourceType.GenericTypeArguments); deserializedCollectionAsList = (IList)Activator.CreateInstance(listType); } else { deserializedCollectionAsList = (IList)deserializedCollection; } if (!stream.HasEnded) { BinaryConverter converter = new BinaryConverter(); int sizeCollection = stream.ReadInt(); for (int i = 0; i < sizeCollection; i++) { int sizeData = stream.ReadInt(); if (sizeData == 0) { continue; } byte[] dataValue = stream.ReadBytes(sizeData); MethodInfo method = typeof(BinaryConverter).GetRuntimeMethod("Deserialize", new[] { typeof(byte[]) }); if (sourceType.GenericTypeArguments.Length > 0) { if (isDictionary) { Type elementType = typeof(KeyValuePair <,>).MakeGenericType(sourceType.GenericTypeArguments); method = method.MakeGenericMethod(elementType); } else { method = method.MakeGenericMethod(sourceType.GenericTypeArguments); } } else { method = method.MakeGenericMethod(typeof(object)); } var deserializeItem = method.Invoke(converter, new object[] { dataValue }); if (isDictionary) { KeyValuePair <object, object> keyValuePairFormObject = TypeHelper.CastFrom(deserializeItem); deserializedCollectionAsDictionary.Add(keyValuePairFormObject.Key, keyValuePairFormObject.Value); } else { deserializedCollectionAsList.Add(deserializeItem); } } } if (isLinkedList) { deserializedCollection = Activator.CreateInstance(collectionType, deserializedCollectionAsList); } return(deserializedCollection); }