1 /*
   2  * Copyright (c) 2011, 2013, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.oracle.ipack.util;
  27 
  28 import java.io.DataInput;
  29 import java.io.DataOutput;
  30 import java.io.IOException;
  31 
  32 public final class Util {
  33     private static final char[] HEX_SYMBOLS = {
  34             '0', '1', '2', '3', '4', '5', '6', '7',
  35             '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
  36         };
  37 
  38     private Util() {
  39     }
  40 
  41     public static String hex(final byte[] value) {
  42         final char[] buffer = new char[value.length * 2];
  43         for (int i = 0; i < value.length; ++i) {
  44             Util.addHex8(buffer, i * 2, value[i]);
  45         }
  46         return String.valueOf(buffer);
  47     }
  48 
  49     public static String hex32(final int value) {
  50         final char[] buffer = new char[8];
  51         addHex32(buffer, 0, value);
  52         return String.valueOf(buffer);
  53     }
  54 
  55     public static String hex16(final int value) {
  56         final char[] buffer = new char[4];
  57         addHex16(buffer, 0, value);
  58         return String.valueOf(buffer);
  59     }
  60 
  61     public static String hex8(final int value) {
  62         final char[] buffer = new char[2];
  63         addHex8(buffer, 0, value);
  64         return String.valueOf(buffer);
  65     }
  66 
  67     public static void dumpHex(final byte[] data) {
  68         dumpHex(data, 0, data.length);
  69     }
  70 
  71     public static void dumpHex(final byte[] data,
  72                                final int offset,
  73                                final int length) {
  74         final int fullLinesMax = offset + length & ~15;
  75         int i;
  76 
  77         for (i = offset; i < fullLinesMax; i += 16) {
  78             dumpHexLine(data, i, 16);
  79         }
  80 
  81         if (i != (offset + length)) {
  82             dumpHexLine(data, i, offset + length - i);
  83         }
  84     }
  85 
  86     public static String readString(final DataInput dataInput,
  87                                     final int numBytes) throws IOException {
  88         final byte[] buffer = new byte[numBytes];
  89         dataInput.readFully(buffer);
  90         return new String(buffer);
  91     }
  92 
  93     public static String readString(final DataInput dataInput)
  94             throws IOException {
  95         final StringBuilder sb = new StringBuilder();
  96         byte b;
  97         while ((b = dataInput.readByte()) != 0) {
  98             sb.append((char) (b & 0xff));
  99         }
 100 
 101         return sb.toString();
 102     }
 103 
 104     public static void writeString(final DataOutput dataOutput,
 105                                    final String value,
 106                                    final int size,
 107                                    final char paddingChar)
 108             throws IOException {
 109         final byte[] valueBytes = value.getBytes();
 110         final int numValueBytesToStore = (valueBytes.length < size)
 111                                                  ? valueBytes.length
 112                                                  : size;
 113         dataOutput.write(valueBytes, 0, numValueBytesToStore);
 114 
 115         if (numValueBytesToStore == size) {
 116             return;
 117         }
 118 
 119         final byte paddingByte = (byte) paddingChar;
 120         for (int i = size - numValueBytesToStore; i > 0; --i) {
 121             dataOutput.writeByte(paddingByte);
 122         }
 123     }
 124 
 125     public static void addHex32(final char[] buffer,
 126                                 final int offset,
 127                                 final int value) {
 128         addHex16(buffer, offset, value >> 16);
 129         addHex16(buffer, offset + 4, value);
 130     }
 131 
 132     public static void addHex16(final char[] buffer,
 133                                 final int offset,
 134                                 final int value) {
 135         addHex8(buffer, offset, value >> 8);
 136         addHex8(buffer, offset + 2, value);
 137     }
 138 
 139     public static void addHex8(final char[] buffer,
 140                                final int offset,
 141                                final int value) {
 142         buffer[offset] = HEX_SYMBOLS[(value >> 4) & 0xf];
 143         buffer[offset + 1] = HEX_SYMBOLS[value & 0xf];
 144     }
 145 
 146     private static void dumpHexLine(final byte[] data,
 147                                     final int offset,
 148                                     final int length) {
 149         if (length > 0) {
 150             final char[] lineBuffer = new char[length * 2 + length - 1];
 151             addHex8(lineBuffer, 0, data[offset]);
 152             for (int i = 1; i < length; ++i) {
 153                 lineBuffer[3 * i - 1] = ' ';
 154                 addHex8(lineBuffer, 3 * i, data[offset + i]);
 155             }
 156 
 157             System.out.print(String.valueOf(lineBuffer));
 158         }
 159         System.out.println();
 160     }
 161 }