/Users/lyon/j4p/src/javassist/bytecode/ByteArray.java

1    /* 
2     * Javassist, a Java-bytecode translator toolkit. 
3     * Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved. 
4     * 
5     * The contents of this file are subject to the Mozilla Public License Version 
6     * 1.1 (the "License"); you may not use this file except in compliance with 
7     * the License.  Alternatively, the contents of this file may be used under 
8     * the terms of the GNU Lesser General Public License Version 2.1 or later. 
9     * 
10    * Software distributed under the License is distributed on an "AS IS" basis, 
11    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
12    * for the specific language governing rights and limitations under the 
13    * License. 
14    */ 
15    
16   package javassist.bytecode; 
17    
18   /** 
19    * A collection of static methods for reading and writing a byte array. 
20    */ 
21   public class ByteArray { 
22       /** 
23        * Reads an unsigned 16bit integer at the index. 
24        */ 
25       public static int readU16bit(byte[] code, int index) { 
26           return ((code[index] & 0xff) << 8) | (code[index + 1] & 0xff); 
27       } 
28    
29       /** 
30        * Reads a signed 16bit integer at the index. 
31        */ 
32       public static int readS16bit(byte[] code, int index) { 
33           return (code[index] << 8) | (code[index + 1] & 0xff); 
34       } 
35    
36       /** 
37        * Writes a 16bit integer at the index. 
38        */ 
39       public static void write16bit(int value, byte[] code, int index) { 
40           code[index] = (byte) (value >>> 8); 
41           code[index + 1] = (byte) value; 
42       } 
43    
44       /** 
45        * Reads a 32bit integer at the index. 
46        */ 
47       public static int read32bit(byte[] code, int index) { 
48           return (code[index] << 24) | ((code[index + 1] & 0xff) << 16) 
49                   | ((code[index + 2] & 0xff) << 8) | (code[index + 3] & 0xff); 
50       } 
51    
52       /** 
53        * Writes a 32bit integer at the index. 
54        */ 
55       public static void write32bit(int value, byte[] code, int index) { 
56           code[index] = (byte) (value >>> 24); 
57           code[index + 1] = (byte) (value >>> 16); 
58           code[index + 2] = (byte) (value >>> 8); 
59           code[index + 3] = (byte) value; 
60       } 
61    
62       /** 
63        * Copies a 32bit integer. 
64        * 
65        * @param src       the source byte array. 
66        * @param isrc      the index into the source byte array. 
67        * @param dest      the destination byte array. 
68        * @param idest     the index into the destination byte array. 
69        */ 
70       static void copy32bit(byte[] src, int isrc, byte[] dest, int idest) { 
71           dest[idest] = src[isrc]; 
72           dest[idest + 1] = src[isrc + 1]; 
73           dest[idest + 2] = src[isrc + 2]; 
74           dest[idest + 3] = src[isrc + 3]; 
75       } 
76   } 
77