1 /*
   2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.*;
  25 
  26 class Utils {
  27 
  28     static final String BASE = System.getProperty("test.src", ".");
  29 
  30     private final static char[] hexDigits = "0123456789abcdef".toCharArray();
  31 
  32     public static String toString(byte[] b) {
  33         if (b == null) {
  34             return "(null)";
  35         }
  36         StringBuffer sb = new StringBuffer(b.length * 3);
  37         for (int i = 0; i < b.length; i++) {
  38             int k = b[i] & 0xff;
  39             if (i != 0) {
  40                 sb.append(':');
  41             }
  42             sb.append(hexDigits[k >>> 4]);
  43             sb.append(hexDigits[k & 0xf]);
  44         }
  45         return sb.toString();
  46     }
  47 
  48     public static byte[] parse(String s) {
  49         if (s.equals("(null)")) {
  50             return null;
  51         }
  52         try {
  53             int n = s.length();
  54             ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
  55             StringReader r = new StringReader(s);
  56             while (true) {
  57                 int b1 = nextNibble(r);
  58                 if (b1 < 0) {
  59                     break;
  60                 }
  61                 int b2 = nextNibble(r);
  62                 if (b2 < 0) {
  63                     throw new RuntimeException("Invalid string " + s);
  64                 }
  65                 int b = (b1 << 4) | b2;
  66                 out.write(b);
  67             }
  68             return out.toByteArray();
  69         } catch (IOException e) {
  70             throw new RuntimeException(e);
  71         }
  72     }
  73 
  74     private static int nextNibble(StringReader r) throws IOException {
  75         while (true) {
  76             int ch = r.read();
  77             if (ch == -1) {
  78                 return -1;
  79             } else if ((ch >= '0') && (ch <= '9')) {
  80                 return ch - '0';
  81             } else if ((ch >= 'a') && (ch <= 'f')) {
  82                 return ch - 'a' + 10;
  83             } else if ((ch >= 'A') && (ch <= 'F')) {
  84                 return ch - 'A' + 10;
  85             }
  86         }
  87     }
  88 
  89 }