Exemplo n.º 1
0
    public void Length()
    {
        Stream stream = EmptyStream;

        Assert.AreEqual(0, stream.Length);
        stream.Dispose();
        // ReSharper disable once AccessToModifiedClosure
        Assert.ThrowsException <ObjectDisposedException>(() => stream.Length);

        stream = MultiSegmentSequence.AsStream();
        Assert.AreEqual(MultiSegmentSequence.Length, stream.Length);
    }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public JsonRpcMessage Deserialize(ReadOnlySequence <byte> contentBuffer)
        {
            switch (_serializerKind)
            {
            case MessagePackSerializerKind.LZ4MessagePackSerializer:
                return(LZ4MessagePackSerializer.Deserialize <JsonRpcMessage>(contentBuffer.AsStream(), _resolver));

            case MessagePackSerializerKind.MessagePackSerializer:
                return(MessagePackSerializer.Deserialize <JsonRpcMessage>(contentBuffer.AsStream(), _resolver));

            default:
                throw new InvalidOperationException("internal error");
            }
        }
Exemplo n.º 3
0
 internal static object?Deserialize(ReadOnlySequence <byte> input, DataContractSerializer serializer)
 {
     using var stream = input.AsStream();
     using (var reader = XmlDictionaryReader.CreateBinaryReader(stream, XmlDictionaryReaderQuotas.Max))
     {
         return(serializer.ReadObject(reader));
     }
 }
        public static async Task CopyToStreamAsync(ReadOnlySequence<byte> sequence)
        {
            using var dest = new MemoryStream();
            using var src = sequence.AsStream();

            await src.CopyToAsync(dest);
            dest.Position = 0;
            Equal(data, dest.ToArray());
        }
Exemplo n.º 5
0
        public static void CopyToStream(ReadOnlySequence <byte> sequence)
        {
            using var dest = new MemoryStream();
            using var src  = sequence.AsStream();
            Equal(0L, src.Position);
            Equal(sequence.Length, src.Length);

            src.CopyTo(dest);
            dest.Position = 0;
            Equal(data, dest.ToArray());
        }
Exemplo n.º 6
0
    public void Position()
    {
        Stream stream = EmptyStream;

        Assert.ThrowsException <ArgumentOutOfRangeException>(() => stream.Position = 1);

        Stream singleStream = SingleSegmentSequence.AsStream();
        Stream multiStream  = MultiSegmentSequence.AsStream();
        int    n            = RandomNumberGenerator.GetInt32(2, 100);

        Assert.AreEqual(0, singleStream.Position);
        ++singleStream.Position;
        Assert.AreEqual(1, singleStream.Position);
        singleStream.Position += n;
        Assert.AreEqual(1 + n, singleStream.Position);

        for (int i = 0; i < MultiSegmentSequence.Length; ++i)
        {
            multiStream.Position = i;
            Assert.AreNotEqual(-1, multiStream.ReadByte());
            Assert.AreEqual(i + 1, multiStream.Position);
        }

        multiStream.Position = 0;
        Assert.AreNotEqual(-1, multiStream.ReadByte());
        Assert.AreEqual(1, multiStream.Position);

        multiStream.Position = MultiSegmentSequence.Length;
        Assert.AreEqual(-1, multiStream.ReadByte());

        // ReSharper disable AccessToDisposedClosure
        Assert.ThrowsException <ArgumentOutOfRangeException>(() => multiStream.Position = MultiSegmentSequence.Length + 1);
        Assert.ThrowsException <ArgumentOutOfRangeException>(() => multiStream.Position = -1);
        // ReSharper restore AccessToDisposedClosure

        multiStream.Dispose();
        Assert.ThrowsException <ObjectDisposedException>(() => multiStream.Position = 0);
    }
Exemplo n.º 7
0
 public static async ValueTask <T> DeserializeAsync <T>(this BinarySerializer Serializer, ReadOnlySequence <byte> ReadOnlySequence)
 {
     return(await Serializer.DeserializeAsync <T>(ReadOnlySequence.AsStream()));
 }
Exemplo n.º 8
0
 /// <inheritdoc/>
 public JsonRpcMessage Deserialize(ReadOnlySequence <byte> contentBuffer)
 {
     return(this.compress
         ? (JsonRpcMessage)LZ4MessagePackSerializer.Typeless.Deserialize(contentBuffer.AsStream())
         : (JsonRpcMessage)MessagePackSerializer.Typeless.Deserialize(contentBuffer.AsStream()));
 }
 /// <inheritdoc/>
 public JsonRpcMessage Deserialize(ReadOnlySequence <byte> contentBuffer) => (JsonRpcMessage)MessagePackSerializer.Deserialize <object>(contentBuffer.AsStream(), this.options);