3766N/A/*
3766N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3766N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3766N/A *
3766N/A * This code is free software; you can redistribute it and/or modify it
3766N/A * under the terms of the GNU General Public License version 2 only, as
3766N/A * published by the Free Software Foundation.
3766N/A *
3766N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3766N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3766N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3766N/A * version 2 for more details (a copy is included in the LICENSE file that
3766N/A * accompanied this code).
3766N/A *
3766N/A * You should have received a copy of the GNU General Public License version
3766N/A * 2 along with this work; if not, write to the Free Software Foundation,
3766N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3766N/A *
3766N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3766N/A * or visit www.oracle.com if you need additional information or have any
3766N/A * questions.
3766N/A */
3766N/A
3766N/A
3766N/A/* @test
3766N/A @bug 6803681
3766N/A @summary Test IBM1364
3766N/A */
3766N/A
3766N/Aimport java.util.Arrays;
3766N/Aimport java.nio.*;
3766N/Aimport java.nio.charset.*;
3766N/A
3766N/Apublic class TestIBM1364 {
3766N/A private static String c2bNRStr = "\u00AD\u00B7\u2015\u223C\u2299\uFF5E";
3766N/A private static byte[] c2bNRBytes = new byte[] {
3766N/A (byte)0x0e,
3766N/A (byte)0x41, (byte)0x48,
3766N/A (byte)0x41, (byte)0x43,
3766N/A (byte)0x41, (byte)0x49,
3766N/A (byte)0x42, (byte)0xa1,
3766N/A (byte)0x49, (byte)0x6f,
3766N/A (byte)0x49, (byte)0x54,
3766N/A (byte)0x0f };
3766N/A
3766N/A // end at SO
3766N/A private static String mixedStr = "\u008d\u008e\u0020\u3000\u3001\u71ba\u3164\u0088\ue757";
3766N/A private static byte[] mixedBytes = new byte[] {
3766N/A (byte)0x09,
3766N/A (byte)0x0a,
3766N/A (byte)0x40,
3766N/A (byte)0x0e,
3766N/A (byte)0x40, (byte)0x40,
3766N/A (byte)0x41, (byte)0x41,
3766N/A (byte)0x6c, (byte)0x45,
3766N/A (byte)0x84, (byte)0x41,
3766N/A (byte)0x0f,
3766N/A (byte)0x28,
3766N/A (byte)0x0e,
3766N/A (byte)0xdd, (byte)0xfd,
3766N/A (byte)0x0f };
3766N/A
3766N/A // end at SI
3766N/A private static String mixedStr2 = "\u008d\u008e\u0020\u3000\u3001\u71ba\u3164\u0088";
3766N/A private static byte[] mixedBytes2 = new byte[] {
3766N/A (byte)0x09,
3766N/A (byte)0x0a,
3766N/A (byte)0x40,
3766N/A (byte)0x0e,
3766N/A (byte)0x40, (byte)0x40,
3766N/A (byte)0x41, (byte)0x41,
3766N/A (byte)0x6c, (byte)0x45,
3766N/A (byte)0x84, (byte)0x41,
3766N/A (byte)0x0f,
3766N/A (byte)0x28 };
3766N/A
3766N/A private static byte[][] malformedBytes = {
3766N/A { (byte)0x0e,
3766N/A (byte)0x039, (byte)0x40,
3766N/A (byte)0x0f
3766N/A },
3766N/A { (byte)0x0e,
3766N/A (byte)0x039, (byte)0x42,
3766N/A (byte)0x0f
3766N/A },
3766N/A { (byte)0x0e,
3766N/A (byte)0x040, (byte)0x41,
3766N/A (byte)0x0f
3766N/A },
3766N/A { (byte)0x0e,
3766N/A (byte)0x040, (byte)0xee,
3766N/A (byte)0x0f
3766N/A },
3766N/A { (byte)0x0e,
3766N/A (byte)0x0ef, (byte)0x30,
3766N/A (byte)0x0f
3766N/A },
3766N/A { (byte)0x0e,
3766N/A (byte)0x0ff, (byte)0x41,
3766N/A (byte)0x0f
3766N/A }
3766N/A };
3766N/A
3766N/A private static byte[][] unmappedBytes = {
3766N/A { (byte)0x0e,
3766N/A (byte)0x06c, (byte)0x46,
3766N/A (byte)0x0f,
3766N/A },
3766N/A { (byte)0x0e,
3766N/A (byte)0x078, (byte)0x46,
3766N/A (byte)0x0f,
3766N/A },
3766N/A { (byte)0x0e,
3766N/A (byte)0x083, (byte)0xfe,
3766N/A (byte)0x0f,
3766N/A },
3766N/A { (byte)0xfa },
3766N/A { (byte)0xfe },
3766N/A };
3766N/A
3766N/A public static void main(String[] args) throws Exception {
3766N/A if (!(Arrays.equals(mixedStr.getBytes("cp1364"), mixedBytes)) ||
3766N/A !mixedStr.equals(new String(mixedBytes, "cp1364")))
3766N/A throw new RuntimeException("cp1364 failed on mixed!");
3766N/A
3766N/A if (!(Arrays.equals(mixedStr2.getBytes("cp1364"), mixedBytes2)) ||
3766N/A !mixedStr2.equals(new String(mixedBytes2, "cp1364")))
3766N/A throw new RuntimeException("cp1364 failed on mixed!");
3766N/A
3766N/A if (!(Arrays.equals(c2bNRStr.getBytes("cp1364"), c2bNRBytes)) ||
3766N/A c2bNRStr.equals(new String(c2bNRBytes, "cp1364")))
3766N/A throw new RuntimeException("cp1364 failed on c2bNR!");
3766N/A
3766N/A ByteBuffer bb = ByteBuffer.allocateDirect(mixedBytes.length);
3766N/A bb.put(mixedBytes).flip();
3766N/A CharBuffer cb = Charset.forName("ibm1364").decode(bb);
3766N/A if (!mixedStr.equals(new String(cb.toString())))
3766N/A throw new RuntimeException("cp1364 failed on direct decod()!");
3766N/A
3766N/A bb = ByteBuffer.allocateDirect(mixedBytes2.length);
3766N/A bb.put(mixedBytes2).flip();
3766N/A cb = Charset.forName("ibm1364").decode(bb);
3766N/A if (!mixedStr2.equals(new String(cb.toString())))
3766N/A throw new RuntimeException("cp1364 failed on direct decod()!");
3766N/A
3766N/A cb = ByteBuffer.allocateDirect(mixedStr.length() * 2).asCharBuffer();
3766N/A cb.put(mixedStr.toCharArray()).flip();
3766N/A bb = Charset.forName("x-ibm1364").encode(cb);
3766N/A if (!(Arrays.equals(Arrays.copyOf(bb.array(), bb.limit()), mixedBytes)))
3766N/A throw new RuntimeException("cp1364 failed on direct encode()!");
3766N/A
3766N/A cb = ByteBuffer.allocateDirect(mixedStr2.length() * 2).asCharBuffer();
3766N/A cb.put(mixedStr2.toCharArray()).flip();
3766N/A bb = Charset.forName("x-ibm1364").encode(cb);
3766N/A if (!(Arrays.equals(Arrays.copyOf(bb.array(), bb.limit()), mixedBytes2)))
3766N/A throw new RuntimeException("cp1364 failed on direct encode()!");
3766N/A
3766N/A // malformed
3766N/A cb = CharBuffer.allocate(1024);
3766N/A CharBuffer cbd = ByteBuffer.allocateDirect(1024).asCharBuffer();
3766N/A CharsetDecoder dec = Charset.forName("x-ibm1364").newDecoder();
3766N/A for (byte[] ba:malformedBytes) {
3766N/A cb.clear();
3766N/A dec.reset();
3766N/A if (!dec.reset().decode(ByteBuffer.wrap(ba), cb, true).isMalformed() ||
3766N/A !dec.reset().decode(ByteBuffer.wrap(ba), cbd, true).isMalformed())
3766N/A throw new RuntimeException("cp1364 failed on decode()/malformed!");
3766N/A }
3766N/A
3766N/A //unmappable
3766N/A for (byte[] ba:unmappedBytes) {
3766N/A cb.clear();
3766N/A dec.reset();
3766N/A if (!dec.reset().decode(ByteBuffer.wrap(ba), cb, true).isUnmappable() ||
3766N/A !dec.reset().decode(ByteBuffer.wrap(ba), cbd, true).isUnmappable())
3766N/A throw new RuntimeException("cp1364 failed on decode()/unmappable!");
3766N/A }
3766N/A
3766N/A //overflow
3766N/A cb.limit(mixedStr.length() - 1);
3766N/A cbd.limit(mixedStr.length() - 1);
3766N/A if (!dec.reset().decode(ByteBuffer.wrap(mixedBytes), cb, true).isOverflow() ||
3766N/A !dec.reset().decode(ByteBuffer.wrap(mixedBytes), cbd, true).isOverflow())
3766N/A throw new RuntimeException("cp1364 failed on decode()/overflow!");
3766N/A
3766N/A CharsetEncoder enc = Charset.forName("x-ibm1364").newEncoder();
3766N/A // last "0x0f" is from flush()
3766N/A bb = ByteBuffer.allocate(mixedBytes.length - 2);
3766N/A ByteBuffer bbd = ByteBuffer.allocateDirect(mixedBytes.length - 2);
3766N/A if (!enc.reset()
3766N/A .encode(CharBuffer.wrap(mixedStr.toCharArray()), bb, true)
3766N/A .isOverflow() ||
3766N/A !enc.reset()
3766N/A .encode(CharBuffer.wrap(mixedStr.toCharArray()), bbd, true)
3766N/A .isOverflow())
3766N/A throw new RuntimeException("cp1364 failed on encode()/overflow!");
3766N/A
3766N/A // flush() overflow
3766N/A bb = ByteBuffer.allocate(mixedBytes.length - 1);
3766N/A bbd = ByteBuffer.allocateDirect(mixedBytes.length - 1);
3766N/A
3766N/A enc.reset().encode(CharBuffer.wrap(mixedStr.toCharArray()), bb, true);
3766N/A enc.reset().encode(CharBuffer.wrap(mixedStr.toCharArray()), bbd, true);
3766N/A
3766N/A if (!enc.flush(bb).isOverflow() ||
3766N/A !enc.flush(bbd).isOverflow())
3766N/A throw new RuntimeException("cp1364 failed on encode()/flush()/overflow!");
3766N/A }
3766N/A}