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/*
25   @bug 6392804
26   @summary Decoder fails to detect decoding error
27*/
28import java.nio.*;
29import java.nio.charset.*;
30
31public class Test6392804 {
32    public static void main(String[] args) throws Throwable {
33        test("ISO-2022-JP",
34             new byte[] {0x1b,(byte)0x8e, 0x24, 0x40, 0x0, 0x0});
35        test("ISO-2022-JP-2",
36             new byte[] {0x1b,(byte)0x8e, 0x24, 0x40, 0x0, 0x0});
37        test("x-windows-50220",
38             new byte[] {0x1b,(byte)0x8e, 0x24, 0x40, 0x0, 0x0});
39        test("x-windows-50221",
40             new byte[] {0x1b,(byte)0x8e, 0x24, 0x40, 0x0, 0x0});
41        test("x-windows-iso2022jp",
42             new byte[] {0x1b,(byte)0x8e, 0x24, 0x40, 0x0, 0x0});
43        test("EUC_TW",
44             new byte[] {(byte)0x8e, (byte)0xa8, (byte)0xad, (byte)0xe5});
45        //out of range second  byte
46        test("EUC_TW",
47             new byte[] {(byte)0x8e, (byte)0x92, (byte)0xa1, (byte)0xa1});
48        test("EUC_TW",
49             new byte[] {(byte)0x8e, (byte)0x98, (byte)0xa1, (byte)0xa1});
50    }
51
52    static void test(String csn, byte[] bytes) throws Throwable {
53        CharsetDecoder dec = Charset.forName(csn).newDecoder();
54        CharBuffer cb = CharBuffer.allocate(1024);
55        CoderResult cr = dec.decode(ByteBuffer.wrap(bytes), cb, true);
56        if (cr.isUnderflow())
57            throw new RuntimeException(csn + " failed cr=" + cr);
58    }
59}
60