From 23c41c08339da4bdf677ade01e17d940b7ce6201 Mon Sep 17 00:00:00 2001 From: Dalibor Topic Date: Fri, 9 Jul 2004 13:40:29 +0000 Subject: [PATCH] Buffer.java, [...]: Fixed javadocs all over. 2004-07-09 Dalibor Topic * java/nio/Buffer.java, java/nio/ByteBuffer.java, java/nio/ByteBufferHelper.java, java/nio/ByteBufferImpl.java, java/nio/CharBuffer.java, java/nio/CharBufferImpl.java, java/nio/CharViewBufferImpl.java, java/nio/DirectByteBufferImpl.java, java/nio/DoubleBuffer.java, java/nio/DoubleBufferImpl.java, java/nio/DoubleViewBufferImpl.java, java/nio/FloatBuffer.java, java/nio/FloatBufferImpl.java, java/nio/FloatViewBufferImpl.java, java/nio/IntBuffer.java, java/nio/IntBufferImpl.java, java/nio/IntViewBufferImpl.java, java/nio/LongBuffer.java, java/nio/LongBufferImpl.java, java/nio/LongViewBufferImpl.java, java/nio/MappedByteBufferImpl.java, java/nio/ShortBuffer.java, java/nio/ShortBufferImpl.java, java/nio/ShortViewBufferImpl.java: Fixed javadocs all over. Improved input error checking. * java/nio/Buffer.java (checkForUnderflow, checkForOverflow, checkIndex, checkIfReadOnly, checkArraySize): New helper methods for error checking. * java/nio/ByteBufferHelper.java (checkRemainingForRead, checkRemainingForWrite, checkAvailableForRead, checkAvailableForWrite): Removed no longer needed methods. From-SVN: r84366 --- libjava/ChangeLog | 39 +++++++ libjava/java/nio/Buffer.java | 113 ++++++++++++++++++++- libjava/java/nio/ByteBuffer.java | 49 ++++----- libjava/java/nio/ByteBufferHelper.java | 50 ++------- libjava/java/nio/ByteBufferImpl.java | 24 +++-- libjava/java/nio/CharBuffer.java | 52 +++++----- libjava/java/nio/CharBufferImpl.java | 29 +++--- libjava/java/nio/CharViewBufferImpl.java | 16 +++ libjava/java/nio/DirectByteBufferImpl.java | 24 ++--- libjava/java/nio/DoubleBuffer.java | 42 ++++---- libjava/java/nio/DoubleBufferImpl.java | 26 +++-- libjava/java/nio/DoubleViewBufferImpl.java | 14 +++ libjava/java/nio/FloatBuffer.java | 42 ++++---- libjava/java/nio/FloatBufferImpl.java | 26 +++-- libjava/java/nio/FloatViewBufferImpl.java | 14 +++ libjava/java/nio/IntBuffer.java | 42 ++++---- libjava/java/nio/IntBufferImpl.java | 28 +++-- libjava/java/nio/IntViewBufferImpl.java | 14 +++ libjava/java/nio/LongBuffer.java | 42 ++++---- libjava/java/nio/LongBufferImpl.java | 28 +++-- libjava/java/nio/LongViewBufferImpl.java | 14 +++ libjava/java/nio/MappedByteBufferImpl.java | 24 ++--- libjava/java/nio/ShortBuffer.java | 42 ++++---- libjava/java/nio/ShortBufferImpl.java | 28 +++-- libjava/java/nio/ShortViewBufferImpl.java | 14 +++ 25 files changed, 558 insertions(+), 278 deletions(-) diff --git a/libjava/ChangeLog b/libjava/ChangeLog index a92c0a3a552..ea7ba416559 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,42 @@ +2004-07-09 Dalibor Topic + + * java/nio/Buffer.java, + java/nio/ByteBuffer.java, + java/nio/ByteBufferHelper.java, + java/nio/ByteBufferImpl.java, + java/nio/CharBuffer.java, + java/nio/CharBufferImpl.java, + java/nio/CharViewBufferImpl.java, + java/nio/DirectByteBufferImpl.java, + java/nio/DoubleBuffer.java, + java/nio/DoubleBufferImpl.java, + java/nio/DoubleViewBufferImpl.java, + java/nio/FloatBuffer.java, + java/nio/FloatBufferImpl.java, + java/nio/FloatViewBufferImpl.java, + java/nio/IntBuffer.java, + java/nio/IntBufferImpl.java, + java/nio/IntViewBufferImpl.java, + java/nio/LongBuffer.java, + java/nio/LongBufferImpl.java, + java/nio/LongViewBufferImpl.java, + java/nio/MappedByteBufferImpl.java, + java/nio/ShortBuffer.java, + java/nio/ShortBufferImpl.java, + java/nio/ShortViewBufferImpl.java: + Fixed javadocs all over. Improved input error + checking. + + * java/nio/Buffer.java + (checkForUnderflow, checkForOverflow, checkIndex, + checkIfReadOnly, checkArraySize): New helper methods + for error checking. + + * java/nio/ByteBufferHelper.java + (checkRemainingForRead, checkRemainingForWrite, + checkAvailableForRead, checkAvailableForWrite): Removed + no longer needed methods. + 2004-07-09 Michael Koch * gnu/regexp/CharIndexedInputStream.java: diff --git a/libjava/java/nio/Buffer.java b/libjava/java/nio/Buffer.java index c7f01b67116..e7173852be2 100644 --- a/libjava/java/nio/Buffer.java +++ b/libjava/java/nio/Buffer.java @@ -233,7 +233,7 @@ public abstract class Buffer * Rewinds this buffer. The position is set to zero and the mark * is discarded. * - * @this buffer + * @return this buffer */ public final Buffer rewind() { @@ -241,4 +241,115 @@ public abstract class Buffer mark = -1; return this; } + + /** + * Checks for underflow. This method is used internally to check + * whether a buffer has enough elements left to satisfy a read + * request. + * + * @exception BufferUnderflowException If there are no remaining + * elements in this buffer. + */ + final void checkForUnderflow() + { + if (!hasRemaining()) + throw new BufferUnderflowException(); + } + + /** + * Checks for underflow. This method is used internally to check + * whether a buffer has enough elements left to satisfy a read + * request for a given number of elements. + * + * @param length The length of a sequence of elements. + * + * @exception BufferUnderflowException If there are not enough + * remaining elements in this buffer. + */ + final void checkForUnderflow(int length) + { + if (remaining() < length) + throw new BufferUnderflowException(); + } + + /** + * Checks for overflow. This method is used internally to check + * whether a buffer has enough space left to satisfy a write + * request. + * + * @exception BufferOverflowException If there is no remaining + * space in this buffer. + */ + final void checkForOverflow() + { + if (!hasRemaining()) + throw new BufferOverflowException(); + } + + /** + * Checks for overflow. This method is used internally to check + * whether a buffer has enough space left to satisfy a write + * request for a given number of elements. + * + * @param length The length of a sequence of elements. + * + * @exception BufferUnderflowException If there is not enough + * remaining space in this buffer. + */ + final void checkForOverflow(int length) + { + if (remaining() < length) + throw new BufferOverflowException(); + } + + /** + * Checks if index is negative or not smaller than the buffer's + * limit. This method is used internally to check whether + * an indexed request can be fulfilled. + * + * @param index The requested position in the buffer. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + final void checkIndex(int index) + { + if (index < 0 + || index >= limit ()) + throw new IndexOutOfBoundsException (); + } + + /** + * Checks if buffer is read-only. This method is used internally to + * check if elements can be put into a buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + final void checkIfReadOnly() + { + if (isReadOnly()) + throw new ReadOnlyBufferException (); + } + + /** + * Checks whether an array is large enough to hold the given number of + * elements at the given offset. This method is used internally to + * check if an array is big enough. + * + * @param arraylength The length of the array. + * @param offset The offset within the array of the first byte to be read; + * must be non-negative and no larger than arraylength. + * @param length The number of bytes to be read from the given array; + * must be non-negative and no larger than arraylength - offset. + * + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + */ + final static void checkArraySize(int arraylength, int offset, int length) + { + if ((offset < 0) || + (length < 0) || + (arraylength < length + offset)) + throw new IndexOutOfBoundsException (); + } } diff --git a/libjava/java/nio/ByteBuffer.java b/libjava/java/nio/ByteBuffer.java index 8b43da57910..34e3db9b7c7 100644 --- a/libjava/java/nio/ByteBuffer.java +++ b/libjava/java/nio/ByteBuffer.java @@ -100,8 +100,9 @@ public abstract class ByteBuffer extends Buffer } /** - * This method transfers bytes from this buffer into the given - * destination array. + * This method transfers bytes from this buffer into the given + * destination array. Before the transfer, it checks if there are fewer than + * length bytes remaining in this buffer. * * @param dst The destination array * @param offset The offset within the array of the first byte @@ -110,16 +111,14 @@ public abstract class ByteBuffer extends Buffer * must be non-negative and no larger than dst.length - offset. * * @exception BufferUnderflowException If there are fewer than length - * bytes remaining in this buffer. + * bytes remaining in this buffer. * @exception IndexOutOfBoundsException If the preconditions on the offset * and length parameters do not hold. */ public ByteBuffer get (byte[] dst, int offset, int length) { - if (offset < 0 || length < 0 || offset + length > dst.length) - throw new IndexOutOfBoundsException (); - if (length > remaining()) - throw new BufferUnderflowException(); + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); for (int i = offset; i < offset + length; i++) { @@ -130,13 +129,13 @@ public abstract class ByteBuffer extends Buffer } /** - * This method transfers bytes from this buffer into the given + * This method transfers bytes from this buffer into the given * destination array. * * @param dst The byte array to write into. * * @exception BufferUnderflowException If there are fewer than dst.length - * bytes remaining in this buffer. + * bytes remaining in this buffer. */ public ByteBuffer get (byte[] dst) { @@ -145,12 +144,13 @@ public abstract class ByteBuffer extends Buffer /** * Writes the content of the the ByteBUFFER src - * into the buffer. + * into the buffer. Before the transfer, it checks if there is fewer than + * src.remaining() space remaining in this buffer. * * @param src The source data. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining bytes in the source buffer. + * buffer for the remaining bytes in the source buffer. * @exception IllegalArgumentException If the source buffer is this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ @@ -159,8 +159,7 @@ public abstract class ByteBuffer extends Buffer if (src == this) throw new IllegalArgumentException (); - if (src.remaining () > remaining ()) - throw new BufferOverflowException (); + checkForOverflow(src.remaining()); if (src.remaining () > 0) { @@ -174,7 +173,8 @@ public abstract class ByteBuffer extends Buffer /** * Writes the content of the the byte array src - * into the buffer. + * into the buffer. Before the transfer, it checks if there is fewer than + * length space remaining in this buffer. * * @param src The array to copy into the buffer. * @param offset The offset within the array of the first byte to be read; @@ -183,18 +183,15 @@ public abstract class ByteBuffer extends Buffer * must be non-negative and no larger than src.length - offset. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining bytes in the source array. + * buffer for the remaining bytes in the source array. * @exception IndexOutOfBoundsException If the preconditions on the offset * and length parameters do not hold * @exception ReadOnlyBufferException If this buffer is read-only. */ public ByteBuffer put (byte[] src, int offset, int length) { - if ((offset < 0) || - (offset > src.length) || - (length < 0) || - (length > src.length - offset)) - throw new IndexOutOfBoundsException (); + checkArraySize(src.length, offset, length); + checkForOverflow(length); for (int i = offset; i < offset + length; i++) put (src [i]); @@ -209,7 +206,7 @@ public abstract class ByteBuffer extends Buffer * @param src The array to copy into the buffer. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining bytes in the source array. + * buffer for the remaining bytes in the source array. * @exception ReadOnlyBufferException If this buffer is read-only. */ public final ByteBuffer put (byte[] src) @@ -239,8 +236,7 @@ public abstract class ByteBuffer extends Buffer if (backing_buffer == null) throw new UnsupportedOperationException (); - if (isReadOnly ()) - throw new ReadOnlyBufferException (); + checkIfReadOnly(); return backing_buffer; } @@ -257,8 +253,7 @@ public abstract class ByteBuffer extends Buffer if (backing_buffer == null) throw new UnsupportedOperationException (); - if (isReadOnly ()) - throw new ReadOnlyBufferException (); + checkIfReadOnly(); return array_offset; } @@ -338,7 +333,7 @@ public abstract class ByteBuffer extends Buffer * and then increments the position. * * @exception BufferUnderflowException If there are no remaining - * bytes in this buffer. + * bytes in this buffer. */ public abstract byte get (); @@ -347,7 +342,7 @@ public abstract class ByteBuffer extends Buffer * and then increments the position. * * @exception BufferOverflowException If there no remaining - * bytes in this buffer. + * bytes in this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ public abstract ByteBuffer put (byte b); diff --git a/libjava/java/nio/ByteBufferHelper.java b/libjava/java/nio/ByteBufferHelper.java index c3dcfbe4f2f..e5c522acd4d 100644 --- a/libjava/java/nio/ByteBufferHelper.java +++ b/libjava/java/nio/ByteBufferHelper.java @@ -42,32 +42,6 @@ package java.nio; */ final class ByteBufferHelper { - private static void checkRemainingForRead (ByteBuffer buffer, int bytes) - { - if (buffer.remaining() < bytes) - throw new BufferUnderflowException(); - } - - private static void checkRemainingForWrite (ByteBuffer buffer, int bytes) - { - if (buffer.remaining() < bytes) - throw new BufferOverflowException(); - } - - private static void checkAvailableForRead (ByteBuffer buffer, - int index, int bytes) - { - if (buffer.limit() < (index + bytes)) - throw new BufferUnderflowException(); - } - - private static void checkAvailableForWrite (ByteBuffer buffer, - int index, int bytes) - { - if (buffer.limit() < (index + bytes)) - throw new BufferOverflowException(); - } - public static char getChar (ByteBuffer buffer, ByteOrder order) { return (char) getShort (buffer, order); @@ -91,7 +65,7 @@ final class ByteBufferHelper public static short getShort (ByteBuffer buffer, ByteOrder order) { - checkRemainingForRead (buffer, 2); + buffer.checkForUnderflow(2); if (order == ByteOrder.LITTLE_ENDIAN) { @@ -105,7 +79,7 @@ final class ByteBufferHelper public static void putShort (ByteBuffer buffer, short value, ByteOrder order) { - checkRemainingForWrite (buffer, 2); + buffer.checkForOverflow(2); if (order == ByteOrder.LITTLE_ENDIAN) { @@ -122,8 +96,6 @@ final class ByteBufferHelper public static short getShort (ByteBuffer buffer, int index, ByteOrder order) { - checkAvailableForRead (buffer, index, 2); - if (order == ByteOrder.LITTLE_ENDIAN) { return (short) ((buffer.get (index) & 0xff) @@ -137,8 +109,6 @@ final class ByteBufferHelper public static void putShort (ByteBuffer buffer, int index, short value, ByteOrder order) { - checkAvailableForWrite (buffer, index, 2); - if (order == ByteOrder.LITTLE_ENDIAN) { buffer.put (index, (byte) value); @@ -153,7 +123,7 @@ final class ByteBufferHelper public static int getInt (ByteBuffer buffer, ByteOrder order) { - checkRemainingForRead (buffer, 4); + buffer.checkForUnderflow(4); if (order == ByteOrder.LITTLE_ENDIAN) { @@ -171,7 +141,7 @@ final class ByteBufferHelper public static void putInt (ByteBuffer buffer, int value, ByteOrder order) { - checkRemainingForWrite (buffer, 4); + buffer.checkForOverflow(4); if (order == ByteOrder.LITTLE_ENDIAN) { @@ -191,8 +161,6 @@ final class ByteBufferHelper public static int getInt (ByteBuffer buffer, int index, ByteOrder order) { - checkAvailableForRead (buffer, index, 4); - if (order == ByteOrder.LITTLE_ENDIAN) { return ((buffer.get (index) & 0xff) @@ -210,8 +178,6 @@ final class ByteBufferHelper public static void putInt (ByteBuffer buffer, int index, int value, ByteOrder order) { - checkAvailableForWrite (buffer, index, 4); - if (order == ByteOrder.LITTLE_ENDIAN) { buffer.put (index, (byte) value); @@ -230,7 +196,7 @@ final class ByteBufferHelper public static long getLong (ByteBuffer buffer, ByteOrder order) { - checkRemainingForRead (buffer, 8); + buffer.checkForUnderflow(8); if (order == ByteOrder.LITTLE_ENDIAN) { @@ -256,7 +222,7 @@ final class ByteBufferHelper public static void putLong (ByteBuffer buffer, long value, ByteOrder order) { - checkRemainingForWrite (buffer, 8); + buffer.checkForOverflow(8); if (order == ByteOrder.LITTLE_ENDIAN) { @@ -284,8 +250,6 @@ final class ByteBufferHelper public static long getLong (ByteBuffer buffer, int index, ByteOrder order) { - checkAvailableForRead (buffer, index, 8); - if (order == ByteOrder.LITTLE_ENDIAN) { return ((buffer.get (index) & 0xff) @@ -311,8 +275,6 @@ final class ByteBufferHelper public static void putLong (ByteBuffer buffer, int index, long value, ByteOrder order) { - checkAvailableForWrite (buffer, index, 8); - if (order == ByteOrder.LITTLE_ENDIAN) { buffer.put (index, (byte) value); diff --git a/libjava/java/nio/ByteBufferImpl.java b/libjava/java/nio/ByteBufferImpl.java index f79ae630acb..7734dbf12cd 100644 --- a/libjava/java/nio/ByteBufferImpl.java +++ b/libjava/java/nio/ByteBufferImpl.java @@ -129,10 +129,16 @@ final class ByteBufferImpl extends ByteBuffer } /** - * Relative get method. Reads the next byte from the buffer. + * Reads the byte at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * bytes in this buffer. */ public byte get () { + checkForUnderflow(); + byte result = backing_buffer [position () + array_offset]; position (position () + 1); return result; @@ -141,13 +147,15 @@ final class ByteBufferImpl extends ByteBuffer /** * Relative put method. Writes value to the next position * in the buffer. - * + * + * @exception BufferOverflowException If there is no remaining + * space in this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ public ByteBuffer put (byte value) { - if (readOnly) - throw new ReadOnlyBufferException (); + checkIfReadOnly(); + checkForOverflow(); int pos = position(); backing_buffer [pos + array_offset] = value; @@ -164,6 +172,8 @@ final class ByteBufferImpl extends ByteBuffer */ public byte get (int index) { + checkIndex(index); + return backing_buffer [index + array_offset]; } @@ -177,9 +187,9 @@ final class ByteBufferImpl extends ByteBuffer */ public ByteBuffer put (int index, byte value) { - if (readOnly) - throw new ReadOnlyBufferException (); - + checkIfReadOnly(); + checkIndex(index); + backing_buffer [index + array_offset] = value; return this; } diff --git a/libjava/java/nio/CharBuffer.java b/libjava/java/nio/CharBuffer.java index 5116c254811..e33c5651188 100644 --- a/libjava/java/nio/CharBuffer.java +++ b/libjava/java/nio/CharBuffer.java @@ -137,8 +137,9 @@ public abstract class CharBuffer extends Buffer } /** - * This method transfers chars from this buffer into the given - * destination array. + * This method transfers chars from this buffer into the given + * destination array. Before the transfer, it checks if there are fewer than + * length chars remaining in this buffer. * * @param dst The destination array * @param offset The offset within the array of the first char @@ -147,12 +148,15 @@ public abstract class CharBuffer extends Buffer * must be non-negative and no larger than dst.length - offset. * * @exception BufferUnderflowException If there are fewer than length - * chars remaining in this buffer. + * chars remaining in this buffer. * @exception IndexOutOfBoundsException If the preconditions on the offset * and length parameters do not hold. */ public CharBuffer get (char[] dst, int offset, int length) { + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); + for (int i = offset; i < offset + length; i++) { dst [i] = get (); @@ -162,13 +166,13 @@ public abstract class CharBuffer extends Buffer } /** - * This method transfers chars from this buffer into the given + * This method transfers chars from this buffer into the given * destination array. * * @param dst The byte array to write into. * * @exception BufferUnderflowException If there are fewer than dst.length - * chars remaining in this buffer. + * chars remaining in this buffer. */ public CharBuffer get (char[] dst) { @@ -177,12 +181,13 @@ public abstract class CharBuffer extends Buffer /** * Writes the content of the the CharBUFFER src - * into the buffer. + * into the buffer. Before the transfer, it checks if there is fewer than + * src.remaining() space remaining in this buffer. * * @param src The source data. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining chars in the source buffer. + * buffer for the remaining chars in the source buffer. * @exception IllegalArgumentException If the source buffer is this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ @@ -191,8 +196,7 @@ public abstract class CharBuffer extends Buffer if (src == this) throw new IllegalArgumentException (); - if (src.remaining () > remaining ()) - throw new BufferOverflowException (); + checkForOverflow(src.remaining()); if (src.remaining () > 0) { @@ -206,7 +210,8 @@ public abstract class CharBuffer extends Buffer /** * Writes the content of the the char array src - * into the buffer. + * into the buffer. Before the transfer, it checks if there is fewer than + * length space remaining in this buffer. * * @param src The array to copy into the buffer. * @param offset The offset within the array of the first byte to be read; @@ -215,22 +220,15 @@ public abstract class CharBuffer extends Buffer * must be non-negative and no larger than src.length - offset. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining chars in the source array. + * buffer for the remaining chars in the source array. * @exception IndexOutOfBoundsException If the preconditions on the offset * and length parameters do not hold * @exception ReadOnlyBufferException If this buffer is read-only. */ public CharBuffer put (char[] src, int offset, int length) { - if (offset < 0 - || offset >= src.length - || length < 0 - || length > (src.length - offset)) - throw new IndexOutOfBoundsException (); - - // Put nothing into this buffer when not enough space left. - if (length > remaining ()) - throw new BufferOverflowException (); + checkArraySize(src.length, offset, length); + checkForOverflow(length); for (int i = offset; i < offset + length; i++) put (src [i]); @@ -245,7 +243,7 @@ public abstract class CharBuffer extends Buffer * @param src The array to copy into the buffer. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining chars in the source array. + * buffer for the remaining chars in the source array. * @exception ReadOnlyBufferException If this buffer is read-only. */ public final CharBuffer put (char[] src) @@ -275,9 +273,8 @@ public abstract class CharBuffer extends Buffer if (backing_buffer == null) throw new UnsupportedOperationException (); - if (isReadOnly ()) - throw new ReadOnlyBufferException (); - + checkIfReadOnly(); + return backing_buffer; } @@ -293,8 +290,7 @@ public abstract class CharBuffer extends Buffer if (backing_buffer == null) throw new UnsupportedOperationException (); - if (isReadOnly ()) - throw new ReadOnlyBufferException (); + checkIfReadOnly(); return array_offset; } @@ -362,7 +358,7 @@ public abstract class CharBuffer extends Buffer * and then increments the position. * * @exception BufferUnderflowException If there are no remaining - * chars in this buffer. + * chars in this buffer. */ public abstract char get (); @@ -371,7 +367,7 @@ public abstract class CharBuffer extends Buffer * and then increments the position. * * @exception BufferOverflowException If there no remaining - * chars in this buffer. + * chars in this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ public abstract CharBuffer put (char b); diff --git a/libjava/java/nio/CharBufferImpl.java b/libjava/java/nio/CharBufferImpl.java index 1a8dff1d07c..2ca44d94459 100644 --- a/libjava/java/nio/CharBufferImpl.java +++ b/libjava/java/nio/CharBufferImpl.java @@ -1,5 +1,5 @@ /* CharBufferImpl.java -- - Copyright (C) 2002, 2003 Free Software Foundation, Inc. + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -116,10 +116,16 @@ final class CharBufferImpl extends CharBuffer } /** - * Relative get method. Reads the next char from the buffer. + * Reads the char at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * chars in this buffer. */ public char get () { + checkForUnderflow(); + char result = backing_buffer [position ()]; position (position () + 1); return result; @@ -133,8 +139,7 @@ final class CharBufferImpl extends CharBuffer */ public CharBuffer put (char value) { - if (readOnly) - throw new ReadOnlyBufferException (); + checkIfReadOnly(); backing_buffer [position ()] = value; position (position () + 1); @@ -145,20 +150,20 @@ final class CharBufferImpl extends CharBuffer * Absolute get method. Reads the char at position * index. * + * @param index Position to read the char from. + * * @exception IndexOutOfBoundsException If index is negative or not smaller * than the buffer's limit. */ public char get (int index) { - if (index < 0 - || index >= limit ()) - throw new IndexOutOfBoundsException (); + checkIndex(index); return backing_buffer [index]; } /** - * Absolute put method. Writes value to position + * Absolute put method. Writes value to position * index in the buffer. * * @exception IndexOutOfBoundsException If index is negative or not smaller @@ -167,12 +172,8 @@ final class CharBufferImpl extends CharBuffer */ public CharBuffer put (int index, char value) { - if (index < 0 - || index >= limit ()) - throw new IndexOutOfBoundsException (); - - if (readOnly) - throw new ReadOnlyBufferException (); + checkIndex(index); + checkIfReadOnly(); backing_buffer [index] = value; return this; diff --git a/libjava/java/nio/CharViewBufferImpl.java b/libjava/java/nio/CharViewBufferImpl.java index ee99cfbe069..3c02108debc 100644 --- a/libjava/java/nio/CharViewBufferImpl.java +++ b/libjava/java/nio/CharViewBufferImpl.java @@ -66,6 +66,13 @@ class CharViewBufferImpl extends CharBuffer this.endian = endian; } + /** + * Reads the char at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * chars in this buffer. + */ public char get () { int p = position(); @@ -74,6 +81,15 @@ class CharViewBufferImpl extends CharBuffer return result; } + /** + * Absolute get method. Reads the char at position + * index. + * + * @param index Position to read the char from. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ public char get (int index) { return ByteBufferHelper.getChar(bb, (index << 1) + offset, endian); diff --git a/libjava/java/nio/DirectByteBufferImpl.java b/libjava/java/nio/DirectByteBufferImpl.java index be0fc52c07d..aad5dca1e5d 100644 --- a/libjava/java/nio/DirectByteBufferImpl.java +++ b/libjava/java/nio/DirectByteBufferImpl.java @@ -86,9 +86,9 @@ final class DirectByteBufferImpl extends ByteBuffer public byte get () { + checkForUnderflow(); + int pos = position(); - if (pos >= limit()) - throw new BufferUnderflowException(); byte result = getImpl (address, pos); position (pos + 1); return result; @@ -96,8 +96,8 @@ final class DirectByteBufferImpl extends ByteBuffer public byte get (int index) { - if (index >= limit()) - throw new BufferUnderflowException(); + checkIndex(index); + return getImpl (address, index); } @@ -106,10 +106,8 @@ final class DirectByteBufferImpl extends ByteBuffer public ByteBuffer get (byte[] dst, int offset, int length) { - if (offset < 0 || length < 0 || offset + length > dst.length) - throw new IndexOutOfBoundsException (); - if (length > remaining()) - throw new BufferUnderflowException(); + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); int index = position(); getImpl(address, index, dst, offset, length); @@ -120,9 +118,10 @@ final class DirectByteBufferImpl extends ByteBuffer public ByteBuffer put (byte value) { + checkIfReadOnly(); + checkForOverflow(); + int pos = position(); - if (pos >= limit()) - throw new BufferUnderflowException(); putImpl (address, pos, value); position (pos + 1); return this; @@ -130,8 +129,9 @@ final class DirectByteBufferImpl extends ByteBuffer public ByteBuffer put (int index, byte value) { - if (index >= limit()) - throw new BufferUnderflowException(); + checkIfReadOnly(); + checkIndex(index); + putImpl (address, index, value); return this; } diff --git a/libjava/java/nio/DoubleBuffer.java b/libjava/java/nio/DoubleBuffer.java index 1ad8baede02..820016b8308 100644 --- a/libjava/java/nio/DoubleBuffer.java +++ b/libjava/java/nio/DoubleBuffer.java @@ -83,8 +83,9 @@ public abstract class DoubleBuffer extends Buffer } /** - * This method transfers doubles from this buffer into the given - * destination array. + * This method transfers doubles from this buffer into the given + * destination array. Before the transfer, it checks if there are fewer than + * length doubles remaining in this buffer. * * @param dst The destination array * @param offset The offset within the array of the first double @@ -93,12 +94,15 @@ public abstract class DoubleBuffer extends Buffer * must be non-negative and no larger than dst.length - offset. * * @exception BufferUnderflowException If there are fewer than length - * doubles remaining in this buffer. + * doubles remaining in this buffer. * @exception IndexOutOfBoundsException If the preconditions on the offset * and length parameters do not hold. */ public DoubleBuffer get (double[] dst, int offset, int length) { + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); + for (int i = offset; i < offset + length; i++) { dst [i] = get (); @@ -108,13 +112,13 @@ public abstract class DoubleBuffer extends Buffer } /** - * This method transfers doubles from this buffer into the given + * This method transfers doubles from this buffer into the given * destination array. * * @param dst The byte array to write into. * * @exception BufferUnderflowException If there are fewer than dst.length - * doubles remaining in this buffer. + * doubles remaining in this buffer. */ public DoubleBuffer get (double[] dst) { @@ -123,12 +127,13 @@ public abstract class DoubleBuffer extends Buffer /** * Writes the content of the the DoubleBUFFER src - * into the buffer. + * into the buffer. Before the transfer, it checks if there is fewer than + * src.remaining() space remaining in this buffer. * * @param src The source data. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining doubles in the source buffer. + * buffer for the remaining doubles in the source buffer. * @exception IllegalArgumentException If the source buffer is this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ @@ -137,8 +142,7 @@ public abstract class DoubleBuffer extends Buffer if (src == this) throw new IllegalArgumentException (); - if (src.remaining () > remaining ()) - throw new BufferOverflowException (); + checkForOverflow(src.remaining ()); if (src.remaining () > 0) { @@ -152,7 +156,8 @@ public abstract class DoubleBuffer extends Buffer /** * Writes the content of the the double array src - * into the buffer. + * into the buffer. Before the transfer, it checks if there is fewer than + * length space remaining in this buffer. * * @param src The array to copy into the buffer. * @param offset The offset within the array of the first byte to be read; @@ -161,13 +166,16 @@ public abstract class DoubleBuffer extends Buffer * must be non-negative and no larger than src.length - offset. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining doubles in the source array. + * buffer for the remaining doubles in the source array. * @exception IndexOutOfBoundsException If the preconditions on the offset * and length parameters do not hold * @exception ReadOnlyBufferException If this buffer is read-only. */ public DoubleBuffer put (double[] src, int offset, int length) { + checkArraySize(src.length, offset, length); + checkForOverflow(length); + for (int i = offset; i < offset + length; i++) put (src [i]); @@ -181,7 +189,7 @@ public abstract class DoubleBuffer extends Buffer * @param src The array to copy into the buffer. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining doubles in the source array. + * buffer for the remaining doubles in the source array. * @exception ReadOnlyBufferException If this buffer is read-only. */ public final DoubleBuffer put (double[] src) @@ -211,8 +219,7 @@ public abstract class DoubleBuffer extends Buffer if (backing_buffer == null) throw new UnsupportedOperationException (); - if (isReadOnly ()) - throw new ReadOnlyBufferException (); + checkIfReadOnly(); return backing_buffer; } @@ -229,8 +236,7 @@ public abstract class DoubleBuffer extends Buffer if (backing_buffer == null) throw new UnsupportedOperationException (); - if (isReadOnly ()) - throw new ReadOnlyBufferException (); + checkIfReadOnly(); return array_offset; } @@ -298,7 +304,7 @@ public abstract class DoubleBuffer extends Buffer * and then increments the position. * * @exception BufferUnderflowException If there are no remaining - * doubles in this buffer. + * doubles in this buffer. */ public abstract double get (); @@ -307,7 +313,7 @@ public abstract class DoubleBuffer extends Buffer * and then increments the position. * * @exception BufferOverflowException If there no remaining - * doubles in this buffer. + * doubles in this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ public abstract DoubleBuffer put (double b); diff --git a/libjava/java/nio/DoubleBufferImpl.java b/libjava/java/nio/DoubleBufferImpl.java index 81fde6db6e6..504ee8d61b0 100644 --- a/libjava/java/nio/DoubleBufferImpl.java +++ b/libjava/java/nio/DoubleBufferImpl.java @@ -1,5 +1,5 @@ /* DoubleBufferImpl.java -- - Copyright (C) 2002, 2003 Free Software Foundation, Inc. + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -98,10 +98,16 @@ final class DoubleBufferImpl extends DoubleBuffer } /** - * Relative get method. Reads the next double from the buffer. + * Reads the double at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * doubles in this buffer. */ public double get () { + checkForUnderflow(); + double result = backing_buffer [position ()]; position (position () + 1); return result; @@ -110,13 +116,15 @@ final class DoubleBufferImpl extends DoubleBuffer /** * Relative put method. Writes value to the next position * in the buffer. - * + * + * @exception BufferOverflowException If there no remaining + * space in this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ public DoubleBuffer put (double value) { - if (readOnly) - throw new ReadOnlyBufferException (); + checkIfReadOnly(); + checkForOverflow(); backing_buffer [position ()] = value; position (position () + 1); @@ -132,6 +140,8 @@ final class DoubleBufferImpl extends DoubleBuffer */ public double get (int index) { + checkIndex(index); + return backing_buffer [index]; } @@ -145,9 +155,9 @@ final class DoubleBufferImpl extends DoubleBuffer */ public DoubleBuffer put (int index, double value) { - if (readOnly) - throw new ReadOnlyBufferException (); - + checkIfReadOnly(); + checkIndex(index); + backing_buffer [index] = value; return this; } diff --git a/libjava/java/nio/DoubleViewBufferImpl.java b/libjava/java/nio/DoubleViewBufferImpl.java index 7b04e4ca108..d23b14ab472 100644 --- a/libjava/java/nio/DoubleViewBufferImpl.java +++ b/libjava/java/nio/DoubleViewBufferImpl.java @@ -66,6 +66,13 @@ final class DoubleViewBufferImpl extends DoubleBuffer this.endian = endian; } + /** + * Reads the double at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * doubles in this buffer. + */ public double get () { int p = position(); @@ -74,6 +81,13 @@ final class DoubleViewBufferImpl extends DoubleBuffer return result; } + /** + * Absolute get method. Reads the double at position + * index. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ public double get (int index) { return ByteBufferHelper.getDouble(bb, (index << 3) + offset, endian); diff --git a/libjava/java/nio/FloatBuffer.java b/libjava/java/nio/FloatBuffer.java index ab87b7f898f..2425f0c71d6 100644 --- a/libjava/java/nio/FloatBuffer.java +++ b/libjava/java/nio/FloatBuffer.java @@ -83,8 +83,9 @@ public abstract class FloatBuffer extends Buffer } /** - * This method transfers floats from this buffer into the given - * destination array. + * This method transfers floats from this buffer into the given + * destination array. Before the transfer, it checks if there are fewer than + * length floats remaining in this buffer. * * @param dst The destination array * @param offset The offset within the array of the first float @@ -93,12 +94,15 @@ public abstract class FloatBuffer extends Buffer * must be non-negative and no larger than dst.length - offset. * * @exception BufferUnderflowException If there are fewer than length - * floats remaining in this buffer. + * floats remaining in this buffer. * @exception IndexOutOfBoundsException If the preconditions on the offset * and length parameters do not hold. */ public FloatBuffer get (float[] dst, int offset, int length) { + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); + for (int i = offset; i < offset + length; i++) { dst [i] = get (); @@ -108,13 +112,13 @@ public abstract class FloatBuffer extends Buffer } /** - * This method transfers floats from this buffer into the given + * This method transfers floats from this buffer into the given * destination array. * * @param dst The byte array to write into. * * @exception BufferUnderflowException If there are fewer than dst.length - * floats remaining in this buffer. + * floats remaining in this buffer. */ public FloatBuffer get (float[] dst) { @@ -123,12 +127,13 @@ public abstract class FloatBuffer extends Buffer /** * Writes the content of the the FloatBUFFER src - * into the buffer. + * into the buffer. Before the transfer, it checks if there is fewer than + * src.remaining() space remaining in this buffer. * * @param src The source data. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining floats in the source buffer. + * buffer for the remaining floats in the source buffer. * @exception IllegalArgumentException If the source buffer is this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ @@ -137,8 +142,7 @@ public abstract class FloatBuffer extends Buffer if (src == this) throw new IllegalArgumentException (); - if (src.remaining () > remaining ()) - throw new BufferOverflowException (); + checkForOverflow(src.remaining()); if (src.remaining () > 0) { @@ -152,7 +156,8 @@ public abstract class FloatBuffer extends Buffer /** * Writes the content of the the float array src - * into the buffer. + * into the buffer. Before the transfer, it checks if there is fewer than + * length space remaining in this buffer. * * @param src The array to copy into the buffer. * @param offset The offset within the array of the first byte to be read; @@ -161,13 +166,16 @@ public abstract class FloatBuffer extends Buffer * must be non-negative and no larger than src.length - offset. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining floats in the source array. + * buffer for the remaining floats in the source array. * @exception IndexOutOfBoundsException If the preconditions on the offset * and length parameters do not hold * @exception ReadOnlyBufferException If this buffer is read-only. */ public FloatBuffer put (float[] src, int offset, int length) { + checkArraySize(src.length, offset, length); + checkForOverflow(length); + for (int i = offset; i < offset + length; i++) put (src [i]); @@ -181,7 +189,7 @@ public abstract class FloatBuffer extends Buffer * @param src The array to copy into the buffer. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining floats in the source array. + * buffer for the remaining floats in the source array. * @exception ReadOnlyBufferException If this buffer is read-only. */ public final FloatBuffer put (float[] src) @@ -211,8 +219,7 @@ public abstract class FloatBuffer extends Buffer if (backing_buffer == null) throw new UnsupportedOperationException (); - if (isReadOnly ()) - throw new ReadOnlyBufferException (); + checkIfReadOnly(); return backing_buffer; } @@ -229,8 +236,7 @@ public abstract class FloatBuffer extends Buffer if (backing_buffer == null) throw new UnsupportedOperationException (); - if (isReadOnly ()) - throw new ReadOnlyBufferException (); + checkIfReadOnly(); return array_offset; } @@ -298,7 +304,7 @@ public abstract class FloatBuffer extends Buffer * and then increments the position. * * @exception BufferUnderflowException If there are no remaining - * floats in this buffer. + * floats in this buffer. */ public abstract float get (); @@ -307,7 +313,7 @@ public abstract class FloatBuffer extends Buffer * and then increments the position. * * @exception BufferOverflowException If there no remaining - * floats in this buffer. + * floats in this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ public abstract FloatBuffer put (float b); diff --git a/libjava/java/nio/FloatBufferImpl.java b/libjava/java/nio/FloatBufferImpl.java index 47479845da6..a9eb7c1de8a 100644 --- a/libjava/java/nio/FloatBufferImpl.java +++ b/libjava/java/nio/FloatBufferImpl.java @@ -1,5 +1,5 @@ /* FloatBufferImpl.java -- - Copyright (C) 2002, 2003 Free Software Foundation, Inc. + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -98,10 +98,16 @@ final class FloatBufferImpl extends FloatBuffer } /** - * Relative get method. Reads the next float from the buffer. + * Reads the float at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * floats in this buffer. */ public float get () { + checkForUnderflow(); + float result = backing_buffer [position ()]; position (position () + 1); return result; @@ -111,13 +117,15 @@ final class FloatBufferImpl extends FloatBuffer * Relative put method. Writes value to the next position * in the buffer. * + * @exception BufferOverflowException If there no remaining + * space in this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ public FloatBuffer put (float value) { - if (readOnly) - throw new ReadOnlyBufferException (); - + checkIfReadOnly(); + checkForOverflow(); + backing_buffer [position ()] = value; position (position () + 1); return this; @@ -132,6 +140,8 @@ final class FloatBufferImpl extends FloatBuffer */ public float get (int index) { + checkIndex(index); + return backing_buffer [index]; } @@ -145,9 +155,9 @@ final class FloatBufferImpl extends FloatBuffer */ public FloatBuffer put (int index, float value) { - if (readOnly) - throw new ReadOnlyBufferException (); - + checkIfReadOnly(); + checkIndex(index); + backing_buffer [index] = value; return this; } diff --git a/libjava/java/nio/FloatViewBufferImpl.java b/libjava/java/nio/FloatViewBufferImpl.java index 08c59097d37..40b7339dd4e 100644 --- a/libjava/java/nio/FloatViewBufferImpl.java +++ b/libjava/java/nio/FloatViewBufferImpl.java @@ -66,6 +66,13 @@ final class FloatViewBufferImpl extends FloatBuffer this.endian = endian; } + /** + * Reads the float at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * floats in this buffer. + */ public float get () { int p = position(); @@ -74,6 +81,13 @@ final class FloatViewBufferImpl extends FloatBuffer return result; } + /** + * Absolute get method. Reads the float at position + * index. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ public float get (int index) { return ByteBufferHelper.getFloat(bb, (index << 2) + offset, endian); diff --git a/libjava/java/nio/IntBuffer.java b/libjava/java/nio/IntBuffer.java index 52d822aa81d..825132430e6 100644 --- a/libjava/java/nio/IntBuffer.java +++ b/libjava/java/nio/IntBuffer.java @@ -83,8 +83,9 @@ public abstract class IntBuffer extends Buffer } /** - * This method transfers ints from this buffer into the given - * destination array. + * This method transfers ints from this buffer into the given + * destination array. Before the transfer, it checks if there are fewer than + * length ints remaining in this buffer. * * @param dst The destination array * @param offset The offset within the array of the first int @@ -93,12 +94,15 @@ public abstract class IntBuffer extends Buffer * must be non-negative and no larger than dst.length - offset. * * @exception BufferUnderflowException If there are fewer than length - * ints remaining in this buffer. + * ints remaining in this buffer. * @exception IndexOutOfBoundsException If the preconditions on the offset * and length parameters do not hold. */ public IntBuffer get (int[] dst, int offset, int length) { + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); + for (int i = offset; i < offset + length; i++) { dst [i] = get (); @@ -108,13 +112,13 @@ public abstract class IntBuffer extends Buffer } /** - * This method transfers ints from this buffer into the given + * This method transfers ints from this buffer into the given * destination array. * * @param dst The byte array to write into. * * @exception BufferUnderflowException If there are fewer than dst.length - * ints remaining in this buffer. + * ints remaining in this buffer. */ public IntBuffer get (int[] dst) { @@ -123,12 +127,13 @@ public abstract class IntBuffer extends Buffer /** * Writes the content of the the IntBUFFER src - * into the buffer. + * into the buffer. Before the transfer, it checks if there is fewer than + * src.remaining() space remaining in this buffer. * * @param src The source data. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining ints in the source buffer. + * buffer for the remaining ints in the source buffer. * @exception IllegalArgumentException If the source buffer is this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ @@ -137,8 +142,7 @@ public abstract class IntBuffer extends Buffer if (src == this) throw new IllegalArgumentException (); - if (src.remaining () > remaining ()) - throw new BufferOverflowException (); + checkForOverflow(src.remaining ()); if (src.remaining () > 0) { @@ -152,7 +156,8 @@ public abstract class IntBuffer extends Buffer /** * Writes the content of the the int array src - * into the buffer. + * into the buffer. Before the transfer, it checks if there is fewer than + * length space remaining in this buffer. * * @param src The array to copy into the buffer. * @param offset The offset within the array of the first byte to be read; @@ -161,13 +166,16 @@ public abstract class IntBuffer extends Buffer * must be non-negative and no larger than src.length - offset. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining ints in the source array. + * buffer for the remaining ints in the source array. * @exception IndexOutOfBoundsException If the preconditions on the offset * and length parameters do not hold * @exception ReadOnlyBufferException If this buffer is read-only. */ public IntBuffer put (int[] src, int offset, int length) { + checkArraySize(src.length, offset, length); + checkForOverflow(length); + for (int i = offset; i < offset + length; i++) put (src [i]); @@ -181,7 +189,7 @@ public abstract class IntBuffer extends Buffer * @param src The array to copy into the buffer. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining ints in the source array. + * buffer for the remaining ints in the source array. * @exception ReadOnlyBufferException If this buffer is read-only. */ public final IntBuffer put (int[] src) @@ -211,8 +219,7 @@ public abstract class IntBuffer extends Buffer if (backing_buffer == null) throw new UnsupportedOperationException (); - if (isReadOnly ()) - throw new ReadOnlyBufferException (); + checkIfReadOnly(); return backing_buffer; } @@ -229,8 +236,7 @@ public abstract class IntBuffer extends Buffer if (backing_buffer == null) throw new UnsupportedOperationException (); - if (isReadOnly ()) - throw new ReadOnlyBufferException (); + checkIfReadOnly(); return array_offset; } @@ -298,7 +304,7 @@ public abstract class IntBuffer extends Buffer * and then increments the position. * * @exception BufferUnderflowException If there are no remaining - * ints in this buffer. + * ints in this buffer. */ public abstract int get (); @@ -307,7 +313,7 @@ public abstract class IntBuffer extends Buffer * and then increments the position. * * @exception BufferOverflowException If there no remaining - * ints in this buffer. + * ints in this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ public abstract IntBuffer put (int b); diff --git a/libjava/java/nio/IntBufferImpl.java b/libjava/java/nio/IntBufferImpl.java index a491c1105c4..f68dd92fa04 100644 --- a/libjava/java/nio/IntBufferImpl.java +++ b/libjava/java/nio/IntBufferImpl.java @@ -1,5 +1,5 @@ /* IntBufferImpl.java -- - Copyright (C) 2002, 2003 Free Software Foundation, Inc. + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -98,10 +98,16 @@ final class IntBufferImpl extends IntBuffer } /** - * Relative get method. Reads the next int from the buffer. + * Reads the int at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * ints in this buffer. */ public int get () { + checkForUnderflow(); + int result = backing_buffer [position ()]; position (position () + 1); return result; @@ -110,14 +116,16 @@ final class IntBufferImpl extends IntBuffer /** * Relative put method. Writes value to the next position * in the buffer. - * + * + * @exception BufferOverflowException If there no remaining + * space in this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ public IntBuffer put (int value) { - if (readOnly) - throw new ReadOnlyBufferException (); - + checkIfReadOnly(); + checkForOverflow(); + backing_buffer [position ()] = value; position (position () + 1); return this; @@ -132,6 +140,8 @@ final class IntBufferImpl extends IntBuffer */ public int get (int index) { + checkIndex(index); + return backing_buffer [index]; } @@ -145,9 +155,9 @@ final class IntBufferImpl extends IntBuffer */ public IntBuffer put (int index, int value) { - if (readOnly) - throw new ReadOnlyBufferException (); - + checkIfReadOnly(); + checkIndex(index); + backing_buffer [index] = value; return this; } diff --git a/libjava/java/nio/IntViewBufferImpl.java b/libjava/java/nio/IntViewBufferImpl.java index 074953793be..1f3f9348aae 100644 --- a/libjava/java/nio/IntViewBufferImpl.java +++ b/libjava/java/nio/IntViewBufferImpl.java @@ -66,6 +66,13 @@ final class IntViewBufferImpl extends IntBuffer this.endian = endian; } + /** + * Reads the int at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * ints in this buffer. + */ public int get () { int p = position(); @@ -74,6 +81,13 @@ final class IntViewBufferImpl extends IntBuffer return result; } + /** + * Absolute get method. Reads the int at position + * index. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ public int get (int index) { return ByteBufferHelper.getInt(bb, (index << 2) + offset, endian); diff --git a/libjava/java/nio/LongBuffer.java b/libjava/java/nio/LongBuffer.java index 1b420eb5ed4..f22ab14de2f 100644 --- a/libjava/java/nio/LongBuffer.java +++ b/libjava/java/nio/LongBuffer.java @@ -83,8 +83,9 @@ public abstract class LongBuffer extends Buffer } /** - * This method transfers longs from this buffer into the given - * destination array. + * This method transfers longs from this buffer into the given + * destination array. Before the transfer, it checks if there are fewer than + * length longs remaining in this buffer. * * @param dst The destination array * @param offset The offset within the array of the first long @@ -93,12 +94,15 @@ public abstract class LongBuffer extends Buffer * must be non-negative and no larger than dst.length - offset. * * @exception BufferUnderflowException If there are fewer than length - * longs remaining in this buffer. + * longs remaining in this buffer. * @exception IndexOutOfBoundsException If the preconditions on the offset * and length parameters do not hold. */ public LongBuffer get (long[] dst, int offset, int length) { + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); + for (int i = offset; i < offset + length; i++) { dst [i] = get (); @@ -108,13 +112,13 @@ public abstract class LongBuffer extends Buffer } /** - * This method transfers longs from this buffer into the given + * This method transfers longs from this buffer into the given * destination array. * * @param dst The byte array to write into. * * @exception BufferUnderflowException If there are fewer than dst.length - * longs remaining in this buffer. + * longs remaining in this buffer. */ public LongBuffer get (long[] dst) { @@ -123,12 +127,13 @@ public abstract class LongBuffer extends Buffer /** * Writes the content of the the LongBUFFER src - * into the buffer. + * into the buffer. Before the transfer, it checks if there is fewer than + * src.remaining() space remaining in this buffer. * * @param src The source data. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining longs in the source buffer. + * buffer for the remaining longs in the source buffer. * @exception IllegalArgumentException If the source buffer is this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ @@ -137,8 +142,7 @@ public abstract class LongBuffer extends Buffer if (src == this) throw new IllegalArgumentException (); - if (src.remaining () > remaining ()) - throw new BufferOverflowException (); + checkForOverflow(src.remaining ()); if (src.remaining () > 0) { @@ -152,7 +156,8 @@ public abstract class LongBuffer extends Buffer /** * Writes the content of the the long array src - * into the buffer. + * into the buffer. Before the transfer, it checks if there is fewer than + * length space remaining in this buffer. * * @param src The array to copy into the buffer. * @param offset The offset within the array of the first byte to be read; @@ -161,13 +166,16 @@ public abstract class LongBuffer extends Buffer * must be non-negative and no larger than src.length - offset. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining longs in the source array. + * buffer for the remaining longs in the source array. * @exception IndexOutOfBoundsException If the preconditions on the offset * and length parameters do not hold * @exception ReadOnlyBufferException If this buffer is read-only. */ public LongBuffer put (long[] src, int offset, int length) { + checkArraySize(src.length, offset, length); + checkForOverflow(length); + for (int i = offset; i < offset + length; i++) put (src [i]); @@ -181,7 +189,7 @@ public abstract class LongBuffer extends Buffer * @param src The array to copy into the buffer. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining longs in the source array. + * buffer for the remaining longs in the source array. * @exception ReadOnlyBufferException If this buffer is read-only. */ public final LongBuffer put (long[] src) @@ -211,8 +219,7 @@ public abstract class LongBuffer extends Buffer if (backing_buffer == null) throw new UnsupportedOperationException (); - if (isReadOnly ()) - throw new ReadOnlyBufferException (); + checkIfReadOnly(); return backing_buffer; } @@ -229,8 +236,7 @@ public abstract class LongBuffer extends Buffer if (backing_buffer == null) throw new UnsupportedOperationException (); - if (isReadOnly ()) - throw new ReadOnlyBufferException (); + checkIfReadOnly(); return array_offset; } @@ -298,7 +304,7 @@ public abstract class LongBuffer extends Buffer * and then increments the position. * * @exception BufferUnderflowException If there are no remaining - * longs in this buffer. + * longs in this buffer. */ public abstract long get (); @@ -307,7 +313,7 @@ public abstract class LongBuffer extends Buffer * and then increments the position. * * @exception BufferOverflowException If there no remaining - * longs in this buffer. + * longs in this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ public abstract LongBuffer put (long b); diff --git a/libjava/java/nio/LongBufferImpl.java b/libjava/java/nio/LongBufferImpl.java index 88a9d8c5415..df720eec62c 100644 --- a/libjava/java/nio/LongBufferImpl.java +++ b/libjava/java/nio/LongBufferImpl.java @@ -1,5 +1,5 @@ /* LongBufferImpl.java -- - Copyright (C) 2002, 2003 Free Software Foundation, Inc. + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -98,10 +98,16 @@ final class LongBufferImpl extends LongBuffer } /** - * Relative get method. Reads the next long from the buffer. + * Reads the long at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * longs in this buffer. */ public long get () { + checkForUnderflow(); + long result = backing_buffer [position ()]; position (position () + 1); return result; @@ -110,14 +116,16 @@ final class LongBufferImpl extends LongBuffer /** * Relative put method. Writes value to the next position * in the buffer. - * + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ public LongBuffer put (long value) { - if (readOnly) - throw new ReadOnlyBufferException (); - + checkIfReadOnly(); + checkForOverflow(); + backing_buffer [position ()] = value; position (position () + 1); return this; @@ -132,6 +140,8 @@ final class LongBufferImpl extends LongBuffer */ public long get (int index) { + checkIndex(index); + return backing_buffer [index]; } @@ -145,9 +155,9 @@ final class LongBufferImpl extends LongBuffer */ public LongBuffer put (int index, long value) { - if (readOnly) - throw new ReadOnlyBufferException (); - + checkIfReadOnly(); + checkIndex(index); + backing_buffer [index] = value; return this; } diff --git a/libjava/java/nio/LongViewBufferImpl.java b/libjava/java/nio/LongViewBufferImpl.java index d1dd060d311..8762578a7bf 100644 --- a/libjava/java/nio/LongViewBufferImpl.java +++ b/libjava/java/nio/LongViewBufferImpl.java @@ -66,6 +66,13 @@ final class LongViewBufferImpl extends LongBuffer this.endian = endian; } + /** + * Reads the long at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * longs in this buffer. + */ public long get () { int p = position(); @@ -74,6 +81,13 @@ final class LongViewBufferImpl extends LongBuffer return result; } + /** + * Absolute get method. Reads the long at position + * index. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ public long get (int index) { return ByteBufferHelper.getLong(bb, (index << 3) + offset, endian); diff --git a/libjava/java/nio/MappedByteBufferImpl.java b/libjava/java/nio/MappedByteBufferImpl.java index 5932c99f6d0..5ed579bb0e9 100644 --- a/libjava/java/nio/MappedByteBufferImpl.java +++ b/libjava/java/nio/MappedByteBufferImpl.java @@ -68,9 +68,9 @@ final class MappedByteBufferImpl extends MappedByteBuffer public byte get () { + checkForUnderflow(); + int pos = position(); - if (pos >= limit()) - throw new BufferUnderflowException(); byte result = DirectByteBufferImpl.getImpl(address, pos); position (pos + 1); return result; @@ -78,9 +78,10 @@ final class MappedByteBufferImpl extends MappedByteBuffer public ByteBuffer put (byte value) { + checkIfReadOnly(); + checkForOverflow(); + int pos = position(); - if (pos >= limit()) - throw new BufferUnderflowException(); DirectByteBufferImpl.putImpl(address, pos, value); position(pos + 1); return this; @@ -88,17 +89,15 @@ final class MappedByteBufferImpl extends MappedByteBuffer public byte get (int index) { - if (index >= limit()) - throw new BufferUnderflowException(); + checkIndex(index); + return DirectByteBufferImpl.getImpl(address, index); } public ByteBuffer get (byte[] dst, int offset, int length) { - if (offset < 0 || length < 0 || offset + length > dst.length) - throw new IndexOutOfBoundsException (); - if (length > remaining()) - throw new BufferUnderflowException(); + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); int index = position(); DirectByteBufferImpl.getImpl(address, index, dst, offset, length); @@ -109,8 +108,9 @@ final class MappedByteBufferImpl extends MappedByteBuffer public ByteBuffer put (int index, byte value) { - if (index >= limit()) - throw new BufferUnderflowException(); + checkIfReadOnly(); + checkIndex(index); + DirectByteBufferImpl.putImpl(address, index, value); return this; } diff --git a/libjava/java/nio/ShortBuffer.java b/libjava/java/nio/ShortBuffer.java index 9f542769fac..03ddb73e5ae 100644 --- a/libjava/java/nio/ShortBuffer.java +++ b/libjava/java/nio/ShortBuffer.java @@ -83,8 +83,9 @@ public abstract class ShortBuffer extends Buffer } /** - * This method transfers shorts from this buffer into the given - * destination array. + * This method transfers shorts from this buffer into the given + * destination array. Before the transfer, it checks if there are fewer than + * length shorts remaining in this buffer. * * @param dst The destination array * @param offset The offset within the array of the first short @@ -93,12 +94,15 @@ public abstract class ShortBuffer extends Buffer * must be non-negative and no larger than dst.length - offset. * * @exception BufferUnderflowException If there are fewer than length - * shorts remaining in this buffer. + * shorts remaining in this buffer. * @exception IndexOutOfBoundsException If the preconditions on the offset * and length parameters do not hold. */ public ShortBuffer get (short[] dst, int offset, int length) { + checkArraySize(dst.length, offset, length); + checkForUnderflow(length); + for (int i = offset; i < offset + length; i++) { dst [i] = get (); @@ -108,13 +112,13 @@ public abstract class ShortBuffer extends Buffer } /** - * This method transfers shorts from this buffer into the given + * This method transfers shorts from this buffer into the given * destination array. * * @param dst The byte array to write into. * * @exception BufferUnderflowException If there are fewer than dst.length - * shorts remaining in this buffer. + * shorts remaining in this buffer. */ public ShortBuffer get (short[] dst) { @@ -123,12 +127,13 @@ public abstract class ShortBuffer extends Buffer /** * Writes the content of the the ShortBUFFER src - * into the buffer. + * into the buffer. Before the transfer, it checks if there is fewer than + * src.remaining() space remaining in this buffer. * * @param src The source data. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining shorts in the source buffer. + * buffer for the remaining shorts in the source buffer. * @exception IllegalArgumentException If the source buffer is this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ @@ -137,8 +142,7 @@ public abstract class ShortBuffer extends Buffer if (src == this) throw new IllegalArgumentException (); - if (src.remaining () > remaining ()) - throw new BufferOverflowException (); + checkForOverflow(src.remaining ()); if (src.remaining () > 0) { @@ -152,7 +156,8 @@ public abstract class ShortBuffer extends Buffer /** * Writes the content of the the short array src - * into the buffer. + * into the buffer. Before the transfer, it checks if there is fewer than + * length space remaining in this buffer. * * @param src The array to copy into the buffer. * @param offset The offset within the array of the first byte to be read; @@ -161,13 +166,16 @@ public abstract class ShortBuffer extends Buffer * must be non-negative and no larger than src.length - offset. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining shorts in the source array. + * buffer for the remaining shorts in the source array. * @exception IndexOutOfBoundsException If the preconditions on the offset * and length parameters do not hold * @exception ReadOnlyBufferException If this buffer is read-only. */ public ShortBuffer put (short[] src, int offset, int length) { + checkArraySize(src.length, offset, length); + checkForOverflow(length); + for (int i = offset; i < offset + length; i++) put (src [i]); @@ -181,7 +189,7 @@ public abstract class ShortBuffer extends Buffer * @param src The array to copy into the buffer. * * @exception BufferOverflowException If there is insufficient space in this - * buffer for the remaining shorts in the source array. + * buffer for the remaining shorts in the source array. * @exception ReadOnlyBufferException If this buffer is read-only. */ public final ShortBuffer put (short[] src) @@ -211,8 +219,7 @@ public abstract class ShortBuffer extends Buffer if (backing_buffer == null) throw new UnsupportedOperationException (); - if (isReadOnly ()) - throw new ReadOnlyBufferException (); + checkIfReadOnly(); return backing_buffer; } @@ -229,8 +236,7 @@ public abstract class ShortBuffer extends Buffer if (backing_buffer == null) throw new UnsupportedOperationException (); - if (isReadOnly ()) - throw new ReadOnlyBufferException (); + checkIfReadOnly(); return array_offset; } @@ -298,7 +304,7 @@ public abstract class ShortBuffer extends Buffer * and then increments the position. * * @exception BufferUnderflowException If there are no remaining - * shorts in this buffer. + * shorts in this buffer. */ public abstract short get (); @@ -307,7 +313,7 @@ public abstract class ShortBuffer extends Buffer * and then increments the position. * * @exception BufferOverflowException If there no remaining - * shorts in this buffer. + * shorts in this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ public abstract ShortBuffer put (short b); diff --git a/libjava/java/nio/ShortBufferImpl.java b/libjava/java/nio/ShortBufferImpl.java index 6871f096e9d..28f6efbd804 100644 --- a/libjava/java/nio/ShortBufferImpl.java +++ b/libjava/java/nio/ShortBufferImpl.java @@ -1,5 +1,5 @@ /* ShortBufferImpl.java -- - Copyright (C) 2002, 2003 Free Software Foundation, Inc. + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -98,10 +98,16 @@ final class ShortBufferImpl extends ShortBuffer } /** - * Relative get method. Reads the next short from the buffer. + * Reads the short at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * shorts in this buffer. */ public short get () { + checkForUnderflow(); + short result = backing_buffer [position ()]; position (position () + 1); return result; @@ -110,14 +116,16 @@ final class ShortBufferImpl extends ShortBuffer /** * Relative put method. Writes value to the next position * in the buffer. - * + * + * @exception BufferOverflowException If there no remaining + * space in this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ public ShortBuffer put (short value) { - if (readOnly) - throw new ReadOnlyBufferException (); - + checkIfReadOnly(); + checkForOverflow(); + backing_buffer [position ()] = value; position (position () + 1); return this; @@ -132,6 +140,8 @@ final class ShortBufferImpl extends ShortBuffer */ public short get (int index) { + checkIndex(index); + return backing_buffer [index]; } @@ -145,9 +155,9 @@ final class ShortBufferImpl extends ShortBuffer */ public ShortBuffer put (int index, short value) { - if (readOnly) - throw new ReadOnlyBufferException (); - + checkIfReadOnly(); + checkIndex(index); + backing_buffer [index] = value; return this; } diff --git a/libjava/java/nio/ShortViewBufferImpl.java b/libjava/java/nio/ShortViewBufferImpl.java index 26aabad519c..a9d086d2127 100644 --- a/libjava/java/nio/ShortViewBufferImpl.java +++ b/libjava/java/nio/ShortViewBufferImpl.java @@ -66,6 +66,13 @@ final class ShortViewBufferImpl extends ShortBuffer this.endian = endian; } + /** + * Reads the short at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * shorts in this buffer. + */ public short get () { int p = position(); @@ -74,6 +81,13 @@ final class ShortViewBufferImpl extends ShortBuffer return result; } + /** + * Absolute get method. Reads the short at position + * index. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ public short get (int index) { return ByteBufferHelper.getShort(bb, (index << 1) + offset, endian); -- 2.30.2