UnpackArrayLength() public static method

Unpacks length of the array value from the specified Stream.

The processed bytes count can be calculated via P:Stream.Position of source when the P:Stream.CanSeek is true.

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 compatible to nullable . /// Note that the state of will be unpredictable espicially it is not seekable. ///
public static UnpackArrayLength ( Stream source ) : Int64?
source Stream The which contains Message Pack binary stream.
return Int64?
コード例 #1
0
        public void TestUnpackArrayLength_ByteArray_Offset_OffsetIsValid_OffsetIsRespected()
        {
            var result = Unpacking.UnpackArrayLength(new byte[] { 0xFF, 0x90, 0xFF }, 1);

            Assert.AreEqual(1, result.ReadCount);
            Assert.AreEqual(0, result.Value);
        }
コード例 #2
0
        public void TestUnpackArrayLength_ByteArray_FixArray0Value_AsArray16_AsIs()
        {
            var result = Unpacking.UnpackArrayLength(new byte[] { 0xDC, 0x00, 0x00 });

            Assert.AreEqual(3, result.ReadCount);
            Assert.That(result.Value, Is.EqualTo(0));
        }
コード例 #3
0
        public void TestUnpackArrayLength_ByteArray_Null_Nil()
        {
            var result = Unpacking.UnpackArrayLength(new byte[] { 0xC0 });

            Assert.AreEqual(1, result.ReadCount);
            Assert.IsNull(result.Value);
        }
コード例 #4
0
        public void TestUnpackArrayLength_ByteArray_Array32MinValue_AsArray32_AsIs()
        {
            var result = Unpacking.UnpackArrayLength(new byte[] { 0xDD, 0x00, 0x01, 0x00, 0x00 }.Concat(Enumerable.Repeat(( byte )0x57, 0x10000)).ToArray());

            Assert.AreEqual(5, result.ReadCount);
            Assert.That(result.Value, Is.EqualTo(0x10000));
        }
コード例 #5
0
        public void TestUnpackArrayLength_ByteArray_Array16MaxValue_AsArray16_AsIs()
        {
            var result = Unpacking.UnpackArrayLength(new byte[] { 0xDC, 0xFF, 0xFF }.Concat(Enumerable.Repeat(( byte )0x57, 0xFFFF)).ToArray());

            Assert.AreEqual(3, result.ReadCount);
            Assert.That(result.Value, Is.EqualTo(0xFFFF));
        }
コード例 #6
0
        public void TestUnpackArrayLength_ArrayLengthIsGreaterThanInt32MaxValue()
        {
            var result = Unpacking.UnpackArrayLength(new byte[] { 0xDD, 0x80, 0x00, 0x00, 0x00, 0xFF });

            Assert.That(result.ReadCount, Is.EqualTo(5));
            Assert.That(result.Value, Is.EqualTo(Int32.MaxValue + 1L));
        }
コード例 #7
0
        public void TestUnpackArrayLength_ByteArray_FixArray1Value_AsFixArray1_AsIs()
        {
            var result = Unpacking.UnpackArrayLength(new byte[] { 0x91 }.Concat(Enumerable.Repeat(( byte )0x57, 1)).ToArray());

            Assert.AreEqual(1, result.ReadCount);
            Assert.That(result.Value, Is.EqualTo(1));
        }
コード例 #8
0
 public void TestUnpackArrayLength_Stream_FixArray0Value_AsArray16_AsIs()
 {
     using (var buffer = new MemoryStream(new byte[] { 0xDC, 0x00, 0x00 }))
     {
         var result = Unpacking.UnpackArrayLength(buffer);
         Assert.AreEqual(3, buffer.Position);
         Assert.That(result, Is.EqualTo(0));
     }
 }
コード例 #9
0
 public void TestUnpackArrayLength_Stream_FixArray1Value_AsFixArray1_AsIs()
 {
     using (var buffer = new MemoryStream(new byte[] { 0x91 }.Concat(Enumerable.Repeat(( byte )0x57, 1)).ToArray()))
     {
         var result = Unpacking.UnpackArrayLength(buffer);
         Assert.AreEqual(1, buffer.Position);
         Assert.That(result, Is.EqualTo(1));
     }
 }
コード例 #10
0
 public void TestUnpackArrayLength_Stream_Array32MinValue_AsArray32_AsIs()
 {
     using (var buffer = new MemoryStream(new byte[] { 0xDD, 0x00, 0x01, 0x00, 0x00 }.Concat(Enumerable.Repeat(( byte )0x57, 0x10000)).ToArray()))
     {
         var result = Unpacking.UnpackArrayLength(buffer);
         Assert.AreEqual(5, buffer.Position);
         Assert.That(result, Is.EqualTo(0x10000));
     }
 }
