-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBytes.java
More file actions
252 lines (225 loc) · 8.16 KB
/
Copy pathBytes.java
File metadata and controls
252 lines (225 loc) · 8.16 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
*
* Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product
* that is described in this document. In particular, and without limitation, these intellectual property
* rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or
* more additional patents or pending patent applications in the U.S. and in other countries.
*
* U.S. Government Rights - Commercial software. Government users are subject to the Sun
* Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its
* supplements.
*
* Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or
* registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks
* are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the
* U.S. and other countries.
*
* UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open
* Company, Ltd.
*/
package com.sun.max.lang;
import java.io.*;
import com.sun.max.io.*;
import com.sun.max.util.*;
/**
* Byte and byte array operations.
*
* @author Bernd Mathiske
* @author Michael Van De Vanter
*/
public final class Bytes {
private Bytes() {
}
public static final int SIZE = 1;
public static final int WIDTH = 8;
public static final int MASK = 0xff;
public static final Range VALUE_RANGE = new Range(Byte.MIN_VALUE, Byte.MAX_VALUE);
public static int zeroExtendedToInt(byte b) {
final char ch = (char) b;
return ch;
}
/**
* Returns the number of zero bits following the lowest-order ("rightmost")
* one-bit in the two's complement binary representation of the specified
* {@code byte} value. Returns 8 if the specified value has no
* one-bits in its two's complement representation, in other words if it is
* equal to zero.
*
* @return the number of zero bits following the lowest-order ("rightmost")
* one-bit in the two's complement binary representation of the
* specified {@code byte} value, or 8 if the value is equal
* to zero.
*/
public static int numberOfTrailingZeros(byte b) {
// HD, Figure 5-14
int y;
int i = b & 0xFF;
if (i == 0) {
return 8;
}
int n = 7;
y = (i << 4) & 0xFF;
if (y != 0) {
n = n - 4;
i = y;
}
y = (i << 2) & 0xFF;
if (y != 0) {
n = n - 2;
i = y;
}
return n - (((i << 1) & 0xFF) >>> 7);
}
/**
* Are the bytes of an array, starting at some position, equal to the contents
* of a second array.
* @param array1 An array of bytes
* @param startIndex1 Index in {@code array1} at which to start comparison
* @param array2 An array of bytes to be compared
* @return tree iff the entire contents of {@code array2} are equal to
* the contents of {@code array1}, starting at {@code startIndex1}.
*/
public static boolean equals(byte[] array1, int startIndex1, byte[] array2) {
if (array1.length < startIndex1 + array2.length) {
return false;
}
for (int i = 0; i < array2.length; i++) {
if (array1[startIndex1 + i] != array2[i]) {
return false;
}
}
return true;
}
/**
* Compares the first {@code length} bytes in two byte arrays
* for equality.
*/
public static boolean equals(byte[] array1, byte[] array2, int length) {
if (array1.length < length || array2.length < length) {
return false;
}
for (int i = 0; i < length; i++) {
if (array1[i] != array2[i]) {
return false;
}
}
return true;
}
/**
* Compares the contents of two byte arrays for equality.
*/
public static boolean equals(byte[] array1, byte[] array2) {
if (array1 == array2) {
return true;
}
if (array1.length != array2.length) {
return false;
}
return equals(array1, array2, array1.length);
}
public static void copy(byte[] fromArray, int fromStartIndex, byte[] toArray,
final int toStartIndex, int nBytes) {
for (int i = 0; i < nBytes; i++) {
toArray[toStartIndex + i] = fromArray[fromStartIndex + i];
}
}
public static void copy(byte[] fromArray, byte[] toArray, int nBytes) {
copy(fromArray, 0, toArray, 0, nBytes);
}
public static void copyAll(byte[] fromArray, byte[] toArray, int toStartIndex) {
copy(fromArray, 0, toArray, toStartIndex, fromArray.length);
}
public static void copyAll(byte[] fromArray, byte[] toArray) {
copy(fromArray, 0, toArray, 0, fromArray.length);
}
public static byte[] withLength(byte[] array, int length) {
final byte[] result = new byte[length];
if (length >= array.length) {
copyAll(array, result);
} else {
copy(array, result, length);
}
return result;
}
public static byte[] getSection(byte[] fromArray, int startIndex, int endIndex) {
final int length = endIndex - startIndex;
final byte[] result = new byte[length];
copy(fromArray, startIndex, result, 0, length);
return result;
}
/**
* Assigns zero to every byte in an array.
*/
public static void clear(byte[] array) {
for (int i = 0; i < array.length; i++) {
array[i] = (byte) 0;
}
}
public static boolean areClear(byte[] array) {
for (int i = 0; i < array.length; i++) {
if (array[i] != (byte) 0) {
return false;
}
}
return true;
}
/**
* @param value a byte
* @return A string representation of the byte in hexadecimal, e.g. "0x0A"
*/
public static String toHexLiteral(byte value) {
String hexString = Integer.toHexString(value).toUpperCase();
if (hexString.length() == 1) {
hexString = "0" + hexString;
} else if (hexString.length() > 2) {
hexString = hexString.substring(hexString.length() - 2);
}
return "0x" + hexString;
}
/**
* @param values an array of bytes
* @return A string representation of the bytes in hexadecimal, e.g. "0x01A203"
*/
public static String toHexLiteral(byte[] values) {
String s = "0x";
for (byte value : values) {
String hexString = Integer.toHexString(value).toUpperCase();
if (hexString.length() == 1) {
hexString = "0" + hexString;
}
s += hexString;
}
return s;
}
/**
* Returns a string representation of the contents of the specified array.
* Adjacent elements are separated by the specified separator. Elements are
* converted to strings with {@link #toHexLiteral(byte)}.
*
* @param array the array whose string representation to return
* @param separator the separator to use
* @return a string representation of <tt>array</tt>
* @throws NullPointerException if {@code array} or {@code separator} is null
*/
public static String toHexString(byte[] array, String separator) {
if (array == null || separator == null) {
throw new NullPointerException();
}
if (array.length == 0) {
return "";
}
final StringBuilder buf = new StringBuilder();
buf.append(toHexLiteral(array[0]));
for (int i = 1; i < array.length; i++) {
buf.append(separator);
buf.append(toHexLiteral(array[i]));
}
return buf.toString();
}
public static byte[] fromInputStream(InputStream inputStream) throws IOException {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Streams.copy(inputStream, outputStream);
return outputStream.toByteArray();
}
}