Extension SDK 10.1.2

oracle.ide.util
Class ByteBuffer

java.lang.Object
  extended byoracle.ide.util.ByteBuffer

public final class ByteBuffer
extends java.lang.Object

A byte buffer implements a mutable sequence of bytes. At any point in time it contains some particular sequence of bytes, but the length and content of the sequence can be changed through certain method calls.


Constructor Summary
ByteBuffer()
          Constructs a byte buffer with no bytes in it and an initial capacity of 16 bytes.
ByteBuffer(byte[] bytes)
          Constructs a byte buffer so that it represents the same sequence of bytes as the array argument; in other words, the initial contents of the byte buffer is a copy of the argument array.
ByteBuffer(int length)
          Constructs a byte buffer with no bytes in it and an initial capacity specified by the length argument.
 
Method Summary
 ByteBuffer append(byte[] bytes)
          Appends the contents of the byte array argument to this byte buffer.
 ByteBuffer append(byte[] bytes, int offset, int len)
          Appends a subarray of the byte array argument to this byte buffer.
 byte byteAt(int index)
          The specified byte of the sequence currently represented by the byte buffer, as indicated by the index argument, is returned.
 int capacity()
          Returns the current capacity of the byte buffer.
 ByteBuffer delete(int start, int end)
          Removes the bytes in a subarray of this ByteBuffer.
 ByteBuffer deleteByteAt(int index)
          Removes the byte at the specified position in this ByteBuffer (shortening the ByteBuffer by one byte).
 void ensureCapacity(int minimumCapacity)
          Ensures that the capacity of the buffer is at least equal to the specified minimum.
 void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
          Bytes are copied from this byte buffer into the destination byte array dst.
 ByteBuffer insert(int offset, byte[] bytes)
          Inserts the bytes of the bytes array argument into this byte buffer.
 ByteBuffer insert(int index, byte[] bytes, int offset, int len)
          Inserts the bytes of a subarray of the bytes array argument into this byte buffer.
 int length()
          Returns the length (byte count) of this byte buffer.
 ByteBuffer replace(int start, int end, byte[] bytes)
          Replaces the bytes in a subarray of this ByteBuffer with bytes in the specified array.
 ByteBuffer reverse()
          The byte sequence contained in this byte buffer is replaced by the reverse of the sequence.
 void setByteAt(int index, byte ch)
          The byte at the specified index of this byte buffer is set to ch.
 void setLength(int newLength)
          Sets the length of this byte buffer.
 byte[] subarray(int start)
          Returns a new byte array that contains a subsequence of bytes currently contained in this ByteBuffer.The subarray begins at the specified index and extends to the end of the ByteBuffer.
 byte[] subarray(int start, int end)
          Returns a new byte array that contains a subsequence of bytes currently contained in this ByteBuffer.
 byte[] toBytes()
          Converts to a byte array copy of the data in this byte buffer.
 java.lang.String toString()
          Converts to a string representing the data in this byte buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ByteBuffer

public ByteBuffer()
Constructs a byte buffer with no bytes in it and an initial capacity of 16 bytes.


ByteBuffer

public ByteBuffer(int length)
Constructs a byte buffer with no bytes in it and an initial capacity specified by the length argument.

Parameters:
length - the initial capacity.
Throws:
java.lang.NegativeArraySizeException - if the length argument is less than 0.

ByteBuffer

public ByteBuffer(byte[] bytes)
Constructs a byte buffer so that it represents the same sequence of bytes as the array argument; in other words, the initial contents of the byte buffer is a copy of the argument array. The initial capacity of the byte buffer is 16 plus the length of the array argument.

Parameters:
bytes - the initial contents of the buffer.
Method Detail

length

public int length()
Returns the length (byte count) of this byte buffer.

Returns:
the length of the sequence of bytes currently represented by this byte buffer.

capacity

public int capacity()
Returns the current capacity of the byte buffer. The capacity is the amount of storage available for newly inserted bytes; beyond which an allocation will occur.

Returns:
the current capacity of this byte buffer.

ensureCapacity

