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