Path: blob/master/test/jdk/sun/nio/cs/EUCJPUnderflowDecodeTest.java
41149 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 486745725@summary Check for correct byte buffer underflow handling in EUC-JP26*/2728import java.io.*;29import java.nio.*;30import java.nio.charset.*;3132public class EUCJPUnderflowDecodeTest {33public static void main(String[] args) throws Exception{3435ByteBuffer bb = ByteBuffer.allocateDirect(255);36CharBuffer cc = CharBuffer.allocate(255);373839// Test both regular EUC-JP and Linux variant4041String[] charsetNames = { "EUC_JP", "EUC-JP-LINUX" };4243for (int i = 0 ; i < charsetNames.length; i++) {44Charset cs = Charset.forName(charsetNames[i]);45CharsetDecoder decoder = cs.newDecoder();46bb.clear();47cc.clear();4849// Fakes a partial 3 byte EUC_JP (JIS-X-0212 range)50// encoded character/byte sequence51bb.put((byte)0x8f);52bb.put((byte)0xa2);53bb.flip();54// Now decode with endOfInput method param set to55// indicate to decoder that there is more encoded56// data to follow in a subsequent invocation5758CoderResult result = decoder.decode(bb, cc, false);5960// java.nio.charset.CharsetDecoder spec specifies61// that the coder ought to return CoderResult.UNDERFLOW62// when insufficient bytes have been supplied to complete63// the decoding operation6465if (result != CoderResult.UNDERFLOW) {66throw new Exception("test failed - UNDERFLOW not returned");67}6869// Repeat the test with the lead byte (minus its pursuing70// trail byte) for the EUC-JP 2 byte (JIS208) range71decoder.reset();72bb.clear();73cc.clear();74bb.put((byte)0xa1);75bb.flip();76result = decoder.decode(bb, cc, false);77if (result != CoderResult.UNDERFLOW) {78throw new Exception("test failed");79}8081// finally ensure that a valid JIS208 range EUC-JP82// 2 byte value is correctly decoded when it is presented83// at the trailing bounds of a ByteBuffer in the case where84// charset decoder expects (endOfInput ==false) more85//input to follow8687decoder.reset();88bb.clear();89cc.clear();90bb.put((byte)0xa1);91bb.put((byte)0xc0);92bb.flip();9394result = decoder.decode(bb, cc, false);9596cc.flip();9798if (result != CoderResult.UNDERFLOW && cc.get() != '\uFF3c') {99throw new Exception("test failed to decode EUC-JP (0xA1C0)");100}101}102}103}104105106