public void ensureCapacity(int minimumCapacity)
Ensures that the capacity of the buffer is at least equal to the specified minimum. If the current capacity of this byte buffer is less than the argument, then a new internal buffer is allocated with greater capacity. The new capacity is the larger of: If the minimumCapacity argument is nonpositive, this method takes no action and simply returns.

Parameters:
minimumCapacity - the minimum desired capacity.

setLength

public void setLength(int newLength)
Sets the length of this byte buffer. This byte buffer is altered to represent a new byte sequence whose length is specified by the argument. For every nonnegative index k less than newLength, the byte at index k in the new byte sequence is the same as the byte at index k in the old sequence if k is less than the length of the old byte sequence; otherwise, it is the null byte 0. In other words, if the newLength argument is less than the current length of the byte buffer, the byte buffer is truncated to contain exactly the number of bytes given by the newLength argument.

If the newLength argument is greater than or equal to the current length, sufficient null bytes are appended to the byte buffer so that length becomes the newLength argument.

The newLength argument must be greater than or equal to 0.

Parameters:
newLength - the new length of the buffer.
Throws:
java.lang.IndexOutOfBoundsException - if the newLength argument is negative.
See Also:
length()

byteAt

public byte byteAt(int index)
The specified byte of the sequence currently represented by the byte buffer, as indicated by the index argument, is returned. The first byte of a byte buffer is at index 0, the next at index 1, and so on, for array indexing.

The index argument must be greater than or equal to 0, and less than the length of this byte buffer.

Parameters:
index - the index of the desired byte.
Returns:
the byte at the specified index of this byte buffer.
Throws:
java.lang.IndexOutOfBoundsException - if index is negative or greater than or equal to length().
See Also:
length()

getBytes

public void getBytes(int srcBegin,
                     int srcEnd,
                     byte[] dst,
                     int dstBegin)
Bytes are copied from this byte buffer into the destination byte array dst. The first byte to be copied is at index srcBegin; the last byte to be copied is at index srcEnd-1. The total number of bytes to be copied is srcEnd-srcBegin. The bytes are copied into the subarray of dst starting at index dstBegin and ending at index:

 dstbegin + (srcEnd-srcBegin) - 1
 

Parameters:
srcBegin - start copying at this offset in the byte buffer.
srcEnd - stop copying at this offset in the byte buffer.
dst - the array to copy the data into.
dstBegin - offset into dst.
Throws:
java.lang.NullPointerException - if dst is null.
java.lang.IndexOutOfBoundsException - if any of the following is true:
  • srcBegin is negative
  • dstBegin is negative
  • the srcBegin argument is greater than the srcEnd argument.
  • srcEnd is greater than this.length(), the current length of this byte buffer.
  • dstBegin+srcEnd-srcBegin is greater than dst.length

setByteAt

public void setByteAt(int index,
                      byte ch)
The byte at the specified index of this byte buffer is set to ch. The byte buffer is altered to represent a new byte sequence that is identical to the old byte sequence, except that it contains the byte ch at position index.

The offset argument must be greater than or equal to 0, and less than the length of this byte buffer.

Parameters:
index - the index of the byte to modify.
ch - the new byte.
Throws:
java.lang.IndexOutOfBoundsException - if index is negative or greater than or equal to length().
See Also:
length()

append

public ByteBuffer append(byte[] bytes)
Appends the contents of the byte array argument to this byte buffer.

The bytes of the array argument are appended, in order, to the contents of this byte buffer. The length of this byte buffer increases by the length of the argument.

Parameters:
bytes - the bytes to be appended.
Returns:
a reference to this ByteBuffer object.

append

public ByteBuffer append(byte[] bytes,
                         int offset,
                         int len)
Appends a subarray of the byte array argument to this byte buffer.

bytes of the byte array bytes, starting at index offset, are appended, in order, to the contents of this byte buffer. The length of this byte buffer increases by the value of len.

Parameters:
bytes - the bytes to be appended.
offset - the index of the first byte to append.
len - the number of bytes to append.
Returns:
a reference to this ByteBuffer object.

delete

public ByteBuffer delete(int start,
                         int end)
