395N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
395N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
395N/A *
395N/A * This code is free software; you can redistribute it and/or modify it
395N/A * under the terms of the GNU General Public License version 2 only, as
395N/A * published by the Free Software Foundation.
395N/A *
395N/A * This code is distributed in the hope that it will be useful, but WITHOUT
395N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
395N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
395N/A * version 2 for more details (a copy is included in the LICENSE file that
395N/A * accompanied this code).
395N/A *
395N/A * You should have received a copy of the GNU General Public License version
395N/A * 2 along with this work; if not, write to the Free Software Foundation,
395N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
395N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
395N/A */
395N/A
395N/A/* @test
395N/A @bug 4262894 6233303
395N/A @summary Testing substitute character Escape sequence
395N/A */
395N/A
395N/Aimport java.nio.*;
395N/Aimport java.nio.charset.*;
395N/A
395N/Apublic class TestISO2022JPSubBytes {
395N/A /* \U2460 is not valid character in ISO2022JP and will be substituted
395N/A * with replacement character. If the replacement character is not the
395N/A * "current charset" character, correct escape sequence should be output
395N/A * for changing character set.
395N/A */
395N/A static char[][] in = { {'\u25cb', '\u2460', '\u25cb'},
395N/A {'\u0061', '\u2460', '\u0061'},
395N/A {'\u25cb', '\u2460', '\u25cb'},
395N/A {'\u0061', '\u2460', '\u0061'},
395N/A };
395N/A static byte[][] expected = { {0x1b, 0x24, 0x42, 0x21, 0x7b,
395N/A 0x21, 0x29,
395N/A 0x21, 0x7b,
395N/A 0x1b, 0x28, 0x42},
395N/A {0x61,
395N/A 0x1b, 0x24, 0x42, 0x21, 0x29,
395N/A 0x1b, 0x28, 0x42, 0x61},
395N/A {0x1b, 0x24, 0x42, 0x21, 0x7b,
395N/A 0x1b, 0x28, 0x42, 0x3f,
395N/A 0x1b, 0x24, 0x42, 0x21, 0x7b,
395N/A 0x1b, 0x28, 0x42},
395N/A {0x61,
395N/A 0x3f,
395N/A 0x61}
395N/A };
395N/A
395N/A public static void main(String args[]) throws Exception {
395N/A CharsetEncoder enc = Charset.forName("ISO2022JP")
395N/A .newEncoder()
395N/A .onUnmappableCharacter(CodingErrorAction.REPLACE);
395N/A
395N/A test(enc, in[0], expected[0]);
395N/A
395N/A enc.reset();
395N/A test(enc, in[1], expected[1]);
395N/A
395N/A enc.reset();
395N/A enc.replaceWith(new byte[]{(byte)'?'});
395N/A test(enc, in[2], expected[2]);
395N/A
395N/A enc.reset();
395N/A test(enc, in[3], expected[3]);
395N/A }
395N/A
395N/A public static void test (CharsetEncoder enc,
395N/A char[] inputChars,
395N/A byte[] expectedBytes) throws Exception
395N/A {
395N/A ByteBuffer bb = ByteBuffer.allocate(expectedBytes.length);
395N/A enc.encode(CharBuffer.wrap(inputChars), bb, true);
395N/A enc.flush(bb);
395N/A bb.flip();
395N/A byte[] outputBuff = bb.array();
395N/A int outputLen = bb.limit();
395N/A if (outputLen != expectedBytes.length) {
395N/A throw new Exception("Output bytes does not match");
395N/A }
395N/A for (int i = 0; i < outputLen; ++i) {
395N/A System.out.printf("<%x:%x> ",
395N/A expectedBytes[i] & 0xff,
395N/A outputBuff[i] & 0xff);
395N/A if (expectedBytes[i] != outputBuff[i]) {
395N/A System.out.println("...");
395N/A throw new Exception("Output bytes does not match");
395N/A }
395N/A }
395N/A System.out.println();
395N/A }
395N/A}