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 5016049
26   @summary ensure euc-jp-linux charset decoder recovery for unmappable input
27 */
28
29import java.io.*;
30
31public class EucJpLinuxDecoderRecoveryTest {
32    public static void main(String[] args) throws Exception {
33        byte[] encoded = {
34                // EUC_JP_LINUX mappable JIS X 0208 range
35                (byte)0xa6, (byte)0xc5,
36                // EUC_JP_LINUX Unmappable (JIS X 0212 range)
37                (byte)0x8f, (byte)0xa2, (byte)0xb7,
38                // EUC_JP_LINUX mappable JIS X 0208 range
39                (byte)0xa6, (byte)0xc7 };
40
41        char[] decodedChars = new char[3];
42        char[] expectedChars =
43                        {
44                        '\u03B5',  // mapped
45                        '\ufffd',  // unmapped
46                        '\u03B7'   // mapped
47                        };
48
49        ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
50        InputStreamReader isr = new InputStreamReader(bais, "EUC_JP_LINUX");
51        int n = 0;   // number of chars decoded
52
53        try {
54            n = isr.read(decodedChars);
55        } catch (Exception ex) {
56            throw new Error("euc-jp-linux decoding broken");
57        }
58
59        // check number of decoded chars is what is expected
60        if (n != expectedChars.length)
61            throw new Error("Unexpected number of chars decoded");
62
63        // Compare actual decoded with expected
64
65        for (int i = 0; i < n; i++) {
66            if (expectedChars[i] != decodedChars[i])
67                throw new Error("euc-jp-linux decoding incorrect");
68        }
69    }
70}
71