Removes the bytes in a subarray of this ByteBuffer. The subarray begins at the specified start and extends to the byte at index end - 1 or to the end of the ByteBuffer if no such byte exists. If start is equal to end, no changes are made.

Parameters:
start - The beginning index, inclusive.
end - The ending index, exclusive.
Returns:
This byte buffer.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if start is negative, greater than length(), or greater than end.

deleteByteAt

public ByteBuffer deleteByteAt(int index)
Removes the byte at the specified position in this ByteBuffer (shortening the ByteBuffer by one byte).

Parameters:
index - Index of byte to remove
Returns:
This byte buffer.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index is negative or greater than or equal to length().

replace

public ByteBuffer replace(int start,
                          int end,
                          byte[] bytes)
Replaces the bytes in a subarray of this ByteBuffer with bytes in the specified array. The subarray begins at the specified start and extends to the byte at index end - 1 or to the end of the ByteBuffer if no such byte exists. First the bytes in the subarray are removed and then the specified array is inserted at start. (The ByteBuffer will be lengthened to accommodate the specified array if necessary.)

Parameters:
start - The beginning index, inclusive.
end - The ending index, exclusive.
bytes - Array that will replace previous contents.
Returns:
This byte buffer.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if start is negative, greater than length(), or greater than end.

subarray

public byte[] subarray(int start)
Returns a new byte array that contains a subsequence of bytes currently contained in this ByteBuffer.The subarray begins at the specified index and extends to the end of the ByteBuffer.

Parameters:
start - The beginning index, inclusive.
Returns:
The new array.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if start is less than zero, or greater than the length of this ByteBuffer.

subarray

public byte[] subarray(int start,
                       int end)
Returns a new byte array that contains a subsequence of bytes currently contained in this ByteBuffer. The subarray begins at the specified start and extends to the byte at index end - 1. An exception is thrown if

Parameters:
start - The beginning index, inclusive.
end - The ending index, exclusive.
Returns:
The new array.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if start or end are negative or greater than length(), or start is greater than end.

insert

public ByteBuffer insert(int index,
                         byte[] bytes,
                         int offset,
                         int len)
Inserts the bytes of a subarray of the bytes array argument into this byte buffer. The subarray begins at the specified offset and extends len bytes. The bytes of the subarray are inserted into this byte buffer at the position indicated by index. The length of this ByteBuffer increases by len bytes.

Parameters:
index - position at which to insert subarray.
bytes - A byte array.
offset - the index of the first byte in subarray to to be inserted.
len - the number of bytes in the subarray to to be inserted.
Returns:
This byte buffer.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if index is negative or greater than length(), or offset or len are negative, or (offset+len) is greater than bytes.length.

insert

public ByteBuffer insert(int offset,
                         byte[] bytes)
Inserts the bytes of the bytes array argument into this byte buffer.

The bytes of the array argument are inserted into the contents of this byte buffer at the position indicated by offset. The length of this byte buffer increases by the length of the argument.

Parameters:
offset - the offset.
bytes - a byte array.
Returns:
a reference to this ByteBuffer object.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the offset is invalid.

reverse

public ByteBuffer reverse()
The byte sequence contained in this byte buffer is replaced by the reverse of the sequence.

Let n be the length of the old byte sequence, the one contained in the byte buffer just prior to execution of the reverse method. Then the byte at index k in the new byte sequence is equal to the byte at index n-k-1 in the old byte sequence.

Returns:
a reference to this object..

toString

public java.lang.String toString()
Converts to a string representing the data in this byte buffer. A new String object is allocated and initialized to contain the byte sequence currently represented by this byte buffer. This String is then returned. Subsequent changes to the byte buffer do not affect the contents of the String.

Returns:
a string representation of the byte buffer.

toBytes

public byte[] toBytes()
Converts to a byte array copy of the data in this byte buffer. A new byte array is allocated and initialized to contain the byte sequence currently represented by this byte buffer. This array is then returned. Subsequent changes to the byte buffer do not affect the contents of the array.

Returns:
a byte array copy of the byte buffer.

Extension SDK

 

Copyright © 1997, 2004, Oracle. All rights reserved.