Package org.postgresql.core

Examples of org.postgresql.core.Encoding


    }

    public void testUTF8Decode() throws Exception {
        // Tests for our custom UTF-8 decoder.

        Encoding utf8Encoding = Encoding.getJVMEncoding("UTF-8");
       
        for (int ch = 0; ch < 0x110000; ++ch) {
            if (ch >= 0xd800 && ch < 0xe000)
                continue; // Surrogate range.
           
            String testString;
            if (ch >= 0x10000) {
                testString = new String(new char[] {
                    (char) (0xd800 + ((ch-0x10000) >> 10)),
                    (char) (0xdc00 + ((ch-0x10000) & 0x3ff)) });
            } else {
                testString = new String(new char[] { (char)ch });
            }
           
            byte[] jvmEncoding = testString.getBytes("UTF-8");
            String jvmDecoding = new String(jvmEncoding, 0, jvmEncoding.length, "UTF-8");
            String ourDecoding = utf8Encoding.decode(jvmEncoding, 0, jvmEncoding.length);
           
            assertEquals("Test string: " + dumpString(testString), dumpString(testString), dumpString(jvmDecoding));
            assertEquals("Test string: " + dumpString(testString), dumpString(testString), dumpString(ourDecoding));
        }
    }
View Full Code Here


            assertEquals("Test string: " + dumpString(testString), dumpString(testString), dumpString(ourDecoding));
        }
    }
    public void testBadUTF8Decode() throws Exception {
        Encoding utf8Encoding = Encoding.getJVMEncoding("UTF-8");

        byte[][] badSequences = new byte[][] {
            // One-byte illegal sequences
            { (byte)0x80 }, // First byte may not be 10xxxxxx
           
            // Two-byte illegal sequences
            { (byte)0xc0, (byte)0x00 }// Second byte must be 10xxxxxx
            { (byte)0xc0, (byte)0x80 }// Can't represent a value < 0x80
           
            // Three-byte illegal sequences
            { (byte)0xe0, (byte)0x00 }// Second byte must be 10xxxxxx
            { (byte)0xe0, (byte)0x80, (byte)0x00 }// Third byte must be 10xxxxxx
            { (byte)0xe0, (byte)0x80, (byte)0x80 }// Can't represent a value < 0x800
            { (byte)0xed, (byte)0xa0, (byte)0x80 }// Not allowed to encode the range d800..dfff
           
            // Four-byte illegal sequences
            { (byte)0xf0, (byte)0x00 }// Second byte must be 10xxxxxx
            { (byte)0xf0, (byte)0x80, (byte)0x00 }// Third byte must be 10xxxxxx
            { (byte)0xf0, (byte)0x80, (byte)0x80, (byte)0x00 }// Fourth byte must be 10xxxxxx
            { (byte)0xf0, (byte)0x80, (byte)0x80, (byte)0x80 }// Can't represent a value < 0x10000
           
            // Five-byte illegal sequences
            { (byte)0xf8 }, // Can't have a five-byte sequence.

            // Six-byte illegal sequences
            { (byte)0xfc }, // Can't have a six-byte sequence.

            // Seven-byte illegal sequences
            { (byte)0xfe }, // Can't have a seven-byte sequence.
           
            // Eigth-byte illegal sequences
            { (byte)0xff }, // Can't have an eight-byte sequence.
        };

        byte[] paddedSequence = new byte[32];
        for (int i = 0; i < badSequences.length; ++i) {
            byte[] sequence = badSequences[i];
           
            try {
                String str = utf8Encoding.decode(sequence, 0, sequence.length);
                fail("Expected an IOException on sequence " + i + ", but decoded to <" + str + ">");
            } catch (IOException ioe) {
                // Expected exception.
            }
           
            // Try it with padding.
            Arrays.fill(paddedSequence, (byte)0);
            System.arraycopy(sequence, 0, paddedSequence, 0, sequence.length);
           
            try {
                String str = utf8Encoding.decode(paddedSequence, 0, paddedSequence.length);
                fail("Expected an IOException on sequence " + i + ", but decoded to <" + str + ">");
            } catch (IOException ioe) {
                // Expected exception.
            }
        }
View Full Code Here

            }
        }
    }

    public void testTruncatedUTF8Decode() throws Exception {
        Encoding utf8Encoding = Encoding.getJVMEncoding("UTF-8");

        byte[][] shortSequences = new byte[][] {
            { (byte)0xc0 },              // Second byte must be present
           
            { (byte)0xe0 },              // Second byte must be present
            { (byte)0xe0, (byte)0x80 }// Third byte must be present
           
            { (byte)0xf0 },              // Second byte must be present
            { (byte)0xf0, (byte)0x80 }// Third byte must be present
            { (byte)0xf0, (byte)0x80, (byte)0x80 }// Fourth byte must be present
        };
       
        byte[] paddedSequence = new byte[32];
        for (int i = 0; i < shortSequences.length; ++i) {
            byte[] sequence = shortSequences[i];
           
            try {
                String str = utf8Encoding.decode(sequence, 0, sequence.length);
                fail("Expected an IOException on sequence " + i + ", but decoded to <" + str + ">");
            } catch (IOException ioe) {
                // Expected exception.
            }
           
           
            // Try it with padding and a truncated length.
            Arrays.fill(paddedSequence, (byte)0);
            System.arraycopy(sequence, 0, paddedSequence, 0, sequence.length);
           
            try {
                String str = utf8Encoding.decode(paddedSequence, 0, sequence.length);
                fail("Expected an IOException on sequence " + i + ", but decoded to <" + str + ">");
            } catch (IOException ioe) {
                // Expected exception.
            }
        }
View Full Code Here

        super(name);
    }

    public void testCreation() throws Exception
    {
        Encoding encoding;
        encoding = Encoding.getDatabaseEncoding("UNICODE");
        assertEquals("UTF", encoding.name().substring(0, 3).toUpperCase());
        encoding = Encoding.getDatabaseEncoding("SQL_ASCII");
        assertTrue(encoding.name().toUpperCase().indexOf("ASCII") != -1);
        assertEquals("When encoding is unknown the default encoding should be used",
                     Encoding.defaultEncoding(),
                     Encoding.getDatabaseEncoding("UNKNOWN"));
    }
