1/*
2 * Copyright (c) 2010, 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 6226510
26 * @summary Check that ISO-2022-JP's encoder correctly resets to ASCII mode
27 * @modules jdk.charsets
28 * @author Martin Buchholz
29 */
30
31import java.nio.*;
32import java.nio.charset.*;
33
34public class ResetISO2022JP {
35
36    public static void main(String[] args) throws Exception {
37        if (! (encode(true).equals(encode(false))))
38            throw new Exception("Mismatch!");
39    }
40
41    static String encode(boolean reuseEncoder) {
42        String s = "\u3042\u3043\u3044";
43
44        CharsetEncoder e = Charset.forName("ISO-2022-JP").newEncoder();
45
46        if (reuseEncoder) {
47            // I'm turning japanese. Yes I'm turning japanese.  Yes I think so!
48            e.encode(CharBuffer.wrap(s), ByteBuffer.allocate(64), true);
49
50            // Should put encoder back into ASCII mode
51            e.reset();
52        }
53
54        ByteBuffer bb = ByteBuffer.allocate(64);
55        e.encode(CharBuffer.wrap(s), bb, true);
56        e.flush(bb);
57        bb.flip();
58        StringBuilder sb = new StringBuilder();
59        for (int i = 0; i < bb.limit(); i++)
60            sb.append(String.format("%02x ", bb.get(i)));
61        System.out.println(sb);
62        return sb.toString();
63    }
64}
65