View Javadoc
1   /*
2    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3    *
4    * This code is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License version 2 only, as
6    * published by the Free Software Foundation.  Oracle designates this
7    * particular file as subject to the "Classpath" exception as provided
8    * by Oracle in the LICENSE file that accompanied this code.
9    *
10   * This code is distributed in the hope that it will be useful, but WITHOUT
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13   * version 2 for more details (a copy is included in the LICENSE file that
14   * accompanied this code).
15   *
16   * You should have received a copy of the GNU General Public License version
17   * 2 along with this work; if not, write to the Free Software Foundation,
18   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19   *
20   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21   * or visit www.oracle.com if you need additional information or have any
22   * questions.
23   */
24  
25  /*
26   * This file is available under and governed by the GNU General Public
27   * License version 2 only, as published by the Free Software Foundation.
28   * However, the following notice accompanied the original version of this
29   * file:
30   *
31   * ASM: a very small and fast Java bytecode manipulation framework
32   * Copyright (c) 2000-2011 INRIA, France Telecom
33   * All rights reserved.
34   *
35   * Redistribution and use in source and binary forms, with or without
36   * modification, are permitted provided that the following conditions
37   * are met:
38   * 1. Redistributions of source code must retain the above copyright
39   *    notice, this list of conditions and the following disclaimer.
40   * 2. Redistributions in binary form must reproduce the above copyright
41   *    notice, this list of conditions and the following disclaimer in the
42   *    documentation and/or other materials provided with the distribution.
43   * 3. Neither the name of the copyright holders nor the names of its
44   *    contributors may be used to endorse or promote products derived from
45   *    this software without specific prior written permission.
46   *
47   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
57   * THE POSSIBILITY OF SUCH DAMAGE.
58   */
59  package jdk.internal.org.objectweb.asm;
60  
61  /**
62   * A dynamically extensible vector of bytes. This class is roughly equivalent to
63   * a DataOutputStream on top of a ByteArrayOutputStream, but is more efficient.
64   *
65   * @author Eric Bruneton
66   */
67  public class ByteVector {
68  
69      /**
70       * The content of this vector.
71       */
72      byte[] data;
73  
74      /**
75       * Actual number of bytes in this vector.
76       */
77      int length;
78  
79      /**
80       * Constructs a new {@link ByteVector ByteVector} with a default initial
81       * size.
82       */
83      public ByteVector() {
84          data = new byte[64];
85      }
86  
87      /**
88       * Constructs a new {@link ByteVector ByteVector} with the given initial
89       * size.
90       *
91       * @param initialSize
92       *            the initial size of the byte vector to be constructed.
93       */
94      public ByteVector(final int initialSize) {
95          data = new byte[initialSize];
96      }
97  
98      /**
99       * Puts a byte into this byte vector. The byte vector is automatically
100      * enlarged if necessary.
101      *
102      * @param b
103      *            a byte.
104      * @return this byte vector.
105      */
106     public ByteVector putByte(final int b) {
107         int length = this.length;
108         if (length + 1 > data.length) {
109             enlarge(1);
110         }
111         data[length++] = (byte) b;
112         this.length = length;
113         return this;
114     }
115 
116     /**
117      * Puts two bytes into this byte vector. The byte vector is automatically
118      * enlarged if necessary.
119      *
120      * @param b1
121      *            a byte.
122      * @param b2
123      *            another byte.
124      * @return this byte vector.
125      */
126     ByteVector put11(final int b1, final int b2) {
127         int length = this.length;
128         if (length + 2 > data.length) {
129             enlarge(2);
130         }
131         byte[] data = this.data;
132         data[length++] = (byte) b1;
133         data[length++] = (byte) b2;
134         this.length = length;
135         return this;
136     }
137 
138     /**
139      * Puts a short into this byte vector. The byte vector is automatically
140      * enlarged if necessary.
141      *
142      * @param s
143      *            a short.
144      * @return this byte vector.
145      */
146     public ByteVector putShort(final int s) {
147         int length = this.length;
148         if (length + 2 > data.length) {
149             enlarge(2);
150         }
151         byte[] data = this.data;
152         data[length++] = (byte) (s >>> 8);
153         data[length++] = (byte) s;
154         this.length = length;
155         return this;
156     }
157 
158     /**
159      * Puts a byte and a short into this byte vector. The byte vector is
160      * automatically enlarged if necessary.
161      *
162      * @param b
163      *            a byte.
164      * @param s
165      *            a short.
166      * @return this byte vector.
167      */
168     ByteVector put12(final int b, final int s) {
169         int length = this.length;
170         if (length + 3 > data.length) {
171             enlarge(3);
172         }
173         byte[] data = this.data;
174         data[length++] = (byte) b;
175         data[length++] = (byte) (s >>> 8);
176         data[length++] = (byte) s;
177         this.length = length;
178         return this;
179     }
180 
181     /**
182      * Puts an int into this byte vector. The byte vector is automatically
183      * enlarged if necessary.
184      *
185      * @param i
186      *            an int.
187      * @return this byte vector.
188      */
189     public ByteVector putInt(final int i) {
190         int length = this.length;
191         if (length + 4 > data.length) {
192             enlarge(4);
193         }
194         byte[] data = this.data;
195         data[length++] = (byte) (i >>> 24);
196         data[length++] = (byte) (i >>> 16);
197         data[length++] = (byte) (i >>> 8);
198         data[length++] = (byte) i;
199         this.length = length;
200         return this;
201     }
202 
203     /**
204      * Puts a long into this byte vector. The byte vector is automatically
205      * enlarged if necessary.
206      *
207      * @param l
208      *            a long.
209      * @return this byte vector.
210      */
211     public ByteVector putLong(final long l) {
212         int length = this.length;
213         if (length + 8 > data.length) {
214             enlarge(8);
215         }
216         byte[] data = this.data;
217         int i = (int) (l >>> 32);
218         data[length++] = (byte) (i >>> 24);
219         data[length++] = (byte) (i >>> 16);
220         data[length++] = (byte) (i >>> 8);
221         data[length++] = (byte) i;
222         i = (int) l;
223         data[length++] = (byte) (i >>> 24);
224         data[length++] = (byte) (i >>> 16);
225         data[length++] = (byte) (i >>> 8);
226         data[length++] = (byte) i;
227         this.length = length;
228         return this;
229     }
230 
231     /**
232      * Puts an UTF8 string into this byte vector. The byte vector is
233      * automatically enlarged if necessary.
234      *
235      * @param s
236      *            a String whose UTF8 encoded length must be less than 65536.
237      * @return this byte vector.
238      */
239     public ByteVector putUTF8(final String s) {
240         int charLength = s.length();
241         if (charLength > 65535) {
242             throw new IllegalArgumentException();
243         }
244         int len = length;
245         if (len + 2 + charLength > data.length) {
246             enlarge(2 + charLength);
247         }
248         byte[] data = this.data;
249         // optimistic algorithm: instead of computing the byte length and then
250         // serializing the string (which requires two loops), we assume the byte
251         // length is equal to char length (which is the most frequent case), and
252         // we start serializing the string right away. During the serialization,
253         // if we find that this assumption is wrong, we continue with the
254         // general method.
255         data[len++] = (byte) (charLength >>> 8);
256         data[len++] = (byte) charLength;
257         for (int i = 0; i < charLength; ++i) {
258             char c = s.charAt(i);
259             if (c >= '\001' && c <= '\177') {
260                 data[len++] = (byte) c;
261             } else {
262                 int byteLength = i;
263                 for (int j = i; j < charLength; ++j) {
264                     c = s.charAt(j);
265                     if (c >= '\001' && c <= '\177') {
266                         byteLength++;
267                     } else if (c > '\u07FF') {
268                         byteLength += 3;
269                     } else {
270                         byteLength += 2;
271                     }
272                 }
273                 if (byteLength > 65535) {
274                     throw new IllegalArgumentException();
275                 }
276                 data[length] = (byte) (byteLength >>> 8);
277                 data[length + 1] = (byte) byteLength;
278                 if (length + 2 + byteLength > data.length) {
279                     length = len;
280                     enlarge(2 + byteLength);
281                     data = this.data;
282                 }
283                 for (int j = i; j < charLength; ++j) {
284                     c = s.charAt(j);
285                     if (c >= '\001' && c <= '\177') {
286                         data[len++] = (byte) c;
287                     } else if (c > '\u07FF') {
288                         data[len++] = (byte) (0xE0 | c >> 12 & 0xF);
289                         data[len++] = (byte) (0x80 | c >> 6 & 0x3F);
290                         data[len++] = (byte) (0x80 | c & 0x3F);
291                     } else {
292                         data[len++] = (byte) (0xC0 | c >> 6 & 0x1F);
293                         data[len++] = (byte) (0x80 | c & 0x3F);
294                     }
295                 }
296                 break;
297             }
298         }
299         length = len;
300         return this;
301     }
302 
303     /**
304      * Puts an array of bytes into this byte vector. The byte vector is
305      * automatically enlarged if necessary.
306      *
307      * @param b
308      *            an array of bytes. May be <tt>null</tt> to put <tt>len</tt>
309      *            null bytes into this byte vector.
310      * @param off
311      *            index of the fist byte of b that must be copied.
312      * @param len
313      *            number of bytes of b that must be copied.
314      * @return this byte vector.
315      */
316     public ByteVector putByteArray(final byte[] b, final int off, final int len) {
317         if (length + len > data.length) {
318             enlarge(len);
319         }
320         if (b != null) {
321             System.arraycopy(b, off, data, length, len);
322         }
323         length += len;
324         return this;
325     }
326 
327     /**
328      * Enlarge this byte vector so that it can receive n more bytes.
329      *
330      * @param size
331      *            number of additional bytes that this byte vector should be
332      *            able to receive.
333      */
334     private void enlarge(final int size) {
335         int length1 = 2 * data.length;
336         int length2 = length + size;
337         byte[] newData = new byte[length1 > length2 ? length1 : length2];
338         System.arraycopy(data, 0, newData, 0, length);
339         data = newData;
340     }
341 }