View Full Code Here

                     Encoding.getDatabaseEncoding("UNKNOWN"));
    }

    public void testTransformations() throws Exception
    {
        Encoding encoding = Encoding.getDatabaseEncoding("UNICODE");
        assertEquals("ab", encoding.decode(new byte[] { 97, 98 }));

        assertEquals(2, encoding.encode("ab").length);
        assertEquals(97, encoding.encode("a")[0]);
        assertEquals(98, encoding.encode("b")[0]);

        encoding = Encoding.defaultEncoding();
        assertEquals("a".getBytes()[0], encoding.encode("a")[0]);
        assertEquals(new String(new byte[] { 97 }),
                     encoding.decode(new byte[] { 97 }));
    }
View Full Code Here

                     encoding.decode(new byte[] { 97 }));
    }

    public void testReader() throws Exception
    {
        Encoding encoding = Encoding.getDatabaseEncoding("SQL_ASCII");
        InputStream stream = new ByteArrayInputStream(new byte[] { 97, 98 });
        Reader reader = encoding.getDecodingReader(stream);
        assertEquals(97, reader.read());
        assertEquals(98, reader.read());
        assertEquals( -1, reader.read());
    }
View Full Code Here

        super(name);
    }

    public void testCreation() throws Exception
    {
        Encoding encoding;
        encoding = Encoding.getDatabaseEncoding("UNICODE");
        assertEquals("UTF", encoding.name().substring(0, 3).toUpperCase(Locale.US));
        encoding = Encoding.getDatabaseEncoding("SQL_ASCII");
        assertTrue(encoding.name().toUpperCase(Locale.US).indexOf("ASCII") != -1);
        assertEquals("When encoding is unknown the default encoding should be used",
                     Encoding.defaultEncoding(),
                     Encoding.getDatabaseEncoding("UNKNOWN"));
    }
View Full Code Here

                     Encoding.getDatabaseEncoding("UNKNOWN"));
    }

    public void testTransformations() throws Exception
    {
        Encoding encoding = Encoding.getDatabaseEncoding("UNICODE");
        assertEquals("ab", encoding.decode(new byte[] { 97, 98 }));

        assertEquals(2, encoding.encode("ab").length);
        assertEquals(97, encoding.encode("a")[0]);
        assertEquals(98, encoding.encode("b")[0]);

        encoding = Encoding.defaultEncoding();
        assertEquals("a".getBytes()[0], encoding.encode("a")[0]);
        assertEquals(new String(new byte[] { 97 }),
                     encoding.decode(new byte[] { 97 }));
    }
View Full Code Here

                     encoding.decode(new byte[] { 97 }));
    }

    public void testReader() throws Exception
    {
        Encoding encoding = Encoding.getDatabaseEncoding("SQL_ASCII");
        InputStream stream = new ByteArrayInputStream(new byte[] { 97, 98 });
        Reader reader = encoding.getDecodingReader(stream);
        assertEquals(97, reader.read());
        assertEquals(98, reader.read());
        assertEquals( -1, reader.read());
    }
View Full Code Here

        super(name);
    }

    public void testCreation() throws Exception
    {
        Encoding encoding;
        encoding = Encoding.getDatabaseEncoding("UTF8");
        assertEquals("UTF", encoding.name().substring(0, 3).toUpperCase(Locale.US));
        encoding = Encoding.getDatabaseEncoding("SQL_ASCII");
        assertTrue(encoding.name().toUpperCase(Locale.US).indexOf("ASCII") != -1);
        assertEquals("When encoding is unknown the default encoding should be used",
                     Encoding.defaultEncoding(),
                     Encoding.getDatabaseEncoding("UNKNOWN"));
    }
View Full Code Here

TOP

Related Classes of org.postgresql.core.Encoding

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.