IsLegalReplacement.java revision 2362:00cd9dc3c2b5
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 4821286
26 * @summary Check correctness of CharsetEncoder.isLegalReplacement(byte[])
27 */
28
29import java.io.*;
30import java.nio.*;
31import java.nio.charset.*;
32import java.util.*;
33
34
35public class IsLegalReplacement {
36
37    static PrintStream out = System.err;
38    static int errors = 0;
39
40    static String toString(byte[] ba) {
41        StringBuffer sb = new StringBuffer();
42        for (int i = 0; i < ba.length; i++) {
43            byte b = ba[i];
44            if (i > 0)
45                sb.append(' ');
46            sb.append(Integer.toHexString((b >> 4) & 0xf));
47            sb.append(Integer.toHexString((b >> 0) & 0xf));
48        }
49        return sb.toString();
50    }
51
52    static CoderResult ilr(String csn, byte[] repl) {
53        CharsetDecoder dec = Charset.forName(csn).newDecoder();
54        dec.onMalformedInput(CodingErrorAction.REPORT);
55        dec.onUnmappableCharacter(CodingErrorAction.REPORT);
56        ByteBuffer bb = ByteBuffer.wrap(repl);
57        CharBuffer cb = CharBuffer.allocate((int)(bb.remaining()
58                                                  * dec.maxCharsPerByte()));
59        return dec.decode(bb, cb, true);
60    }
61
62    static void test(String csn, byte[] repl, boolean expected)
63        throws Exception
64    {
65        CharsetEncoder enc = Charset.forName(csn).newEncoder();
66        out.print(csn + ": " + toString(repl) + ": ");
67        if (enc.isLegalReplacement(repl) == expected) {
68            out.print("Okay");
69        } else {
70            out.print("Wrong: Expected " + expected);
71            errors++;
72        }
73        out.println(" (" + ilr(csn, repl) + ")");
74    }
75
76    public static void main(String[] args) throws Exception {
77
78        test("UTF-16", new byte [] { (byte)0xd8, 0, (byte)0xdc, 0 }, true);
79        test("UTF-16", new byte [] { (byte)0xdc, 0, (byte)0xd8, 0 }, false);
80        test("UTF-16", new byte [] { (byte)0xd8, 0 }, false);
81        test("UTF-16BE", new byte [] { (byte)0xd8, 0, (byte)0xdc, 0 }, true);
82        test("UTF-16BE", new byte [] { (byte)0xdc, 0, (byte)0xd8, 0 }, false);
83        test("UTF-16BE", new byte [] { (byte)0xd8, 0 }, false);
84        test("UTF-16LE", new byte [] { 0, (byte)0xd8, 0, (byte)0xdc }, true);
85        test("UTF-16LE", new byte [] { 0, (byte)0xdc, 0, (byte)0xd8 }, false);
86        test("UTF-16LE", new byte [] { 0, (byte)0xd8 }, false);
87
88        if (errors > 0)
89            throw new Exception(errors + " error(s) occurred");
90
91    }
92
93}
94