コード例 #11
0
 public void TestUnpackArrayLength_Stream_Array16MaxValue_AsArray16_AsIs()
 {
     using (var buffer = new MemoryStream(new byte[] { 0xDC, 0xFF, 0xFF }.Concat(Enumerable.Repeat(( byte )0x57, 0xFFFF)).ToArray()))
     {
         var result = Unpacking.UnpackArrayLength(buffer);
         Assert.AreEqual(3, buffer.Position);
         Assert.That(result, Is.EqualTo(0xFFFF));
     }
 }
コード例 #12
0
 public void TestUnpackArrayLength_ByteArray_NotArray()
 {
     Assert.Throws <MessageTypeException>(() => Unpacking.UnpackArrayLength(new byte[] { 0x1 }));
 }
コード例 #13
0
 public void TestUnpackArrayLength_Stream_Null()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackArrayLength(default(Stream)));
 }
コード例 #14
0
 public void TestUnpackArrayLength_ByteArray_Offset_OffsetIsTooBig()
 {
     Assert.Throws <ArgumentException>(() => Unpacking.UnpackArrayLength(new byte[] { 0x1 }, 1));
 }
コード例 #15
0
 public void TestUnpackArrayLength_ByteArray_Offset_OffsetIsNegative()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => Unpacking.UnpackArrayLength(new byte[] { 0x1 }, -1));
 }
コード例 #16
0
 public void TestUnpackArrayLength_ByteArray_Offset_Null()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackArrayLength(default(byte[]), 0));
 }
コード例 #17
0
        public void TestArray()
        {
            var emptyList = new List <int>();
            {
                var output = new MemoryStream();
                Packer.Create(output).PackCollection(emptyList);
                Assert.AreEqual(0, Unpacking.UnpackArrayLength(new MemoryStream(output.ToArray())));
                Assert.AreEqual(0, Unpacking.UnpackArrayLength(output.ToArray()).Value);
            }

            var random = new Random();

            for (int i = 0; i < 100; i++)
            {
                var l   = new List <int>();
                int len = ( int )random.Next() % 1000 + 1;
                for (int j = 0; j < len; j++)
                {
                    l.Add(j);
                }
                var output = new MemoryStream();
                Packer.Create(output).PackCollection(l);

                Stream streamInput = new MemoryStream(output.ToArray());
                Assert.AreEqual(len, Unpacking.UnpackArrayLength(streamInput));
                for (int j = 0; j < len; j++)
                {
                    Assert.AreEqual(l[j], Unpacking.UnpackInt32(streamInput));
                }

                byte[] byteArrayInput = output.ToArray();
                var    arrayLength    = Unpacking.UnpackArrayLength(byteArrayInput);
                Assert.AreEqual(len, arrayLength.Value);
                int offset = arrayLength.ReadCount;
                for (int j = 0; j < len; j++)
                {
                    var uar = Unpacking.UnpackInt32(byteArrayInput, offset);
                    Assert.AreNotEqual(0, uar.ReadCount);
                    Assert.AreEqual(l[j], uar.Value);
                    offset += uar.ReadCount;
                }
            }

            for (int i = 0; i < 100; i++)
            {
                var l   = new List <String>();
                int len = ( int )random.Next() % 1000 + 1;
                for (int j = 0; j < len; j++)
                {
                    l.Add(j.ToString());
                }
                var output = new MemoryStream();
                Packer.Create(output).PackCollection(l);

                Stream streamInput = new MemoryStream(output.ToArray());
                Assert.AreEqual(len, Unpacking.UnpackArrayLength(streamInput));
                for (int j = 0; j < len; j++)
                {
                    Assert.AreEqual(l[j], Unpacking.UnpackString(streamInput));
                }

                byte[] byteArrayInput = output.ToArray();
                var    arrayLength    = Unpacking.UnpackArrayLength(byteArrayInput);
                Assert.AreEqual(len, arrayLength.Value);
                int offset = arrayLength.ReadCount;
                for (int j = 0; j < len; j++)
                {
                    var usr = Unpacking.UnpackString(byteArrayInput, offset);
                    Assert.AreEqual(l[j], usr.Value);
                    offset += usr.ReadCount;
                }
            }
        }
コード例 #18
0
 public void TestUnpackArrayLength_ByteArray_Offset_Empty()
 {
     Assert.Throws <ArgumentException>(() => Unpacking.UnpackArrayLength(new byte[0], 0));
 }