Path: blob/master/test/jdk/java/nio/charset/coders/Util.java
41153 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324public class Util {2526private Util() { }2728// Returns -1 if equal, o.w. returns index of first difference29//30public static int cmp(byte[] ba, byte[] bb) {31int n = Math.min(ba.length, bb.length);32for (int i = 0; i < n; i++) {33if ((i >= ba.length) || (i >= bb.length))34return i;35if (ba[i] != bb[i])36return i;37}38if (ba.length != bb.length)39return 0;40return -1;41}4243// Returns -1 if equal, o.w. returns index of first difference44//45public static int cmp(char[] ca, char[] cb) {46int n = Math.min(ca.length, cb.length);47for (int i = 0; i < n; i++) {48if ((i >= ca.length) || (i >= cb.length))49return i;50if (ca[i] != cb[i])51return i;52}53if (ca.length != cb.length)54return 0;55return -1;56}5758public static String toString(byte[] ba, int off, int len) {59StringBuffer sb = new StringBuffer();60for (int i = off; i < off + len; i++) {61int c = ba[i];62if (c == '\\') {63sb.append("\\\\");64continue;65}66if ((c >= ' ') && (c < 0x7f)) {67sb.append((char)c);68continue;69}70sb.append("\\x");71sb.append(Integer.toHexString(c & 0xff));72}73return sb.toString();74}7576public static String toString(byte[] ba) {77return toString(ba, 0, ba.length);78}7980public static String toString(char[] ca, int off, int len) {81StringBuffer sb = new StringBuffer();82for (int i = off; i < off + len; i++) {83char c = ca[i];84if (c == '\\') {85sb.append("\\\\");86continue;87}88if ((c >= ' ') && (c < 0x7f)) {89sb.append(c);90continue;91}92sb.append("\\u");93String s = Integer.toHexString(c);94while (s.length() < 4)95s = "0" + s;96sb.append(s);97}98return sb.toString();99}100101public static String toString(char[] ca) {102return toString(ca, 0, ca.length);103}104105public static String toString(String s) {106return toString(s.toCharArray());107}108109public static String toString(char c) {110return toString(new char[]{ c });111}112113}114115116