org.ow2.asmdex.lowLevelUtils
Class ByteVector

java.lang.Object
  extended by org.ow2.asmdex.lowLevelUtils.ByteVector

public class ByteVector
extends java.lang.Object

A dynamically extensible vector of bytes. This class is roughly equivalent to a DataOutputStream on top of a ByteArrayOutputStream, but is more efficient. NOTE : - putMUTF8() uses the same algorithm as "modified" UTF8 from Java (and ASM), with a removed the size at the beginning and a 0 at the end.

Author:
Eric Bruneton, Julien Névo (change to little-endian, addition of putULeb128 and putULeb128p1 methods, possibility to write at random access, addPadding, putSleb128, UTF8 to MUTF8).

Constructor Summary
ByteVector()
          Constructs a new ByteVector with a default initial size.
ByteVector(byte[] bytes)
          Constructs a new ByteVector with the given byte array.
ByteVector(int initialSize)
          Constructs a new ByteVector with the given initial size.
 
Method Summary
 void addPadding()
          Adds the default padding of 4 bytes at the end of the file, if necessary.
 void addPadding(int alignment)
          Adds padding at the end of the file, if necessary.
 byte[] getBuffer()
          Returns the whole buffer, which contains the data but also the blank bytes that (probably) follows.
 byte[] getData()
          Returns the data actually written in this vector.
 int getLength()
          Returns the number of bytes this vector contains.
 ByteVector putByte(int b)
          Puts a byte into this byte vector.
 ByteVector putByte(int b, int position)
          Puts a byte into this byte vector at the given position.
 ByteVector putByteArray(byte[] b)
          Puts an array of bytes into this byte vector.
 ByteVector putByteArray(byte[] b, int position)
          Puts an array of bytes into this byte vector, at the given position.
 ByteVector putByteArray(byte[] b, int off, int len)
          Puts an array of bytes into this byte vector.
 ByteVector putByteVector(ByteVector bv)
          Puts a Byte Vector at the end of this byte vector.
 ByteVector putByteVector(ByteVector bv, int position)
          Puts a Byte Vector into this byte vector at the given position.
 ByteVector putInt(int i)
          Puts an int into this byte vector.
 ByteVector putInt(int i, int position)
          Puts an int into this byte vector at the given position.
 ByteVector putLong(long l)
          Puts a long into this byte vector.
 ByteVector putMUTF8(java.lang.String s)
          Puts a MUTF8 string into this byte vector.
 ByteVector putShort(int s)
          Puts a short into this byte vector.
 ByteVector putShort(int s, int position)
          The byte vector is not enlarged if necessary.
 ByteVector putSleb128(int s)
          Puts a SLeb128 number into this byte vector.
 ByteVector putUleb128(int s)
          Puts a ULeb128 number into this byte vector.
 ByteVector putUleb128p1(int s)
          Puts a ULeb128p1 number into this byte vector.
 ByteVector putUTF8(java.lang.String s)
          Puts an UTF8 string into this byte vector.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteVector

public ByteVector()
Constructs a new ByteVector with a default initial size.


ByteVector

public ByteVector(int initialSize)
Constructs a new ByteVector with the given initial size.

Parameters:
initialSize - the initial size of the byte vector to be constructed.

ByteVector

public ByteVector(byte[] bytes)
Constructs a new ByteVector with the given byte array. The byte array is NOT copied.

Parameters:
bytes - the byte array.
Method Detail

getData

public byte[] getData()
Returns the data actually written in this vector.

Returns:
the data actually written in this vector.

getBuffer

public byte[] getBuffer()
Returns the whole buffer, which contains the data but also the blank bytes that (probably) follows. Use this to get the data without wanting to copy them, and use getLength() to treat only what is necessary.

Returns:
the whole buffer, which contains the data but also the blank bytes that (probably) follows.

getLength

public int getLength()
Returns the number of bytes this vector contains.

Returns:
the number of bytes this vector contains.

putByte

public ByteVector putByte(int b)
Puts a byte into this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
b - a byte.
Returns:
this byte vector.

putByte

public ByteVector putByte(int b,
                          int position)
Puts a byte into this byte vector at the given position. The byte vector is not enlarged if necessary.

Parameters:
b - a byte.
position - position in byte in the byte vector.
Returns:
this byte vector.

putByteVector

public ByteVector putByteVector(ByteVector bv)
Puts a Byte Vector at the end of this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
bv - the byte vector.
Returns:
this byte vector.

putByteVector

public ByteVector putByteVector(ByteVector bv,
                                int position)
Puts a Byte Vector into this byte vector at the given position. The byte vector is not enlarged if necessary.

Parameters:
bv - the byte vector.
position - position in byte in the byte vector.
Returns:
this byte vector.

putShort

public ByteVector putShort(int s)
Puts a short into this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
s - a short.
Returns:
this byte vector.

putShort

public ByteVector putShort(int s,
                           int position)
The byte vector is not enlarged if necessary.

Parameters:
s - an int.
position - position in byte in the byte vector.
Returns:
this byte vector.

putUleb128

public ByteVector putUleb128(int s)
Puts a ULeb128 number into this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
s - an int.
Returns:
this byte vector.

putUleb128p1

public ByteVector putUleb128p1(int s)
Puts a ULeb128p1 number into this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
s - an int.
Returns:
this byte vector.

putSleb128

public ByteVector putSleb128(int s)
Puts a SLeb128 number into this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
s - an int.
Returns:
this byte vector.

putInt

public ByteVector putInt(int i)
Puts an int into this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
i - an int.
Returns:
this byte vector.

putInt

public ByteVector putInt(int i,
                         int position)
Puts an int into this byte vector at the given position. The byte vector is not enlarged if necessary.

Parameters:
i - an int.
position - position in byte in the byte vector.
Returns:
this byte vector.

putLong

public ByteVector putLong(long l)
Puts a long into this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
l - a long.
Returns:
this byte vector.

putUTF8

public ByteVector putUTF8(java.lang.String s)
Puts an UTF8 string into this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
s - a String.
Returns:
this byte vector.

putMUTF8

public ByteVector putMUTF8(java.lang.String s)
Puts a MUTF8 string into this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
s - a String.
Returns:
this byte vector.

putByteArray

public ByteVector putByteArray(byte[] b,
                               int off,
                               int len)
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.

putByteArray

public ByteVector putByteArray(byte[] b)
Puts an array of bytes into this byte vector. The byte vector is automatically enlarged if necessary.

Parameters:
b - an array of bytes.
Returns:
this byte vector.

putByteArray

public ByteVector putByteArray(byte[] b,
                               int position)
Puts an array of bytes into this byte vector, at the given position. The byte vector is not enlarged if necessary.

Parameters:
b - an array of bytes.
position - position in bytes in the vector.
Returns:
this byte vector.

addPadding

public void addPadding(int alignment)
Adds padding at the end of the file, if necessary.

Parameters:
alignment - alignment of the padding (between 1 and 4).

addPadding

public void addPadding()
Adds the default padding of 4 bytes at the end of the file, if necessary.