UnpackInt32() public static method

Unpacks System.Int32 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 . /// Note that the state of will be unpredictable espicially it is not seekable. ///
public static UnpackInt32 ( Stream source ) : Int32
source Stream The which contains Message Pack binary stream.
return System.Int32
Exemplo n.º 1
0
        private static void TestInt32(Int32 value)
        {
            var output = new MemoryStream();

            Packer.Create(output).Pack(value);
            Assert.AreEqual(value, Unpacking.UnpackInt32(new MemoryStream(output.ToArray())));
            Assert.AreEqual(value, Unpacking.UnpackInt32(output.ToArray()).Value);
        }
Exemplo n.º 2
0
        public void TestUnpackCharStream_Stream_1Char_AsIsAndBounded()
        {
            using (var stream = new MemoryStream(new byte[] { 0xA1, ( byte )'A', 0xFF }))
            {
                using (var result = Unpacking.UnpackCharStream(stream))
                {
                    AssertStringReader(result, 1, "A");
                }

                // Assert is valid position on unerlying stream.
                Assert.That(Unpacking.UnpackInt32(stream), Is.EqualTo(-1));
            }
        }
Exemplo n.º 3
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));
            }
        }
Exemplo n.º 4
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));
            }
        }
Exemplo n.º 5
0
        public void TestUnpackCharStream_Stream_Encoding_Empty_AsIsAndBounded()
        {
            using (var stream = new MemoryStream(new byte[] { 0xA0, 0xFF }))
            {
#if !NETFX_CORE && !SILVERLIGHT
                using (var result = Unpacking.UnpackCharStream(stream, Encoding.UTF32))
#else
                using (var result = Unpacking.UnpackCharStream(stream, Encoding.UTF8))
#endif // !NETFX_CORE && !SILVERLIGHT
                {
                    AssertStringReader(result, 0, String.Empty);
                }

                // Assert is valid position on unerlying stream.
                Assert.That(Unpacking.UnpackInt32(stream), Is.EqualTo(-1));
            }
        }
Exemplo n.º 6
0
        public void TestUnpackCharStream_Stream_Encoding_1Byte_AsIsAndBounded()
        {
#if !NETFX_CORE && !SILVERLIGHT
            using (var stream = new MemoryStream(new byte[] { 0xA4, 0x00, 0x00, 0x00, ( byte )'A', 0xFF }))
#else
            using (var stream = new MemoryStream(new byte[] { 0xA2, 0x00, ( byte )'A', 0xFF }))
#endif // !NETFX_CORE && !SILVERLIGHT
            {
#if !NETFX_CORE && !SILVERLIGHT
                using (var result = Unpacking.UnpackCharStream(stream, new UTF32Encoding(bigEndian: true, byteOrderMark: false, throwOnInvalidCharacters: true)))
                {
                    AssertStringReader(result, 4, "A");
                }
#else
                using (var result = Unpacking.UnpackCharStream(stream, new UnicodeEncoding(bigEndian: true, byteOrderMark: false, throwOnInvalidBytes: true)))
                {
                    AssertStringReader(result, 2, "A");
                }
#endif // !NETFX_CORE && !SILVERLIGHT
                // Assert is valid position on unerlying stream.
                Assert.That(Unpacking.UnpackInt32(stream), Is.EqualTo(-1));
            }
        }
Exemplo n.º 7
0
        public void TestMap()
        {
            var emptyMap = new Dictionary <int, int>();
            {
                var output = new MemoryStream();
                Packer.Create(output).PackDictionary(emptyMap);
                Assert.AreEqual(0, Unpacking.UnpackDictionaryCount(new MemoryStream(output.ToArray())));
                Assert.AreEqual(0, Unpacking.UnpackDictionaryCount(output.ToArray()).Value);
            }

            var random = new Random();

            for (int i = 0; i < 100; i++)
            {
                var m   = new Dictionary <int, int>();
                int len = ( int )random.Next() % 1000 + 1;
                for (int j = 0; j < len; j++)
                {
                    m[j] = j;
                }
                var output = new MemoryStream();
                Packer.Create(output).PackDictionary(m);

                Stream streamInput = new MemoryStream(output.ToArray());
                Assert.AreEqual(len, Unpacking.UnpackDictionaryCount(streamInput));
                for (int j = 0; j < len; j++)
                {
                    int value;
                    Assert.IsTrue(m.TryGetValue(Unpacking.UnpackInt32(streamInput), out value));
                    Assert.AreEqual(value, Unpacking.UnpackInt32(streamInput));
                }

                byte[] byteArrayInput = output.ToArray();
                var    arrayLength    = Unpacking.UnpackDictionaryCount(byteArrayInput);
                Assert.AreEqual(len, arrayLength.Value);
                int offset = arrayLength.ReadCount;
                for (int j = 0; j < len; j++)
                {
                    var keyUar = Unpacking.UnpackInt32(byteArrayInput, offset);
                    Assert.AreNotEqual(0, keyUar.ReadCount);
                    int value;
                    Assert.IsTrue(m.TryGetValue(keyUar.Value, out value));
                    var valueUar = Unpacking.UnpackInt32(byteArrayInput, offset + keyUar.ReadCount);
                    Assert.AreEqual(value, valueUar.Value);
                    offset += keyUar.ReadCount + valueUar.ReadCount;
                }
            }

            for (int i = 0; i < 100; i++)
            {
                var m   = new Dictionary <string, int>();
                int len = ( int )random.Next() % 1000 + 1;
                for (int j = 0; j < len; j++)
                {
                    m[j.ToString()] = j;
                }
                var output = new MemoryStream();
                Packer.Create(output).PackDictionary(m);

                Stream streamInput = new MemoryStream(output.ToArray());
                Assert.AreEqual(len, Unpacking.UnpackDictionaryCount(streamInput));
                for (int j = 0; j < len; j++)
                {
                    int value;
                    Assert.IsTrue(m.TryGetValue(Unpacking.UnpackString(streamInput), out value));
                    Assert.AreEqual(value, Unpacking.UnpackInt32(streamInput));
                }

                byte[] byteArrayInput = output.ToArray();
                var    arrayLength    = Unpacking.UnpackDictionaryCount(byteArrayInput);
                Assert.AreEqual(len, arrayLength.Value);
                int offset = arrayLength.ReadCount;
                for (int j = 0; j < len; j++)
                {
                    var usr = Unpacking.UnpackString(byteArrayInput, offset);
                    Assert.AreNotEqual(0, usr.ReadCount);
                    int value;
                    Assert.IsTrue(m.TryGetValue(usr.Value, out value));
                    var uar = Unpacking.UnpackInt32(byteArrayInput, offset + usr.ReadCount);
                    Assert.AreEqual(value, uar.Value);
                    offset += usr.ReadCount + uar.ReadCount;
                }
            }
        }
Exemplo n.º 8
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;
                }
            }
        }
Exemplo n.º 9
0
 public void TestUnpackInt32_Eof()
 {
     Assert.Throws <InvalidMessagePackStreamException>(() => Unpacking.UnpackInt32(new byte[] { 0xD0 }));
 }
Exemplo n.º 10
0
 public void TestUnpackInt32_NotNumeric()
 {
     Assert.Throws <MessageTypeException>(() => Unpacking.UnpackInt32(new byte[] { 0x80 }));
 }
Exemplo n.º 11
0
 public void TestUnpackInt32_Eof()
 {
     Assert.Throws <UnpackException>(() => Unpacking.UnpackInt32(new byte[] { 0xD0 }));
 }