Path: blob/master/test/jdk/com/sun/crypto/provider/TLS/Utils.java
41155 views
/*1* Copyright (c) 2005, 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*/2223import java.io.*;2425class Utils {2627static final String BASE = System.getProperty("test.src", ".");2829private final static char[] hexDigits = "0123456789abcdef".toCharArray();3031public static String toString(byte[] b) {32if (b == null) {33return "(null)";34}35StringBuffer sb = new StringBuffer(b.length * 3);36for (int i = 0; i < b.length; i++) {37int k = b[i] & 0xff;38if (i != 0) {39sb.append(':');40}41sb.append(hexDigits[k >>> 4]);42sb.append(hexDigits[k & 0xf]);43}44return sb.toString();45}4647public static byte[] parse(String s) {48if (s.equals("(null)")) {49return null;50}51try {52int n = s.length();53ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);54StringReader r = new StringReader(s);55while (true) {56int b1 = nextNibble(r);57if (b1 < 0) {58break;59}60int b2 = nextNibble(r);61if (b2 < 0) {62throw new RuntimeException("Invalid string " + s);63}64int b = (b1 << 4) | b2;65out.write(b);66}67return out.toByteArray();68} catch (IOException e) {69throw new RuntimeException(e);70}71}7273private static int nextNibble(StringReader r) throws IOException {74while (true) {75int ch = r.read();76if (ch == -1) {77return -1;78} else if ((ch >= '0') && (ch <= '9')) {79return ch - '0';80} else if ((ch >= 'a') && (ch <= 'f')) {81return ch - 'a' + 10;82} else if ((ch >= 'A') && (ch <= 'F')) {83return ch - 'A' + 10;84}85}86}8788}899091