Mercurial > hg > truffle
comparison graal/com.oracle.max.base/src/com/sun/max/lang/Endianness.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 | bc8527f3071c |
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 import java.nio.*; | |
27 | |
28 /** | |
29 * Enumerated type with values for the most common more ways to arrange bits, bytes, etc. | |
30 */ | |
31 public enum Endianness { | |
32 | |
33 LITTLE { | |
34 @Override | |
35 public short readShort(InputStream stream) throws IOException { | |
36 final int low = readByte(stream) & 0xff; | |
37 final int high = readByte(stream); | |
38 return (short) ((high << 8) | low); | |
39 } | |
40 | |
41 @Override | |
42 public int readInt(InputStream stream) throws IOException { | |
43 final int low = readShort(stream) & 0xffff; | |
44 final int high = readShort(stream); | |
45 return (high << 16) | low; | |
46 } | |
47 | |
48 @Override | |
49 public long readLong(InputStream stream) throws IOException { | |
50 final long low = readInt(stream) & 0xffffffffL; | |
51 final long high = readInt(stream); | |
52 return (high << 32) | low; | |
53 } | |
54 | |
55 @Override | |
56 public void writeShort(OutputStream stream, short value) throws IOException { | |
57 short v = value; | |
58 stream.write(v & 0xff); | |
59 v >>= 8; | |
60 stream.write(v & 0xff); | |
61 } | |
62 | |
63 @Override | |
64 public void writeInt(OutputStream stream, int value) throws IOException { | |
65 int v = value; | |
66 stream.write(v & 0xff); | |
67 v >>= 8; | |
68 stream.write(v & 0xff); | |
69 v >>= 8; | |
70 stream.write(v & 0xff); | |
71 v >>= 8; | |
72 stream.write(v & 0xff); | |
73 } | |
74 | |
75 @Override | |
76 public void writeLong(OutputStream stream, long value) throws IOException { | |
77 long v = value; | |
78 stream.write((int) v & 0xff); | |
79 v >>= 8; | |
80 stream.write((int) v & 0xff); | |
81 v >>= 8; | |
82 stream.write((int) v & 0xff); | |
83 v >>= 8; | |
84 stream.write((int) v & 0xff); | |
85 v >>= 8; | |
86 stream.write((int) v & 0xff); | |
87 v >>= 8; | |
88 stream.write((int) v & 0xff); | |
89 v >>= 8; | |
90 stream.write((int) v & 0xff); | |
91 v >>= 8; | |
92 stream.write((int) v & 0xff); | |
93 } | |
94 | |
95 @Override | |
96 public byte[] toBytes(short value) { | |
97 short v = value; | |
98 final byte[] bytes = new byte[2]; | |
99 bytes[0] = (byte) (v & 0xff); | |
100 v >>= 8; | |
101 bytes[1] = (byte) (v & 0xff); | |
102 return bytes; | |
103 } | |
104 | |
105 @Override | |
106 public byte[] toBytes(int value) { | |
107 int v = value; | |
108 final byte[] bytes = new byte[4]; | |
109 bytes[0] = (byte) (v & 0xff); | |
110 v >>= 8; | |
111 bytes[1] = (byte) (v & 0xff); | |
112 v >>= 8; | |
113 bytes[2] = (byte) (v & 0xff); | |
114 v >>= 8; | |
115 bytes[3] = (byte) (v & 0xff); | |
116 return bytes; | |
117 } | |
118 | |
119 @Override | |
120 public byte[] toBytes(long value) { | |
121 long v = value; | |
122 final byte[] bytes = new byte[8]; | |
123 bytes[0] = (byte) (v & 0xff); | |
124 v >>= 8; | |
125 bytes[1] = (byte) (v & 0xff); | |
126 v >>= 8; | |
127 bytes[2] = (byte) (v & 0xff); | |
128 v >>= 8; | |
129 bytes[3] = (byte) (v & 0xff); | |
130 v >>= 8; | |
131 bytes[4] = (byte) (v & 0xff); | |
132 v >>= 8; | |
133 bytes[5] = (byte) (v & 0xff); | |
134 v >>= 8; | |
135 bytes[6] = (byte) (v & 0xff); | |
136 v >>= 8; | |
137 bytes[7] = (byte) (v & 0xff); | |
138 return bytes; | |
139 } | |
140 | |
141 @Override | |
142 public void toBytes(short value, byte[] result, int offset) { | |
143 short v = value; | |
144 for (int i = 0; i < Shorts.SIZE && i < result.length; i++) { | |
145 result[i + offset] = (byte) (v & 0xff); | |
146 v >>= 8; | |
147 } | |
148 } | |
149 | |
150 @Override | |
151 public void toBytes(int value, byte[] result, int offset) { | |
152 int v = value; | |
153 for (int i = 0; i < Ints.SIZE && i < result.length; i++) { | |
154 result[i + offset] = (byte) (v & 0xff); | |
155 v >>= 8; | |
156 } | |
157 } | |
158 | |
159 @Override | |
160 public void toBytes(long value, byte[] result, int offset) { | |
161 long v = value; | |
162 for (int i = 0; i < Longs.SIZE && i < result.length; i++) { | |
163 result[i + offset] = (byte) (v & 0xff); | |
164 v >>= 8; | |
165 } | |
166 } | |
167 | |
168 @Override | |
169 public int offsetWithinWord(WordWidth wordWidth, WordWidth dataWidth) { | |
170 return 0; | |
171 } | |
172 | |
173 @Override | |
174 public ByteOrder asByteOrder() { | |
175 return ByteOrder.LITTLE_ENDIAN; | |
176 } | |
177 }, | |
178 BIG { | |
179 @Override | |
180 public short readShort(InputStream stream) throws IOException { | |
181 final int high = readByte(stream); | |
182 final int low = readByte(stream) & 0xff; | |
183 return (short) ((high << 8) | low); | |
184 } | |
185 | |
186 @Override | |
187 public int readInt(InputStream stream) throws IOException { | |
188 final int high = readShort(stream); | |
189 final int low = readShort(stream) & 0xffff; | |
190 return (high << 16) | low; | |
191 } | |
192 | |
193 @Override | |
194 public long readLong(InputStream stream) throws IOException { | |
195 final long high = readInt(stream); | |
196 final long low = readInt(stream) & 0xffffffffL; | |
197 return (high << 32) | low; | |
198 } | |
199 | |
200 @Override | |
201 public void writeShort(OutputStream stream, short value) throws IOException { | |
202 stream.write((value >> 8) & 0xff); | |
203 stream.write(value & 0xff); | |
204 } | |
205 | |
206 @Override | |
207 public void writeInt(OutputStream stream, int value) throws IOException { | |
208 stream.write((value >> 24) & 0xff); | |
209 stream.write((value >> 16) & 0xff); | |
210 stream.write((value >> 8) & 0xff); | |
211 stream.write(value & 0xff); | |
212 } | |
213 | |
214 @Override | |
215 public void writeLong(OutputStream stream, long value) throws IOException { | |
216 stream.write((int) (value >> 56) & 0xff); | |
217 stream.write((int) (value >> 48) & 0xff); | |
218 stream.write((int) (value >> 40) & 0xff); | |
219 stream.write((int) (value >> 32) & 0xff); | |
220 stream.write((int) (value >> 24) & 0xff); | |
221 stream.write((int) (value >> 16) & 0xff); | |
222 stream.write((int) (value >> 8) & 0xff); | |
223 stream.write((int) value & 0xff); | |
224 } | |
225 | |
226 @Override | |
227 public byte[] toBytes(short value) { | |
228 short v = value; | |
229 final byte[] bytes = new byte[2]; | |
230 bytes[1] = (byte) (v & 0xff); | |
231 v >>= 8; | |
232 bytes[0] = (byte) (v & 0xff); | |
233 return bytes; | |
234 } | |
235 | |
236 @Override | |
237 public byte[] toBytes(int value) { | |
238 int v = value; | |
239 final byte[] bytes = new byte[4]; | |
240 bytes[3] = (byte) (v & 0xff); | |
241 v >>= 8; | |
242 bytes[2] = (byte) (v & 0xff); | |
243 v >>= 8; | |
244 bytes[1] = (byte) (v & 0xff); | |
245 v >>= 8; | |
246 bytes[0] = (byte) (v & 0xff); | |
247 return bytes; | |
248 } | |
249 | |
250 @Override | |
251 public byte[] toBytes(long value) { | |
252 long v = value; | |
253 final byte[] bytes = new byte[8]; | |
254 bytes[7] = (byte) (v & 0xff); | |
255 v >>= 8; | |
256 bytes[6] = (byte) (v & 0xff); | |
257 v >>= 8; | |
258 bytes[5] = (byte) (v & 0xff); | |
259 v >>= 8; | |
260 bytes[4] = (byte) (v & 0xff); | |
261 v >>= 8; | |
262 bytes[3] = (byte) (v & 0xff); | |
263 v >>= 8; | |
264 bytes[2] = (byte) (v & 0xff); | |
265 v >>= 8; | |
266 bytes[1] = (byte) (v & 0xff); | |
267 v >>= 8; | |
268 bytes[0] = (byte) (v & 0xff); | |
269 return bytes; | |
270 } | |
271 | |
272 @Override | |
273 public void toBytes(short value, byte[] result, int offset) { | |
274 short v = value; | |
275 int toIndex = offset + Shorts.SIZE - 1; | |
276 for (int i = 1; i <= Shorts.SIZE && i <= result.length; i++) { | |
277 result[toIndex--] = (byte) (v & 0xff); | |
278 v >>= 8; | |
279 } | |
280 } | |
281 | |
282 @Override | |
283 public void toBytes(int value, byte[] result, int offset) { | |
284 int v = value; | |
285 int toIndex = offset + Ints.SIZE - 1; | |
286 for (int i = 1; i <= Ints.SIZE && i <= result.length; i++) { | |
287 result[toIndex--] = (byte) (v & 0xff); | |
288 v >>= 8; | |
289 } | |
290 } | |
291 | |
292 @Override | |
293 public void toBytes(long value, byte[] result, int offset) { | |
294 long v = value; | |
295 int toIndex = offset + Longs.SIZE - 1; | |
296 for (int i = 1; i <= Longs.SIZE && i <= result.length; i++) { | |
297 result[toIndex--] = (byte) (v & 0xff); | |
298 v >>= 8; | |
299 } | |
300 } | |
301 | |
302 @Override | |
303 public int offsetWithinWord(WordWidth wordWidth, WordWidth dataWidth) { | |
304 assert wordWidth.numberOfBytes >= dataWidth.numberOfBytes; | |
305 return wordWidth.numberOfBytes - dataWidth.numberOfBytes; | |
306 } | |
307 | |
308 @Override | |
309 public ByteOrder asByteOrder() { | |
310 return ByteOrder.BIG_ENDIAN; | |
311 } | |
312 }; | |
313 | |
314 @Override | |
315 public String toString() { | |
316 return name().toLowerCase(); | |
317 } | |
318 | |
319 public byte readByte(InputStream stream) throws IOException { | |
320 final int result = stream.read(); | |
321 if (result < 0) { | |
322 throw new IOException(); | |
323 } | |
324 return (byte) result; | |
325 } | |
326 | |
327 public abstract short readShort(InputStream stream) throws IOException; | |
328 | |
329 public abstract int readInt(InputStream stream) throws IOException; | |
330 | |
331 public abstract long readLong(InputStream stream) throws IOException; | |
332 | |
333 public abstract void writeShort(OutputStream stream, short value) throws IOException; | |
334 | |
335 public abstract void writeInt(OutputStream stream, int value) throws IOException; | |
336 | |
337 public abstract void writeLong(OutputStream stream, long value) throws IOException; | |
338 | |
339 public byte[] toBytes(byte value) { | |
340 final byte[] bytes = new byte[1]; | |
341 bytes[0] = value; | |
342 return bytes; | |
343 } | |
344 | |
345 public abstract byte[] toBytes(short value); | |
346 | |
347 public abstract byte[] toBytes(int value); | |
348 | |
349 public abstract byte[] toBytes(long value); | |
350 | |
351 public void toBytes(byte value, byte[] result, int offset) { | |
352 if (result.length > 0) { | |
353 result[0] = value; | |
354 } | |
355 } | |
356 | |
357 public abstract void toBytes(short value, byte[] result, int offset); | |
358 | |
359 public abstract void toBytes(int value, byte[] result, int offset); | |
360 | |
361 public abstract void toBytes(long value, byte[] result, int offset); | |
362 | |
363 public abstract int offsetWithinWord(WordWidth wordWith, WordWidth dataWidth); | |
364 | |
365 public abstract ByteOrder asByteOrder(); | |
366 } |