01: /*
02: * Javassist, a Java-bytecode translator toolkit.
03: * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
04: *
05: * The contents of this file are subject to the Mozilla Public License Version
06: * 1.1 (the "License"); you may not use this file except in compliance with
07: * the License. Alternatively, the contents of this file may be used under
08: * the terms of the GNU Lesser General Public License Version 2.1 or later.
09: *
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)
50: | (code[index + 3] & 0xff);
51: }
52:
53: /**
54: * Writes a 32bit integer at the index.
55: */
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: */
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: }
|