• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Javassist, a Java-bytecode translator toolkit.
3  * Copyright (C) 1999- 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  * or the Apache License Version 2.0.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  */
16 
17 package javassist.bytecode;
18 
19 /**
20  * A collection of static methods for reading and writing a byte array.
21  */
22 public class ByteArray {
23     /**
24      * Reads an unsigned 16bit integer at the index.
25      */
readU16bit(byte[] code, int index)26     public static int readU16bit(byte[] code, int index) {
27         return ((code[index] & 0xff) << 8) | (code[index + 1] & 0xff);
28     }
29 
30     /**
31      * Reads a signed 16bit integer at the index.
32      */
readS16bit(byte[] code, int index)33     public static int readS16bit(byte[] code, int index) {
34         return (code[index] << 8) | (code[index + 1] & 0xff);
35     }
36 
37     /**
38      * Writes a 16bit integer at the index.
39      */
write16bit(int value, byte[] code, int index)40     public static void write16bit(int value, byte[] code, int index) {
41         code[index] = (byte)(value >>> 8);
42         code[index + 1] = (byte)value;
43     }
44 
45     /**
46      * Reads a 32bit integer at the index.
47      */
read32bit(byte[] code, int index)48     public static int read32bit(byte[] code, int index) {
49         return (code[index] << 24) | ((code[index + 1] & 0xff) << 16)
50                | ((code[index + 2] & 0xff) << 8) | (code[index + 3] & 0xff);
51     }
52 
53     /**
54      * Writes a 32bit integer at the index.
55      */
write32bit(int value, byte[] code, int index)56     public static void write32bit(int value, byte[] code, int index) {
57         code[index] = (byte)(value >>> 24);
58         code[index + 1] = (byte)(value >>> 16);
59         code[index + 2] = (byte)(value >>> 8);
60         code[index + 3] = (byte)value;
61     }
62 
63     /**
64      * Copies a 32bit integer.
65      *
66      * @param src       the source byte array.
67      * @param isrc      the index into the source byte array.
68      * @param dest      the destination byte array.
69      * @param idest     the index into the destination byte array.
70      */
copy32bit(byte[] src, int isrc, byte[] dest, int idest)71     static void copy32bit(byte[] src, int isrc, byte[] dest, int idest) {
72         dest[idest] = src[isrc];
73         dest[idest + 1] = src[isrc + 1];
74         dest[idest + 2] = src[isrc + 2];
75         dest[idest + 3] = src[isrc + 3];
76     }
77 }
78