diff options
author | Michael Koch <konqueror@gmx.de> | 2003-05-12 20:45:20 +0000 |
---|---|---|
committer | Michael Koch <mkoch@gcc.gnu.org> | 2003-05-12 20:45:20 +0000 |
commit | 36d4669b735c0e6f665e9a83bdd0848e23fc6662 (patch) | |
tree | eba2e2a8271971b25fa39af84cbebe056dd27e91 /libjava/java | |
parent | d3e0dffb76fd85d777f5c5de71f47816121bc5e3 (diff) | |
download | gcc-36d4669b735c0e6f665e9a83bdd0848e23fc6662.zip gcc-36d4669b735c0e6f665e9a83bdd0848e23fc6662.tar.gz gcc-36d4669b735c0e6f665e9a83bdd0848e23fc6662.tar.bz2 |
ByteBufferImpl.java: Reformatted.
2003-05-12 Michael Koch <konqueror@gmx.de>
* gnu/java/nio/ByteBufferImpl.java: Reformatted.
(nio_get_*): Removed.
(nio_put_*): Removed.
(as*Buffer): Implemented.
(compact): Implemented.
(get): Documentation added.
(put): Documentation added.
(get*): Newly implemented.
(put*): Newly implemented.
* gnu/java/nio/CharBufferImpl.java: Reformatted.
(CharBufferImpl): Revised.
(slice): New implementation.
(duplicate): New implementation.
(compact): New implementation.
(asReadOnlyBuffer): New implementation.
(get): Documentation revised.
(order): Return native byte order.
* gnu/java/nio/DirectByteBufferImpl.java
(allocateDirect): objects can be null not 0.
* gnu/java/nio/DoubleBufferImpl.java: Reformatted.
(DoubleBufferImpl): Revised.
(slice): New implementation.
(duplicate): New implementation.
(compact): New implementation.
(asReadOnlyBuffer): New implementation.
(get): Documentation revised.
(order): Return native byte order.
* gnu/java/nio/FloatBufferImpl.java: Reformatted.
(FloatBufferImpl): Revised.
(slice): New implementation.
(duplicate): New implementation.
(compact): New implementation.
(asReadOnlyBuffer): New implementation.
(get): Documentation revised.
(order): Return native byte order.
* gnu/java/nio/IntBufferImpl.java: Reformatted.
(IntBufferImpl): Revised.
(slice): New implementation.
(duplicate): New implementation.
(compact): New implementation.
(asReadOnlyBuffer): New implementation.
(get): Documentation revised.
(order): Return native byte order.
* gnu/java/nio/LongBufferImpl.java: Reformatted.
(LongBufferImpl): Revised.
(slice): New implementation.
(duplicate): New implementation.
(compact): New implementation.
(asReadOnlyBuffer): New implementation.
(get): Documentation revised.
(order): Return native byte order.
* gnu/java/nio/ShortBufferImpl.java: Reformatted.
(ShortBufferImpl): Revised.
(slice): New implementation.
(duplicate): New implementation.
(compact): New implementation.
(asReadOnlyBuffer): New implementation.
(get): Documentation revised.
(order): Return native byte order.
* java/nio/CharBuffer.java: Reformatted, much documentation rewritten.
(CharBuffer): Revised.
(order): Removed.
* java/nio/DoubleBuffer.java: Reformatted, much documentation rewritten.
(DoubleBuffer): Revised.
(allocateDirect): Removed.
(order): Removed.
* java/nio/FloatBuffer.java: Reformatted, much documentation rewritten.
(FloatBuffer): Revised.
(allocateDirect): Removed.
(order): Removed.
* java/nio/IntBuffer.java: Reformatted, much documentation rewritten.
(IntBuffer): Revised.
(allocateDirect): Removed.
(order): Removed.
* java/nio/LongBuffer.java: Reformatted, much documentation rewritten.
(LongBuffer): Revised.
(allocateDirect): Removed.
(order): Removed.
* java/nio/ShortBuffer.java: Reformatted, much documentation rewritten.
(ShortBuffer): Revised.
(allocateDirect): Removed.
(order): Removed.
* gnu/java/nio/natByteBufferImpl.cc: Removed.
* gnu/java/nio/natCharBufferImpl.cc: Removed.
* Makefile.am
(ordinary_java_source_files): Added the following files:
gnu/java/nio/CharViewBufferImpl.java,
gnu/java/nio/DoubleViewBufferImpl.java,
gnu/java/nio/FloatViewBufferImpl.java,
gnu/java/nio/IntViewBufferImpl.java,
gnu/java/nio/LongViewBufferImpl.java,
gnu/java/nio/ShortViewBufferImpl.java
(nat_source_files): Removed the following files:
gnu/java/nio/natByteBufferImpl.cc,
gnu/java/nio/natCharBufferImpl.cc
* Makefile.in: Regenerated.
From-SVN: r66733
Diffstat (limited to 'libjava/java')
-rw-r--r-- | libjava/java/nio/CharBuffer.java | 240 | ||||
-rw-r--r-- | libjava/java/nio/DoubleBuffer.java | 279 | ||||
-rw-r--r-- | libjava/java/nio/FloatBuffer.java | 284 | ||||
-rw-r--r-- | libjava/java/nio/IntBuffer.java | 288 | ||||
-rw-r--r-- | libjava/java/nio/LongBuffer.java | 285 | ||||
-rw-r--r-- | libjava/java/nio/ShortBuffer.java | 283 |
6 files changed, 1282 insertions, 377 deletions
diff --git a/libjava/java/nio/CharBuffer.java b/libjava/java/nio/CharBuffer.java index 2158af0..fbee6df 100644 --- a/libjava/java/nio/CharBuffer.java +++ b/libjava/java/nio/CharBuffer.java @@ -1,5 +1,5 @@ /* CharBuffer.java -- - Copyright (C) 2002 Free Software Foundation, Inc. + Copyright (C) 2002, 2003 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -45,26 +45,40 @@ import gnu.java.nio.CharBufferImpl; public abstract class CharBuffer extends Buffer implements Comparable, CharSequence { - int array_offset = 0; + int array_offset; char[] backing_buffer; - + + CharBuffer (int capacity, int limit, int position, int mark) + { + super (capacity, limit, position, mark); + array_offset = 0; + } + + CharBuffer (char[] buffer, int offset, int capacity, int limit, int position, int mark) + { + super (capacity, limit, position, mark); + this.backing_buffer = buffer; + this.array_offset = offset; + } + /** * Allocates a new <code>CharBuffer</code> object with a given capacity. */ public static CharBuffer allocate (int capacity) { - return new CharBufferImpl (capacity, 0, capacity); + return new CharBufferImpl (capacity); } - + /** - * Wraps a character array into a <code>CharBuffer</code> object. - * + * Wraps a <code>char</code> array into a <code>CharBuffer</code> + * 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) { - return new CharBufferImpl (array, offset, length); + return new CharBufferImpl (array, 0, array.length, offset + length, offset, -1, false); } /** @@ -101,27 +115,30 @@ public abstract class CharBuffer extends Buffer return wrap (buffer, offset, length); } - + /** - * Wraps a character array into a <code>CharBuffer</code> object. + * Wraps a <code>char</code> array into a <code>CharBuffer</code> + * object. */ final public static CharBuffer wrap (char[] array) { return wrap (array, 0, array.length); } - - CharBuffer (int cap, int lim, int pos, int mark) - { - super (cap, lim, pos, mark); - } /** - * Relative get method. - * - * @exception BufferUnderflowException If the buffer's current position is - * not smaller than its limit. + * This method transfers <code>chars<code> from this buffer into the given + * destination array. + * + * @param dst The destination array + * @param offset The offset within the array of the first <code>char</code> + * to be written; must be non-negative and no larger than dst.length. + * @param length The maximum number of bytes to be written to the given array; + * must be non-negative and no larger than dst.length - offset. + * + * @exception BufferUnderflowException If there are fewer than length + * <code>chars</code> remaining in this buffer. * @exception IndexOutOfBoundsException If the preconditions on the offset - * and length parameters do not hold + * and length parameters do not hold. */ public CharBuffer get (char[] dst, int offset, int length) { @@ -129,24 +146,32 @@ public abstract class CharBuffer extends Buffer { dst [i] = get (); } - + return this; } - + /** - * Relative get method. - * - * @exception BufferUnderflowException If there are fewer than length - * characters remaining in this buffer. + * This method transfers <code>chars<code> 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 + * <code>chars</code> remaining in this buffer. */ public CharBuffer get (char[] dst) { return get (dst, 0, dst.length); } - + /** - * @exception BufferOverflowException If there are fewer than length of - * source buffer characters remaining in this buffer. + * Writes the content of the the <code>CharBUFFER</code> src + * into the buffer. + * + * @param src The source data. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>chars<code> in the source buffer. * @exception IllegalArgumentException If the source buffer is this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ @@ -155,19 +180,31 @@ public abstract class CharBuffer extends Buffer if (src == this) throw new IllegalArgumentException (); - if (src.length () > 0) + if (src.remaining () > remaining ()) + throw new BufferOverflowException (); + + if (src.remaining () > 0) { - char [] toPut = new char [src.length ()]; + char[] toPut = new char [src.remaining ()]; src.get (toPut); src.put (toPut); } return this; } - + /** - * @exception BufferOverflowException If there are fewer then length - * characters remaining in this buffer. + * Writes the content of the the <code>char array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. + * @param offset The offset within the array of the first byte to be read; + * must be non-negative and no larger than src.length. + * @param length The number of bytes to be read from the given array; + * must be non-negative and no larger than src.length - offset. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>chars<code> 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. @@ -185,18 +222,19 @@ public abstract class CharBuffer extends Buffer throw new BufferOverflowException (); for (int i = offset; i < offset + length; i++) - { - put (src [i]); - } + put (src [i]); return this; } /** - * Relative put method. + * Writes the content of the the <code>char array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. * - * @exception BufferOverflowException If there are fewer then length of the - * array characters remaining in this buffer. + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>chars<code> in the source array. * @exception ReadOnlyBufferException If this buffer is read-only. */ public final CharBuffer put (char[] src) @@ -205,17 +243,18 @@ public abstract class CharBuffer extends Buffer } /** - * Tells wether this is buffer is backed by an accessible array or not. + * Tells whether ot not this buffer is backed by an accessible + * <code>char</code> array. */ public final boolean hasArray () { return (backing_buffer != null - && ! isReadOnly ()); + && !isReadOnly ()); } /** - * Returns the array that backs this buffer. - * + * Returns the <code>char</code> 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. @@ -230,10 +269,10 @@ public abstract class CharBuffer extends Buffer return backing_buffer; } - + /** - * Returns the offset to the position of a character in this buffer. - * + * Returns the offset within this buffer's backing array of the first element. + * * @exception ReadOnlyBufferException If this buffer is read-only. * @exception UnsupportedOperationException If this buffer is not backed * by an accessible array. @@ -248,78 +287,92 @@ public abstract class CharBuffer extends Buffer return array_offset; } - + /** - * Calculates a hash code for this buffer- + * 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) - return compareTo (obj) == 0; - + { + return compareTo (obj) == 0; + } + return false; } - + /** - * Compares two character buffer objects. - * + * Compares two <code>CharBuffer</code> objects. + * * @exception ClassCastException If obj is not an object derived from * <code>CharBuffer</code>. */ - public int compareTo(Object obj) + public int compareTo (Object obj) { CharBuffer a = (CharBuffer) obj; - + if (a.remaining () != remaining ()) return 1; - - if (! hasArray () || ! a.hasArray ()) - return 1; - + + if (! hasArray () || + ! a.hasArray ()) + { + return 1; + } + int r = remaining (); int i1 = position (); int i2 = a.position (); - + for (int i = 0; i < r; i++) { - int t = (int) (get (i1)- a.get (i2)); - + int t = (int) (get (i1) - a.get (i2)); + if (t != 0) - return (int) t; + { + return (int) t; + } } - + return 0; } - + /** - * Relative get method. - * - * @exception BufferUnderflowException If there are no remaining characters - * in this buffer. + * Returns the byte order of this buffer. + */ + public abstract ByteOrder order (); + + /** + * Reads the <code>char</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>chars</code> in this buffer. */ public abstract char get (); - + /** - * Relative put method. - * - * @exception BufferOverflowException If there no remaining characters in - * this buffer. + * Writes the <code>char</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferOverflowException If there no remaining + * <code>chars</code> in this buffer. * @exception ReadOnlyBufferException If this buffer is read-only. */ public abstract CharBuffer put (char b); - + /** * Absolute get method. - * + * * @exception IndexOutOfBoundsException If index is negative or not smaller * than the buffer's limit. */ @@ -327,32 +380,40 @@ public abstract class CharBuffer extends Buffer /** * 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); - + /** + * Compacts this buffer. + * * @exception ReadOnlyBufferException If this buffer is read-only. */ public abstract CharBuffer compact (); - + /** - * Tells wether this buffer is direct or not. + * Tells wether or not this buffer is direct. */ public abstract boolean isDirect (); - + + /** + * Creates a new <code>CharBuffer</code> whose content is a shared + * subsequence of this buffer's content. + */ public abstract CharBuffer slice (); - + /** - * Duplicates this buffer. + * Creates a new <code>CharBuffer</code> that shares this buffer's + * content. */ public abstract CharBuffer duplicate (); - + /** - * Returns this buffer made read-only. + * Creates a new read-only <code>CharBuffer</code> that shares this + * buffer's content. */ public abstract CharBuffer asReadOnlyBuffer (); @@ -378,11 +439,6 @@ public abstract class CharBuffer extends Buffer } /** - * Returns the byte order of this buffer. - */ - public abstract ByteOrder order (); - - /** * Creates a new character buffer that represents the specified subsequence * of this buffer, relative to the current position. * diff --git a/libjava/java/nio/DoubleBuffer.java b/libjava/java/nio/DoubleBuffer.java index ef39ac1..f212566 100644 --- a/libjava/java/nio/DoubleBuffer.java +++ b/libjava/java/nio/DoubleBuffer.java @@ -1,5 +1,5 @@ /* DoubleBuffer.java -- - Copyright (C) 2002 Free Software Foundation, Inc. + Copyright (C) 2002, 2003 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -39,72 +39,141 @@ package java.nio; import gnu.java.nio.DoubleBufferImpl; -public abstract class DoubleBuffer extends Buffer implements Comparable +/** + * @since 1.4 + */ +public abstract class DoubleBuffer extends Buffer + implements Comparable { int array_offset; double[] backing_buffer; - public static DoubleBuffer allocateDirect(int capacity) - { - throw new Error ("direct buffers are not implemented"); - } - - public static DoubleBuffer allocate(int capacity) + DoubleBuffer (int capacity, int limit, int position, int mark) { - return new DoubleBufferImpl(capacity, 0, capacity); + super (capacity, limit, position, mark); + array_offset = 0; } - final public static DoubleBuffer wrap (double[] array, int offset, int length) + DoubleBuffer (double[] buffer, int offset, int capacity, int limit, int position, int mark) { - return new DoubleBufferImpl(array, offset, length); + super (capacity, limit, position, mark); + this.backing_buffer = buffer; + this.array_offset = offset; } - final public static DoubleBuffer wrap(String a) + /** + * Allocates a new <code>DoubleBuffer</code> object with a given capacity. + */ + public static DoubleBuffer allocate (int capacity) { - int len = a.length(); - double[] buffer = new double[len]; - - for (int i=0;i<len;i++) - { - buffer[i] = (double) a.charAt(i); - } - - return wrap(buffer, 0, len); + return new DoubleBufferImpl (capacity); } - final public static DoubleBuffer wrap(double[] array) + /** + * Wraps a <code>double</code> array into a <code>DoubleBuffer</code> + * object. + * + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + */ + final public static DoubleBuffer wrap (double[] array, int offset, int length) { - return wrap(array, 0, array.length); + return new DoubleBufferImpl (array, 0, array.length, offset + length, offset, -1, false); } - DoubleBuffer (int capacity, int limit, int position, int mark) + /** + * Wraps a <code>double</code> array into a <code>DoubleBuffer</code> + * object. + */ + final public static DoubleBuffer wrap (double[] array) { - super (capacity, limit, position, mark); + return wrap (array, 0, array.length); } + /** + * This method transfers <code>doubles<code> from this buffer into the given + * destination array. + * + * @param dst The destination array + * @param offset The offset within the array of the first <code>double</code> + * to be written; must be non-negative and no larger than dst.length. + * @param length The maximum number of bytes to be written to the given array; + * must be non-negative and no larger than dst.length - offset. + * + * @exception BufferUnderflowException If there are fewer than length + * <code>doubles</code> 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) { for (int i = offset; i < offset + length; i++) { - dst[i] = get(); + dst [i] = get (); } return this; } + /** + * This method transfers <code>doubles<code> 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 + * <code>doubles</code> remaining in this buffer. + */ public DoubleBuffer get (double[] dst) { - return get(dst, 0, dst.length); + return get (dst, 0, dst.length); } + /** + * Writes the content of the the <code>DoubleBUFFER</code> src + * into the buffer. + * + * @param src The source data. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>doubles<code> in the source buffer. + * @exception IllegalArgumentException If the source buffer is this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ public DoubleBuffer put (DoubleBuffer src) { - while (src.hasRemaining()) - put(src.get()); + if (src == this) + throw new IllegalArgumentException (); + + if (src.remaining () > remaining ()) + throw new BufferOverflowException (); + + if (src.remaining () > 0) + { + double[] toPut = new double [src.remaining ()]; + src.get (toPut); + src.put (toPut); + } return this; } + /** + * Writes the content of the the <code>double array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. + * @param offset The offset within the array of the first byte to be read; + * must be non-negative and no larger than src.length. + * @param length The number of bytes to be read from the given array; + * must be non-negative and no larger than src.length - offset. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>doubles<code> 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) { for (int i = offset; i < offset + length; i++) @@ -113,18 +182,39 @@ public abstract class DoubleBuffer extends Buffer implements Comparable return this; } - public final DoubleBuffer put(double[] src) + /** + * Writes the content of the the <code>double array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>doubles<code> in the source array. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public final DoubleBuffer put (double[] src) { - return put(src, 0, src.length); + return put (src, 0, src.length); } - public final boolean hasArray() + /** + * Tells whether ot not this buffer is backed by an accessible + * <code>double</code> array. + */ + public final boolean hasArray () { return (backing_buffer != null && !isReadOnly ()); } - public final double[] array() + /** + * Returns the <code>double</code> 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 double[] array () { if (backing_buffer == null) throw new UnsupportedOperationException (); @@ -135,7 +225,14 @@ public abstract class DoubleBuffer extends Buffer implements Comparable return backing_buffer; } - public final int arrayOffset() + /** + * Returns the offset within this buffer's backing array of the first element. + * + * @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 (); @@ -146,41 +243,55 @@ public abstract class DoubleBuffer extends Buffer implements Comparable return array_offset; } - public int hashCode() + /** + * Calculates a hash code for this buffer. + */ + public int hashCode () { - return super.hashCode(); + // FIXME: Check what SUN calculates here. + return super.hashCode (); } - public boolean equals(Object obj) + /** + * Checks if this buffer is equal to obj. + */ + public boolean equals (Object obj) { if (obj instanceof DoubleBuffer) { - return compareTo(obj) == 0; + return compareTo (obj) == 0; } return false; } - public int compareTo(Object ob) + /** + * Compares two <code>DoubleBuffer</code> objects. + * + * @exception ClassCastException If obj is not an object derived from + * <code>DoubleBuffer</code>. + */ + public int compareTo (Object obj) { - DoubleBuffer a = (DoubleBuffer) ob; + DoubleBuffer a = (DoubleBuffer) obj; - if (a.remaining() != remaining()) + if (a.remaining () != remaining ()) return 1; - if (! hasArray() || - ! a.hasArray()) + if (! hasArray () || + ! a.hasArray ()) { return 1; } - int r = remaining(); + int r = remaining (); int i1 = position (); int i2 = a.position (); - for (int i=0;i<r;i++) + for (int i = 0; i < r; i++) { - int t = (int) (get(i1)- a.get(i2)); + int t = (int) (get (i1) - a.get (i2)); + if (t != 0) { return (int) t; @@ -190,14 +301,74 @@ public abstract class DoubleBuffer extends Buffer implements Comparable return 0; } + /** + * Returns the byte order of this buffer. + */ public abstract ByteOrder order (); - public abstract double get(); + + /** + * Reads the <code>double</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>doubles</code> in this buffer. + */ + public abstract double get (); + + /** + * Writes the <code>double</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferOverflowException If there no remaining + * <code>doubles</code> in this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ public abstract DoubleBuffer put (double b); - public abstract double get(int index); - public abstract DoubleBuffer put(int index, double b); - public abstract DoubleBuffer compact(); - public abstract boolean isDirect(); - public abstract DoubleBuffer slice(); - public abstract DoubleBuffer duplicate(); - public abstract DoubleBuffer asReadOnlyBuffer(); + + /** + * Absolute get method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public abstract double get (int index); + + /** + * 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 DoubleBuffer put (int index, double b); + + /** + * Compacts this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract DoubleBuffer compact (); + + /** + * Tells wether or not this buffer is direct. + */ + public abstract boolean isDirect (); + + /** + * Creates a new <code>DoubleBuffer</code> whose content is a shared + * subsequence of this buffer's content. + */ + public abstract DoubleBuffer slice (); + + /** + * Creates a new <code>DoubleBuffer</code> that shares this buffer's + * content. + */ + public abstract DoubleBuffer duplicate (); + + /** + * Creates a new read-only <code>DoubleBuffer</code> that shares this + * buffer's content. + */ + public abstract DoubleBuffer asReadOnlyBuffer (); } diff --git a/libjava/java/nio/FloatBuffer.java b/libjava/java/nio/FloatBuffer.java index 81feb45..65d680b 100644 --- a/libjava/java/nio/FloatBuffer.java +++ b/libjava/java/nio/FloatBuffer.java @@ -1,5 +1,5 @@ /* FloatBuffer.java -- - Copyright (C) 2002 Free Software Foundation, Inc. + Copyright (C) 2002, 2003 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -39,93 +39,182 @@ package java.nio; import gnu.java.nio.FloatBufferImpl; -public abstract class FloatBuffer extends Buffer implements Comparable +/** + * @since 1.4 + */ +public abstract class FloatBuffer extends Buffer + implements Comparable { int array_offset; float[] backing_buffer; - public static FloatBuffer allocateDirect(int capacity) + FloatBuffer (int capacity, int limit, int position, int mark) { - throw new Error ("direct buffers not implemented"); + super (capacity, limit, position, mark); + array_offset = 0; } - public static FloatBuffer allocate(int capacity) + FloatBuffer (float[] buffer, int offset, int capacity, int limit, int position, int mark) { - return new FloatBufferImpl (capacity, 0, capacity); + super (capacity, limit, position, mark); + this.backing_buffer = buffer; + this.array_offset = offset; } - final public static FloatBuffer wrap(float[] array, int offset, int length) + /** + * Allocates a new <code>FloatBuffer</code> object with a given capacity. + */ + public static FloatBuffer allocate (int capacity) { - return new FloatBufferImpl(array, offset, length); + return new FloatBufferImpl (capacity); } - final public static FloatBuffer wrap(String a) + /** + * Wraps a <code>float</code> array into a <code>FloatBuffer</code> + * object. + * + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + */ + final public static FloatBuffer wrap (float[] array, int offset, int length) { - int len = a.length(); - float[] buffer = new float[len]; - - for (int i=0;i<len;i++) - { - buffer[i] = (float) a.charAt(i); - } - - return wrap(buffer, 0, len); + return new FloatBufferImpl (array, 0, array.length, offset + length, offset, -1, false); } - final public static FloatBuffer wrap(float[] array) + /** + * Wraps a <code>float</code> array into a <code>FloatBuffer</code> + * object. + */ + final public static FloatBuffer wrap (float[] array) { - return wrap(array, 0, array.length); - } - - FloatBuffer (int capacity, int limit, int position, int mark) - { - super (capacity, limit, position, mark); - array_offset = 0; + return wrap (array, 0, array.length); } + /** + * This method transfers <code>floats<code> from this buffer into the given + * destination array. + * + * @param dst The destination array + * @param offset The offset within the array of the first <code>float</code> + * to be written; must be non-negative and no larger than dst.length. + * @param length The maximum number of bytes to be written to the given array; + * must be non-negative and no larger than dst.length - offset. + * + * @exception BufferUnderflowException If there are fewer than length + * <code>floats</code> 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) { for (int i = offset; i < offset + length; i++) { - dst[i] = get(); + dst [i] = get (); } return this; } + /** + * This method transfers <code>floats<code> 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 + * <code>floats</code> remaining in this buffer. + */ public FloatBuffer get (float[] dst) { - return get(dst, 0, dst.length); + return get (dst, 0, dst.length); } + /** + * Writes the content of the the <code>FloatBUFFER</code> src + * into the buffer. + * + * @param src The source data. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>floats<code> in the source buffer. + * @exception IllegalArgumentException If the source buffer is this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ public FloatBuffer put (FloatBuffer src) { - while (src.hasRemaining()) - put(src.get()); + if (src == this) + throw new IllegalArgumentException (); + + if (src.remaining () > remaining ()) + throw new BufferOverflowException (); + + if (src.remaining () > 0) + { + float[] toPut = new float [src.remaining ()]; + src.get (toPut); + src.put (toPut); + } return this; } + /** + * Writes the content of the the <code>float array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. + * @param offset The offset within the array of the first byte to be read; + * must be non-negative and no larger than src.length. + * @param length The number of bytes to be read from the given array; + * must be non-negative and no larger than src.length - offset. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>floats<code> 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) { for (int i = offset; i < offset + length; i++) - put(src[i]); + put (src [i]); return this; } - public final FloatBuffer put(float[] src) + /** + * Writes the content of the the <code>float array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>floats<code> in the source array. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public final FloatBuffer put (float[] src) { - return put(src, 0, src.length); + return put (src, 0, src.length); } - public final boolean hasArray() + /** + * Tells whether ot not this buffer is backed by an accessible + * <code>float</code> array. + */ + public final boolean hasArray () { return (backing_buffer != null && !isReadOnly ()); } - public final float[] array() + /** + * Returns the <code>float</code> 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 float[] array () { if (backing_buffer == null) throw new UnsupportedOperationException (); @@ -136,7 +225,14 @@ public abstract class FloatBuffer extends Buffer implements Comparable return backing_buffer; } - public final int arrayOffset() + /** + * Returns the offset within this buffer's backing array of the first element. + * + * @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 (); @@ -147,41 +243,55 @@ public abstract class FloatBuffer extends Buffer implements Comparable return array_offset; } - public int hashCode() + /** + * Calculates a hash code for this buffer. + */ + public int hashCode () { - return super.hashCode(); + // FIXME: Check what SUN calculates here. + return super.hashCode (); } - public boolean equals(Object obj) + /** + * Checks if this buffer is equal to obj. + */ + public boolean equals (Object obj) { if (obj instanceof FloatBuffer) { - return compareTo(obj) == 0; + return compareTo (obj) == 0; } return false; } - public int compareTo(Object ob) + /** + * Compares two <code>FloatBuffer</code> objects. + * + * @exception ClassCastException If obj is not an object derived from + * <code>FloatBuffer</code>. + */ + public int compareTo (Object obj) { - FloatBuffer a = (FloatBuffer) ob; + FloatBuffer a = (FloatBuffer) obj; - if (a.remaining() != remaining()) + if (a.remaining () != remaining ()) return 1; - if (! hasArray() || - ! a.hasArray()) + if (! hasArray () || + ! a.hasArray ()) { return 1; } - int r = remaining(); + int r = remaining (); int i1 = position (); int i2 = a.position (); - for (int i=0;i<r;i++) + for (int i = 0; i < r; i++) { - int t = (int) (get(i1)- a.get(i2)); + int t = (int) (get (i1) - a.get (i2)); + if (t != 0) { return (int) t; @@ -191,14 +301,74 @@ public abstract class FloatBuffer extends Buffer implements Comparable return 0; } + /** + * Returns the byte order of this buffer. + */ public abstract ByteOrder order (); - public abstract float get(); - public abstract java.nio. FloatBuffer put(float b); - public abstract float get(int index); - public abstract java.nio. FloatBuffer put(int index, float b); - public abstract FloatBuffer compact(); - public abstract boolean isDirect(); - public abstract FloatBuffer slice(); - public abstract FloatBuffer duplicate(); - public abstract FloatBuffer asReadOnlyBuffer(); + + /** + * Reads the <code>float</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>floats</code> in this buffer. + */ + public abstract float get (); + + /** + * Writes the <code>float</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferOverflowException If there no remaining + * <code>floats</code> in this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract FloatBuffer put (float b); + + /** + * Absolute get method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public abstract float get (int index); + + /** + * 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 FloatBuffer put (int index, float b); + + /** + * Compacts this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract FloatBuffer compact (); + + /** + * Tells wether or not this buffer is direct. + */ + public abstract boolean isDirect (); + + /** + * Creates a new <code>FloatBuffer</code> whose content is a shared + * subsequence of this buffer's content. + */ + public abstract FloatBuffer slice (); + + /** + * Creates a new <code>FloatBuffer</code> that shares this buffer's + * content. + */ + public abstract FloatBuffer duplicate (); + + /** + * Creates a new read-only <code>FloatBuffer</code> that shares this + * buffer's content. + */ + public abstract FloatBuffer asReadOnlyBuffer (); } diff --git a/libjava/java/nio/IntBuffer.java b/libjava/java/nio/IntBuffer.java index 295ad6a..c8e0d0f 100644 --- a/libjava/java/nio/IntBuffer.java +++ b/libjava/java/nio/IntBuffer.java @@ -1,5 +1,5 @@ /* IntBuffer.java -- - Copyright (C) 2002 Free Software Foundation, Inc. + Copyright (C) 2002, 2003 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -39,93 +39,182 @@ package java.nio; import gnu.java.nio.IntBufferImpl; -public abstract class IntBuffer extends Buffer implements Comparable +/** + * @since 1.4 + */ +public abstract class IntBuffer extends Buffer + implements Comparable { int array_offset; int[] backing_buffer; - public static IntBuffer allocateDirect(int capacity) - { - throw new Error ("direct buffers not implemented"); - } - - public static IntBuffer allocate(int capacity) + IntBuffer (int capacity, int limit, int position, int mark) { - return new IntBufferImpl (capacity, 0, capacity); + super (capacity, limit, position, mark); + array_offset = 0; } - final public static IntBuffer wrap(int[] array, int offset, int length) + IntBuffer (int[] buffer, int offset, int capacity, int limit, int position, int mark) { - return new IntBufferImpl(array, offset, length); + super (capacity, limit, position, mark); + this.backing_buffer = buffer; + this.array_offset = offset; } - final public static IntBuffer wrap(String a) + /** + * Allocates a new <code>IntBuffer</code> object with a given capacity. + */ + public static IntBuffer allocate (int capacity) { - int len = a.length(); - int[] buffer = new int[len]; - - for (int i=0;i<len;i++) - { - buffer[i] = (int) a.charAt(i); - } - - return wrap(buffer, 0, len); + return new IntBufferImpl (capacity); } - final public static IntBuffer wrap(int[] array) + /** + * Wraps a <code>int</code> array into a <code>IntBuffer</code> + * object. + * + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + */ + final public static IntBuffer wrap (int[] array, int offset, int length) { - return wrap(array, 0, array.length); + return new IntBufferImpl (array, 0, array.length, offset + length, offset, -1, false); } - IntBuffer (int capacity, int limit, int position, int mark) + /** + * Wraps a <code>int</code> array into a <code>IntBuffer</code> + * object. + */ + final public static IntBuffer wrap (int[] array) { - super (capacity, limit, position, mark); - array_offset = 0; + return wrap (array, 0, array.length); } - public IntBuffer get(int[] dst, int offset, int length) + /** + * This method transfers <code>ints<code> from this buffer into the given + * destination array. + * + * @param dst The destination array + * @param offset The offset within the array of the first <code>int</code> + * to be written; must be non-negative and no larger than dst.length. + * @param length The maximum number of bytes to be written to the given array; + * must be non-negative and no larger than dst.length - offset. + * + * @exception BufferUnderflowException If there are fewer than length + * <code>ints</code> 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) { for (int i = offset; i < offset + length; i++) { - dst[i] = get(); + dst [i] = get (); } return this; } + /** + * This method transfers <code>ints<code> 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 + * <code>ints</code> remaining in this buffer. + */ public IntBuffer get (int[] dst) { - return get(dst, 0, dst.length); + return get (dst, 0, dst.length); } + /** + * Writes the content of the the <code>IntBUFFER</code> src + * into the buffer. + * + * @param src The source data. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>ints<code> in the source buffer. + * @exception IllegalArgumentException If the source buffer is this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ public IntBuffer put (IntBuffer src) { - while (src.hasRemaining()) - put(src.get()); + if (src == this) + throw new IllegalArgumentException (); + + if (src.remaining () > remaining ()) + throw new BufferOverflowException (); + + if (src.remaining () > 0) + { + int[] toPut = new int [src.remaining ()]; + src.get (toPut); + src.put (toPut); + } return this; } + /** + * Writes the content of the the <code>int array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. + * @param offset The offset within the array of the first byte to be read; + * must be non-negative and no larger than src.length. + * @param length The number of bytes to be read from the given array; + * must be non-negative and no larger than src.length - offset. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>ints<code> 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) { for (int i = offset; i < offset + length; i++) - put(src[i]); + put (src [i]); return this; } - public final IntBuffer put(int[] src) + /** + * Writes the content of the the <code>int array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>ints<code> in the source array. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public final IntBuffer put (int[] src) { - return put(src, 0, src.length); + return put (src, 0, src.length); } - public final boolean hasArray() + /** + * Tells whether ot not this buffer is backed by an accessible + * <code>int</code> array. + */ + public final boolean hasArray () { return (backing_buffer != null && !isReadOnly ()); } - public final int[] array() + /** + * Returns the <code>int</code> 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 int[] array () { if (backing_buffer == null) throw new UnsupportedOperationException (); @@ -136,7 +225,14 @@ public abstract class IntBuffer extends Buffer implements Comparable return backing_buffer; } - public final int arrayOffset() + /** + * Returns the offset within this buffer's backing array of the first element. + * + * @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 (); @@ -147,41 +243,55 @@ public abstract class IntBuffer extends Buffer implements Comparable return array_offset; } - public int hashCode() + /** + * Calculates a hash code for this buffer. + */ + public int hashCode () { - return super.hashCode(); + // FIXME: Check what SUN calculates here. + return super.hashCode (); } - public boolean equals(Object obj) + /** + * Checks if this buffer is equal to obj. + */ + public boolean equals (Object obj) { if (obj instanceof IntBuffer) { - return compareTo(obj) == 0; + return compareTo (obj) == 0; } return false; } - public int compareTo(Object ob) + /** + * Compares two <code>IntBuffer</code> objects. + * + * @exception ClassCastException If obj is not an object derived from + * <code>IntBuffer</code>. + */ + public int compareTo (Object obj) { - IntBuffer a = (IntBuffer) ob; + IntBuffer a = (IntBuffer) obj; - if (a.remaining() != remaining()) + if (a.remaining () != remaining ()) return 1; - if (! hasArray() || - ! a.hasArray()) + if (! hasArray () || + ! a.hasArray ()) { return 1; } - int r = remaining(); + int r = remaining (); int i1 = position (); int i2 = a.position (); - for (int i=0;i<r;i++) + for (int i = 0; i < r; i++) { - int t = (int) (get(i1)- a.get(i2)); + int t = (int) (get (i1) - a.get (i2)); + if (t != 0) { return (int) t; @@ -191,14 +301,74 @@ public abstract class IntBuffer extends Buffer implements Comparable return 0; } - public abstract ByteOrder order(); - public abstract int get(); - public abstract IntBuffer put(int b); - public abstract int get(int index); - public abstract IntBuffer put(int index, int b); - public abstract IntBuffer compact(); - public abstract boolean isDirect(); - public abstract IntBuffer slice(); - public abstract IntBuffer duplicate(); - public abstract IntBuffer asReadOnlyBuffer(); + /** + * Returns the byte order of this buffer. + */ + public abstract ByteOrder order (); + + /** + * Reads the <code>int</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>ints</code> in this buffer. + */ + public abstract int get (); + + /** + * Writes the <code>int</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferOverflowException If there no remaining + * <code>ints</code> in this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract IntBuffer put (int b); + + /** + * Absolute get method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public abstract int get (int index); + + /** + * 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 IntBuffer put (int index, int b); + + /** + * Compacts this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract IntBuffer compact (); + + /** + * Tells wether or not this buffer is direct. + */ + public abstract boolean isDirect (); + + /** + * Creates a new <code>IntBuffer</code> whose content is a shared + * subsequence of this buffer's content. + */ + public abstract IntBuffer slice (); + + /** + * Creates a new <code>IntBuffer</code> that shares this buffer's + * content. + */ + public abstract IntBuffer duplicate (); + + /** + * Creates a new read-only <code>IntBuffer</code> that shares this + * buffer's content. + */ + public abstract IntBuffer asReadOnlyBuffer (); } diff --git a/libjava/java/nio/LongBuffer.java b/libjava/java/nio/LongBuffer.java index 3597854..8808fd5 100644 --- a/libjava/java/nio/LongBuffer.java +++ b/libjava/java/nio/LongBuffer.java @@ -1,5 +1,5 @@ /* LongBuffer.java -- - Copyright (C) 2002 Free Software Foundation, Inc. + Copyright (C) 2002, 2003 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -39,93 +39,182 @@ package java.nio; import gnu.java.nio.LongBufferImpl; -public abstract class LongBuffer extends Buffer implements Comparable +/** + * @since 1.4 + */ +public abstract class LongBuffer extends Buffer + implements Comparable { int array_offset; long[] backing_buffer; - public static LongBuffer allocateDirect(int capacity) + LongBuffer (int capacity, int limit, int position, int mark) { - throw new Error ("direct buffers not implemented"); + super (capacity, limit, position, mark); + array_offset = 0; } - public static LongBuffer allocate(int capacity) + LongBuffer (long[] buffer, int offset, int capacity, int limit, int position, int mark) { - return new LongBufferImpl(capacity, 0, capacity); + super (capacity, limit, position, mark); + this.backing_buffer = buffer; + this.array_offset = offset; } - final public static LongBuffer wrap(long[] array, int offset, int length) + /** + * Allocates a new <code>LongBuffer</code> object with a given capacity. + */ + public static LongBuffer allocate (int capacity) { - return new LongBufferImpl (array, offset, length); + return new LongBufferImpl (capacity); } - final public static LongBuffer wrap(String a) + /** + * Wraps a <code>long</code> array into a <code>LongBuffer</code> + * object. + * + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + */ + final public static LongBuffer wrap (long[] array, int offset, int length) { - int len = a.length(); - long[] buffer = new long[len]; - - for (int i=0;i<len;i++) - { - buffer[i] = (long) a.charAt(i); - } - - return wrap(buffer, 0, len); + return new LongBufferImpl (array, 0, array.length, offset + length, offset, -1, false); } - final public static LongBuffer wrap(long[] array) + /** + * Wraps a <code>long</code> array into a <code>LongBuffer</code> + * object. + */ + final public static LongBuffer wrap (long[] array) { - return wrap(array, 0, array.length); - } - - LongBuffer (int capacity, int limit, int position, int mark) - { - super (capacity, limit, position, mark); - array_offset = 0; + return wrap (array, 0, array.length); } + /** + * This method transfers <code>longs<code> from this buffer into the given + * destination array. + * + * @param dst The destination array + * @param offset The offset within the array of the first <code>long</code> + * to be written; must be non-negative and no larger than dst.length. + * @param length The maximum number of bytes to be written to the given array; + * must be non-negative and no larger than dst.length - offset. + * + * @exception BufferUnderflowException If there are fewer than length + * <code>longs</code> 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) { for (int i = offset; i < offset + length; i++) { - dst[i] = get(); + dst [i] = get (); } return this; } + /** + * This method transfers <code>longs<code> 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 + * <code>longs</code> remaining in this buffer. + */ public LongBuffer get (long[] dst) { - return get(dst, 0, dst.length); + return get (dst, 0, dst.length); } + /** + * Writes the content of the the <code>LongBUFFER</code> src + * into the buffer. + * + * @param src The source data. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>longs<code> in the source buffer. + * @exception IllegalArgumentException If the source buffer is this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ public LongBuffer put (LongBuffer src) { - while (src.hasRemaining()) - put(src.get()); + if (src == this) + throw new IllegalArgumentException (); + + if (src.remaining () > remaining ()) + throw new BufferOverflowException (); + + if (src.remaining () > 0) + { + long[] toPut = new long [src.remaining ()]; + src.get (toPut); + src.put (toPut); + } return this; } + /** + * Writes the content of the the <code>long array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. + * @param offset The offset within the array of the first byte to be read; + * must be non-negative and no larger than src.length. + * @param length The number of bytes to be read from the given array; + * must be non-negative and no larger than src.length - offset. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>longs<code> 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) { for (int i = offset; i < offset + length; i++) - put(src[i]); + put (src [i]); return this; } - public final LongBuffer put(long[] src) + /** + * Writes the content of the the <code>long array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>longs<code> in the source array. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public final LongBuffer put (long[] src) { - return put(src, 0, src.length); + return put (src, 0, src.length); } - public final boolean hasArray() + /** + * Tells whether ot not this buffer is backed by an accessible + * <code>long</code> array. + */ + public final boolean hasArray () { return (backing_buffer != null && !isReadOnly ()); } - public final long[] array() + /** + * Returns the <code>long</code> 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 long[] array () { if (backing_buffer == null) throw new UnsupportedOperationException (); @@ -136,7 +225,14 @@ public abstract class LongBuffer extends Buffer implements Comparable return backing_buffer; } - public final int arrayOffset() + /** + * Returns the offset within this buffer's backing array of the first element. + * + * @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 (); @@ -147,41 +243,54 @@ public abstract class LongBuffer extends Buffer implements Comparable return array_offset; } - public int hashCode() + /** + * Calculates a hash code for this buffer. + */ + public int hashCode () { - return super.hashCode(); + // FIXME: Check what SUN calculates here. + return super.hashCode (); } - public boolean equals(Object obj) + /** + * Checks if this buffer is equal to obj. + */ + public boolean equals (Object obj) { if (obj instanceof LongBuffer) { - return compareTo(obj) == 0; + return compareTo (obj) == 0; } return false; } - public int compareTo(Object ob) + /** + * Compares two <code>LongBuffer</code> objects. + * + * @exception ClassCastException If obj is not an object derived from + * <code>LongBuffer</code>. + */ + public int compareTo (Object obj) { - LongBuffer a = (LongBuffer) ob; + LongBuffer a = (LongBuffer) obj; - if (a.remaining() != remaining()) + if (a.remaining () != remaining ()) return 1; - if (! hasArray() || - ! a.hasArray()) + if (! hasArray () || + ! a.hasArray ()) { return 1; } - int r = remaining(); + int r = remaining (); int i1 = position (); int i2 = a.position (); - for (int i=0;i<r;i++) + for (int i = 0; i < r; i++) { - int t = (int) (get(i1)- a.get(i2)); + int t = (int) (get (i1) - a.get (i2)); if (t != 0) { @@ -192,14 +301,74 @@ public abstract class LongBuffer extends Buffer implements Comparable return 0; } - public abstract ByteOrder order(); - public abstract long get(); - public abstract java.nio. LongBuffer put(long b); - public abstract long get(int index); - public abstract java.nio. LongBuffer put(int index, long b); - public abstract LongBuffer compact(); - public abstract boolean isDirect(); - public abstract LongBuffer slice(); - public abstract LongBuffer duplicate(); - public abstract LongBuffer asReadOnlyBuffer(); + /** + * Returns the byte order of this buffer. + */ + public abstract ByteOrder order (); + + /** + * Reads the <code>long</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>longs</code> in this buffer. + */ + public abstract long get (); + + /** + * Writes the <code>long</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferOverflowException If there no remaining + * <code>longs</code> in this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract LongBuffer put (long b); + + /** + * Absolute get method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public abstract long get (int index); + + /** + * 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 LongBuffer put (int index, long b); + + /** + * Compacts this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract LongBuffer compact (); + + /** + * Tells wether or not this buffer is direct. + */ + public abstract boolean isDirect (); + + /** + * Creates a new <code>LongBuffer</code> whose content is a shared + * subsequence of this buffer's content. + */ + public abstract LongBuffer slice (); + + /** + * Creates a new <code>LongBuffer</code> that shares this buffer's + * content. + */ + public abstract LongBuffer duplicate (); + + /** + * Creates a new read-only <code>LongBuffer</code> that shares this + * buffer's content. + */ + public abstract LongBuffer asReadOnlyBuffer (); } diff --git a/libjava/java/nio/ShortBuffer.java b/libjava/java/nio/ShortBuffer.java index 5e449f8..db14b48 100644 --- a/libjava/java/nio/ShortBuffer.java +++ b/libjava/java/nio/ShortBuffer.java @@ -1,5 +1,5 @@ /* ShortBuffer.java -- - Copyright (C) 2002 Free Software Foundation, Inc. + Copyright (C) 2002, 2003 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -39,93 +39,182 @@ package java.nio; import gnu.java.nio.ShortBufferImpl; -public abstract class ShortBuffer extends Buffer implements Comparable +/** + * @since 1.4 + */ +public abstract class ShortBuffer extends Buffer + implements Comparable { int array_offset; short[] backing_buffer; - public static ShortBuffer allocateDirect(int capacity) - { - throw new Error ("direct buffers not implemented"); - } - - public static ShortBuffer allocate(int capacity) + ShortBuffer (int capacity, int limit, int position, int mark) { - return new ShortBufferImpl(capacity, 0, capacity); + super (capacity, limit, position, mark); + array_offset = 0; } - final public static ShortBuffer wrap(short[] array, int offset, int length) + ShortBuffer (short[] buffer, int offset, int capacity, int limit, int position, int mark) { - return new ShortBufferImpl(array, offset, length); + super (capacity, limit, position, mark); + this.backing_buffer = buffer; + this.array_offset = offset; } - final public static ShortBuffer wrap(String a) + /** + * Allocates a new <code>ShortBuffer</code> object with a given capacity. + */ + public static ShortBuffer allocate (int capacity) { - int len = a.length(); - short[] buffer = new short[len]; - - for (int i=0;i<len;i++) - { - buffer[i] = (short) a.charAt(i); - } - - return wrap(buffer, 0, len); + return new ShortBufferImpl (capacity); } - final public static ShortBuffer wrap(short[] array) + /** + * Wraps a <code>short</code> array into a <code>ShortBuffer</code> + * object. + * + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + */ + final public static ShortBuffer wrap (short[] array, int offset, int length) { - return wrap(array, 0, array.length); + return new ShortBufferImpl (array, 0, array.length, offset + length, offset, -1, false); } - ShortBuffer (int capacity, int limit, int position, int mark) + /** + * Wraps a <code>short</code> array into a <code>ShortBuffer</code> + * object. + */ + final public static ShortBuffer wrap (short[] array) { - super (capacity, limit, position, mark); - array_offset = 0; + return wrap (array, 0, array.length); } + /** + * This method transfers <code>shorts<code> from this buffer into the given + * destination array. + * + * @param dst The destination array + * @param offset The offset within the array of the first <code>short</code> + * to be written; must be non-negative and no larger than dst.length. + * @param length The maximum number of bytes to be written to the given array; + * must be non-negative and no larger than dst.length - offset. + * + * @exception BufferUnderflowException If there are fewer than length + * <code>shorts</code> 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) { for (int i = offset; i < offset + length; i++) { - dst[i] = get(); + dst [i] = get (); } return this; } + /** + * This method transfers <code>shorts<code> 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 + * <code>shorts</code> remaining in this buffer. + */ public ShortBuffer get (short[] dst) { - return get(dst, 0, dst.length); + return get (dst, 0, dst.length); } + /** + * Writes the content of the the <code>ShortBUFFER</code> src + * into the buffer. + * + * @param src The source data. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>shorts<code> in the source buffer. + * @exception IllegalArgumentException If the source buffer is this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ public ShortBuffer put (ShortBuffer src) { - while (src.hasRemaining()) - put(src.get()); + if (src == this) + throw new IllegalArgumentException (); + + if (src.remaining () > remaining ()) + throw new BufferOverflowException (); + + if (src.remaining () > 0) + { + short[] toPut = new short [src.remaining ()]; + src.get (toPut); + src.put (toPut); + } return this; } + /** + * Writes the content of the the <code>short array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. + * @param offset The offset within the array of the first byte to be read; + * must be non-negative and no larger than src.length. + * @param length The number of bytes to be read from the given array; + * must be non-negative and no larger than src.length - offset. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>shorts<code> 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) { for (int i = offset; i < offset + length; i++) - put(src[i]); + put (src [i]); return this; } - public final ShortBuffer put(short[] src) + /** + * Writes the content of the the <code>short array</code> src + * into the buffer. + * + * @param src The array to copy into the buffer. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining <code>shorts<code> in the source array. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public final ShortBuffer put (short[] src) { - return put(src, 0, src.length); + return put (src, 0, src.length); } - public final boolean hasArray() + /** + * Tells whether ot not this buffer is backed by an accessible + * <code>short</code> array. + */ + public final boolean hasArray () { return (backing_buffer != null && !isReadOnly ()); } - public final short[] array() + /** + * Returns the <code>short</code> 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 short[] array () { if (backing_buffer == null) throw new UnsupportedOperationException (); @@ -136,7 +225,14 @@ public abstract class ShortBuffer extends Buffer implements Comparable return backing_buffer; } - public final int arrayOffset() + /** + * Returns the offset within this buffer's backing array of the first element. + * + * @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 (); @@ -147,41 +243,54 @@ public abstract class ShortBuffer extends Buffer implements Comparable return array_offset; } - public int hashCode() + /** + * Calculates a hash code for this buffer. + */ + public int hashCode () { - return super.hashCode(); + // FIXME: Check what SUN calculates here. + return super.hashCode (); } - public boolean equals(Object obj) + /** + * Checks if this buffer is equal to obj. + */ + public boolean equals (Object obj) { if (obj instanceof ShortBuffer) { - return compareTo(obj) == 0; + return compareTo (obj) == 0; } return false; } - public int compareTo(Object ob) + /** + * Compares two <code>ShortBuffer</code> objects. + * + * @exception ClassCastException If obj is not an object derived from + * <code>ShortBuffer</code>. + */ + public int compareTo (Object obj) { - ShortBuffer a = (ShortBuffer) ob; + ShortBuffer a = (ShortBuffer) obj; - if (a.remaining() != remaining()) + if (a.remaining () != remaining ()) return 1; - if (! hasArray() || - ! a.hasArray()) + if (! hasArray () || + ! a.hasArray ()) { return 1; } - int r = remaining(); + int r = remaining (); int i1 = position (); int i2 = a.position (); - for (int i=0;i<r;i++) + for (int i = 0; i < r; i++) { - int t = (int) (get(i1)- a.get(i2)); + int t = (int) (get (i1) - a.get (i2)); if (t != 0) { @@ -192,14 +301,74 @@ public abstract class ShortBuffer extends Buffer implements Comparable return 0; } + /** + * Returns the byte order of this buffer. + */ public abstract ByteOrder order (); - public abstract short get(); - public abstract java.nio. ShortBuffer put(short b); - public abstract short get(int index); - public abstract java.nio. ShortBuffer put(int index, short b); - public abstract ShortBuffer compact(); - public abstract boolean isDirect(); - public abstract ShortBuffer slice(); - public abstract ShortBuffer duplicate(); - public abstract ShortBuffer asReadOnlyBuffer(); + + /** + * Reads the <code>short</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferUnderflowException If there are no remaining + * <code>shorts</code> in this buffer. + */ + public abstract short get (); + + /** + * Writes the <code>short</code> at this buffer's current position, + * and then increments the position. + * + * @exception BufferOverflowException If there no remaining + * <code>shorts</code> in this buffer. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract ShortBuffer put (short b); + + /** + * Absolute get method. + * + * @exception IndexOutOfBoundsException If index is negative or not smaller + * than the buffer's limit. + */ + public abstract short get (int index); + + /** + * 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 ShortBuffer put (int index, short b); + + /** + * Compacts this buffer. + * + * @exception ReadOnlyBufferException If this buffer is read-only. + */ + public abstract ShortBuffer compact (); + + /** + * Tells wether or not this buffer is direct. + */ + public abstract boolean isDirect (); + + /** + * Creates a new <code>ShortBuffer</code> whose content is a shared + * subsequence of this buffer's content. + */ + public abstract ShortBuffer slice (); + + /** + * Creates a new <code>ShortBuffer</code> that shares this buffer's + * content. + */ + public abstract ShortBuffer duplicate (); + + /** + * Creates a new read-only <code>ShortBuffer</code> that shares this + * buffer's content. + */ + public abstract ShortBuffer asReadOnlyBuffer (); } |