Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
ASM: a very small and fast Java bytecode manipulation framework Copyright (c) 2000-2007 INRIA, France Telecom All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 package org.objectweb.asm;

A dynamically extensible vector of bytes. This class is roughly equivalent to a DataOutputStream on top of a ByteArrayOutputStream, but is more efficient.

Author(s):
Eric Bruneton
 
 public class ByteVector {

    
The content of this vector.
 
     byte[] data;

    
Actual number of bytes in this vector.
 
     int length;

    
Constructs a new ByteVector with a default initial size.
 
     public ByteVector() {
          = new byte[64];
     }

    
Constructs a new ByteVector with the given initial size.

Parameters:
initialSize the initial size of the byte vector to be constructed.
 
     public ByteVector(final int initialSize) {
          = new byte[initialSize];
     }

    
Puts a byte into this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
b a byte.
Returns:
this byte vector.
 
     public ByteVector putByte(final int b) {
         int length = this.;
         if (length + 1 > .) {
             enlarge(1);
         }
         [length++] = (byteb;
         this. = length;
         return this;
     }

    
Puts two bytes into this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
b1 a byte.
b2 another byte.
Returns:
this byte vector.
 
     ByteVector put11(final int b1final int b2) {
         int length = this.;
         if (length + 2 > .) {
             enlarge(2);
         }
         byte[] data = this.;
         data[length++] = (byteb1;
        data[length++] = (byteb2;
        this. = length;
        return this;
    }

    
Puts a short into this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
s a short.
Returns:
this byte vector.
    public ByteVector putShort(final int s) {
        int length = this.;
        if (length + 2 > .) {
            enlarge(2);
        }
        byte[] data = this.;
        data[length++] = (byte) (s >>> 8);
        data[length++] = (bytes;
        this. = length;
        return this;
    }

    
Puts a byte and a short into this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
b a byte.
s a short.
Returns:
this byte vector.
    ByteVector put12(final int bfinal int s) {
        int length = this.;
        if (length + 3 > .) {
            enlarge(3);
        }
        byte[] data = this.;
        data[length++] = (byteb;
        data[length++] = (byte) (s >>> 8);
        data[length++] = (bytes;
        this. = length;
        return this;
    }

    
Puts an int into this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
i an int.
Returns:
this byte vector.
    public ByteVector putInt(final int i) {
        int length = this.;
        if (length + 4 > .) {
            enlarge(4);
        }
        byte[] data = this.;
        data[length++] = (byte) (i >>> 24);
        data[length++] = (byte) (i >>> 16);
        data[length++] = (byte) (i >>> 8);
        data[length++] = (bytei;
        this. = length;
        return this;
    }

    
Puts a long into this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
l a long.
Returns:
this byte vector.
    public ByteVector putLong(final long l) {
        int length = this.;
        if (length + 8 > .) {
            enlarge(8);
        }
        byte[] data = this.;
        int i = (int) (l >>> 32);
        data[length++] = (byte) (i >>> 24);
        data[length++] = (byte) (i >>> 16);
        data[length++] = (byte) (i >>> 8);
        data[length++] = (bytei;
        i = (intl;
        data[length++] = (byte) (i >>> 24);
        data[length++] = (byte) (i >>> 16);
        data[length++] = (byte) (i >>> 8);
        data[length++] = (bytei;
        this. = length;
        return this;
    }

    
Puts an UTF8 string into this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
s a String.
Returns:
this byte vector.
    public ByteVector putUTF8(final String s) {
        int charLength = s.length();
        if ( + 2 + charLength > .) {
            enlarge(2 + charLength);
        }
        int len = ;
        byte[] data = this.;
        // optimistic algorithm: instead of computing the byte length and then
        // serializing the string (which requires two loops), we assume the byte
        // length is equal to char length (which is the most frequent case), and
        // we start serializing the string right away. During the serialization,
        // if we find that this assumption is wrong, we continue with the
        // general method.
        data[len++] = (byte) (charLength >>> 8);
        data[len++] = (bytecharLength;
        for (int i = 0; i < charLength; ++i) {
            char c = s.charAt(i);
            if (c >= '\001' && c <= '\177') {
                data[len++] = (bytec;
            } else {
                int byteLength = i;
                for (int j = ij < charLength; ++j) {
                    c = s.charAt(j);
                    if (c >= '\001' && c <= '\177') {
                        byteLength++;
                    } else if (c > '\u07FF') {
                        byteLength += 3;
                    } else {
                        byteLength += 2;
                    }
                }
                data[] = (byte) (byteLength >>> 8);
                data[ + 1] = (bytebyteLength;
                if ( + 2 + byteLength > data.length) {
                     = len;
                    enlarge(2 + byteLength);
                    data = this.;
                }
                for (int j = ij < charLength; ++j) {
                    c = s.charAt(j);
                    if (c >= '\001' && c <= '\177') {
                        data[len++] = (bytec;
                    } else if (c > '\u07FF') {
                        data[len++] = (byte) (0xE0 | c >> 12 & 0xF);
                        data[len++] = (byte) (0x80 | c >> 6 & 0x3F);
                        data[len++] = (byte) (0x80 | c & 0x3F);
                    } else {
                        data[len++] = (byte) (0xC0 | c >> 6 & 0x1F);
                        data[len++] = (byte) (0x80 | c & 0x3F);
                    }
                }
                break;
            }
        }
         = len;
        return this;
    }

    
Puts an array of bytes into this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
b an array of bytes. May be null to put len null bytes into this byte vector.
off index of the fist byte of b that must be copied.
len number of bytes of b that must be copied.
Returns:
this byte vector.
    public ByteVector putByteArray(final byte[] bfinal int offfinal int len)
    {
        if ( + len > .) {
            enlarge(len);
        }
        if (b != null) {
            System.arraycopy(bofflen);
        }
         += len;
        return this;
    }

    
Enlarge this byte vector so that it can receive n more bytes.

Parameters:
size number of additional bytes that this byte vector should be able to receive.
    private void enlarge(final int size) {
        int length1 = 2 * .;
        int length2 =  + size;
        byte[] newData = new byte[length1 > length2 ? length1 : length2];
        System.arraycopy(, 0, newData, 0, );
         = newData;
    }
New to GrepCode? Check out our FAQ X