169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/* 269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Javassist, a Java-bytecode translator toolkit. 369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved. 469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The contents of this file are subject to the Mozilla Public License Version 669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1.1 (the "License"); you may not use this file except in compliance with 769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the License. Alternatively, the contents of this file may be used under 869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the terms of the GNU Lesser General Public License Version 2.1 or later. 969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Software distributed under the License is distributed on an "AS IS" basis, 1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * for the specific language governing rights and limitations under the 1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * License. 1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpackage javassist.bytecode; 1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/** 1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * A collection of static methods for reading and writing a byte array. 2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpublic class ByteArray { 2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Reads an unsigned 16bit integer at the index. 2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public static int readU16bit(byte[] code, int index) { 2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return ((code[index] & 0xff) << 8) | (code[index + 1] & 0xff); 2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Reads a signed 16bit integer at the index. 3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public static int readS16bit(byte[] code, int index) { 3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return (code[index] << 8) | (code[index + 1] & 0xff); 3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Writes a 16bit integer at the index. 3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public static void write16bit(int value, byte[] code, int index) { 4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code[index] = (byte)(value >>> 8); 4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code[index + 1] = (byte)value; 4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Reads a 32bit integer at the index. 4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public static int read32bit(byte[] code, int index) { 4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return (code[index] << 24) | ((code[index + 1] & 0xff) << 16) 4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal | ((code[index + 2] & 0xff) << 8) | (code[index + 3] & 0xff); 5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Writes a 32bit integer at the index. 5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public static void write32bit(int value, byte[] code, int index) { 5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code[index] = (byte)(value >>> 24); 5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code[index + 1] = (byte)(value >>> 16); 5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code[index + 2] = (byte)(value >>> 8); 5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code[index + 3] = (byte)value; 6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Copies a 32bit integer. 6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param src the source byte array. 6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param isrc the index into the source byte array. 6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param dest the destination byte array. 6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param idest the index into the destination byte array. 6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal static void copy32bit(byte[] src, int isrc, byte[] dest, int idest) { 7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal dest[idest] = src[isrc]; 7269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal dest[idest + 1] = src[isrc + 1]; 7369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal dest[idest + 2] = src[isrc + 2]; 7469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal dest[idest + 3] = src[isrc + 3]; 7569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 7669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal} 77