FileDocCategorySizeDatePackage
ByteBuffer.javaAPI DocJava SE 5 API5904Fri Aug 26 14:54:22 BST 2005com.sun.corba.se.impl.ior

ByteBuffer

public class ByteBuffer extends Object

Fields Summary
protected byte[]
elementData
The array buffer into which the components of the ByteBuffer are stored. The capacity of the ByteBuffer is the length of this array buffer, and is at least large enough to contain all the ByteBuffer's elements.

Any array elements following the last element in the ByteBuffer are 0.

protected int
elementCount
The number of valid components in this ByteBuffer object. Components elementData[0] through elementData[elementCount-1] are the actual items.
protected int
capacityIncrement
The amount by which the capacity of the ByteBuffer is automatically incremented when its size becomes greater than its capacity. If the capacity increment is less than or equal to zero, the capacity of the ByteBuffer is doubled each time it needs to grow.
Constructors Summary
public ByteBuffer(int initialCapacity, int capacityIncrement)
Constructs an empty ByteBuffer with the specified initial capacity and capacity increment.

param
initialCapacity the initial capacity of the ByteBuffer.
param
capacityIncrement the amount by which the capacity is increased when the ByteBuffer overflows.
exception
IllegalArgumentException if the specified initial capacity is negative

	super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
	this.elementData = new byte[initialCapacity];
	this.capacityIncrement = capacityIncrement;
    
public ByteBuffer(int initialCapacity)
Constructs an empty ByteBuffer with the specified initial capacity and with its capacity increment equal to zero.

param
initialCapacity the initial capacity of the ByteBuffer.
exception
IllegalArgumentException if the specified initial capacity is negative

	this(initialCapacity, 0);
    
public ByteBuffer()
Constructs an empty ByteBuffer so that its internal data array has size 10 and its standard capacity increment is zero.

	this(200);
    
Methods Summary
public voidappend(int value)

	ensureCapacityHelper(elementCount + 4);
	doAppend( value ) ;
    
public voidappend(java.lang.String value)

	byte[] data = value.getBytes() ;
	ensureCapacityHelper( elementCount + data.length + 4 ) ;
	doAppend( data.length ) ;
	System.arraycopy( data, 0, elementData, elementCount, data.length ) ;
	elementCount += data.length ;
    
public voidappend(byte value)

	ensureCapacityHelper(elementCount + 1);
	elementData[elementCount++] = value;
    
public intcapacity()
Returns the current capacity of this ByteBuffer.

return
the current capacity (the length of its internal data arary, kept in the field elementData of this ByteBuffer.

	return elementData.length;
    
private voiddoAppend(int value)

	int current = value ;
	for (int ctr=0; ctr<4; ctr++) {
	    elementData[elementCount+ctr] = (byte)(current & 255) ;
	    current = current >> 8 ;
	}
	elementCount += 4 ;
    
private voidensureCapacityHelper(int minCapacity)
This implements the unsynchronized semantics of ensureCapacity. Synchronized methods in this class can internally call this method for ensuring capacity without incurring the cost of an extra synchronization.

see
java.util.ByteBuffer#ensureCapacity(int)

	int oldCapacity = elementData.length;
	if (minCapacity > oldCapacity) {
	    byte oldData[] = elementData;
	    int newCapacity = (capacityIncrement > 0) ?
		(oldCapacity + capacityIncrement) : (oldCapacity * 2);
    	    if (newCapacity < minCapacity) {
		newCapacity = minCapacity;
	    }
	    elementData = new byte[newCapacity];
	    System.arraycopy(oldData, 0, elementData, 0, elementCount);
	}
    
public booleanisEmpty()
Tests if this ByteBuffer has no components.

return
true if and only if this ByteBuffer has no components, that is, its size is zero; false otherwise.

	return elementCount == 0;
    
public intsize()
Returns the number of components in this ByteBuffer.

return
the number of components in this ByteBuffer.

	return elementCount;
    
public byte[]toArray()
Returns an array containing all of the elements in this ByteBuffer in the correct order.

since
1.2

	return elementData ;
    
public voidtrimToSize()
Trims the capacity of this ByteBuffer to be the ByteBuffer's current size. If the capacity of this cector is larger than its current size, then the capacity is changed to equal the size by replacing its internal data array, kept in the field elementData, with a smaller one. An application can use this operation to minimize the storage of a ByteBuffer.

	int oldCapacity = elementData.length;
	if (elementCount < oldCapacity) {
	    byte oldData[] = elementData;
	    elementData = new byte[elementCount];
	    System.arraycopy(oldData, 0, elementData, 0, elementCount);
	}