UnpackString() public static method

Unpacks String value from the head of specified byte array with UTF-8 encoding.

Invocation of this method is equivalant to call UnpackString(byte[], int) with offset is 0.

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

/// is null. /// /// is empty. /// /// is not valid MessagePack stream. /// /// The unpacked result in the is not compatible to . /// Or, the unpacked result in the is invalid as UTF-8 encoded byte stream. ///
public static UnpackString ( byte source ) : UnpackingResult
source byte The byte array which contains Message Pack binary stream.
return UnpackingResult
Esempio n. 1
0
        public void TestUnpackString_ByteArray_1Char_AsIsAndBounded()
        {
            var result = Unpacking.UnpackString(new byte[] { 0xA1, ( byte )'A', 0xFF });

            Assert.That(result.ReadCount, Is.EqualTo(2));
            Assert.That(result.Value, Is.EqualTo("A"));
        }
Esempio n. 2
0
 public void TestUnpackString_Stream_1ByteNonUtf8String()
 {
     using (var stream = new MemoryStream(new byte[] { 0xA1, 0xFF }))
     {
         Assert.Throws <MessageTypeException>(() => Unpacking.UnpackString(stream));
     }
 }
Esempio n. 3
0
        public void TestUnpackString_ByteArray_Empty_AsIsAndBounded()
        {
            var result = Unpacking.UnpackString(new byte[] { 0xA0, 0xFF });

            Assert.That(result.ReadCount, Is.EqualTo(1));
            Assert.That(result.Value, Is.EqualTo(String.Empty));
        }
Esempio n. 4
0
        private static void TestString(String value)
        {
            var output = new MemoryStream();

            Packer.Create(output).PackString(value);
            Assert.AreEqual(value, Unpacking.UnpackString(new MemoryStream(output.ToArray())));
            Assert.AreEqual(value, Unpacking.UnpackString(output.ToArray()).Value);
        }
Esempio n. 5
0
 public void TestUnpackString_Stream_1Char_AsIsAndBounded()
 {
     using (var stream = new MemoryStream(new byte[] { 0xA1, ( byte )'A', 0xFF }))
     {
         var result = Unpacking.UnpackString(stream);
         Assert.That(stream.Position, Is.EqualTo(2));
         Assert.That(result, Is.EqualTo("A"));
     }
 }
Esempio n. 6
0
 public void TestUnpackString_Stream_Empty_AsIsAndBounded()
 {
     using (var stream = new MemoryStream(new byte[] { 0xA0, 0xFF }))
     {
         var result = Unpacking.UnpackString(stream);
         Assert.That(stream.Position, Is.EqualTo(1));
         Assert.That(result, Is.EqualTo(String.Empty));
     }
 }
Esempio n. 7
0
        public void TestUnpackString_ByteArray_Encoding_Empty_AsIsAndBounded()
        {
#if !NETFX_CORE && !SILVERLIGHT
            var result = Unpacking.UnpackString(new byte[] { 0xA0, 0xFF }, Encoding.UTF32);
#else
            var result = Unpacking.UnpackString(new byte[] { 0xA0, 0xFF }, Encoding.UTF8);
#endif // !NETFX_CORE && !SILVERLIGHT
            Assert.That(result.ReadCount, Is.EqualTo(1));
            Assert.That(result.Value, Is.EqualTo(String.Empty));
        }
Esempio n. 8
0
        public void TestUnpackString_ByteArray_Encoding_1ByteNonSpecifiedString()
        {
#if MONO
            Assert.Inconclusive("UTF32Encoding does not throw exception on Mono FCL.");
#elif !NETFX_CORE
            Assert.Throws <MessageTypeException>(() => Unpacking.UnpackString(new byte[] { 0xA4, 0x7F, 0x7F, 0x7F, 0x7F }, new UTF32Encoding(bigEndian: true, byteOrderMark: false, throwOnInvalidCharacters: true)));
#else
            Assert.Throws <MessageTypeException>(() => Unpacking.UnpackString(new byte[] { 0xA5, 0xF8, 0x88, 0x80, 0x80, 0x80 }, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true)));
#endif
        }
Esempio n. 9
0
        public void TestUnpackString_ByteArray_Encoding_1Byte_AsIsAndBounded()
        {
#if !NETFX_CORE && !SILVERLIGHT
            var result = Unpacking.UnpackString(new byte[] { 0xA4, 0x00, 0x00, 0x00, ( byte )'A', 0xFF }, new UTF32Encoding(bigEndian: true, byteOrderMark: false, throwOnInvalidCharacters: true));
            Assert.That(result.ReadCount, Is.EqualTo(5));
            Assert.That(result.Value, Is.EqualTo("A"));
#else
            var result = Unpacking.UnpackString(new byte[] { 0xA2, 0x00, ( byte )'A', 0xFF }, new UnicodeEncoding(bigEndian: true, byteOrderMark: false, throwOnInvalidBytes: true));
            Assert.That(result.ReadCount, Is.EqualTo(3));
            Assert.That(result.Value, Is.EqualTo("A"));
#endif // !NETFX_CORE && !SILVERLIGHT
        }
