PageRenderTime 72ms CodeModel.GetById 50ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bsh/org/objectweb/asm/ByteVector.java

#
Java | 281 lines | 144 code | 29 blank | 108 comment | 23 complexity | 5d66f621a960a23629a53e3eb50258c1 MD5 | raw file
  1/***
  2 * ASM: a very small and fast Java bytecode manipulation framework
  3 * Copyright (C) 2000 INRIA, France Telecom
  4 * Copyright (C) 2002 France Telecom
  5 *
  6 * This library is free software; you can redistribute it and/or
  7 * modify it under the terms of the GNU Lesser General Public
  8 * License as published by the Free Software Foundation; either
  9 * version 2 of the License, or (at your option) any later version.
 10 *
 11 * This library is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14 * Lesser General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU Lesser General Public
 17 * License along with this library; if not, write to the Free Software
 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 19 *
 20 * Contact: Eric.Bruneton@rd.francetelecom.com
 21 *
 22 * Author: Eric Bruneton
 23 */
 24
 25package org.gjt.sp.jedit.bsh.org.objectweb.asm;
 26
 27/**
 28 * A dynamically extensible vector of bytes. This class is roughly equivalent to
 29 * a DataOutputStream on top of a ByteArrayOutputStream, but is more efficient.
 30 */
 31
 32final class ByteVector {
 33
 34  /**
 35   * The content of this vector.
 36   */
 37
 38  byte[] data;
 39
 40  /**
 41   * Actual number of bytes in this vector.
 42   */
 43
 44  int length;
 45
 46  /**
 47   * Constructs a new {@link ByteVector ByteVector} with a default initial size.
 48   */
 49
 50  public ByteVector () {
 51    data = new byte[64];
 52  }
 53
 54  /**
 55   * Constructs a new {@link ByteVector ByteVector} with the given initial size.
 56   *
 57   * @param initialSize the initial size of the byte vector to be constructed.
 58   */
 59
 60  public ByteVector (final int initialSize) {
 61    data = new byte[initialSize];
 62  }
 63
 64  /**
 65   * Puts a byte into this byte vector. The byte vector is automatically
 66   * enlarged if necessary.
 67   *
 68   * @param b a byte.
 69   * @return this byte vector.
 70   */
 71
 72  public ByteVector put1 (final int b) {
 73    int length = this.length;
 74    if (length + 1 > data.length) {
 75      enlarge(1);
 76    }
 77    data[length++] = (byte)b;
 78    this.length = length;
 79    return this;
 80  }
 81
 82  /**
 83   * Puts two bytes into this byte vector. The byte vector is automatically
 84   * enlarged if necessary.
 85   *
 86   * @param b1 a byte.
 87   * @param b2 another byte.
 88   * @return this byte vector.
 89   */
 90
 91  public ByteVector put11 (final int b1, final int b2) {
 92    int length = this.length;
 93    if (length + 2 > data.length) {
 94      enlarge(2);
 95    }
 96    byte[] data = this.data;
 97    data[length++] = (byte)b1;
 98    data[length++] = (byte)b2;
 99    this.length = length;
100    return this;
101  }
102
103  /**
104   * Puts a short into this byte vector. The byte vector is automatically
105   * enlarged if necessary.
106   *
107   * @param s a short.
108   * @return this byte vector.
109   */
110
111  public ByteVector put2 (final int s) {
112    int length = this.length;
113    if (length + 2 > data.length) {
114      enlarge(2);
115    }
116    byte[] data = this.data;
117    data[length++] = (byte)(s >>> 8);
118    data[length++] = (byte)s;
119    this.length = length;
120    return this;
121  }
122
123  /**
124   * Puts a byte and a short into this byte vector. The byte vector is
125   * automatically enlarged if necessary.
126   *
127   * @param b a byte.
128   * @param s a short.
129   * @return this byte vector.
130   */
131
132  public ByteVector put12 (final int b, final int s) {
133    int length = this.length;
134    if (length + 3 > data.length) {
135      enlarge(3);
136    }
137    byte[] data = this.data;
138    data[length++] = (byte)b;
139    data[length++] = (byte)(s >>> 8);
140    data[length++] = (byte)s;
141    this.length = length;
142    return this;
143  }
144
145  /**
146   * Puts an int into this byte vector. The byte vector is automatically
147   * enlarged if necessary.
148   *
149   * @param i an int.
150   * @return this byte vector.
151   */
152
153  public ByteVector put4 (final int i) {
154    int length = this.length;
155    if (length + 4 > data.length) {
156      enlarge(4);
157    }
158    byte[] data = this.data;
159    data[length++] = (byte)(i >>> 24);
160    data[length++] = (byte)(i >>> 16);
161    data[length++] = (byte)(i >>> 8);
162    data[length++] = (byte)i;
163    this.length = length;
164    return this;
165  }
166
167  /**
168   * Puts a long into this byte vector. The byte vector is automatically
169   * enlarged if necessary.
170   *
171   * @param l a long.
172   * @return this byte vector.
173   */
174
175  public ByteVector put8 (final long l) {
176    int length = this.length;
177    if (length + 8 > data.length) {
178      enlarge(8);
179    }
180    byte[] data = this.data;
181    int i = (int)(l >>> 32);
182    data[length++] = (byte)(i >>> 24);
183    data[length++] = (byte)(i >>> 16);
184    data[length++] = (byte)(i >>> 8);
185    data[length++] = (byte)i;
186    i = (int)l;
187    data[length++] = (byte)(i >>> 24);
188    data[length++] = (byte)(i >>> 16);
189    data[length++] = (byte)(i >>> 8);
190    data[length++] = (byte)i;
191    this.length = length;
192    return this;
193  }
194
195  /**
196   * Puts a String in UTF format into this byte vector. The byte vector is
197   * automatically enlarged if necessary.
198   *
199   * @param s a String.
200   * @return this byte vector.
201   */
202
203  public ByteVector putUTF (final String s) {
204    int charLength = s.length();
205    int byteLength = 0;
206    for (int i = 0; i < charLength; ++i) {
207      char c = s.charAt(i);
208      if (c >= '\001' && c <= '\177') {
209        byteLength++;
210      } else if (c > '\u07FF') {
211        byteLength += 3;
212      } else {
213        byteLength += 2;
214      }
215    }
216    if (byteLength > 65535) {
217      throw new IllegalArgumentException();
218    }
219    int length = this.length;
220    if (length + 2 + byteLength > data.length) {
221      enlarge(2 + byteLength);
222    }
223    byte[] data = this.data;
224    data[length++] = (byte)(byteLength >>> 8);
225    data[length++] = (byte)(byteLength);
226    for (int i = 0; i < charLength; ++i) {
227      char c = s.charAt(i);
228      if (c >= '\001' && c <= '\177') {
229        data[length++] = (byte)c;
230      } else if (c > '\u07FF') {
231        data[length++] = (byte)(0xE0 | c >> 12 & 0xF);
232        data[length++] = (byte)(0x80 | c >> 6 & 0x3F);
233        data[length++] = (byte)(0x80 | c & 0x3F);
234      } else {
235        data[length++] = (byte)(0xC0 | c >> 6 & 0x1F);
236        data[length++] = (byte)(0x80 | c & 0x3F);
237      }
238    }
239    this.length = length;
240    return this;
241  }
242
243  /**
244   * Puts an array of bytes into this byte vector. The byte vector is
245   * automatically enlarged if necessary.
246   *
247   * @param b an array of bytes. May be <tt>null</tt> to put <tt>len</tt> null
248   *      bytes into this byte vector.
249   * @param off index of the fist byte of b that must be copied.
250   * @param len number of bytes of b that must be copied.
251   * @return this byte vector.
252   */
253
254  public ByteVector putByteArray (
255    final byte[] b,
256    final int off,
257    final int len)
258  {
259    if (length + len > data.length) {
260      enlarge(len);
261    }
262    if (b != null) {
263      System.arraycopy(b, off, data, length, len);
264    }
265    length += len;
266    return this;
267  }
268
269  /**
270   * Enlarge this byte vector so that it can receive n more bytes.
271   *
272   * @param size number of additional bytes that this byte vector should be
273   *      able to receive.
274   */
275
276  private void enlarge (final int size) {
277    byte[] newData = new byte[Math.max(2*data.length, length + size)];
278    System.arraycopy(data, 0, newData, 0, length);
279    data = newData;
280  }
281}