예제 #1
0
        private static void VerifyUtf32Encoding(UTF32Encoding encoding, bool bigEndian, bool byteOrderMark, bool throwOnInvalidBytes)
        {
            if (byteOrderMark)
            {
                if (bigEndian)
                {
                    Assert.Equal(new byte[] { 0, 0, 254, 255 }, encoding.GetPreamble());
                }
                else
                {
                    Assert.Equal(new byte[] { 255, 254, 0, 0 }, encoding.GetPreamble());
                }
            }
            else
            {
                Assert.Empty(encoding.GetPreamble());
            }

            if (throwOnInvalidBytes)
            {
                Assert.Equal(EncoderFallback.ExceptionFallback, encoding.EncoderFallback);
                Assert.Equal(DecoderFallback.ExceptionFallback, encoding.DecoderFallback);
            }
            else
            {
                Assert.Equal(new EncoderReplacementFallback("\uFFFD"), encoding.EncoderFallback);
                Assert.Equal(new DecoderReplacementFallback("\uFFFD"), encoding.DecoderFallback);
            }
        }
예제 #2
0
        public static void VerifyUtf32Encoding(UTF32Encoding encoding, bool bigEndian, bool byteOrderMark, bool throwOnInvalidBytes)
        {
            if (byteOrderMark)
            {
                if (bigEndian)
                {
                    Assert.Equal(new byte[] { 0, 0, 254, 255 }, encoding.GetPreamble());
                }
                else
                {
                    Assert.Equal(new byte[] { 255, 254, 0, 0 }, encoding.GetPreamble());
                }
            }
            else
            {
                Assert.Empty(encoding.GetPreamble());
            }

            if (throwOnInvalidBytes)
            {
                Assert.Equal(EncoderFallback.ExceptionFallback, encoding.EncoderFallback);
                Assert.Equal(DecoderFallback.ExceptionFallback, encoding.DecoderFallback);
            }
            else
            {
                Assert.Equal(new EncoderReplacementFallback("\uFFFD"), encoding.EncoderFallback);
                Assert.Equal(new DecoderReplacementFallback("\uFFFD"), encoding.DecoderFallback);
            }
        }
예제 #3
0
    public static void Main()
    {
        var utf32 = new UTF32Encoding(!BitConverter.IsLittleEndian, true);

        String s = "It was the best of times, it was the worst of times...";

        // We need to dimension the array, since we'll populate it with 2 method calls.
        Byte[] bytes = new Byte[utf32.GetByteCount(s) + utf32.GetPreamble().Length];
        // Encode the string.
        Array.Copy(utf32.GetPreamble(), bytes, utf32.GetPreamble().Length);
        utf32.GetBytes(s, 0, s.Length, bytes, utf32.GetPreamble().Length);

        // Decode the byte array.
        String s2 = utf32.GetString(bytes, 0, bytes.Length);

        Console.WriteLine(s2);
    }
예제 #4
0
    public static void Main()
    {
        // Create instances of UTF32Encoding, with the byte order mark and without.
        UTF32Encoding u32LeNone = new UTF32Encoding();
        UTF32Encoding u32BeNone = new UTF32Encoding(true, false);
        UTF32Encoding u32LeBom  = new UTF32Encoding(false, true);
        UTF32Encoding u32BeBom  = new UTF32Encoding(true, true);

        // Display the preamble for each instance.
        PrintHexBytes(u32LeNone.GetPreamble());
        PrintHexBytes(u32BeNone.GetPreamble());
        PrintHexBytes(u32LeBom.GetPreamble());
        PrintHexBytes(u32BeBom.GetPreamble());
    }
