Skip to main content
summaryrefslogtreecommitdiffstats
blob: 16536c84f1ed289ee974aca2c811a8d4d161e517 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.jdk.core.util;

import java.io.PrintStream;
import java.nio.ByteBuffer;

/**
 * @author Ryan D. Brooks
 * @author Andrew M. Finkbeiner
 */
public class ByteUtil {

   public static void printBinary(byte[] data, int bytesPerGroup, int groupPerLine, PrintStream out) {
      int groups = 0;
      for (int i = 0; i < data.length; i++) {
         out.print(ByteUtil.toBinaryString(data[i]));
         if ((i + 1) % bytesPerGroup == 0) {
            out.print(" ");
            groups++;
            if (groups % groupPerLine == 0) {
               out.println();
            }
         }
      }
   }

   public static void printHex(byte[] data, int bytesPerGroup, int groupPerLine, PrintStream out) {
      int groups = 0;
      for (int i = 0; i < data.length; i++) {
         out.print(ByteUtil.toHexString(data[i]));
         if ((i + 1) % bytesPerGroup == 0) {
            out.print(" ");
            groups++;
            if (groups % groupPerLine == 0) {
               out.println();
            }
         }
      }
   }

   public static void printHex(byte[] data, int bytesPerGroup, int groupPerLine, StringBuilder strBuilder) {
      int groups = 0;
      for (int i = 0; i < data.length; i++) {
         strBuilder.append(ByteUtil.toHexString(data[i]));
         if ((i + 1) % bytesPerGroup == 0) {
            strBuilder.append(" ");
            groups++;
         }
      }
   }

   public static void printHex(byte[] data, int bytesPerGroup, int groupPerLine, boolean isSpaced, StringBuilder strBuilder) {
      int groups = 0;
      for (int i = 0; i < data.length; i++) {
         strBuilder.append(ByteUtil.toHexString(data[i]));
         if ((i + 1) % bytesPerGroup == 0 && isSpaced) {
            strBuilder.append(" ");
            groups++;
         }
      }
   }

   /**
    * NOTE the SDK supplies a Integer.toBinaryString but it is not formatted to a standard number of chars so it was not
    * a good option.
    */
   public static String toBinaryString(byte b) {
      StringBuffer sb = new StringBuffer();
      sb.append((b >> 7 & 0x01));
      sb.append((b >> 6 & 0x01));
      sb.append((b >> 5 & 0x01));
      sb.append((b >> 4 & 0x01));
      sb.append((b >> 3 & 0x01));
      sb.append((b >> 2 & 0x01));
      sb.append((b >> 1 & 0x01));
      sb.append((b & 0x01));
      return sb.toString();
   }

   public static byte[] toBytes(long n) {
      byte[] bytes = new byte[8];
      toBytes(bytes, 0, n);
      return bytes;
   }

   public static void toBytes(byte[] bytes, int startPos, long n) {
      for (int i = startPos + 7; i >= startPos; i--) {
         bytes[i] = (byte) n;
         n >>>= 8;
      }
   }

   public static void toBytes(byte[] bytes, int startPos, int n) {
      for (int i = startPos + 3; i >= startPos; i--) {
         bytes[i] = (byte) n;
         n >>>= 8;
      }
   }

   public static String toHexString(byte b) {
      String temp = Integer.toHexString(b);
      if (temp.length() >= 2) {
         return temp.substring(temp.length() - 2).toUpperCase();
      } else {
         return "0" + temp.toUpperCase();
      }
   }

   /**
    * Build a long from first 8 bytes of the array.
    * 
    * @param b The byte[] to convert.
    * @return A long.
    */
   public static long toLong(byte[] b) {
      if (b.length != 8) {
         throw new IllegalArgumentException();
      }

      return ((long) b[7] & 0xFF) + (((long) b[6] & 0xFF) << 8) + (((long) b[5] & 0xFF) << 16) + (((long) b[4] & 0xFF) << 24) + (((long) b[3] & 0xFF) << 32) + (((long) b[2] & 0xFF) << 40) + (((long) b[1] & 0xFF) << 48) + (((long) b[0] & 0xFF) << 56);
   }

   public ByteUtil() {
      super();
   }

   /**
    * writes message data to a buffer in hex format
    */
   public static void printByteDump(StringBuilder strBuilder, byte[] data, int offset, int length, int columnNum) {
      printByteDump(strBuilder, data, offset, length, columnNum, true);
   }

   /**
    * writes message data to a buffer in hex format
    */
   public static void printByteDump(StringBuilder strBuilder, byte[] data, int offset, int length, int columnNum, boolean hex) {
      int columnCount = 0;
      final int endIndex = offset + length;
      for (int i = offset; i < endIndex; i++) {
         if (columnCount == columnNum) {
            strBuilder.append('\n');
            columnCount = 0;
         }
         if (hex) {
            strBuilder.append(String.format("%02x ", data[i]));
         } else {

            strBuilder.append(data[i]).append(' ');
         }
         columnCount++;
      }
      strBuilder.append('\n');
   }

   public static void printByteDump(StringBuilder strBuilder, ByteBuffer data, int offset, int length, int columnNum) {
      int currentPosition = data.position();
      //	   data.position(offset);
      int columnCount = 0;
      final int endIndex = offset + length;
      for (int i = offset; i < endIndex; i++) {
         if (columnCount == columnNum) {
            strBuilder.append('\n');
            columnCount = 0;
         }
         strBuilder.append(String.format("%02x ", data.get(i)));
         columnCount++;
      }
      strBuilder.append('\n');

      data.position(currentPosition);
   }

   public static void main(String[] args) {
      System.out.println(ByteUtil.toHexString((byte) 128));
   }
}

Back to the top