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 4251646
26 * @summary Make sure buffer boundary convert works
27 * @modules jdk.charsets
28 */
29
30import java.nio.*;
31import java.nio.charset.*;
32
33public class TestISO2022JPEncoder {
34    static char[] inputChars = {'\u0020', '\u0020', '\u0020', '\u0020',
35                                '\u0020', '\u0020', '\u0020', '\u0020',
36                                '\u0020', '\u4e00'};
37    static byte[] expectedBytes1 = {0x20, 0x20, 0x20, 0x20, 0x20,
38                                    0x20, 0x20, 0x20, 0x20};
39    static byte[] expectedBytes2 = {0x1b, 0x24, 0x42, 0x30, 0x6c,
40                                    0x1b, 0x28, 0x42};
41    static byte[] outputBuff = new byte[10];
42
43    public static void main(String args[]) throws Exception {
44        CharsetEncoder enc = Charset.forName("ISO2022JP").newEncoder();
45        CharBuffer cb = CharBuffer.wrap(inputChars);
46        ByteBuffer bb = ByteBuffer.wrap(outputBuff);
47        CoderResult cr = enc.encode(cb, bb, false);
48        if (!cr.isOverflow())
49            throw new Exception("Expected CodeResult.OVERFLOW was not returned");
50        for (int i = 0; i < expectedBytes1.length; ++i) {
51            //System.out.println(expectedBytes1[i] + ":" + outputBuff[i]);
52            if (expectedBytes1[i] != outputBuff[i]) {
53                throw new Exception("Output bytes does not match at first conversion");
54            }
55        }
56        int nci = cb.position();
57        if (nci != expectedBytes1.length)
58            throw new Exception("Output length does not match at first conversion");
59        bb.clear();
60        cr = enc.encode(cb, bb, true);
61        enc.flush(bb);
62        //System.out.println(ret + "," + expectedBytes2.length);
63        bb.flip();
64        int len = bb.remaining();
65        if (len != expectedBytes2.length)
66            throw new Exception("Output length does not match at second conversion");
67        for (int i = 0; i < expectedBytes2.length; ++i) {
68            //System.out.println(expectedBytes2[i] + ":" + outputBuff[i]);
69            if (expectedBytes2[i] != outputBuff[i]) {
70                throw new Exception("Output bytes does not match at second conversion");
71            }
72        }
73    }
74}
75