예제 #5
0
파일: bom1.cs 프로젝트: zhimaqiao51/docs
    public static void Main()
    {
        // Create a UTF-32 encoding that supports a BOM.
        var enc = new UTF32Encoding();

        // A Unicode string with two characters outside an 8-bit code range.
        String s = "This Unicode string has 2 characters " +
                   "outside the ASCII range: \n" +
                   "Pi (\u03A0), and Sigma (\u03A3).";

        Console.WriteLine("Original string:");
        Console.WriteLine(s);
        Console.WriteLine();

        // Encode the string.
        Byte[] encodedBytes = enc.GetBytes(s);
        Console.WriteLine("The encoded string has {0} bytes.\n",
                          encodedBytes.Length);

        // Write the bytes to a file with a BOM.
        var fs = new FileStream(@".\UTF32Encoding.txt", FileMode.Create);

        Byte[] bom = enc.GetPreamble();
        fs.Write(bom, 0, bom.Length);
        fs.Write(encodedBytes, 0, encodedBytes.Length);
        Console.WriteLine("Wrote {0} bytes to the file.\n", fs.Length);
        fs.Close();

        // Open the file using StreamReader.
        var    sr        = new StreamReader(@".\UTF32Encoding.txt");
        String newString = sr.ReadToEnd();

        sr.Close();
        Console.WriteLine("String read using StreamReader:");
        Console.WriteLine(newString);
        Console.WriteLine();

        // Open the file as a binary file and decode the bytes back to a string.
        fs = new FileStream(@".\Utf32Encoding.txt", FileMode.Open);
        Byte[] bytes = new Byte[fs.Length];
        fs.Read(bytes, 0, (int)fs.Length);
        fs.Close();

        String decodedString = enc.GetString(encodedBytes);

        Console.WriteLine("Decoded bytes from binary file:");
        Console.WriteLine(decodedString);
    }
예제 #6
0
            static EncodingUtility()
            {
                Encoding utf32BE = new UTF32Encoding(true, true, true);
                Encoding utf32LE = new UTF32Encoding(false, true, true);
                Encoding utf16BE = new UnicodeEncoding(true, true, true);
                Encoding utf16LE = new UnicodeEncoding(false, true, true);
                Encoding utf8BOM = new UTF8Encoding(true, true);

                encodingLookup = new KeyValuePair <byte[], Encoding>[]
                {
                    new KeyValuePair <byte[], Encoding>(utf32BE.GetPreamble(), utf32BE),
                    new KeyValuePair <byte[], Encoding>(utf32LE.GetPreamble(), utf32LE),
                    new KeyValuePair <byte[], Encoding>(utf16BE.GetPreamble(), utf16BE),
                    new KeyValuePair <byte[], Encoding>(utf16LE.GetPreamble(), utf16LE),
                    new KeyValuePair <byte[], Encoding>(utf8BOM.GetPreamble(), utf8BOM),
                };
            }
        public void OpensReaderWithAutoDetectEncoding()
        {
            string   expected = "test";
            Encoding utf32    = new UTF32Encoding(false, true);

            byte[] resourceData = GetBytes(expected, utf32);
            resourceData = (byte[])ArrayUtils.Concat(utf32.GetPreamble(), resourceData);
            EncodedResource r      = new EncodedResource(new InputStreamResource(new MemoryStream(resourceData), "description"), Encoding.UTF8, true);
            StreamReader    reader = (StreamReader)r.OpenReader();

            Assert.AreEqual(Encoding.UTF8.EncodingName, reader.CurrentEncoding.EncodingName);
            string actual = reader.ReadToEnd();

            Assert.AreEqual("\uFEFF" + expected, actual);
// interestingly the line below is *not* true!
//            Assert.AreEqual(utf32.GetString(resourceData), actual);
            Assert.AreEqual(utf32, reader.CurrentEncoding);
        }
예제 #8
0
            static EncodingUtility()
            {
                TextAsset.EncodingUtility.targetEncoding = Encoding.GetEncoding(Encoding.UTF8.CodePage, new EncoderReplacementFallback("�"), new DecoderReplacementFallback("�"));
                Encoding encoding  = new UTF32Encoding(true, true, true);
                Encoding encoding2 = new UTF32Encoding(false, true, true);
                Encoding encoding3 = new UnicodeEncoding(true, true, true);
                Encoding encoding4 = new UnicodeEncoding(false, true, true);
                Encoding encoding5 = new UTF8Encoding(true, true);

                TextAsset.EncodingUtility.encodingLookup = new KeyValuePair <byte[], Encoding>[]
                {
                    new KeyValuePair <byte[], Encoding>(encoding.GetPreamble(), encoding),
                    new KeyValuePair <byte[], Encoding>(encoding2.GetPreamble(), encoding2),
                    new KeyValuePair <byte[], Encoding>(encoding3.GetPreamble(), encoding3),
                    new KeyValuePair <byte[], Encoding>(encoding4.GetPreamble(), encoding4),
                    new KeyValuePair <byte[], Encoding>(encoding5.GetPreamble(), encoding5)
                };
            }