Return-Path:
capacity
is less than zero
+ */
+ public static ByteBuffer allocate(int capacity) {
+ if (capacity < 0) {
+ throw new IllegalArgumentException();
+ }
+ return BufferFactory.newByteBuffer(capacity);
+ }
+
+ /**
+ * Creates a direct byte buffer based on a new allocated memory block.
+ *
+ * @param capacity
+ * The capacity of the new buffer
+ * @return The created byte buffer
+ * @throws IllegalArgumentException
+ * If capacity
is less than zero
+ */
+ public static ByteBuffer allocateDirect(int capacity) {
+ if (capacity < 0) {
+ throw new IllegalArgumentException();
+ }
+ return BufferFactory.newDirectByteBuffer(capacity);
+ }
- /**
- * Creates a byte buffer based on a new allocated byte array.
- *
- * @param capacity
- * The capacity of the new buffer
- * @return The created byte buffer
- * @throws IllegalArgumentException
- * If capacity
is less than zero
- */
- public static ByteBuffer allocate(int capacity) {
- if (capacity < 0) {
- throw new IllegalArgumentException();
- }
- return BufferFactory.newByteBuffer(capacity);
- }
-
- /**
- * Creates a direct byte buffer based on a new allocated memory block.
- *
- * @param capacity
- * The capacity of the new buffer
- * @return The created byte buffer
- * @throws IllegalArgumentException
- * If capacity
is less than zero
- */
- public static ByteBuffer allocateDirect(int capacity) {
- if (capacity < 0) {
- throw new IllegalArgumentException();
- }
- return BufferFactory.newDirectByteBuffer(capacity);
- }
-
- /**
- * Creates a new byte buffer by wrapping the given byte array.
- *
- * Calling this method has the same effect as
- * wrap(array, 0, array.length)
.
- * The new buffer's position will be start
, limit will be
- * start + len
, capacity will be the length of the array.
- *
array.length
- * @param len
- * The length, must be no less than zero and no greater than
- * array.length - start
- * @return The created byte buffer
- * @exception IndexOutOfBoundsException
- * If either start
or len
is
- * invalid
- */
- public static ByteBuffer wrap(byte[] array, int start, int len) {
+ /**
+ * Creates a new byte buffer by wrapping the given byte array.
+ *
+ * Calling this method has the same effect as
+ * wrap(array, 0, array.length)
.
+ *
+ * The new buffer's position will be start
, limit will be
+ * start + len
, capacity will be the length of the array.
+ *
array.length
+ * @param len
+ * The length, must be no less than zero and no greater than
+ * array.length - start
+ * @return The created byte buffer
+ * @exception IndexOutOfBoundsException
+ * If either start
or len
is
+ * invalid
+ */
+ public static ByteBuffer wrap(byte[] array, int start, int len) {
int length = array.length;
if ((start < 0) || (len < 0) || ((long) start + (long) len > length)) {
throw new IndexOutOfBoundsException();
}
- ByteBuffer buf = BufferFactory.newByteBuffer(array);
- buf.position = start;
- buf.limit = start + len;
-
- return buf;
- }
-
- /**
- * The byte order of this buffer, default is BIG_ENDIAN
.
- */
- Endianness order = Endianness.BIG_ENDIAN;
-
- /**
- * Constructs a ByteBuffer
with given capacity.
- *
- * @param capacity The capacity of the buffer
- */
- ByteBuffer(int capacity) {
- super(capacity);
- }
-
- /**
- * Returns the byte array which this buffer is based on, if there's one.
- *
- * @return The byte array which this buffer is based on
- * @exception ReadOnlyBufferException
- * If this buffer is based on a readonly array
- * @exception UnsupportedOperationException
- * If this buffer is not based on an array
- */
- public final byte[] array() {
- return protectedArray();
- }
-
- /**
- * Returns the offset of the byte array which this buffer is based on, if
- * there's one.
- * - * The offset is the index of the array corresponds to the zero position of - * the buffer. - *
- * - * @return The offset of the byte array which this buffer is based on - * @exception ReadOnlyBufferException - * If this buffer is based on a readonly array - * @exception UnsupportedOperationException - * If this buffer is not based on an array - */ - public final int arrayOffset() { - return protectedArrayOffset(); - } - - /** - * Returns a char buffer which is based on the remaining content of - * this byte buffer. - *- * The new buffer's position is zero, its limit and capacity is - * the number of remaining bytes divided by two, and its mark is not set. - * The new buffer's readonly property and byte order are same as this - * buffer. The new buffer is direct, if this byte buffer is direct.
- *- * The new buffer shares content with this buffer, which means - * either buffer's change of content will be visible to the other. - * The two buffer's position, limit and mark are independent.
- * - * @return A char buffer which is based on the content of - * this byte buffer. - */ - public abstract CharBuffer asCharBuffer(); - - /** - * Returns a double buffer which is based on the remaining content of - * this byte buffer. - *- * The new buffer's position is zero, its limit and capacity is - * the number of remaining bytes divided by two, and its mark is not set. - * The new buffer's readonly property and byte order are same as this - * buffer. The new buffer is direct, if this byte buffer is direct.
- *- * The new buffer shares content with this buffer, which means - * either buffer's change of content will be visible to the other. - * The two buffer's position, limit and mark are independent.
- * - * @return A double buffer which is based on the content of - * this byte buffer. - */ - public abstract DoubleBuffer asDoubleBuffer(); - - /** - * Returns a float buffer which is based on the remaining content of - * this byte buffer. - *- * The new buffer's position is zero, its limit and capacity is - * the number of remaining bytes divided by two, and its mark is not set. - * The new buffer's readonly property and byte order are same as this - * buffer. The new buffer is direct, if this byte buffer is direct.
- *- * The new buffer shares content with this buffer, which means - * either buffer's change of content will be visible to the other. - * The two buffer's position, limit and mark are independent.
- * - * @return A float buffer which is based on the content of - * this byte buffer. - */ - public abstract FloatBuffer asFloatBuffer(); - - /** - * Returns a int buffer which is based on the remaining content of - * this byte buffer. - *- * The new buffer's position is zero, its limit and capacity is - * the number of remaining bytes divided by two, and its mark is not set. - * The new buffer's readonly property and byte order are same as this - * buffer. The new buffer is direct, if this byte buffer is direct.
- *- * The new buffer shares content with this buffer, which means - * either buffer's change of content will be visible to the other. - * The two buffer's position, limit and mark are independent.
- * - * @return A int buffer which is based on the content of - * this byte buffer. - */ - public abstract IntBuffer asIntBuffer(); - - /** - * Returns a long buffer which is based on the remaining content of - * this byte buffer. - *- * The new buffer's position is zero, its limit and capacity is - * the number of remaining bytes divided by two, and its mark is not set. - * The new buffer's readonly property and byte order are same as this - * buffer. The new buffer is direct, if this byte buffer is direct.
- *- * The new buffer shares content with this buffer, which means - * either buffer's change of content will be visible to the other. - * The two buffer's position, limit and mark are independent.
- * - * @return A long buffer which is based on the content of - * this byte buffer. - */ - public abstract LongBuffer asLongBuffer(); - - /** - * Returns a readonly buffer that shares content with this buffer. - *- * The returned buffer is guaranteed to be a new instance, even this - * buffer is readonly itself. The new buffer's position, limit, capacity - * and mark are the same as this buffer.
- *- * The new buffer shares content with this buffer, which means - * this buffer's change of content will be visible to the new buffer. - * The two buffer's position, limit and mark are independent.
- * - * @return A readonly version of this buffer. - */ - public abstract ByteBuffer asReadOnlyBuffer(); - - /** - * Returns a short buffer which is based on the remaining content of - * this byte buffer. - *- * The new buffer's position is zero, its limit and capacity is - * the number of remaining bytes divided by two, and its mark is not set. - * The new buffer's readonly property and byte order are same as this - * buffer. The new buffer is direct, if this byte buffer is direct.
- *- * The new buffer shares content with this buffer, which means - * either buffer's change of content will be visible to the other. - * The two buffer's position, limit and mark are independent.
- * - * @return A short buffer which is based on the content of - * this byte buffer. - */ - public abstract ShortBuffer asShortBuffer(); - - /** - * Compacts this byte buffer. - *
- * The remaining byte
s will be moved to the head of the
- * buffer, staring from position zero. Then the position is set to
- * remaining()
; the limit is set to capacity; the mark is
- * cleared.
- *
byte
s of this buffer to another
- * byte buffer's remaining byte
s.
- *
- * @param otherBuffer
- * Another byte buffer
- * @return a negative value if this is less than other
; 0 if
- * this equals to other
; a positive value if this is
- * greater than other
- * @exception ClassCastException
- * If other
is not a byte buffer
- */
- public int compareTo(ByteBuffer otherBuffer) {
- int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining()
- : otherBuffer.remaining();
- int thisPos = position;
- int otherPos = otherBuffer.position;
- byte thisByte, otherByte;
- while (compareRemaining > 0) {
- thisByte = get(thisPos);
- otherByte = otherBuffer.get(otherPos);
- if (thisByte != otherByte) {
- return thisByte < otherByte ? -1 : 1;
- }
- thisPos++;
- otherPos++;
- compareRemaining--;
- }
- return remaining() - otherBuffer.remaining();
- }
-
- /**
- * Returns a duplicated buffer that shares content with this buffer.
- * - * The duplicated buffer's position, limit, capacity and mark are the - * same as this buffer. The duplicated buffer's readonly property and - * byte order are same as this buffer too.
- *- * The new buffer shares content with this buffer, which means - * either buffer's change of content will be visible to the other. - * The two buffer's position, limit and mark are independent.
- * - * @return A duplicated buffer that shares content with this buffer. - */ - public abstract ByteBuffer duplicate(); - - /** - * Tests whether this byte buffer equals to another object. - *
- * If other
is not a byte buffer, then false is returned.
- * Two byte buffers are equals if, and only if, their remaining
- * byte
s are exactly the same. Position, limit, capacity and
- * mark are not considered.
byte
s from the current position into the specified
- * byte array and increase the position by the number of byte
s
- * read.
- *
- * Calling this method has the same effect as
- * get(dest, 0, dest.length)
.
- *
dest.length
is greater than
- * remaining()
- */
- public ByteBuffer get(byte[] dest) {
- return get(dest, 0, dest.length);
- }
-
- /**
- * Reads byte
s from the current position into the specified
- * byte array, starting from the specified offset, and increase the position
- * by the number of byte
s read.
- *
- * @param dest
- * The target byte array
- * @param off
- * The offset of the byte array, must be no less than zero and no
- * greater than dest.length
- * @param len
- * The number of byte
s to read, must be no less
- * than zero and no greater than dest.length - off
- * @return This buffer
- * @exception IndexOutOfBoundsException
- * If either off
or len
is
- * invalid
- * @exception BufferUnderflowException
- * If len
is greater than
- * remaining()
- */
- public ByteBuffer get(byte[] dest, int off, int len) {
+ ByteBuffer buf = BufferFactory.newByteBuffer(array);
+ buf.position = start;
+ buf.limit = start + len;
+
+ return buf;
+ }
+
+ /**
+ * The byte order of this buffer, default is BIG_ENDIAN
.
+ */
+ Endianness order = Endianness.BIG_ENDIAN;
+
+ /**
+ * Constructs a ByteBuffer
with given capacity.
+ *
+ * @param capacity
+ * The capacity of the buffer
+ */
+ ByteBuffer(int capacity) {
+ super(capacity);
+ }
+
+ /**
+ * Returns the byte array which this buffer is based on, if there's one.
+ *
+ * @return The byte array which this buffer is based on
+ * @exception ReadOnlyBufferException
+ * If this buffer is based on a readonly array
+ * @exception UnsupportedOperationException
+ * If this buffer is not based on an array
+ */
+ public final byte[] array() {
+ return protectedArray();
+ }
+
+ /**
+ * Returns the offset of the byte array which this buffer is based on, if
+ * there's one.
+ * + * The offset is the index of the array corresponds to the zero position of + * the buffer. + *
+ * + * @return The offset of the byte array which this buffer is based on + * @exception ReadOnlyBufferException + * If this buffer is based on a readonly array + * @exception UnsupportedOperationException + * If this buffer is not based on an array + */ + public final int arrayOffset() { + return protectedArrayOffset(); + } + + /** + * Returns a char buffer which is based on the remaining content of this + * byte buffer. + *+ * The new buffer's position is zero, its limit and capacity is the number + * of remaining bytes divided by two, and its mark is not set. The new + * buffer's readonly property and byte order are same as this buffer. The + * new buffer is direct, if this byte buffer is direct. + *
+ *+ * The new buffer shares content with this buffer, which means either + * buffer's change of content will be visible to the other. The two buffer's + * position, limit and mark are independent. + *
+ * + * @return A char buffer which is based on the content of this byte buffer. + */ + public abstract CharBuffer asCharBuffer(); + + /** + * Returns a double buffer which is based on the remaining content of this + * byte buffer. + *+ * The new buffer's position is zero, its limit and capacity is the number + * of remaining bytes divided by two, and its mark is not set. The new + * buffer's readonly property and byte order are same as this buffer. The + * new buffer is direct, if this byte buffer is direct. + *
+ *+ * The new buffer shares content with this buffer, which means either + * buffer's change of content will be visible to the other. The two buffer's + * position, limit and mark are independent. + *
+ * + * @return A double buffer which is based on the content of this byte + * buffer. + */ + public abstract DoubleBuffer asDoubleBuffer(); + + /** + * Returns a float buffer which is based on the remaining content of this + * byte buffer. + *+ * The new buffer's position is zero, its limit and capacity is the number + * of remaining bytes divided by two, and its mark is not set. The new + * buffer's readonly property and byte order are same as this buffer. The + * new buffer is direct, if this byte buffer is direct. + *
+ *+ * The new buffer shares content with this buffer, which means either + * buffer's change of content will be visible to the other. The two buffer's + * position, limit and mark are independent. + *
+ * + * @return A float buffer which is based on the content of this byte buffer. + */ + public abstract FloatBuffer asFloatBuffer(); + + /** + * Returns a int buffer which is based on the remaining content of this byte + * buffer. + *+ * The new buffer's position is zero, its limit and capacity is the number + * of remaining bytes divided by two, and its mark is not set. The new + * buffer's readonly property and byte order are same as this buffer. The + * new buffer is direct, if this byte buffer is direct. + *
+ *+ * The new buffer shares content with this buffer, which means either + * buffer's change of content will be visible to the other. The two buffer's + * position, limit and mark are independent. + *
+ * + * @return A int buffer which is based on the content of this byte buffer. + */ + public abstract IntBuffer asIntBuffer(); + + /** + * Returns a long buffer which is based on the remaining content of this + * byte buffer. + *+ * The new buffer's position is zero, its limit and capacity is the number + * of remaining bytes divided by two, and its mark is not set. The new + * buffer's readonly property and byte order are same as this buffer. The + * new buffer is direct, if this byte buffer is direct. + *
+ *+ * The new buffer shares content with this buffer, which means either + * buffer's change of content will be visible to the other. The two buffer's + * position, limit and mark are independent. + *
+ * + * @return A long buffer which is based on the content of this byte buffer. + */ + public abstract LongBuffer asLongBuffer(); + + /** + * Returns a readonly buffer that shares content with this buffer. + *+ * The returned buffer is guaranteed to be a new instance, even this buffer + * is readonly itself. The new buffer's position, limit, capacity and mark + * are the same as this buffer. + *
+ *+ * The new buffer shares content with this buffer, which means this buffer's + * change of content will be visible to the new buffer. The two buffer's + * position, limit and mark are independent. + *
+ * + * @return A readonly version of this buffer. + */ + public abstract ByteBuffer asReadOnlyBuffer(); + + /** + * Returns a short buffer which is based on the remaining content of this + * byte buffer. + *+ * The new buffer's position is zero, its limit and capacity is the number + * of remaining bytes divided by two, and its mark is not set. The new + * buffer's readonly property and byte order are same as this buffer. The + * new buffer is direct, if this byte buffer is direct. + *
+ *+ * The new buffer shares content with this buffer, which means either + * buffer's change of content will be visible to the other. The two buffer's + * position, limit and mark are independent. + *
+ * + * @return A short buffer which is based on the content of this byte buffer. + */ + public abstract ShortBuffer asShortBuffer(); + + /** + * Compacts this byte buffer. + *
+ * The remaining byte
s will be moved to the head of the
+ * buffer, staring from position zero. Then the position is set to
+ * remaining()
; the limit is set to capacity; the mark is
+ * cleared.
+ *
byte
s of this buffer to another
+ * byte buffer's remaining byte
s.
+ *
+ * @param otherBuffer
+ * Another byte buffer
+ * @return a negative value if this is less than other
; 0 if
+ * this equals to other
; a positive value if this is
+ * greater than other
+ * @exception ClassCastException
+ * If other
is not a byte buffer
+ */
+ public int compareTo(ByteBuffer otherBuffer) {
+ int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining()
+ : otherBuffer.remaining();
+ int thisPos = position;
+ int otherPos = otherBuffer.position;
+ byte thisByte, otherByte;
+ while (compareRemaining > 0) {
+ thisByte = get(thisPos);
+ otherByte = otherBuffer.get(otherPos);
+ if (thisByte != otherByte) {
+ return thisByte < otherByte ? -1 : 1;
+ }
+ thisPos++;
+ otherPos++;
+ compareRemaining--;
+ }
+ return remaining() - otherBuffer.remaining();
+ }
+
+ /**
+ * Returns a duplicated buffer that shares content with this buffer.
+ * + * The duplicated buffer's position, limit, capacity and mark are the same + * as this buffer. The duplicated buffer's readonly property and byte order + * are same as this buffer too. + *
+ *+ * The new buffer shares content with this buffer, which means either + * buffer's change of content will be visible to the other. The two buffer's + * position, limit and mark are independent. + *
+ * + * @return A duplicated buffer that shares content with this buffer. + */ + public abstract ByteBuffer duplicate(); + + /** + * Tests whether this byte buffer equals to another object. + *
+ * If other
is not a byte buffer, then false is returned.
+ *
+ * Two byte buffers are equals if, and only if, their remaining
+ * byte
s are exactly the same. Position, limit, capacity and
+ * mark are not considered.
+ *
byte
s from the current position into the specified
+ * byte array and increase the position by the number of byte
s
+ * read.
+ *
+ * Calling this method has the same effect as
+ * get(dest, 0, dest.length)
.
+ *
dest.length
is greater than
+ * remaining()
+ */
+ public ByteBuffer get(byte[] dest) {
+ return get(dest, 0, dest.length);
+ }
+
+ /**
+ * Reads byte
s from the current position into the specified
+ * byte array, starting from the specified offset, and increase the position
+ * by the number of byte
s read.
+ *
+ * @param dest
+ * The target byte array
+ * @param off
+ * The offset of the byte array, must be no less than zero and no
+ * greater than dest.length
+ * @param len
+ * The number of byte
s to read, must be no less
+ * than zero and no greater than dest.length - off
+ * @return This buffer
+ * @exception IndexOutOfBoundsException
+ * If either off
or len
is
+ * invalid
+ * @exception BufferUnderflowException
+ * If len
is greater than
+ * remaining()
+ */
+ public ByteBuffer get(byte[] dest, int off, int len) {
int length = dest.length;
- if ((off < 0 ) || (len < 0) || ((long)off + (long)len > length)) {
+ if ((off < 0) || (len < 0) || ((long) off + (long) len > length)) {
throw new IndexOutOfBoundsException();
}
-
- if (len > remaining()) {
- throw new BufferUnderflowException();
- }
- for (int i = off; i < off + len; i++) {
- dest[i] = get();
- }
- return this;
- }
-
- /**
- * Returns a byte at the specified index, and the position is not changed.
- *
- * @param index The index, must be no less than zero and less than limit
- * @return A byte at the specified index.
- * @exception IndexOutOfBoundsException If index is invalid
- */
- public abstract byte get(int index);
-
- /**
- * Returns the char at the current position and increase the position by 2.
- * - * The 2 bytes start from the current position are composed into a char - * according to current byte order and returned. The position increases by - * 2. - *
- * - * @return The char at the current position. - * @exception BufferUnderflowException - * If the position is greater thanlimit - 2
- */
- public abstract char getChar();
-
- /**
- * Returns the char at the specified index.
- * - * The 2 bytes start from the specified index are composed into a char - * according to current byte order and returned. The position is not - * changed. - *
- * - * @param index - * The index, must be no less than zero and equal or less than - *limit - 2
- * @return The char at the specified index.
- * @exception IndexOutOfBoundsException
- * If index
is invalid
- */
- public abstract char getChar(int index);
-
- /**
- * Returns the double at the current position and increase the position by
- * 8.
- * - * The 8 bytes start from the current position are composed into a double - * according to current byte order and returned. The position increases by - * 8. - *
- * - * @return The double at the current position. - * @exception BufferUnderflowException - * If the position is greater thanlimit - 8
- */
- public abstract double getDouble();
-
- /**
- * Returns the double at the specified index.
- * - * The 8 bytes start from the specified index are composed into a double - * according to current byte order and returned. The position is not - * changed. - *
- * - * @param index - * The index, must be no less than zero and equal or less than - *limit - 8
- * @return The double at the specified index.
- * @exception IndexOutOfBoundsException
- * If index
is invalid
- */
- public abstract double getDouble(int index);
-
- /**
- * Returns the float at the current position and increase the position by 4.
- * - * The 4 bytes start from the current position are composed into a float - * according to current byte order and returned. The position increases by - * 4. - *
- * - * @return The float at the current position. - * @exception BufferUnderflowException - * If the position is greater thanlimit - 4
- */
- public abstract float getFloat();
-
- /**
- * Returns the float at the specified index.
- * - * The 4 bytes start from the specified index are composed into a float - * according to current byte order and returned. The position is not - * changed. - *
- * - * @param index - * The index, must be no less than zero and equal or less than - *limit - 4
- * @return The float at the specified index.
- * @exception IndexOutOfBoundsException
- * If index
is invalid
- */
- public abstract float getFloat(int index);
-
- /**
- * Returns the int at the current position and increase the position by 4.
- * - * The 4 bytes start from the current position are composed into a int - * according to current byte order and returned. - * The position increases by 4.
- * - * @return The int at the current position. - * @exception BufferUnderflowException If the position is greater thanlimit - 4
- */
- public abstract int getInt();
-
- /**
- * Returns the int at the specified index.
- * - * The 4 bytes start from the specified index are composed into a int - * according to current byte order and returned. The position is not - * changed. - *
- * - * @param index - * The index, must be no less than zero and equal or less than - *limit - 4
- * @return The int at the specified index.
- * @exception IndexOutOfBoundsException
- * If index
is invalid
- */
- public abstract int getInt(int index);
-
- /**
- * Returns the long at the current position and increase the position by 8.
- * - * The 8 bytes start from the current position are composed into a long - * according to current byte order and returned. The position increases by - * 8. - *
- * - * @return The long at the current position. - * @exception BufferUnderflowException - * If the position is greater thanlimit - 8
- */
- public abstract long getLong();
-
- /**
- * Returns the long at the specified index.
- * - * The 8 bytes start from the specified index are composed into a long - * according to current byte order and returned. The position is not - * changed. - *
- * - * @param index - * The index, must be no less than zero and equal or less than - *limit - 8
- * @return The long at the specified index.
- * @exception IndexOutOfBoundsException
- * If index
is invalid
- */
- public abstract long getLong(int index);
-
- /**
- * Returns the short at the current position and increase the position by 2.
- * - * The 2 bytes start from the current position are composed into a short - * according to current byte order and returned. - * The position increases by 2.
- * - * @return The short at the current position. - * @exception BufferUnderflowException If the position is greater thanlimit - 2
- */
- public abstract short getShort();
-
- /**
- * Returns the short at the specified index.
- * - * The 2 bytes start from the specified index are composed into a short - * according to current byte order and returned. The position is not - * changed. - *
- * - * @param index - * The index, must be no less than zero and equal or less than - *limit - 2
- * @return The short at the specified index.
- * @exception IndexOutOfBoundsException
- * If index
is invalid
- */
- public abstract short getShort(int index);
-
- /**
- * Returns whether this buffer is based on a byte array and is read/write.
- * - * If this buffer is readonly, then false is returned.
- * - * @return Whether this buffer is based on a byte array and is read/write. - */ - public final boolean hasArray() { - return protectedHasArray(); - } - - /** - * Hash code is calculated from the remainingbyte
s.
- * - * Position, limit, capacity and mark don't affect the hash code.
- * - * @return The hash code calculated from the remainingbyte
s.
- */
- public int hashCode() {
- int myPosition = position;
- int hash = 0;
- while (myPosition < limit) {
- hash = hash + get(myPosition++);
- }
- return hash;
- }
-
- /**
- * Returns true if this buffer is direct.
- * - * A byte buffer is direct, if it is based on a byte buffer and the byte - * buffer is direct. - *
- * - * @return True if this buffer is direct. - */ - public abstract boolean isDirect(); - - /** - * Returns the byte order used by this buffer when converting - *byte
s from/to other primitive types.
- * - * The default byte order of byte buffer is always BIG_ENDIAN.
- * - * @return The byte order used by this buffer when converting - *byte
s from/to other primitive types.
- */
- public final ByteOrder order() {
- return order == Endianness.BIG_ENDIAN ? ByteOrder.BIG_ENDIAN
- : ByteOrder.LITTLE_ENDIAN;
- }
- /**
+ if (len > remaining()) {
+ throw new BufferUnderflowException();
+ }
+ for (int i = off; i < off + len; i++) {
+ dest[i] = get();
+ }
+ return this;
+ }
+
+ /**
+ * Returns a byte at the specified index, and the position is not changed.
+ *
+ * @param index
+ * The index, must be no less than zero and less than limit
+ * @return A byte at the specified index.
+ * @exception IndexOutOfBoundsException
+ * If index is invalid
+ */
+ public abstract byte get(int index);
+
+ /**
+ * Returns the char at the current position and increase the position by 2.
+ * + * The 2 bytes start from the current position are composed into a char + * according to current byte order and returned. The position increases by + * 2. + *
+ * + * @return The char at the current position. + * @exception BufferUnderflowException + * If the position is greater thanlimit - 2
+ */
+ public abstract char getChar();
+
+ /**
+ * Returns the char at the specified index.
+ * + * The 2 bytes start from the specified index are composed into a char + * according to current byte order and returned. The position is not + * changed. + *
+ * + * @param index + * The index, must be no less than zero and equal or less than + *limit - 2
+ * @return The char at the specified index.
+ * @exception IndexOutOfBoundsException
+ * If index
is invalid
+ */
+ public abstract char getChar(int index);
+
+ /**
+ * Returns the double at the current position and increase the position by
+ * 8.
+ * + * The 8 bytes start from the current position are composed into a double + * according to current byte order and returned. The position increases by + * 8. + *
+ * + * @return The double at the current position. + * @exception BufferUnderflowException + * If the position is greater thanlimit - 8
+ */
+ public abstract double getDouble();
+
+ /**
+ * Returns the double at the specified index.
+ * + * The 8 bytes start from the specified index are composed into a double + * according to current byte order and returned. The position is not + * changed. + *
+ * + * @param index + * The index, must be no less than zero and equal or less than + *limit - 8
+ * @return The double at the specified index.
+ * @exception IndexOutOfBoundsException
+ * If index
is invalid
+ */
+ public abstract double getDouble(int index);
+
+ /**
+ * Returns the float at the current position and increase the position by 4.
+ * + * The 4 bytes start from the current position are composed into a float + * according to current byte order and returned. The position increases by + * 4. + *
+ * + * @return The float at the current position. + * @exception BufferUnderflowException + * If the position is greater thanlimit - 4
+ */
+ public abstract float getFloat();
+
+ /**
+ * Returns the float at the specified index.
+ * + * The 4 bytes start from the specified index are composed into a float + * according to current byte order and returned. The position is not + * changed. + *
+ * + * @param index + * The index, must be no less than zero and equal or less than + *limit - 4
+ * @return The float at the specified index.
+ * @exception IndexOutOfBoundsException
+ * If index
is invalid
+ */
+ public abstract float getFloat(int index);
+
+ /**
+ * Returns the int at the current position and increase the position by 4.
+ * + * The 4 bytes start from the current position are composed into a int + * according to current byte order and returned. The position increases by + * 4. + *
+ * + * @return The int at the current position. + * @exception BufferUnderflowException + * If the position is greater thanlimit - 4
+ */
+ public abstract int getInt();
+
+ /**
+ * Returns the int at the specified index.
+ * + * The 4 bytes start from the specified index are composed into a int + * according to current byte order and returned. The position is not + * changed. + *
+ * + * @param index + * The index, must be no less than zero and equal or less than + *limit - 4
+ * @return The int at the specified index.
+ * @exception IndexOutOfBoundsException
+ * If index
is invalid
+ */
+ public abstract int getInt(int index);
+
+ /**
+ * Returns the long at the current position and increase the position by 8.
+ * + * The 8 bytes start from the current position are composed into a long + * according to current byte order and returned. The position increases by + * 8. + *
+ * + * @return The long at the current position. + * @exception BufferUnderflowException + * If the position is greater thanlimit - 8
+ */
+ public abstract long getLong();
+
+ /**
+ * Returns the long at the specified index.
+ * + * The 8 bytes start from the specified index are composed into a long + * according to current byte order and returned. The position is not + * changed. + *
+ * + * @param index + * The index, must be no less than zero and equal or less than + *limit - 8
+ * @return The long at the specified index.
+ * @exception IndexOutOfBoundsException
+ * If index
is invalid
+ */
+ public abstract long getLong(int index);
+
+ /**
+ * Returns the short at the current position and increase the position by 2.
+ * + * The 2 bytes start from the current position are composed into a short + * according to current byte order and returned. The position increases by + * 2. + *
+ * + * @return The short at the current position. + * @exception BufferUnderflowException + * If the position is greater thanlimit - 2
+ */
+ public abstract short getShort();
+
+ /**
+ * Returns the short at the specified index.
+ * + * The 2 bytes start from the specified index are composed into a short + * according to current byte order and returned. The position is not + * changed. + *
+ * + * @param index + * The index, must be no less than zero and equal or less than + *limit - 2
+ * @return The short at the specified index.
+ * @exception IndexOutOfBoundsException
+ * If index
is invalid
+ */
+ public abstract short getShort(int index);
+
+ /**
+ * Returns whether this buffer is based on a byte array and is read/write.
+ * + * If this buffer is readonly, then false is returned. + *
+ * + * @return Whether this buffer is based on a byte array and is read/write. + */ + public final boolean hasArray() { + return protectedHasArray(); + } + + /** + * Hash code is calculated from the remainingbyte
s.
+ * + * Position, limit, capacity and mark don't affect the hash code. + *
+ * + * @return The hash code calculated from the remainingbyte
s.
+ */
+ @Override
+ public int hashCode() {
+ int myPosition = position;
+ int hash = 0;
+ while (myPosition < limit) {
+ hash = hash + get(myPosition++);
+ }
+ return hash;
+ }
+
+ /**
+ * Returns true if this buffer is direct.
+ * + * A byte buffer is direct, if it is based on a byte buffer and the byte + * buffer is direct. + *
+ * + * @return True if this buffer is direct. + */ + public abstract boolean isDirect(); + + /** + * Returns the byte order used by this buffer when converting + *byte
s from/to other primitive types.
+ * + * The default byte order of byte buffer is always BIG_ENDIAN. + *
+ * + * @return The byte order used by this buffer when converting + *byte
s from/to other primitive types.
+ */
+ public final ByteOrder order() {
+ return order == Endianness.BIG_ENDIAN ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN;
+ }
+
+ /**
* Sets the byte order of this buffer.
*
* @param byteOrder
@@ -694,409 +722,412 @@
* @return This buffer
* @see ByteOrder
*/
- public final ByteBuffer order(ByteOrder byteOrder) {
- return orderImpl(byteOrder);
- }
-
- ByteBuffer orderImpl(ByteOrder byteOrder) {
- order = byteOrder == ByteOrder.BIG_ENDIAN ? Endianness.BIG_ENDIAN
- : Endianness.LITTLE_ENDIAN;
- return this;
- }
-
- /**
- * Child class implements this method to realize array()
.
- *
- * @return see array()
- */
- abstract byte[] protectedArray();
-
- /**
- * Child class implements this method to realize arrayOffset()
.
- *
- * @return see arrayOffset()
- */
- abstract int protectedArrayOffset();
-
- /**
- * Child class implements this method to realize hasArray()
.
- *
- * @return see hasArray()
- */
- abstract boolean protectedHasArray();
-
- /**
- * Writes the given byte to the current position and increase the position
- * by 1.
- *
- * @param b
- * The byte to write
- * @return This buffer
- * @exception BufferOverflowException
- * If position is equal or greater than limit
- * @exception ReadOnlyBufferException
- * If no changes may be made to the contents of this buffer
- */
- public abstract ByteBuffer put(byte b);
-
- /**
- * Writes byte
s in the given byte array to the current
- * position and increase the position by the number of byte
s
- * written.
- *
- * Calling this method has the same effect as
- * put(src, 0, src.length)
.
- *
remaining()
is less than
- * src.length
- * @exception ReadOnlyBufferException
- * If no changes may be made to the contents of this buffer
- */
- public final ByteBuffer put(byte[] src) {
- return put(src, 0, src.length);
- }
-
- /**
- * Writes byte
s in the given byte array, starting from the
- * specified offset, to the current position and increase the position by
- * the number of byte
s written.
- *
- * @param src
- * The source byte array
- * @param off
- * The offset of byte array, must be no less than zero and no
- * greater than src.length
- * @param len
- * The number of byte
s to write, must be no less
- * than zero and no greater than src.length - off
- * @return This buffer
- * @exception BufferOverflowException
- * If remaining()
is less than
- * len
- * @exception IndexOutOfBoundsException
- * If either off
or len
is
- * invalid
- * @exception ReadOnlyBufferException
- * If no changes may be made to the contents of this buffer
- */
- public ByteBuffer put(byte[] src, int off, int len) {
+ public final ByteBuffer order(ByteOrder byteOrder) {
+ return orderImpl(byteOrder);
+ }
+
+ ByteBuffer orderImpl(ByteOrder byteOrder) {
+ order = byteOrder == ByteOrder.BIG_ENDIAN ? Endianness.BIG_ENDIAN
+ : Endianness.LITTLE_ENDIAN;
+ return this;
+ }
+
+ /**
+ * Child class implements this method to realize array()
.
+ *
+ * @return see array()
+ */
+ abstract byte[] protectedArray();
+
+ /**
+ * Child class implements this method to realize arrayOffset()
.
+ *
+ * @return see arrayOffset()
+ */
+ abstract int protectedArrayOffset();
+
+ /**
+ * Child class implements this method to realize hasArray()
.
+ *
+ * @return see hasArray()
+ */
+ abstract boolean protectedHasArray();
+
+ /**
+ * Writes the given byte to the current position and increase the position
+ * by 1.
+ *
+ * @param b
+ * The byte to write
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If position is equal or greater than limit
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract ByteBuffer put(byte b);
+
+ /**
+ * Writes byte
s in the given byte array to the current
+ * position and increase the position by the number of byte
s
+ * written.
+ *
+ * Calling this method has the same effect as
+ * put(src, 0, src.length)
.
+ *
remaining()
is less than
+ * src.length
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public final ByteBuffer put(byte[] src) {
+ return put(src, 0, src.length);
+ }
+
+ /**
+ * Writes byte
s in the given byte array, starting from the
+ * specified offset, to the current position and increase the position by
+ * the number of byte
s written.
+ *
+ * @param src
+ * The source byte array
+ * @param off
+ * The offset of byte array, must be no less than zero and no
+ * greater than src.length
+ * @param len
+ * The number of byte
s to write, must be no less
+ * than zero and no greater than src.length - off
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If remaining()
is less than
+ * len
+ * @exception IndexOutOfBoundsException
+ * If either off
or len
is
+ * invalid
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public ByteBuffer put(byte[] src, int off, int len) {
int length = src.length;
- if ((off < 0 ) || (len < 0) || ((long)off + (long)len > length)) {
+ if ((off < 0) || (len < 0) || ((long) off + (long) len > length)) {
throw new IndexOutOfBoundsException();
}
-
- if (len > remaining()) {
- throw new BufferOverflowException();
- }
- for (int i = off; i < off + len; i++) {
- put(src[i]);
- }
- return this;
- }
-
- /**
- * Writes all the remaining byte
s of the src
- * byte buffer to this buffer's current position, and increase both buffers'
- * position by the number of byte
s copied.
- *
- * @param src
- * The source byte buffer
- * @return This buffer
- * @exception BufferOverflowException
- * If src.remaining()
is greater than this
- * buffer's remaining()
- * @exception IllegalArgumentException
- * If src
is this buffer
- * @exception ReadOnlyBufferException
- * If no changes may be made to the contents of this buffer
- */
- public ByteBuffer put(ByteBuffer src) {
- if (src == this) {
- throw new IllegalArgumentException();
- }
- if (src.remaining() > remaining()) {
- throw new BufferOverflowException();
- }
+
+ if (len > remaining()) {
+ throw new BufferOverflowException();
+ }
+ for (int i = off; i < off + len; i++) {
+ put(src[i]);
+ }
+ return this;
+ }
+
+ /**
+ * Writes all the remaining byte
s of the src
+ * byte buffer to this buffer's current position, and increase both buffers'
+ * position by the number of byte
s copied.
+ *
+ * @param src
+ * The source byte buffer
+ * @return This buffer
+ * @exception BufferOverflowException
+ * If src.remaining()
is greater than this
+ * buffer's remaining()
+ * @exception IllegalArgumentException
+ * If src
is this buffer
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public ByteBuffer put(ByteBuffer src) {
+ if (src == this) {
+ throw new IllegalArgumentException();
+ }
+ if (src.remaining() > remaining()) {
+ throw new BufferOverflowException();
+ }
byte[] contents = new byte[src.remaining()];
src.get(contents);
put(contents);
- return this;
- }
+ return this;
+ }
+
+ /**
+ * Write a byte to the specified index of this buffer and the position is
+ * not changed.
+ *
+ * @param index
+ * The index, must be no less than zero and less than the limit
+ * @param b
+ * The byte to write
+ * @return This buffer
+ * @exception IndexOutOfBoundsException
+ * If index
is invalid
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract ByteBuffer put(int index, byte b);
- /**
- * Write a byte to the specified index of this buffer and the position is
- * not changed.
- *
- * @param index
- * The index, must be no less than zero and less than the limit
- * @param b
- * The byte to write
- * @return This buffer
- * @exception IndexOutOfBoundsException
- * If index
is invalid
- * @exception ReadOnlyBufferException
- * If no changes may be made to the contents of this buffer
- */
- public abstract ByteBuffer put(int index, byte b);
-
- /**
- * Writes the given char to the current position and increase the position
- * by 2.
- * - * The char is converted to bytes using the current byte order. - *
- * - * @param value - * The char to write - * @return This buffer - * @exception BufferOverflowException - * If position is greater thanlimit - 2
- * @exception ReadOnlyBufferException
- * If no changes may be made to the contents of this buffer
- */
- public abstract ByteBuffer putChar(char value);
-
- /**
- * Write a char to the specified index of this buffer.
- * - * The char is converted to bytes using the current byte order. The position - * is not changed. - *
- * - * @param index - * The index, must be no less than zero and equal or less than - *limit - 2
- * @param value
- * The char to write
- * @return This buffer
- * @exception IndexOutOfBoundsException
- * If index
is invalid
- * @exception ReadOnlyBufferException
- * If no changes may be made to the contents of this buffer
- */
- public abstract ByteBuffer putChar(int index, char value);
-
- /**
- * Writes the given double to the current position and increase the position
- * by 8.
- * - * The double is converted to bytes using the current byte order. - *
- * - * @param value - * The double to write - * @return This buffer - * @exception BufferOverflowException - * If position is greater thanlimit - 8
- * @exception ReadOnlyBufferException
- * If no changes may be made to the contents of this buffer
- */
- public abstract ByteBuffer putDouble(double value);
-
- /**
- * Write a double to the specified index of this buffer.
- * - * The double is converted to bytes using the current byte order. The - * position is not changed. - *
- * - * @param index - * The index, must be no less than zero and equal or less than - *limit - 8
- * @param value
- * The double to write
- * @return This buffer
- * @exception IndexOutOfBoundsException
- * If index
is invalid
- * @exception ReadOnlyBufferException
- * If no changes may be made to the contents of this buffer
- */
- public abstract ByteBuffer putDouble(int index, double value);
-
- /**
- * Writes the given float to the current position and increase the position
- * by 4.
- * - * The float is converted to bytes using the current byte order. - *
- * - * @param value - * The float to write - * @return This buffer - * @exception BufferOverflowException - * If position is greater thanlimit - 4
- * @exception ReadOnlyBufferException
- * If no changes may be made to the contents of this buffer
- */
- public abstract ByteBuffer putFloat(float value);
-
- /**
- * Write a float to the specified index of this buffer.
- * - * The float is converted to bytes using the current byte order. The - * position is not changed. - *
- * - * @param index - * The index, must be no less than zero and equal or less than - *limit - 4
- * @param value
- * The float to write
- * @return This buffer
- * @exception IndexOutOfBoundsException
- * If index
is invalid
- * @exception ReadOnlyBufferException
- * If no changes may be made to the contents of this buffer
- */
- public abstract ByteBuffer putFloat(int index, float value);
-
- /**
- * Writes the given int to the current position and increase the position by
- * 4.
- * - * The int is converted to bytes using the current byte order. - *
- * - * @param value - * The int to write - * @return This buffer - * @exception BufferOverflowException - * If position is greater thanlimit - 4
- * @exception ReadOnlyBufferException
- * If no changes may be made to the contents of this buffer
- */
- public abstract ByteBuffer putInt(int value);
-
- /**
- * Write a int to the specified index of this buffer.
- * - * The int is converted to bytes using the current byte order. The position - * is not changed. - *
- * - * @param index - * The index, must be no less than zero and equal or less than - *limit - 4
- * @param value
- * The int to write
- * @return This buffer
- * @exception IndexOutOfBoundsException
- * If index
is invalid
- * @exception ReadOnlyBufferException
- * If no changes may be made to the contents of this buffer
- */
- public abstract ByteBuffer putInt(int index, int value);
-
- /**
- * Writes the given long to the current position and increase the position
- * by 8.
- * - * The long is converted to bytes using the current byte order. - *
- * - * @param value - * The long to write - * @return This buffer - * @exception BufferOverflowException - * If position is greater thanlimit - 8
- * @exception ReadOnlyBufferException
- * If no changes may be made to the contents of this buffer
- */
- public abstract ByteBuffer putLong(long value);
-
- /**
- * Write a long to the specified index of this buffer.
- * - * The long is converted to bytes using the current byte order. The position - * is not changed. - *
- * - * @param index - * The index, must be no less than zero and equal or less than - *limit - 8
- * @param value
- * The long to write
- * @return This buffer
- * @exception IndexOutOfBoundsException
- * If index
is invalid
- * @exception ReadOnlyBufferException
- * If no changes may be made to the contents of this buffer
- */
- public abstract ByteBuffer putLong(int index, long value);
-
- /**
- * Writes the given short to the current position and increase the position
- * by 2.
- * - * The short is converted to bytes using the current byte order. - *
- * - * @param value - * The short to write - * @return This buffer - * @exception BufferOverflowException - * If position is greater thanlimit - 2
- * @exception ReadOnlyBufferException
- * If no changes may be made to the contents of this buffer
- */
- public abstract ByteBuffer putShort(short value);
-
- /**
- * Write a short to the specified index of this buffer.
- * - * The short is converted to bytes using the current byte order. The - * position is not changed. - *
- * - * @param index - * The index, must be no less than zero and equal or less than - *limit - 2
- * @param value
- * The short to write
- * @return This buffer
- * @exception IndexOutOfBoundsException
- * If index
is invalid
- * @exception ReadOnlyBufferException
- * If no changes may be made to the contents of this buffer
- */
- public abstract ByteBuffer putShort(int index, short value);
-
- /**
- * Returns a sliced buffer that shares content with this buffer.
- *
- * The sliced buffer's capacity will be this buffer's
- * remaining()
, and its zero position will correspond to
- * this buffer's current position. The new buffer's position will be
- * 0, limit will be its capacity, and its mark is unset. The new buffer's
- * readonly property and byte order are same as this buffer.
- * The new buffer shares content with this buffer, which means - * either buffer's change of content will be visible to the other. - * The two buffer's position, limit and mark are independent.
- * - * @return A sliced buffer that shares content with this buffer. - */ - public abstract ByteBuffer slice(); - - /** - * Returns a string represents the state of this byte buffer. - * - * @return A string represents the state of this byte buffer. - */ - public String toString() { - StringBuffer buf = new StringBuffer(); - buf.append(getClass().getName()); - buf.append(", status: capacity="); //$NON-NLS-1$ - buf.append(capacity()); - buf.append(" position="); //$NON-NLS-1$ - buf.append(position()); - buf.append(" limit="); //$NON-NLS-1$ - buf.append(limit()); - return buf.toString(); - } + /** + * Writes the given char to the current position and increase the position + * by 2. + *+ * The char is converted to bytes using the current byte order. + *
+ * + * @param value + * The char to write + * @return This buffer + * @exception BufferOverflowException + * If position is greater thanlimit - 2
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract ByteBuffer putChar(char value);
+
+ /**
+ * Write a char to the specified index of this buffer.
+ * + * The char is converted to bytes using the current byte order. The position + * is not changed. + *
+ * + * @param index + * The index, must be no less than zero and equal or less than + *limit - 2
+ * @param value
+ * The char to write
+ * @return This buffer
+ * @exception IndexOutOfBoundsException
+ * If index
is invalid
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract ByteBuffer putChar(int index, char value);
+
+ /**
+ * Writes the given double to the current position and increase the position
+ * by 8.
+ * + * The double is converted to bytes using the current byte order. + *
+ * + * @param value + * The double to write + * @return This buffer + * @exception BufferOverflowException + * If position is greater thanlimit - 8
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract ByteBuffer putDouble(double value);
+
+ /**
+ * Write a double to the specified index of this buffer.
+ * + * The double is converted to bytes using the current byte order. The + * position is not changed. + *
+ * + * @param index + * The index, must be no less than zero and equal or less than + *limit - 8
+ * @param value
+ * The double to write
+ * @return This buffer
+ * @exception IndexOutOfBoundsException
+ * If index
is invalid
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract ByteBuffer putDouble(int index, double value);
+
+ /**
+ * Writes the given float to the current position and increase the position
+ * by 4.
+ * + * The float is converted to bytes using the current byte order. + *
+ * + * @param value + * The float to write + * @return This buffer + * @exception BufferOverflowException + * If position is greater thanlimit - 4
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract ByteBuffer putFloat(float value);
+
+ /**
+ * Write a float to the specified index of this buffer.
+ * + * The float is converted to bytes using the current byte order. The + * position is not changed. + *
+ * + * @param index + * The index, must be no less than zero and equal or less than + *limit - 4
+ * @param value
+ * The float to write
+ * @return This buffer
+ * @exception IndexOutOfBoundsException
+ * If index
is invalid
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract ByteBuffer putFloat(int index, float value);
+
+ /**
+ * Writes the given int to the current position and increase the position by
+ * 4.
+ * + * The int is converted to bytes using the current byte order. + *
+ * + * @param value + * The int to write + * @return This buffer + * @exception BufferOverflowException + * If position is greater thanlimit - 4
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract ByteBuffer putInt(int value);
+
+ /**
+ * Write a int to the specified index of this buffer.
+ * + * The int is converted to bytes using the current byte order. The position + * is not changed. + *
+ * + * @param index + * The index, must be no less than zero and equal or less than + *limit - 4
+ * @param value
+ * The int to write
+ * @return This buffer
+ * @exception IndexOutOfBoundsException
+ * If index
is invalid
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract ByteBuffer putInt(int index, int value);
+
+ /**
+ * Writes the given long to the current position and increase the position
+ * by 8.
+ * + * The long is converted to bytes using the current byte order. + *
+ * + * @param value + * The long to write + * @return This buffer + * @exception BufferOverflowException + * If position is greater thanlimit - 8
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract ByteBuffer putLong(long value);
+
+ /**
+ * Write a long to the specified index of this buffer.
+ * + * The long is converted to bytes using the current byte order. The position + * is not changed. + *
+ * + * @param index + * The index, must be no less than zero and equal or less than + *limit - 8
+ * @param value
+ * The long to write
+ * @return This buffer
+ * @exception IndexOutOfBoundsException
+ * If index
is invalid
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract ByteBuffer putLong(int index, long value);
+
+ /**
+ * Writes the given short to the current position and increase the position
+ * by 2.
+ * + * The short is converted to bytes using the current byte order. + *
+ * + * @param value + * The short to write + * @return This buffer + * @exception BufferOverflowException + * If position is greater thanlimit - 2
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract ByteBuffer putShort(short value);
+
+ /**
+ * Write a short to the specified index of this buffer.
+ * + * The short is converted to bytes using the current byte order. The + * position is not changed. + *
+ * + * @param index + * The index, must be no less than zero and equal or less than + *limit - 2
+ * @param value
+ * The short to write
+ * @return This buffer
+ * @exception IndexOutOfBoundsException
+ * If index
is invalid
+ * @exception ReadOnlyBufferException
+ * If no changes may be made to the contents of this buffer
+ */
+ public abstract ByteBuffer putShort(int index, short value);
+
+ /**
+ * Returns a sliced buffer that shares content with this buffer.
+ *
+ * The sliced buffer's capacity will be this buffer's
+ * remaining()
, and its zero position will correspond to
+ * this buffer's current position. The new buffer's position will be 0,
+ * limit will be its capacity, and its mark is unset. The new buffer's
+ * readonly property and byte order are same as this buffer.
+ *
+ * The new buffer shares content with this buffer, which means either + * buffer's change of content will be visible to the other. The two buffer's + * position, limit and mark are independent. + *
+ * + * @return A sliced buffer that shares content with this buffer. + */ + public abstract ByteBuffer slice(); + + /** + * Returns a string represents the state of this byte buffer. + * + * @return A string represents the state of this byte buffer. + */ + @Override + public String toString() { + StringBuffer buf = new StringBuffer(); + buf.append(getClass().getName()); + buf.append(", status: capacity="); //$NON-NLS-1$ + buf.append(capacity()); + buf.append(" position="); //$NON-NLS-1$ + buf.append(position()); + buf.append(" limit="); //$NON-NLS-1$ + buf.append(limit()); + return buf.toString(); + } } Modified: harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/ByteOrder.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/ByteOrder.java?rev=578091&r1=578090&r2=578091&view=diff ============================================================================== --- harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/ByteOrder.java (original) +++ harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/ByteOrder.java Fri Sep 21 06:05:59 2007 @@ -16,7 +16,6 @@ package java.nio; - import org.apache.harmony.luni.platform.Platform; /** @@ -25,49 +24,50 @@ */ public final class ByteOrder { - /** - * This constant represents big endian. - */ - public static final ByteOrder BIG_ENDIAN = new ByteOrder("BIG_ENDIAN"); //$NON-NLS-1$ - - /** - * This constant represents little endian. - */ - public static final ByteOrder LITTLE_ENDIAN = new ByteOrder("LITTLE_ENDIAN"); //$NON-NLS-1$ - - private static final ByteOrder NATIVE_ORDER; - - static { - if (Platform.getMemorySystem().isLittleEndian()) { - NATIVE_ORDER = LITTLE_ENDIAN; - } else { - NATIVE_ORDER = BIG_ENDIAN; - } - } - - /** - * Answers the current platform byte order. - * - * @return the byte order object, which is either identical to LITTLE_ENDIAN - * or BIG_ENDIAN. - */ - public static ByteOrder nativeOrder() { - return NATIVE_ORDER; - } - - private final String name; - - private ByteOrder(String name) { - super(); - this.name = name; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - public String toString() { - return name; - } + /** + * This constant represents big endian. + */ + public static final ByteOrder BIG_ENDIAN = new ByteOrder("BIG_ENDIAN"); //$NON-NLS-1$ + + /** + * This constant represents little endian. + */ + public static final ByteOrder LITTLE_ENDIAN = new ByteOrder("LITTLE_ENDIAN"); //$NON-NLS-1$ + + private static final ByteOrder NATIVE_ORDER; + + static { + if (Platform.getMemorySystem().isLittleEndian()) { + NATIVE_ORDER = LITTLE_ENDIAN; + } else { + NATIVE_ORDER = BIG_ENDIAN; + } + } + + /** + * Answers the current platform byte order. + * + * @return the byte order object, which is either identical to LITTLE_ENDIAN + * or BIG_ENDIAN. + */ + public static ByteOrder nativeOrder() { + return NATIVE_ORDER; + } + + private final String name; + + private ByteOrder(String name) { + super(); + this.name = name; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return name; + } } Modified: harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharArrayBuffer.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharArrayBuffer.java?rev=578091&r1=578090&r2=578091&view=diff ============================================================================== --- harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharArrayBuffer.java (original) +++ harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharArrayBuffer.java Fri Sep 21 06:05:59 2007 @@ -31,71 +31,78 @@ */ abstract class CharArrayBuffer extends CharBuffer { - protected final char[] backingArray; + protected final char[] backingArray; - protected final int offset; + protected final int offset; - CharArrayBuffer(char[] array) { - this(array.length, array, 0); - } - - CharArrayBuffer(int capacity) { - this(capacity, new char[capacity], 0); - } - - CharArrayBuffer(int capacity, char[] backingArray, int offset) { - super(capacity); - this.backingArray = backingArray; - this.offset = offset; - } - - public final char get() { - if (position == limit) { - throw new BufferUnderflowException(); - } - return backingArray[offset + position++]; - } - - public final char get(int index) { - if (index < 0 || index >= limit) { - throw new IndexOutOfBoundsException(); - } - return backingArray[offset + index]; - } + CharArrayBuffer(char[] array) { + this(array.length, array, 0); + } + + CharArrayBuffer(int capacity) { + this(capacity, new char[capacity], 0); + } + CharArrayBuffer(int capacity, char[] backingArray, int offset) { + super(capacity); + this.backingArray = backingArray; + this.offset = offset; + } + + @Override + public final char get() { + if (position == limit) { + throw new BufferUnderflowException(); + } + return backingArray[offset + position++]; + } + + @Override + public final char get(int index) { + if (index < 0 || index >= limit) { + throw new IndexOutOfBoundsException(); + } + return backingArray[offset + index]; + } + + @Override public final CharBuffer get(char[] dest, int off, int len) { int length = dest.length; - if ((off < 0 ) || (len < 0) || (long)off + (long)len > length) { + if ((off < 0) || (len < 0) || (long) off + (long) len > length) { throw new IndexOutOfBoundsException(); } if (len > remaining()) { throw new BufferUnderflowException(); } - System.arraycopy(backingArray, offset+position, dest, off, len); + System.arraycopy(backingArray, offset + position, dest, off, len); position += len; return this; } - - public final boolean isDirect() { - return false; - } - - public final ByteOrder order() { - return ByteOrder.nativeOrder(); - } - public final CharSequence subSequence(int start, int end) { + @Override + public final boolean isDirect() { + return false; + } + + @Override + public final ByteOrder order() { + return ByteOrder.nativeOrder(); + } + + @Override + public final CharSequence subSequence(int start, int end) { if (start < 0 || end < start || end > remaining()) { throw new IndexOutOfBoundsException(); } - - CharBuffer result = duplicate(); - result.limit(position + end); - result.position(position + start); - return result; - } - - public final String toString() { - return String.copyValueOf(backingArray, offset + position, remaining()); - } + + CharBuffer result = duplicate(); + result.limit(position + end); + result.position(position + start); + return result; + } + + @Override + public final String toString() { + return String.copyValueOf(backingArray, offset + position, remaining()); + } }