001/* 002 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.bytecode; 024 025/** 026 * A collection of utility methods for dealing with bytes, particularly in byte arrays. 027 */ 028public class Bytes { 029 030 /** 031 * Gets a signed 1-byte value. 032 * 033 * @param data the array containing the data 034 * @param bci the start index of the value to retrieve 035 * @return the signed 1-byte value at index {@code bci} in array {@code data} 036 */ 037 public static int beS1(byte[] data, int bci) { 038 return data[bci]; 039 } 040 041 /** 042 * Gets a signed 2-byte big-endian value. 043 * 044 * @param data the array containing the data 045 * @param bci the start index of the value to retrieve 046 * @return the signed 2-byte, big-endian, value at index {@code bci} in array {@code data} 047 */ 048 public static int beS2(byte[] data, int bci) { 049 return (data[bci] << 8) | (data[bci + 1] & 0xff); 050 } 051 052 /** 053 * Gets an unsigned 1-byte value. 054 * 055 * @param data the array containing the data 056 * @param bci the start index of the value to retrieve 057 * @return the unsigned 1-byte value at index {@code bci} in array {@code data} 058 */ 059 public static int beU1(byte[] data, int bci) { 060 return data[bci] & 0xff; 061 } 062 063 /** 064 * Gets an unsigned 2-byte big-endian value. 065 * 066 * @param data the array containing the data 067 * @param bci the start index of the value to retrieve 068 * @return the unsigned 2-byte, big-endian, value at index {@code bci} in array {@code data} 069 */ 070 public static int beU2(byte[] data, int bci) { 071 return ((data[bci] & 0xff) << 8) | (data[bci + 1] & 0xff); 072 } 073 074 /** 075 * Gets a signed 4-byte big-endian value. 076 * 077 * @param data the array containing the data 078 * @param bci the start index of the value to retrieve 079 * @return the signed 4-byte, big-endian, value at index {@code bci} in array {@code data} 080 */ 081 public static int beS4(byte[] data, int bci) { 082 return (data[bci] << 24) | ((data[bci + 1] & 0xff) << 16) | ((data[bci + 2] & 0xff) << 8) | (data[bci + 3] & 0xff); 083 } 084 085 /** 086 * Gets either a signed 2-byte or a signed 4-byte big-endian value. 087 * 088 * @param data the array containing the data 089 * @param bci the start index of the value to retrieve 090 * @param fourByte if true, this method will return a 4-byte value 091 * @return the signed 2 or 4-byte, big-endian, value at index {@code bci} in array {@code data} 092 */ 093 public static int beSVar(byte[] data, int bci, boolean fourByte) { 094 if (fourByte) { 095 return beS4(data, bci); 096 } else { 097 return beS2(data, bci); 098 } 099 } 100}