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