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      */
ByteVector()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      */
ByteVector(final int initialSize)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      */
putByte(final int b)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      */
put11(final int b1, final int b2)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      */
putShort(final int s)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      */
put12(final int b, final int s)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      */
putInt(final int i)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      */
putLong(final long l)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      */
putUTF8(final String s)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                 length = len;
263                 return encodeUTF8(s, i, 65535);
264             }
265         }
266         length = len;
267         return this;
268     }
269 
270     /**
271      * Puts an UTF8 string into this byte vector. The byte vector is
272      * automatically enlarged if necessary. The string length is encoded in two
273      * bytes before the encoded characters, if there is space for that (i.e. if
274      * this.length - i - 2 >= 0).
275      *
276      * @param s
277      *            the String to encode.
278      * @param i
279      *            the index of the first character to encode. The previous
280      *            characters are supposed to have already been encoded, using
281      *            only one byte per character.
282      * @param maxByteLength
283      *            the maximum byte length of the encoded string, including the
284      *            already encoded characters.
285      * @return this byte vector.
286      */
encodeUTF8(final String s, int i, int maxByteLength)287     ByteVector encodeUTF8(final String s, int i, int maxByteLength) {
288         int charLength = s.length();
289         int byteLength = i;
290         char c;
291         for (int j = i; j < charLength; ++j) {
292             c = s.charAt(j);
293             if (c >= '\001' && c <= '\177') {
294                 byteLength++;
295             } else if (c > '\u07FF') {
296                 byteLength += 3;
297             } else {
298                 byteLength += 2;
299             }
300         }
301         if (byteLength > maxByteLength) {
302             throw new IllegalArgumentException();
303         }
304         int start = length - i - 2;
305         if (start >= 0) {
306           data[start] = (byte) (byteLength >>> 8);
307           data[start + 1] = (byte) byteLength;
308         }
309         if (length + byteLength - i > data.length) {
310             enlarge(byteLength - i);
311         }
312         int len = length;
313         for (int j = i; j < charLength; ++j) {
314             c = s.charAt(j);
315             if (c >= '\001' && c <= '\177') {
316                 data[len++] = (byte) c;
317             } else if (c > '\u07FF') {
318                 data[len++] = (byte) (0xE0 | c >> 12 & 0xF);
319                 data[len++] = (byte) (0x80 | c >> 6 & 0x3F);
320                 data[len++] = (byte) (0x80 | c & 0x3F);
321             } else {
322                 data[len++] = (byte) (0xC0 | c >> 6 & 0x1F);
323                 data[len++] = (byte) (0x80 | c & 0x3F);
324             }
325         }
326         length = len;
327         return this;
328     }
329 
330     /**
331      * Puts an array of bytes into this byte vector. The byte vector is
332      * automatically enlarged if necessary.
333      *
334      * @param b
335      *            an array of bytes. May be <tt>null</tt> to put <tt>len</tt>
336      *            null bytes into this byte vector.
337      * @param off
338      *            index of the fist byte of b that must be copied.
339      * @param len
340      *            number of bytes of b that must be copied.
341      * @return this byte vector.
342      */
putByteArray(final byte[] b, final int off, final int len)343     public ByteVector putByteArray(final byte[] b, final int off, final int len) {
344         if (length + len > data.length) {
345             enlarge(len);
346         }
347         if (b != null) {
348             System.arraycopy(b, off, data, length, len);
349         }
350         length += len;
351         return this;
352     }
353 
354     /**
355      * Enlarge this byte vector so that it can receive n more bytes.
356      *
357      * @param size
358      *            number of additional bytes that this byte vector should be
359      *            able to receive.
360      */
enlarge(final int size)361     private void enlarge(final int size) {
362         int length1 = 2 * data.length;
363         int length2 = length + size;
364         byte[] newData = new byte[length1 > length2 ? length1 : length2];
365         System.arraycopy(data, 0, newData, 0, length);
366         data = newData;
367     }
368 }
369