2115N/A/*
2362N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2115N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2115N/A *
2115N/A * This code is free software; you can redistribute it and/or modify it
2115N/A * under the terms of the GNU General Public License version 2 only, as
2115N/A * published by the Free Software Foundation.
2115N/A *
2115N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2115N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2115N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2115N/A * version 2 for more details (a copy is included in the LICENSE file that
2115N/A * accompanied this code).
2115N/A *
2115N/A * You should have received a copy of the GNU General Public License version
2115N/A * 2 along with this work; if not, write to the Free Software Foundation,
2115N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2115N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2115N/A */
2115N/A
2115N/A
2115N/Apublic class Util {
2115N/A
2115N/A private Util() { }
2115N/A
2115N/A // Returns -1 if equal, o.w. returns index of first difference
2115N/A //
2115N/A public static int cmp(byte[] ba, byte[] bb) {
2115N/A int n = Math.min(ba.length, bb.length);
2115N/A for (int i = 0; i < n; i++) {
2115N/A if ((i >= ba.length) || (i >= bb.length))
2115N/A return i;
2115N/A if (ba[i] != bb[i])
2115N/A return i;
2115N/A }
2115N/A if (ba.length != bb.length)
2115N/A return 0;
2115N/A return -1;
2115N/A }
2115N/A
2115N/A // Returns -1 if equal, o.w. returns index of first difference
2115N/A //
2115N/A public static int cmp(char[] ca, char[] cb) {
2115N/A int n = Math.min(ca.length, cb.length);
2115N/A for (int i = 0; i < n; i++) {
2115N/A if ((i >= ca.length) || (i >= cb.length))
2115N/A return i;
2115N/A if (ca[i] != cb[i])
2115N/A return i;
2115N/A }
2115N/A if (ca.length != cb.length)
2115N/A return 0;
2115N/A return -1;
2115N/A }
2115N/A
2115N/A public static String toString(byte[] ba, int off, int len) {
2115N/A StringBuffer sb = new StringBuffer();
2115N/A for (int i = off; i < off + len; i++) {
2115N/A int c = ba[i];
2115N/A if (c == '\\') {
2115N/A sb.append("\\\\");
2115N/A continue;
2115N/A }
2115N/A if ((c >= ' ') && (c < 0x7f)) {
2115N/A sb.append((char)c);
2115N/A continue;
2115N/A }
2115N/A sb.append("\\x");
2115N/A sb.append(Integer.toHexString(c & 0xff));
2115N/A }
2115N/A return sb.toString();
2115N/A }
2115N/A
2115N/A public static String toString(byte[] ba) {
2115N/A return toString(ba, 0, ba.length);
2115N/A }
2115N/A
2115N/A public static String toString(char[] ca, int off, int len) {
2115N/A StringBuffer sb = new StringBuffer();
2115N/A for (int i = off; i < off + len; i++) {
2115N/A char c = ca[i];
2115N/A if (c == '\\') {
2115N/A sb.append("\\\\");
2115N/A continue;
2115N/A }
2115N/A if ((c >= ' ') && (c < 0x7f)) {
2115N/A sb.append(c);
2115N/A continue;
2115N/A }
2115N/A sb.append("\\u");
2115N/A String s = Integer.toHexString(c);
2115N/A while (s.length() < 4)
2115N/A s = "0" + s;
2115N/A sb.append(s);
2115N/A }
2115N/A return sb.toString();
2115N/A }
2115N/A
2115N/A public static String toString(char[] ca) {
2115N/A return toString(ca, 0, ca.length);
2115N/A }
2115N/A
2115N/A public static String toString(String s) {
2115N/A return toString(s.toCharArray());
2115N/A }
2115N/A
2115N/A public static String toString(char c) {
2115N/A return toString(new char[]{ c });
2115N/A }
2115N/A
2115N/A}