comparison graal/com.oracle.max.base/src/com/sun/max/lang/Bytes.java @ 3733:e233f5660da4

Added Java files from Maxine project.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Sat, 17 Dec 2011 19:59:18 +0100
parents
children
comparison
equal deleted inserted replaced
3732:3e2e8b8abdaf 3733:e233f5660da4
1 /*
2 * Copyright (c) 2007, 2011, 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 package com.sun.max.lang;
24
25 import java.io.*;
26
27 import com.sun.max.io.*;
28 import com.sun.max.util.*;
29
30 /**
31 * Byte and byte array operations.
32 */
33 public final class Bytes {
34
35 private Bytes() {
36 }
37
38 public static final int SIZE = 1;
39 public static final int WIDTH = 8;
40 public static final int MASK = 0xff;
41
42 public static final Range VALUE_RANGE = new Range(Byte.MIN_VALUE, Byte.MAX_VALUE);
43
44 public static int zeroExtendedToInt(byte b) {
45 final char ch = (char) b;
46 return ch;
47 }
48
49 /**
50 * Returns the number of zero bits following the lowest-order ("rightmost")
51 * one-bit in the two's complement binary representation of the specified
52 * {@code byte} value. Returns 8 if the specified value has no
53 * one-bits in its two's complement representation, in other words if it is
54 * equal to zero.
55 *
56 * @return the number of zero bits following the lowest-order ("rightmost")
57 * one-bit in the two's complement binary representation of the
58 * specified {@code byte} value, or 8 if the value is equal
59 * to zero.
60 */
61 public static int numberOfTrailingZeros(byte b) {
62 // HD, Figure 5-14
63 int y;
64 int i = b & 0xFF;
65 if (i == 0) {
66 return 8;
67 }
68 int n = 7;
69 y = (i << 4) & 0xFF;
70 if (y != 0) {
71 n = n - 4;
72 i = y;
73 }
74 y = (i << 2) & 0xFF;
75 if (y != 0) {
76 n = n - 2;
77 i = y;
78 }
79 return n - (((i << 1) & 0xFF) >>> 7);
80 }
81
82 /**
83 * Are the bytes of an array, starting at some position, equal to the contents
84 * of a second array.
85 * @param array1 An array of bytes
86 * @param startIndex1 Index in {@code array1} at which to start comparison
87 * @param array2 An array of bytes to be compared
88 * @return tree iff the entire contents of {@code array2} are equal to
89 * the contents of {@code array1}, starting at {@code startIndex1}.
90 */
91 public static boolean equals(byte[] array1, int startIndex1, byte[] array2) {
92 if (array1.length < startIndex1 + array2.length) {
93 return false;
94 }
95 for (int i = 0; i < array2.length; i++) {
96 if (array1[startIndex1 + i] != array2[i]) {
97 return false;
98 }
99 }
100 return true;
101 }
102
103 /**
104 * Compares the first {@code length} bytes in two byte arrays
105 * for equality.
106 */
107 public static boolean equals(byte[] array1, byte[] array2, int length) {
108 if (array1.length < length || array2.length < length) {
109 return false;
110 }
111 for (int i = 0; i < length; i++) {
112 if (array1[i] != array2[i]) {
113 return false;
114 }
115 }
116 return true;
117 }
118
119 /**
120 * Compares the contents of two byte arrays for equality.
121 */
122 public static boolean equals(byte[] array1, byte[] array2) {
123 if (array1 == array2) {
124 return true;
125 }
126 if (array1.length != array2.length) {
127 return false;
128 }
129 return equals(array1, array2, array1.length);
130 }
131
132 public static void copy(byte[] fromArray, int fromStartIndex, byte[] toArray,
133 final int toStartIndex, int nBytes) {
134 for (int i = 0; i < nBytes; i++) {
135 toArray[toStartIndex + i] = fromArray[fromStartIndex + i];
136 }
137 }
138
139 public static void copy(byte[] fromArray, byte[] toArray, int nBytes) {
140 copy(fromArray, 0, toArray, 0, nBytes);
141 }
142
143 public static void copyAll(byte[] fromArray, byte[] toArray, int toStartIndex) {
144 copy(fromArray, 0, toArray, toStartIndex, fromArray.length);
145 }
146
147 public static void copyAll(byte[] fromArray, byte[] toArray) {
148 copy(fromArray, 0, toArray, 0, fromArray.length);
149 }
150
151 public static byte[] withLength(byte[] array, int length) {
152 final byte[] result = new byte[length];
153 if (length >= array.length) {
154 copyAll(array, result);
155 } else {
156 copy(array, result, length);
157 }
158 return result;
159 }
160
161 public static byte[] getSection(byte[] fromArray, int startIndex, int endIndex) {
162 final int length = endIndex - startIndex;
163 final byte[] result = new byte[length];
164 copy(fromArray, startIndex, result, 0, length);
165 return result;
166 }
167
168 /**
169 * Assigns zero to every byte in an array.
170 */
171 public static void clear(byte[] array) {
172 for (int i = 0; i < array.length; i++) {
173 array[i] = (byte) 0;
174 }
175 }
176
177 public static boolean areClear(byte[] array) {
178 for (int i = 0; i < array.length; i++) {
179 if (array[i] != (byte) 0) {
180 return false;
181 }
182 }
183 return true;
184 }
185
186 /**
187 * @param value a byte
188 * @return A string representation of the byte in hexadecimal, e.g. "0x0A"
189 */
190 public static String toHexLiteral(byte value) {
191 String hexString = Integer.toHexString(value).toUpperCase();
192 if (hexString.length() == 1) {
193 hexString = "0" + hexString;
194 } else if (hexString.length() > 2) {
195 hexString = hexString.substring(hexString.length() - 2);
196 }
197 return "0x" + hexString;
198 }
199
200 /**
201 * @param values an array of bytes
202 * @return A string representation of the bytes in hexadecimal, e.g. "0x01A203"
203 */
204 public static String toHexLiteral(byte[] values) {
205 String s = "0x";
206 for (byte value : values) {
207 String hexString = Integer.toHexString(value).toUpperCase();
208 if (hexString.length() == 1) {
209 hexString = "0" + hexString;
210 }
211 s += hexString;
212 }
213 return s;
214 }
215
216 /**
217 * Returns a string representation of the contents of the specified array.
218 * Adjacent elements are separated by the specified separator. Elements are
219 * converted to strings with {@link #toHexLiteral(byte)}.
220 *
221 * @param array the array whose string representation to return
222 * @param separator the separator to use
223 * @return a string representation of <tt>array</tt>
224 * @throws NullPointerException if {@code array} or {@code separator} is null
225 */
226 public static String toHexString(byte[] array, String separator) {
227 if (array == null || separator == null) {
228 throw new NullPointerException();
229 }
230 if (array.length == 0) {
231 return "";
232 }
233
234 final StringBuilder buf = new StringBuilder();
235 buf.append(toHexLiteral(array[0]));
236
237 for (int i = 1; i < array.length; i++) {
238 buf.append(separator);
239 buf.append(toHexLiteral(array[i]));
240 }
241
242 return buf.toString();
243 }
244
245 public static byte[] fromInputStream(InputStream inputStream) throws IOException {
246 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
247 Streams.copy(inputStream, outputStream);
248 return outputStream.toByteArray();
249 }
250
251 }