Esempio n. 10
0
        public void TestUnpackString_Stream_Encoding_Empty_AsIsAndBounded()
        {
            using (var stream = new MemoryStream(new byte[] { 0xA0, 0xFF }))
            {
#if !NETFX_CORE && !SILVERLIGHT
                var result = Unpacking.UnpackString(stream, Encoding.UTF32);
#else
                var result = Unpacking.UnpackString(stream, Encoding.UTF8);
#endif // !NETFX_CORE && !SILVERLIGHT
                Assert.That(stream.Position, Is.EqualTo(1));
                Assert.That(result, Is.EqualTo(String.Empty));
            }
        }
Esempio n. 11
0
        public void TestUnpackString_Stream_Encoding_1ByteNonSpecifiedString()
        {
#if MONO || XAMDROID
            Assert.Inconclusive("UTF32Encoding does not throw exception on Mono FCL.");
#endif

#if !NETFX_CORE && !SILVERLIGHT
            using (var stream = new MemoryStream(new byte[] { 0xA4, 0x7F, 0x7F, 0x7F, 0x7F }))
            {
                Assert.Throws <MessageTypeException>(() => Unpacking.UnpackString(stream, new UTF32Encoding(bigEndian: true, byteOrderMark: false, throwOnInvalidCharacters: true)));
            }
#else
            using (var stream = new MemoryStream(new byte[] { 0xA5, 0xF8, 0x88, 0x80, 0x80, 0x80 }))
            {
                Assert.Throws <MessageTypeException>(() => Unpacking.UnpackString(stream, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true)));
            }
#endif // !NETFX_CORE && !SILVERLIGHT
        }
Esempio n. 12
0
        public void TestUnpackString_Stream_Encoding_1Byte_AsIsAndBounded()
        {
#if !NETFX_CORE && !SILVERLIGHT
            using (var stream = new MemoryStream(new byte[] { 0xA4, 0x00, 0x00, 0x00, ( byte )'A', 0xFF }))
            {
                var result = Unpacking.UnpackString(stream, new UTF32Encoding(bigEndian: true, byteOrderMark: false, throwOnInvalidCharacters: true));
                Assert.That(stream.Position, Is.EqualTo(5));
                Assert.That(result, Is.EqualTo("A"));
            }
#else
            using (var stream = new MemoryStream(new byte[] { 0xA2, 0x00, ( byte )'A', 0xFF }))
            {
                var result = Unpacking.UnpackString(stream, new UnicodeEncoding(bigEndian: true, byteOrderMark: false, throwOnInvalidBytes: true));
                Assert.That(stream.Position, Is.EqualTo(3));
                Assert.That(result, Is.EqualTo("A"));
            }
#endif // !NETFX_CORE && !SILVERLIGHT
        }
Esempio n. 13
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;
                }
            }
        }
Esempio n. 14
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;
                }
            }
        }
Esempio n. 15
0
 public void TestUnpackString_ByteArray_1ByteNonUtf8String_ExceptionInReaderOperation()
 {
     Assert.Throws <MessageTypeException>(() => Unpacking.UnpackString(new byte[] { 0xA1, 0xFF }));
 }
Esempio n. 16
0
 public void TestUnpackString_ByteArray_ByteArrayIsNull()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackString(default(byte[])));
 }
Esempio n. 17
0
 public void TestUnpackString_ByteArray_ByteArrayIsEmpty()
 {
     Assert.Throws <ArgumentException>(() => Unpacking.UnpackString(new byte[0]));
 }
Esempio n. 18
0
 public void TestUnpackString_Stream_Encoding_EncodingIsNull()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackString(new MemoryStream(new byte[] { 0xA1, ( byte )'A' }), null));
 }
Esempio n. 19
0
 public void TestUnpackString_Stream_Encoding_StreamIsNull()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackString(default(Stream), Encoding.UTF8));
 }
Esempio n. 20
0
 public void TestUnpackString_ByteArray_Int32_Encoding_ByteArrayIsNull()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackString(null, 0, Encoding.UTF8));
 }
Esempio n. 21
0
 public void TestUnpackString_ByteArray_Int32_Encoding_ByteArrayIsEmpty()
 {
     Assert.Throws <ArgumentException>(() => Unpacking.UnpackString(new byte[0], 0, Encoding.UTF8));
 }
Esempio n. 22
0
 public void TestUnpackString_ByteArray_Int32_Encoding_OffsetIsTooBig()
 {
     Assert.Throws <ArgumentException>(() => Unpacking.UnpackString(new byte[] { 0x1 }, 1, Encoding.UTF8));
 }
Esempio n. 23
0
 public void TestUnpackString_ByteArray_Int32_Encoding_OffsetIsNegative()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => Unpacking.UnpackString(new byte[] { 0x1 }, -1, Encoding.UTF8));
 }
Esempio n. 24
0
 public void TestUnpackString_ByteArray_Int32_Encoding_EncodingIsNull()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackString(new byte[] { 0x1 }, 0, null));
 }