示例#1
0
        /// <summary>
        /// Reads string from the stream.
        /// </summary>
        /// <remarks>
        /// String is prefixed with the string length encoded as "packed"
        /// Int32.
        /// </remarks>
        /// <returns>
        /// A String value read from the stream.
        /// </returns>
        public override string ReadString()
        {
            int length = ReadPackedInt32();

            switch (length)
            {
            case -1:
            case PofConstants.V_REFERENCE_NULL:
                return(null);

            case 0:
                return(String.Empty);

            default:
                byte[] bytes = ReadBytes(length);
                return(SerializationHelper.ConvertUTF(bytes, 0, bytes.Length));
            }
        }
示例#2
0
        public void TestUTF8Serialization()
        {
            // Create a string with multi-bytes character.
            String surrogate = "abc" + Char.ConvertFromUtf32(Int32.Parse("2A601", NumberStyles.HexNumber)) + "def";

            // Safe UTF-8 encoding & decoding of string.
            Stream     stream = new MemoryStream();
            DataWriter writer = new DataWriter(stream);

            byte[] bytes = writer.FormatUTF(surrogate);

            Console.WriteLine("Safe UTF-8-encoded code units:");
            foreach (var utf8Byte in bytes)
            {
                Console.Write("{0:X2} ", utf8Byte);
            }
            Console.WriteLine();
            string s = SerializationHelper.ConvertUTF(bytes, 0, bytes.Length);

            Assert.AreEqual(s, surrogate);
        }