001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (C) 2000 INRIA, France Telecom
004: * Copyright (C) 2002 France Telecom
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * Contact: Eric.Bruneton@rd.francetelecom.com
021: *
022: * Author: Eric Bruneton
023: */package bsh.org.objectweb.asm;
024:
025: /**
026: * A dynamically extensible vector of bytes. This class is roughly equivalent to
027: * a DataOutputStream on top of a ByteArrayOutputStream, but is more efficient.
028: */
029:
030: final class ByteVector {
031:
032: /**
033: * The content of this vector.
034: */
035:
036: byte[] data;
037:
038: /**
039: * Actual number of bytes in this vector.
040: */
041:
042: int length;
043:
044: /**
045: * Constructs a new {@link ByteVector ByteVector} with a default initial size.
046: */
047:
048: public ByteVector() {
049: data = new byte[64];
050: }
051:
052: /**
053: * Constructs a new {@link ByteVector ByteVector} with the given initial size.
054: *
055: * @param initialSize the initial size of the byte vector to be constructed.
056: */
057:
058: public ByteVector(final int initialSize) {
059: data = new byte[initialSize];
060: }
061:
062: /**
063: * Puts a byte into this byte vector. The byte vector is automatically
064: * enlarged if necessary.
065: *
066: * @param b a byte.
067: * @return this byte vector.
068: */
069:
070: public ByteVector put1(final int b) {
071: int length = this .length;
072: if (length + 1 > data.length) {
073: enlarge(1);
074: }
075: data[length++] = (byte) b;
076: this .length = length;
077: return this ;
078: }
079:
080: /**
081: * Puts two bytes into this byte vector. The byte vector is automatically
082: * enlarged if necessary.
083: *
084: * @param b1 a byte.
085: * @param b2 another byte.
086: * @return this byte vector.
087: */
088:
089: public ByteVector put11(final int b1, final int b2) {
090: int length = this .length;
091: if (length + 2 > data.length) {
092: enlarge(2);
093: }
094: byte[] data = this .data;
095: data[length++] = (byte) b1;
096: data[length++] = (byte) b2;
097: this .length = length;
098: return this ;
099: }
100:
101: /**
102: * Puts a short into this byte vector. The byte vector is automatically
103: * enlarged if necessary.
104: *
105: * @param s a short.
106: * @return this byte vector.
107: */
108:
109: public ByteVector put2(final int s) {
110: int length = this .length;
111: if (length + 2 > data.length) {
112: enlarge(2);
113: }
114: byte[] data = this .data;
115: data[length++] = (byte) (s >>> 8);
116: data[length++] = (byte) s;
117: this .length = length;
118: return this ;
119: }
120:
121: /**
122: * Puts a byte and a short into this byte vector. The byte vector is
123: * automatically enlarged if necessary.
124: *
125: * @param b a byte.
126: * @param s a short.
127: * @return this byte vector.
128: */
129:
130: public ByteVector put12(final int b, final int s) {
131: int length = this .length;
132: if (length + 3 > data.length) {
133: enlarge(3);
134: }
135: byte[] data = this .data;
136: data[length++] = (byte) b;
137: data[length++] = (byte) (s >>> 8);
138: data[length++] = (byte) s;
139: this .length = length;
140: return this ;
141: }
142:
143: /**
144: * Puts an int into this byte vector. The byte vector is automatically
145: * enlarged if necessary.
146: *
147: * @param i an int.
148: * @return this byte vector.
149: */
150:
151: public ByteVector put4(final int i) {
152: int length = this .length;
153: if (length + 4 > data.length) {
154: enlarge(4);
155: }
156: byte[] data = this .data;
157: data[length++] = (byte) (i >>> 24);
158: data[length++] = (byte) (i >>> 16);
159: data[length++] = (byte) (i >>> 8);
160: data[length++] = (byte) i;
161: this .length = length;
162: return this ;
163: }
164:
165: /**
166: * Puts a long into this byte vector. The byte vector is automatically
167: * enlarged if necessary.
168: *
169: * @param l a long.
170: * @return this byte vector.
171: */
172:
173: public ByteVector put8(final long l) {
174: int length = this .length;
175: if (length + 8 > data.length) {
176: enlarge(8);
177: }
178: byte[] data = this .data;
179: int i = (int) (l >>> 32);
180: data[length++] = (byte) (i >>> 24);
181: data[length++] = (byte) (i >>> 16);
182: data[length++] = (byte) (i >>> 8);
183: data[length++] = (byte) i;
184: i = (int) l;
185: data[length++] = (byte) (i >>> 24);
186: data[length++] = (byte) (i >>> 16);
187: data[length++] = (byte) (i >>> 8);
188: data[length++] = (byte) i;
189: this .length = length;
190: return this ;
191: }
192:
193: /**
194: * Puts a String in UTF format into this byte vector. The byte vector is
195: * automatically enlarged if necessary.
196: *
197: * @param s a String.
198: * @return this byte vector.
199: */
200:
201: public ByteVector putUTF(final String s) {
202: int charLength = s.length();
203: int byteLength = 0;
204: for (int i = 0; i < charLength; ++i) {
205: char c = s.charAt(i);
206: if (c >= '\001' && c <= '\177') {
207: byteLength++;
208: } else if (c > '\u07FF') {
209: byteLength += 3;
210: } else {
211: byteLength += 2;
212: }
213: }
214: if (byteLength > 65535) {
215: throw new IllegalArgumentException();
216: }
217: int length = this .length;
218: if (length + 2 + byteLength > data.length) {
219: enlarge(2 + byteLength);
220: }
221: byte[] data = this .data;
222: data[length++] = (byte) (byteLength >>> 8);
223: data[length++] = (byte) (byteLength);
224: for (int i = 0; i < charLength; ++i) {
225: char c = s.charAt(i);
226: if (c >= '\001' && c <= '\177') {
227: data[length++] = (byte) c;
228: } else if (c > '\u07FF') {
229: data[length++] = (byte) (0xE0 | c >> 12 & 0xF);
230: data[length++] = (byte) (0x80 | c >> 6 & 0x3F);
231: data[length++] = (byte) (0x80 | c & 0x3F);
232: } else {
233: data[length++] = (byte) (0xC0 | c >> 6 & 0x1F);
234: data[length++] = (byte) (0x80 | c & 0x3F);
235: }
236: }
237: this .length = length;
238: return this ;
239: }
240:
241: /**
242: * Puts an array of bytes into this byte vector. The byte vector is
243: * automatically enlarged if necessary.
244: *
245: * @param b an array of bytes. May be <tt>null</tt> to put <tt>len</tt> null
246: * bytes into this byte vector.
247: * @param off index of the fist byte of b that must be copied.
248: * @param len number of bytes of b that must be copied.
249: * @return this byte vector.
250: */
251:
252: public ByteVector putByteArray(final byte[] b, final int off,
253: final int len) {
254: if (length + len > data.length) {
255: enlarge(len);
256: }
257: if (b != null) {
258: System.arraycopy(b, off, data, length, len);
259: }
260: length += len;
261: return this ;
262: }
263:
264: /**
265: * Enlarge this byte vector so that it can receive n more bytes.
266: *
267: * @param size number of additional bytes that this byte vector should be
268: * able to receive.
269: */
270:
271: private void enlarge(final int size) {
272: byte[] newData = new byte[Math.max(2 * data.length, length
273: + size)];
274: System.arraycopy(data, 0, newData, 0, length);
275: data = newData;
276: }
277: }
|