UnpackByteStream() public static method

Unpacks raw value from the specified Stream as UnpackingStream.

UnpackingStream does not own source, so source still should be closed.

When the type of packed value is not known, use UnpackObject(Stream) instead.

/// is null. /// /// The of is false. /// /// is not valid MessagePack stream. /// Note that the state of will be unpredictable espicially it is not seekable. /// /// The unpacked result in the is not raw binary. /// Note that the state of will be unpredictable espicially it is not seekable. ///
public static UnpackByteStream ( Stream source ) : UnpackingStream
source Stream The which contains Message Pack binary stream.
return UnpackingStream
示例#1
0
        public void TestUnpackByteStream_Nil_AsEmpty()
        {
            var target = Unpacking.UnpackByteStream(new MemoryStream(new byte[] { 0xC0 }));

            Assert.That(target, Is.Not.Null);
            Assert.That(target.Length, Is.EqualTo(0));
        }
示例#2
0
 public void TestUnseekableByteStream_setPosition()
 {
     using (var target = Unpacking.UnpackByteStream(new WrapperStream(new MemoryStream(new byte[] { 0xA1, 0xFF }), canSeek: false)))
     {
         Assert.Throws <NotSupportedException>(() => target.Position = 0);
     }
 }
示例#3
0
 public void TestUnpackByteStream_EofInHeader()
 {
     using (var underlying = new MemoryStream(new byte[] { 0xDA, 0x1 }))
     {
         Assert.Throws <UnpackException>(() => Unpacking.UnpackByteStream(underlying));
     }
 }
示例#4
0
 public void TestUnseekableByteStream_Seek()
 {
     using (var target = Unpacking.UnpackByteStream(new WrapperStream(new MemoryStream(new byte[] { 0xA1, 0xFF }), canSeek: false)))
     {
         Assert.Throws <NotSupportedException>(() => target.Seek(0, SeekOrigin.Current));
     }
 }
示例#5
0
 public void TestUnpackByteStream_Stream_SeekableStream_CanSeekIsFalse()
 {
     using (var stream = new WrapperStream(new MemoryStream(new byte[] { 0xA0, 0x57 }), canRead: true, canSeek: false))
     {
         using (var result = Unpacking.UnpackByteStream(stream))
         {
             Assert.That(result.CanSeek, Is.False);
         }
     }
 }
示例#6
0
        public void TestUnpackByteStream_Stream_0x10000Byte_AsIsAndBounded()
        {
            using (var stream = new MemoryStream(new byte[] { 0xDB, 0x00, 0x01, 0x00, 0x00 }.Concat(Enumerable.Repeat(( byte )0xFF, 0x10000)).Concat(Enumerable.Repeat(( byte )0x57, 1)).ToArray()))
            {
                using (var result = Unpacking.UnpackByteStream(stream))
                {
                    AssertRawStream(result, 0x10000);
                }

                // Assert is valid position on unerlying stream.
                Assert.That(Unpacking.UnpackInt32(stream), Is.EqualTo(0x57));
            }
        }
示例#7
0
        public void TestUnpackByteStream_Stream_1Byte_AsIsAndBounded()
        {
            using (var stream = new MemoryStream(new byte[] { 0xA1, 0xFF, 0x57 }))
            {
                using (var result = Unpacking.UnpackByteStream(stream))
                {
                    AssertRawStream(result, 1);
                }

                // Assert is valid position on unerlying stream.
                Assert.That(Unpacking.UnpackInt32(stream), Is.EqualTo(0x57));
            }
        }
示例#8
0
        public void TestUnpackByteStream_EofInBody_CanFeed()
        {
            using (var underlying = new MemoryStream())
            {
                underlying.WriteByte(0xA1);
                underlying.Position = 0;
                var target = Unpacking.UnpackByteStream(underlying);
                Assert.That(target.Length, Is.EqualTo(1));
                // Check precondition
                var b = target.ReadByte();
                Assert.That(b, Is.EqualTo(-1));

                // Feed
                // Assume that underlying supports Feed (appends bytes to tail, and does not move Position).
                underlying.WriteByte(0x57);
                underlying.Position -= 1;
                b = target.ReadByte();
                Assert.That(b, Is.EqualTo(0x57));
            }
        }
示例#9
0
        public void TestUnpackByteStream_Stream_LengthIsGreaterThanInt32MaxValue_CanReadToEnd()
        {
            // Header + Body Length ( Int32.MaxValue + 1 )
            var    bodyLength = Int32.MaxValue + 1L;
            var    length     = 1L + 4L + bodyLength;
            string filePath   = Path.GetTempFileName();

            try
            {
                File.SetAttributes(filePath, FileAttributes.SparseFile);
                using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None, 64 * 1024))
                {
                    fileStream.SetLength(length);
                    fileStream.Position = 0;
                    fileStream.Write(new byte[] { 0xDB, 0x80, 0x00, 0x00, 0x00 }, 0, 5);
                    fileStream.Flush();

                    fileStream.Position = 0;

                    using (var target = Unpacking.UnpackByteStream(fileStream))
                    {
                        Assert.That(target.Length, Is.EqualTo(bodyLength));
                        byte[] buffer      = new byte[64 * 1024];
                        long   totalLength = 0;
                        for (int read = target.Read(buffer, 0, buffer.Length); read > 0; read = target.Read(buffer, 0, buffer.Length))
                        {
                            totalLength += read;
                        }

                        Assert.That(totalLength, Is.EqualTo(bodyLength));
                    }
                }
            }
            finally
            {
                File.Delete(filePath);
            }
        }
示例#10
0
 private static Stream CreateStreamForByteStreamTest()
 {
     // Positin == Value
     return(Unpacking.UnpackByteStream(new MemoryStream(new byte[] { 0xA3, 0x0, 0x1, 0x2 })));
 }
示例#11
0
 public void TestUnpackByteStream_NotRaw()
 {
     Assert.Throws <MessageTypeException>(() => Unpacking.UnpackByteStream(new MemoryStream(new byte[] { 0x80 })));
 }
示例#12
0
 public void TestUnpackByteStream_Empty()
 {
     Assert.Throws <UnpackException>(() => Unpacking.UnpackByteStream(new MemoryStream()));
 }
示例#13
0
 public void TestUnpackByteStream_Stream_Null()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackByteStream(null));
 }