1/* 2 * Copyright (c) 2008, 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 4867457 26 @summary Check for correct byte buffer underflow handling in EUC-JP 27 */ 28 29import java.io.*; 30import java.nio.*; 31import java.nio.charset.*; 32 33public class EUCJPUnderflowDecodeTest { 34 public static void main(String[] args) throws Exception{ 35 36 ByteBuffer bb = ByteBuffer.allocateDirect(255); 37 CharBuffer cc = CharBuffer.allocate(255); 38 39 40 // Test both regular EUC-JP and Linux variant 41 42 String[] charsetNames = { "EUC_JP", "EUC-JP-LINUX" }; 43 44 for (int i = 0 ; i < charsetNames.length; i++) { 45 Charset cs = Charset.forName(charsetNames[i]); 46 CharsetDecoder decoder = cs.newDecoder(); 47 bb.clear(); 48 cc.clear(); 49 50 // Fakes a partial 3 byte EUC_JP (JIS-X-0212 range) 51 // encoded character/byte sequence 52 bb.put((byte)0x8f); 53 bb.put((byte)0xa2); 54 bb.flip(); 55 // Now decode with endOfInput method param set to 56 // indicate to decoder that there is more encoded 57 // data to follow in a subsequent invocation 58 59 CoderResult result = decoder.decode(bb, cc, false); 60 61 // java.nio.charset.CharsetDecoder spec specifies 62 // that the coder ought to return CoderResult.UNDERFLOW 63 // when insufficient bytes have been supplied to complete 64 // the decoding operation 65 66 if (result != CoderResult.UNDERFLOW) { 67 throw new Exception("test failed - UNDERFLOW not returned"); 68 } 69 70 // Repeat the test with the lead byte (minus its pursuing 71 // trail byte) for the EUC-JP 2 byte (JIS208) range 72 decoder.reset(); 73 bb.clear(); 74 cc.clear(); 75 bb.put((byte)0xa1); 76 bb.flip(); 77 result = decoder.decode(bb, cc, false); 78 if (result != CoderResult.UNDERFLOW) { 79 throw new Exception("test failed"); 80 } 81 82 // finally ensure that a valid JIS208 range EUC-JP 83 // 2 byte value is correctly decoded when it is presented 84 // at the trailing bounds of a ByteBuffer in the case where 85 // charset decoder expects (endOfInput ==false) more 86 //input to follow 87 88 decoder.reset(); 89 bb.clear(); 90 cc.clear(); 91 bb.put((byte)0xa1); 92 bb.put((byte)0xc0); 93 bb.flip(); 94 95 result = decoder.decode(bb, cc, false); 96 97 cc.flip(); 98 99 if (result != CoderResult.UNDERFLOW && cc.get() != '\uFF3c') { 100 throw new Exception("test failed to decode EUC-JP (0xA1C0)"); 101 } 102 } 103 } 104} 105