395N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
395N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
395N/A *
395N/A * This code is free software; you can redistribute it and/or modify it
395N/A * under the terms of the GNU General Public License version 2 only, as
395N/A * published by the Free Software Foundation.
395N/A *
395N/A * This code is distributed in the hope that it will be useful, but WITHOUT
395N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
395N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
395N/A * version 2 for more details (a copy is included in the LICENSE file that
395N/A * accompanied this code).
395N/A *
395N/A * You should have received a copy of the GNU General Public License version
395N/A * 2 along with this work; if not, write to the Free Software Foundation,
395N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
395N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
395N/A */
395N/A
395N/A/* @test
395N/A @bug 4896454
395N/A @summary Check GB18030 surrogate encoding/decoding handling
395N/A */
395N/A
395N/Aimport java.nio.*;
395N/Aimport java.nio.charset.*;
395N/A
395N/Apublic class SurrogateGB18030Test {
395N/A public static void main(String[] args) throws Exception {
395N/A SurrogateGB18030Test test = new SurrogateGB18030Test();
395N/A
395N/A test.roundtripTest();
395N/A
395N/A /**
395N/A * Valid Surrogate pair and 4 byte GB18030 representation
395N/A */
395N/A
395N/A String inputString = "\uD800\uDC00";
395N/A
395N/A byte[] expectedBytes = { (byte)0x90,
395N/A (byte)0x30,
395N/A (byte)0x81,
395N/A (byte)0x30
395N/A };
395N/A test.encodeTest(inputString, expectedBytes);
395N/A
395N/A /**
395N/A * Vice-versa : check that 4 byte GB18030 value encodes correctly
395N/A */
395N/A
395N/A String expectedStr = "\uDBFF\uDFFF";
395N/A
395N/A byte[] inputBytes = { (byte)0xe3,
395N/A (byte)0x32,
395N/A (byte)0x9a,
395N/A (byte)0x35
395N/A };
395N/A
395N/A
395N/A test.decodeTest(inputBytes, expectedStr);
395N/A
395N/A }
395N/A
395N/A private void roundtripTest() throws Exception
395N/A {
395N/A byte[] ba;
395N/A char[] pair = new char[2];
395N/A for (char high = '\ud800'; high <= '\udbff'; high++) {
395N/A for (char low = '\udc00'; low <= '\udfff'; low++) {
395N/A pair[0] = high;
395N/A pair[1] = low;
395N/A String s = new String(pair);
395N/A if (!s.equals(new String(s.getBytes("gb18030"), "gb18030")))
395N/A throw new Exception ("GB18030 roundtrip failure");
395N/A }
395N/A }
395N/A
395N/A }
395N/A
395N/A private void encodeTest(String inputString, byte[] expectedBytes)
395N/A throws Exception
395N/A {
395N/A byte[] encoded = inputString.getBytes("GB18030");
395N/A
395N/A CharBuffer cb = CharBuffer.wrap(inputString.toCharArray());
395N/A ByteBuffer bb = ByteBuffer.allocate(4);
395N/A
395N/A CharsetEncoder encoder = Charset.forName("GB18030").newEncoder();
395N/A encoder.encode(cb, bb, true);
395N/A
395N/A bb.flip();
395N/A for (int i = 0 ; i < expectedBytes.length; i++) {
395N/A if (encoded[i] != expectedBytes[i]
395N/A || bb.get() != expectedBytes[i])
395N/A throw new Exception ("GB18030 encode failure");
395N/A }
395N/A }
395N/A
395N/A private void decodeTest(byte[] inputBytes, String expectedStr)
395N/A throws Exception
395N/A {
395N/A String s2 = new String(inputBytes, "GB18030");
395N/A
395N/A CharsetDecoder decoder = Charset.forName("GB18030").newDecoder();
395N/A
395N/A ByteBuffer bb = ByteBuffer.wrap(inputBytes);
395N/A CharBuffer cb = CharBuffer.allocate(2);
395N/A decoder.decode(bb, cb, true);
395N/A
395N/A cb.flip();
395N/A for (int i = 0 ; i < expectedStr.length(); i++) {
395N/A if (expectedStr.charAt(i) != cb.get()
395N/A || s2.charAt(i) != expectedStr.charAt(i))
395N/A throw new Exception ("GB18030 encode failure");
395N/A }
395N/A }
395N/A}