1/*
2 * Copyright (c) 2011, 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
25/* @test
26 * @bug 6803681
27 * @summary Test IBM1364
28 * @modules jdk.charsets
29 */
30
31import java.util.Arrays;
32import java.nio.*;
33import java.nio.charset.*;
34
35public class TestIBM1364 {
36    private static String c2bNRStr = "\u00AD\u00B7\u2015\u223C\u2299\uFF5E";
37    private static byte[] c2bNRBytes = new byte[] {
38        (byte)0x0e,
39        (byte)0x41, (byte)0x48,
40        (byte)0x41, (byte)0x43,
41        (byte)0x41, (byte)0x49,
42        (byte)0x42, (byte)0xa1,
43        (byte)0x49, (byte)0x6f,
44        (byte)0x49, (byte)0x54,
45        (byte)0x0f };
46
47    // end at SO
48    private static String mixedStr = "\u008d\u008e\u0020\u3000\u3001\u71ba\u3164\u0088\ue757";
49    private static byte[] mixedBytes = new byte[] {
50         (byte)0x09,
51         (byte)0x0a,
52         (byte)0x40,
53         (byte)0x0e,
54         (byte)0x40, (byte)0x40,
55         (byte)0x41, (byte)0x41,
56         (byte)0x6c, (byte)0x45,
57         (byte)0x84, (byte)0x41,
58         (byte)0x0f,
59         (byte)0x28,
60         (byte)0x0e,
61         (byte)0xdd, (byte)0xfd,
62         (byte)0x0f };
63
64    // end at SI
65    private static String mixedStr2 = "\u008d\u008e\u0020\u3000\u3001\u71ba\u3164\u0088";
66    private static byte[] mixedBytes2 = new byte[] {
67         (byte)0x09,
68         (byte)0x0a,
69         (byte)0x40,
70         (byte)0x0e,
71         (byte)0x40, (byte)0x40,
72         (byte)0x41, (byte)0x41,
73         (byte)0x6c, (byte)0x45,
74         (byte)0x84, (byte)0x41,
75         (byte)0x0f,
76         (byte)0x28 };
77
78    private static byte[][] malformedBytes = {
79        { (byte)0x0e,
80          (byte)0x039, (byte)0x40,
81          (byte)0x0f
82        },
83        { (byte)0x0e,
84          (byte)0x039, (byte)0x42,
85          (byte)0x0f
86        },
87        { (byte)0x0e,
88          (byte)0x040, (byte)0x41,
89          (byte)0x0f
90        },
91        { (byte)0x0e,
92          (byte)0x040, (byte)0xee,
93          (byte)0x0f
94        },
95        { (byte)0x0e,
96          (byte)0x0ef, (byte)0x30,
97          (byte)0x0f
98        },
99        { (byte)0x0e,
100          (byte)0x0ff, (byte)0x41,
101          (byte)0x0f
102        }
103    };
104
105    private static byte[][] unmappedBytes = {
106        { (byte)0x0e,
107          (byte)0x06c, (byte)0x46,
108          (byte)0x0f,
109        },
110        { (byte)0x0e,
111          (byte)0x078, (byte)0x46,
112          (byte)0x0f,
113        },
114        { (byte)0x0e,
115          (byte)0x083, (byte)0xfe,
116          (byte)0x0f,
117        },
118        { (byte)0xfa },
119        { (byte)0xfe },
120    };
121
122    public static void main(String[] args) throws Exception {
123        if (!(Arrays.equals(mixedStr.getBytes("cp1364"), mixedBytes)) ||
124            !mixedStr.equals(new String(mixedBytes, "cp1364")))
125            throw new RuntimeException("cp1364 failed on mixed!");
126
127        if (!(Arrays.equals(mixedStr2.getBytes("cp1364"), mixedBytes2)) ||
128            !mixedStr2.equals(new String(mixedBytes2, "cp1364")))
129            throw new RuntimeException("cp1364 failed on mixed!");
130
131        if (!(Arrays.equals(c2bNRStr.getBytes("cp1364"), c2bNRBytes)) ||
132            c2bNRStr.equals(new String(c2bNRBytes, "cp1364")))
133            throw new RuntimeException("cp1364 failed on c2bNR!");
134
135        ByteBuffer bb = ByteBuffer.allocateDirect(mixedBytes.length);
136        bb.put(mixedBytes).flip();
137        CharBuffer cb = Charset.forName("ibm1364").decode(bb);
138        if (!mixedStr.equals(new String(cb.toString())))
139            throw new RuntimeException("cp1364 failed on direct decod()!");
140
141        bb = ByteBuffer.allocateDirect(mixedBytes2.length);
142        bb.put(mixedBytes2).flip();
143        cb = Charset.forName("ibm1364").decode(bb);
144        if (!mixedStr2.equals(new String(cb.toString())))
145            throw new RuntimeException("cp1364 failed on direct decod()!");
146
147        cb = ByteBuffer.allocateDirect(mixedStr.length() * 2).asCharBuffer();
148        cb.put(mixedStr.toCharArray()).flip();
149        bb = Charset.forName("x-ibm1364").encode(cb);
150        if (!(Arrays.equals(Arrays.copyOf(bb.array(), bb.limit()), mixedBytes)))
151            throw new RuntimeException("cp1364 failed on direct encode()!");
152
153        cb = ByteBuffer.allocateDirect(mixedStr2.length() * 2).asCharBuffer();
154        cb.put(mixedStr2.toCharArray()).flip();
155        bb = Charset.forName("x-ibm1364").encode(cb);
156        if (!(Arrays.equals(Arrays.copyOf(bb.array(), bb.limit()), mixedBytes2)))
157            throw new RuntimeException("cp1364 failed on direct encode()!");
158
159        // malformed
160        cb = CharBuffer.allocate(1024);
161        CharBuffer cbd = ByteBuffer.allocateDirect(1024).asCharBuffer();
162        CharsetDecoder dec = Charset.forName("x-ibm1364").newDecoder();
163        for (byte[] ba:malformedBytes) {
164            cb.clear();
165            dec.reset();
166            if (!dec.reset().decode(ByteBuffer.wrap(ba), cb, true).isMalformed() ||
167                !dec.reset().decode(ByteBuffer.wrap(ba), cbd, true).isMalformed())
168                throw new RuntimeException("cp1364 failed on decode()/malformed!");
169        }
170
171        //unmappable
172        for (byte[] ba:unmappedBytes) {
173            cb.clear();
174            dec.reset();
175            if (!dec.reset().decode(ByteBuffer.wrap(ba), cb, true).isUnmappable() ||
176                !dec.reset().decode(ByteBuffer.wrap(ba), cbd, true).isUnmappable())
177                throw new RuntimeException("cp1364 failed on decode()/unmappable!");
178        }
179
180        //overflow
181        cb.limit(mixedStr.length() - 1);
182        cbd.limit(mixedStr.length() - 1);
183        if (!dec.reset().decode(ByteBuffer.wrap(mixedBytes), cb, true).isOverflow() ||
184            !dec.reset().decode(ByteBuffer.wrap(mixedBytes), cbd, true).isOverflow())
185            throw new RuntimeException("cp1364 failed on decode()/overflow!");
186
187        CharsetEncoder enc = Charset.forName("x-ibm1364").newEncoder();
188        // last "0x0f" is from flush()
189        bb = ByteBuffer.allocate(mixedBytes.length - 2);
190        ByteBuffer bbd = ByteBuffer.allocateDirect(mixedBytes.length - 2);
191        if (!enc.reset()
192                .encode(CharBuffer.wrap(mixedStr.toCharArray()), bb, true)
193                .isOverflow() ||
194            !enc.reset()
195                .encode(CharBuffer.wrap(mixedStr.toCharArray()), bbd, true)
196                .isOverflow())
197            throw new RuntimeException("cp1364 failed on encode()/overflow!");
198
199        // flush() overflow
200        bb = ByteBuffer.allocate(mixedBytes.length - 1);
201        bbd = ByteBuffer.allocateDirect(mixedBytes.length - 1);
202
203        enc.reset().encode(CharBuffer.wrap(mixedStr.toCharArray()), bb, true);
204        enc.reset().encode(CharBuffer.wrap(mixedStr.toCharArray()), bbd, true);
205
206        if (!enc.flush(bb).isOverflow() ||
207            !enc.flush(bbd).isOverflow())
208            throw new RuntimeException("cp1364 failed on encode()/flush()/overflow!");
209    }
210}
211