comparison graal/com.oracle.max.cri/src/com/sun/cri/bytecode/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) 2009, 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.cri.bytecode;
24
25 /**
26 * A collection of utility methods for dealing with bytes, particularly in byte arrays.
27 */
28 public class Bytes {
29 /**
30 * Gets a signed 1-byte value.
31 * @param data the array containing the data
32 * @param bci the start index of the value to retrieve
33 * @return the signed 1-byte value at index {@code bci} in array {@code data}
34 */
35 public static int beS1(byte[] data, int bci) {
36 return data[bci];
37 }
38
39 /**
40 * Gets a signed 2-byte big-endian value.
41 * @param data the array containing the data
42 * @param bci the start index of the value to retrieve
43 * @return the signed 2-byte, big-endian, value at index {@code bci} in array {@code data}
44 */
45 public static int beS2(byte[] data, int bci) {
46 return (data[bci] << 8) | (data[bci + 1] & 0xff);
47 }
48
49 /**
50 * Gets an unsigned 1-byte value.
51 * @param data the array containing the data
52 * @param bci the start index of the value to retrieve
53 * @return the unsigned 1-byte value at index {@code bci} in array {@code data}
54 */
55 public static int beU1(byte[] data, int bci) {
56 return data[bci] & 0xff;
57 }
58
59 /**
60 * Gets an unsigned 2-byte big-endian value.
61 * @param data the array containing the data
62 * @param bci the start index of the value to retrieve
63 * @return the unsigned 2-byte, big-endian, value at index {@code bci} in array {@code data}
64 */
65 public static int beU2(byte[] data, int bci) {
66 return ((data[bci] & 0xff) << 8) | (data[bci + 1] & 0xff);
67 }
68
69 /**
70 * Gets a signed 4-byte big-endian value.
71 * @param data the array containing the data
72 * @param bci the start index of the value to retrieve
73 * @return the signed 4-byte, big-endian, value at index {@code bci} in array {@code data}
74 */
75 public static int beS4(byte[] data, int bci) {
76 return (data[bci] << 24) | ((data[bci + 1] & 0xff) << 16) | ((data[bci + 2] & 0xff) << 8) | (data[bci + 3] & 0xff);
77 }
78
79 /**
80 * Gets either a signed 2-byte or a signed 4-byte big-endian value.
81 * @param data the array containing the data
82 * @param bci the start index of the value to retrieve
83 * @param fourByte if true, this method will return a 4-byte value
84 * @return the signed 2 or 4-byte, big-endian, value at index {@code bci} in array {@code data}
85 */
86 public static int beSVar(byte[] data, int bci, boolean fourByte) {
87 if (fourByte) {
88 return beS4(data, bci);
89 } else {
90 return beS2(data, bci);
91 }
92 }
93 }