jdk/test/java/nio/charset/coders/Util.java
author iklam
Tue, 12 Aug 2014 17:29:01 -0700
changeset 26212 7585e2404b2a
parent 5506 202f599c92aa
permissions -rw-r--r--
8046070: Class Data Sharing clean up and refactoring Summary: Cleaned up CDS to be more configurable, maintainable and extensible Reviewed-by: dholmes, coleenp, acorn, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4817
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     4
 *
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     8
 *
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    13
 * accompanied this code).
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    14
 *
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4817
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4817
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4817
diff changeset
    21
 * questions.
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    22
 */
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    23
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    24
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    25
public class Util {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    26
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    27
    private Util() { }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    28
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    29
    // Returns -1 if equal, o.w. returns index of first difference
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    30
    //
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    31
    public static int cmp(byte[] ba, byte[] bb) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    32
        int n = Math.min(ba.length, bb.length);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    33
        for (int i = 0; i < n; i++) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    34
            if ((i >= ba.length) || (i >= bb.length))
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    35
                return i;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    36
            if (ba[i] != bb[i])
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    37
                return i;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    38
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    39
        if (ba.length != bb.length)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    40
            return 0;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    41
        return -1;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    42
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    43
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    44
    // Returns -1 if equal, o.w. returns index of first difference
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    45
    //
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    46
    public static int cmp(char[] ca, char[] cb) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    47
        int n = Math.min(ca.length, cb.length);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    48
        for (int i = 0; i < n; i++) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    49
            if ((i >= ca.length) || (i >= cb.length))
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    50
                return i;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    51
            if (ca[i] != cb[i])
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    52
                return i;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    53
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    54
        if (ca.length != cb.length)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    55
            return 0;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    56
        return -1;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    57
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    58
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    59
    public static String toString(byte[] ba, int off, int len) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    60
        StringBuffer sb = new StringBuffer();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    61
        for (int i = off; i < off + len; i++) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    62
            int c = ba[i];
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    63
            if (c == '\\') {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    64
                sb.append("\\\\");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    65
                continue;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    66
            }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    67
            if ((c >= ' ') && (c < 0x7f)) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    68
                sb.append((char)c);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    69
                continue;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    70
            }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    71
            sb.append("\\x");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    72
            sb.append(Integer.toHexString(c & 0xff));
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    73
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    74
        return sb.toString();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    75
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    76
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    77
    public static String toString(byte[] ba) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    78
        return toString(ba, 0, ba.length);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    79
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    80
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    81
    public static String toString(char[] ca, int off, int len) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    82
        StringBuffer sb = new StringBuffer();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    83
        for (int i = off; i < off + len; i++) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    84
            char c = ca[i];
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    85
            if (c == '\\') {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    86
                sb.append("\\\\");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    87
                continue;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    88
            }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    89
            if ((c >= ' ') && (c < 0x7f)) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    90
                sb.append(c);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    91
                continue;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    92
            }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    93
            sb.append("\\u");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    94
            String s = Integer.toHexString(c);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    95
            while (s.length() < 4)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    96
                s = "0" + s;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    97
            sb.append(s);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    98
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    99
        return sb.toString();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   100
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   101
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   102
    public static String toString(char[] ca) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   103
        return toString(ca, 0, ca.length);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   104
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   105
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   106
    public static String toString(String s) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   107
        return toString(s.toCharArray());
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   108
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   109
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   110
    public static String toString(char c) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   111
        return toString(new char[]{ c });
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   112
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   113
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   114
}