test/jdk/java/lang/String/Encodings.java
author herrick
Mon, 14 Oct 2019 14:36:45 -0400
branchJDK-8200758-branch
changeset 58584 910b14f4fe3a
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232042: [macos] Installation fails if application name contains spaces Submitted-by: almatvee Reviewed-by: aherrick, asemenyuk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4085160 4139951 5005831
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test that required character encodings are supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStreamWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.charset.UnsupportedCharsetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class Encodings {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static boolean equals(byte[] a, byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        if (a.length != b.length) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        for (int i = 0; i < a.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            if (a[i] != b[i]) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static void go(String enc, String str, final byte[] bytes, boolean bidir)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        final Charset charset = Charset.forName(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        /* String(byte[] bs, String enc) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (!(new String(bytes, enc).equals(str)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            throw new Exception(enc + ": String constructor failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        /* String(byte[] bs, Charset charset) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (!(new String(bytes, charset).equals(str)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            throw new Exception(charset + ": String constructor failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        /* String(byte[] bs, int off, int len, Charset charset) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        String start = str.substring(0, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        String end = str.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if (!(new String(bytes, 0, 4, charset).equals(start)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                throw new Exception(charset + ": String constructor failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                throw new Exception(charset + ": String constructor failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        } else if (enc.equals("UTF-16")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            if (!(new String(bytes, 0, 6, charset).equals(start)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                throw new Exception(charset + ": String constructor failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (!(new String(bytes, 0, 2, charset).equals(start)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                throw new Exception(charset + ": String constructor failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                throw new Exception(charset + ": String constructor failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        /* InputStreamReader */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        InputStreamReader r = new InputStreamReader(bi, enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        String inEnc = r.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        int n = str.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        char[] cs = new char[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        for (int i = 0; i < n;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            int m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if ((m = r.read(cs, i, n - i)) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                throw new Exception(enc + ": EOF on InputStreamReader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            i += m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (!(new String(cs).equals(str)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throw new Exception(enc + ": InputStreamReader failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (!bidir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            System.err.println(enc + " --> " + inEnc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        /* String.getBytes(String enc) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        byte[] bs = str.getBytes(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (!equals(bs, bytes))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new Exception(enc + ": String.getBytes failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        /* String.getBytes(Charset charset) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        bs = str.getBytes(charset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (!equals(bs, bytes))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throw new Exception(charset + ": String.getBytes failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // Calls to String.getBytes(Charset) shouldn't automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // use the cached thread-local encoder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (charset.name().equals("UTF-16BE")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            String s = new String(bytes, charset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            // Replace the thread-local encoder with this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (bytes.length != bb.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                // Incidental test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                throw new RuntimeException("unequal length: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                           + bytes.length + " != "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                           + bb.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                boolean diff = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                // Expect different byte[] between UTF-16LE and UTF-16BE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                // even though encoder was previously cached by last call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                // to getBytes().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                for (int i = 0; i < bytes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    if (bytes[i] != bb[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        diff = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                if (!diff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    throw new RuntimeException("byte arrays equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        /* OutputStreamWriter */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        OutputStreamWriter w = new OutputStreamWriter(bo, enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        String outEnc = w.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        w.write(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        w.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        bs = bo.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (!equals(bs, bytes))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throw new Exception(enc + ": OutputStreamWriter failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        System.err.println(enc + " --> " + inEnc + " / " + outEnc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    static void go(String enc, String str, byte[] bytes) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        go(enc, str, bytes, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        go("US-ASCII", "abc", new byte[] { 'a', 'b', 'c' });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        go("us-ascii", "abc", new byte[] { 'a', 'b', 'c' });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        go("ISO646-US", "abc", new byte[] { 'a', 'b', 'c' });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        go("ISO-8859-1", "ab\u00c7", new byte[] { 'a', 'b', (byte)'\u00c7' });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        go("UTF-8", "ab\u1e09",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
           new byte[] { 'a', 'b',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        (byte)(0xe0 | (0x0f & (0x1e09 >> 12))),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        (byte)(0x80 | (0x3f & (0x1e09 >> 6))),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        (byte)(0x80 | (0x3f & 0x1e09)) });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        go("UTF-16BE", "ab\u1e09",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
           new byte[] { 0, 'a', 0, 'b', 0x1e, 0x09 });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        go("UTF-16LE", "ab\u1e09",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
           new byte[] { 'a', 0, 'b', 0, 0x09, 0x1e });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        /* UTF-16 accepts both byte orders on input but always uses big-endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         * on output, so test all three cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        go("UTF-16", "ab\u1e09",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
           new byte[] { (byte)0xfe, (byte)0xff, 0, 'a', 0, 'b', 0x1e, 0x09 });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        go("UTF-16", "ab\u1e09",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
           new byte[] { (byte)0xff, (byte)0xfe, 'a', 0, 'b', 0, 0x09, 0x1e },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
           false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}