From: Michael Koch Date: Wed, 12 Feb 2003 08:01:10 +0000 (+0000) Subject: 2003-02-12 Michael Koch X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=ad2e63d5df78b30935e27389a237330ba8c3a34e;p=gcc.git 2003-02-12 Michael Koch * java/nio/ByteBuffer.java (allocate): Implemented. (wrap): Implemented. * java/nio/CharBuffer.java: Some documentation added and reworked. (endian): Removed. (allocate): Implemented. (wrap): Implemented. (array): Throw exceptions. (arrayOffset): Throw exceptions. (toString): Implemented. (length): Implemented. (put): Implemented. (charAt): Implemented. From-SVN: r62760 --- diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 48c09fc17b2..7c205a9c18d 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,20 @@ +2003-02-12 Michael Koch + + * java/nio/ByteBuffer.java + (allocate): Implemented. + (wrap): Implemented. + * java/nio/CharBuffer.java: + Some documentation added and reworked. + (endian): Removed. + (allocate): Implemented. + (wrap): Implemented. + (array): Throw exceptions. + (arrayOffset): Throw exceptions. + (toString): Implemented. + (length): Implemented. + (put): Implemented. + (charAt): Implemented. + 2003-02-11 John Leuner * java/util/zip/ZipInputStream.java: Fix problem with 0-length diff --git a/libjava/java/nio/ByteBuffer.java b/libjava/java/nio/ByteBuffer.java index b46b320cbbd..6ca10db1f29 100644 --- a/libjava/java/nio/ByteBuffer.java +++ b/libjava/java/nio/ByteBuffer.java @@ -37,6 +37,8 @@ exception statement from your version. */ package java.nio; +import gnu.java.nio.ByteBufferImpl; + /** * @since 1.4 */ @@ -58,7 +60,7 @@ public abstract class ByteBuffer extends Buffer implements Comparable */ public static ByteBuffer allocate (int capacity) { - return null; + return new ByteBufferImpl (capacity, 0, capacity); } /** @@ -69,7 +71,7 @@ public abstract class ByteBuffer extends Buffer implements Comparable */ final public static ByteBuffer wrap (byte[] array, int offset, int length) { - return null; + return new ByteBufferImpl (array, offset, length); } /** diff --git a/libjava/java/nio/CharBuffer.java b/libjava/java/nio/CharBuffer.java index 34aedebbd43..e3a3e951c53 100644 --- a/libjava/java/nio/CharBuffer.java +++ b/libjava/java/nio/CharBuffer.java @@ -37,57 +37,73 @@ exception statement from your version. */ package java.nio; +import gnu.java.nio.CharBufferImpl; + +/** + * @since 1.4 + */ public abstract class CharBuffer extends Buffer implements Cloneable, CharSequence { - private ByteOrder endian = ByteOrder.BIG_ENDIAN; - protected char [] backing_buffer; + /** + * Allocates a new CharBuffer object with a given capacity. + */ public static CharBuffer allocate (int capacity) { - return null; + return new CharBufferImpl (capacity, 0, capacity); } /** + * Wraps a character array into a CharBuffer object. + * * @exception IndexOutOfBoundsException If the preconditions on the offset * and length parameters do not hold */ final public static CharBuffer wrap (char[] array, int offset, int length) { - if ((offset < 0) || - (offset > array.length) || - (length < 0) || - (length > (array.length - offset))) - throw new IndexOutOfBoundsException (); - - return null; + return new CharBufferImpl (array, offset, offset + length); } + /** + * Wraps a character sequence into a CharBuffer object. + */ final public static CharBuffer wrap (CharSequence a) { return wrap (a, 0, a.length ()); } /** + * Wraps a character sequence into a CharBuffer object. + * * @exception IndexOutOfBoundsException If the preconditions on the offset * and length parameters do not hold */ final public static CharBuffer wrap (CharSequence a, int offset, int length) { - char [] buffer = new char [length]; + if ((offset < 0) + || (offset > a.length ()) + || (length < 0) + || (length > (a.length () - offset))) + throw new IndexOutOfBoundsException (); + + char [] buffer = new char [a.length ()]; for (int i = offset; i < length; i++) { buffer [i] = a.charAt (i); } - return wrap (buffer, 0, length); + return wrap (buffer, offset, length); } + /** + * Wraps a character array into a CharBuffer object. + */ final public static CharBuffer wrap (char[] array) { - return wrap (array, 0, array.length); + return wrap (array, 0, array.length); } CharBuffer (int cap, int lim, int pos, int mark) @@ -96,7 +112,10 @@ public abstract class CharBuffer extends Buffer } /** - * @exception BufferUnderflowException FIXME + * Relative get method. + * + * @exception BufferUnderflowException If the buffer's current position is + * not smaller than its limit. * @exception IndexOutOfBoundsException If the preconditions on the offset * and length parameters do not hold */ @@ -106,11 +125,15 @@ public abstract class CharBuffer extends Buffer { dst [i] = get (); } + return this; } /** - * @exception BufferUnderflowException FIXME + * Relative get method. + * + * @exception BufferUnderflowException If there are fewer than length + * characters remaining in this buffer. */ final public CharBuffer get (char[] dst) { @@ -118,69 +141,121 @@ public abstract class CharBuffer extends Buffer } /** - * @exception BufferOverflowException FIXME - * @exception IllegalArgumentException FIXME - * @exception ReadOnlyBufferException FIXME + * @exception BufferOverflowException If there are fewer than length of + * source buffer characters remaining in this buffer. + * @exception IllegalArgumentException If the source buffer is this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. */ final public CharBuffer put (CharBuffer src) { - while (src.hasRemaining ()) - put (src.get ()); + if (src == this) + throw new IllegalArgumentException (); + + if (src.length () > 0) + { + char [] toPut = new char [src.length ()]; + src.get (toPut); + src.put (toPut); + } return this; } /** - * @exception BufferOverflowException FIXME + * @exception BufferOverflowException If there are fewer then length + * characters remaining in this buffer. * @exception IndexOutOfBoundsException If the preconditions on the offset * and length parameters do not hold - * @exception ReadOnlyBufferException FIXME + * @exception ReadOnlyBufferException If this buffer is read-only. */ final 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 (); + for (int i = offset; i < offset + length; i++) - put (src [i]); + { + put (src [i]); + } return this; } /** - * @exception BufferOverflowException FIXME - * @exception ReadOnlyBufferException FIXME + * Relative put method. + * + * @exception BufferOverflowException If there are fewer then length of the + * array characters remaining in this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. */ public final CharBuffer put (char[] src) { return put (src, 0, src.length); } + /** + * Tells wether this is buffer is backed by an array or not. + */ public final boolean hasArray () { return backing_buffer != null; } /** - * @exception ReadOnlyBufferException FIXME - * @exception UnsupportedOperationException FIXME + * Returns the array that backs this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + * @exception UnsupportedOperationException If this buffer is not backed + * by an accessible array. */ public final char[] array () { + if (backing_buffer == null) + throw new UnsupportedOperationException (); + + if (isReadOnly ()) + throw new ReadOnlyBufferException (); + return backing_buffer; } /** - * @exception ReadOnlyBufferException FIXME - * @exception UnsupportedOperationException FIXME + * Returns the offset to the position of a character in this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + * @exception UnsupportedOperationException If this buffer is not backed + * by an accessible array. */ public final int arrayOffset () { + if (backing_buffer == null) + throw new UnsupportedOperationException (); + + if (isReadOnly ()) + throw new ReadOnlyBufferException (); + return 0; } + /** + * Calculates a hash code for this buffer- + */ public int hashCode () { + // FIXME: Check what SUN calculates here. return super.hashCode (); } + /** + * Checks if this buffer is equal to obj. + */ public boolean equals (Object obj) { if (obj instanceof CharBuffer) @@ -190,7 +265,10 @@ public abstract class CharBuffer extends Buffer } /** - * @exception ClassCastException FIXME + * Compares two character buffer objects. + * + * @exception ClassCastException If obj is not an object derived from + * CharBuffer. */ public int compareTo(Object obj) { @@ -213,85 +291,128 @@ public abstract class CharBuffer extends Buffer if (t != 0) return (int) t; } + return 0; } /** - * @exception BufferUnderflowException FIXME + * Relative get method. + * + * @exception BufferUnderflowException If there are no remaining characters + * in this buffer. */ public abstract char get (); /** - * @exception BufferOverflowException FIXME - * @exception ReadOnlyBufferException FIXME + * Relative put method. + * + * @exception BufferOverflowException If there no remaining characters in + * this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. */ public abstract CharBuffer put (char b); /** - * @exception IndexOutOfBoundsException FIXME + * Absolute get method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. */ public abstract char get (int index); /** - * @exception IndexOutOfBoundsException FIXME - * @exception ReadOnlyBufferException FIXME + * Absolute put method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + * @exception ReadOnlyBufferException If this buffer is read-only. */ public abstract CharBuffer put (int index, char b); /** - * @exception ReadOnlyBufferException FIXME + * @exception ReadOnlyBufferException If this buffer is read-only. */ public abstract CharBuffer compact (); + /** + * Tells wether this buffer is direct or not. + */ public abstract boolean isDirect (); public abstract CharBuffer slice (); + /** + * Duplicates this buffer. + */ public abstract CharBuffer duplicate (); + /** + * Returns this buffer made read-only. + */ public abstract CharBuffer asReadOnlyBuffer (); + /** + * Returns the remaining content of the buffer as a string. + */ public String toString () { - return ""; + return new String (array (), position (), length ()); } public final int length () { - return 0; + return remaining (); } + /** + * Returns the byte order of this buffer. + */ public abstract ByteOrder order (); /** - * @exception IndexOutOfBoundsException FIXME + * @exception IndexOutOfBoundsException If the preconditions on start and + * end do not hold. */ public abstract CharSequence subSequence (int start, int length); /** - * @exception BufferOverflowException FIXME - * @exception IndexOutOfBoundsException FIXME - * @exception ReadOnlyBufferException FIXME + * Relative put method. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer. + * @exception IndexOutOfBoundsException If the preconditions on the start + * and end parameters do not hold. + * @exception ReadOnlyBufferException If this buffer is read-only. */ public CharBuffer put (String str, int start, int length) { - return null; + return put (str.toCharArray (), start, length); } /** - * @exception BufferOverflowException FIXME - * @exception ReadOnlyBufferException FIXME + * Relative put method. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. */ public final CharBuffer put (String str) { - return null; + return put (str, 0, str.length ()); } /** - * @exception IndexOutOfBoundsException FIXME + * Returns the character at position() + index. + * + * @exception IndexOutOfBoundsException If index is negative not smaller than + * remaining(). */ public final char charAt (int index) { - return ' '; + if (index < 0 + || index >= remaining ()) + throw new IndexOutOfBoundsException (); + + return get (position () + index); } }