aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/nio/CharBuffer.java
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/java/nio/CharBuffer.java')
-rw-r--r--libjava/java/nio/CharBuffer.java246
1 files changed, 190 insertions, 56 deletions
diff --git a/libjava/java/nio/CharBuffer.java b/libjava/java/nio/CharBuffer.java
index 2a21ef93ff5..e2996023047 100644
--- a/libjava/java/nio/CharBuffer.java
+++ b/libjava/java/nio/CharBuffer.java
@@ -37,144 +37,226 @@ exception statement from your version. */
package java.nio;
+import gnu.java.nio.CharBufferImpl;
+
+/**
+ * @since 1.4
+ */
public abstract class CharBuffer extends Buffer
+ implements Comparable, CharSequence
{
- private ByteOrder endian = ByteOrder.BIG_ENDIAN;
-
protected char [] backing_buffer;
+ /**
+ * Allocates a new <code>CharBuffer</code> 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 <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)
{
- if ((offset < 0) ||
- (offset > array.length) ||
- (length < 0) ||
- (length > (array.length - offset)))
- throw new IndexOutOfBoundsException ();
-
- return null;
+ return new CharBufferImpl (array, offset, length);
}
+ /**
+ * Wraps a character sequence into a <code>CharBuffer</code> object.
+ */
final public static CharBuffer wrap (CharSequence a)
{
return wrap (a, 0, a.length ());
}
/**
+ * Wraps a character sequence 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 (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 <code>CharBuffer</code> 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)
+ {
+ super (cap, lim, pos, mark);
}
/**
- * @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
*/
- final public CharBuffer get (char[] dst, int offset, int length)
+ public CharBuffer get (char[] dst, int offset, int length)
{
for (int i = offset; i < offset + length; i++)
{
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)
+ public CharBuffer get (char[] dst)
{
return get (dst, 0, dst.length);
}
/**
- * @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)
+ 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)
+ 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;
+ return (backing_buffer != null
+ && ! isReadOnly ());
}
/**
- * @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)
@@ -184,7 +266,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
+ * <code>CharBuffer</code>.
*/
public int compareTo(Object obj)
{
@@ -197,8 +282,8 @@ public abstract class CharBuffer extends Buffer
return 1;
int r = remaining ();
- int i1 = pos;
- int i2 = a.pos;
+ int i1 = position ();
+ int i2 = a.position ();
for (int i = 0; i < r; i++)
{
@@ -207,85 +292,134 @@ 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 ());
}
+ /**
+ * Returns the length of the remaining chars in this buffer.
+ */
public final int length ()
{
- return 0;
+ return remaining ();
}
+ /**
+ * Returns the byte order of this buffer.
+ */
public abstract ByteOrder order ();
/**
- * @exception IndexOutOfBoundsException FIXME
+ * Creates a new character buffer that represents the specified subsequence
+ * of this buffer, relative to the current position.
+ *
+ * @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 <code>position() + index</code>.
+ *
+ * @exception IndexOutOfBoundsException If index is negative not smaller than
+ * <code>remaining()</code>.
*/
public final char charAt (int index)
{
- return ' ';
+ if (index < 0
+ || index >= remaining ())
+ throw new IndexOutOfBoundsException ();
+
+ return get (position () + index);
}
}