1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 8211382 26 * @summary Check GB18030 27 * @modules jdk.charsets 28 */ 29 30 import java.io.*; 31 import java.nio.*; 32 import java.nio.charset.*; 33 34 public class TestGB18030 { 35 public static void gb18030_1(boolean useDirect) throws Exception { 36 for(char ch : new char[]{'\uFFFE', '\uFFFF'}) { 37 char[] ca = new char[]{ch}; 38 Charset cs = Charset.forName("GB18030"); 39 CharsetEncoder ce = cs.newEncoder(); 40 CharsetDecoder cd = cs.newDecoder(); 41 CharBuffer cb = CharBuffer.wrap(ca); 42 ByteBuffer bb; 43 if (useDirect) { 44 bb = ByteBuffer.allocateDirect( 45 (int)Math.ceil(ce.maxBytesPerChar())); 46 } else { 47 bb = ByteBuffer.allocate( 48 (int)Math.ceil(ce.maxBytesPerChar())); 49 } 50 CoderResult cr = ce.encode(cb, bb, true); 51 if (!cr.isUnderflow()) { 52 throw new RuntimeException( 53 String.format("Encoder Error: \\u%04X: direct=%b: %s", 54 (int)ch, 55 useDirect, 56 cr.toString())); 57 } 58 bb.position(0); 59 cb = CharBuffer.allocate((int)Math.ceil( 60 cd.maxCharsPerByte()*bb.limit())); 61 cr = cd.decode(bb, cb, true); 62 if (!cr.isUnderflow()) { 63 throw new RuntimeException( 64 String.format("Decoder Error: \\u%04X: direct=%b: %s", 65 (int)ch, 66 useDirect, 67 cr.toString())); 68 } 69 if (ca[0] != cb.get(0)) { 70 throw new RuntimeException( 71 String.format("direct=%b: \\u%04X <> \\u%04X", 72 useDirect, 73 (int)ca[0], 74 (int)cb.get(0))); 75 } 76 } 77 } 78 public static void main(String args[]) throws Exception { 79 gb18030_1(false); 80 gb18030_1(true); 81 } 82 }