From d19e783c45ab5b545af64a414e5edb4e946ba9de Mon Sep 17 00:00:00 2001 From: Michael Koch Date: Tue, 11 Feb 2003 07:42:17 +0000 Subject: [PATCH] 2003-02-11 Michael Koch * java/nio/Buffer.java (cap, lim, pos, mark): Made private (Buffer): Added package private constructor. * java/nio/ByteBuffer.java (ByteBuffer): Implements Cloneable. (offset): New member variable. (readOnly): New member variable. (backing_buffer): New member variable. (allocateDirect): Throw exception and tell that direct buffers are not supported yet, documentation added. (allocate): Documentation added. (wrap): Documentation added. (ByteBuffer): New constructor. (hasArray): New method. (array): New method. (arrayOffset): New method. (get): Documentation added. (put): Documentation added. * java/nio/CharBuffer.java (CharBuffer): New constructor. (compareTo): Don't access member variables of Buffer directly. * java/nio/DoubleBuffer.java (allocateDirect): Throw exception and tell that direct buffers are not supported yet. * java/nio/FloatBuffer.java (allocateDirect): Throw exception and tell that direct buffers are not supported yet. * java/nio/IntBuffer.java (allocateDirect): Throw exception and tell that direct buffers are not supported yet. * java/nio/LongBuffer.java (allocateDirect): Throw exception and tell that direct buffers are not supported yet. * java/nio/MappedByteBuffer.java (MappedByteBuffer): New method. (force): New method. (isLoaded): New method. (load): New method. * java/nio/ShortBuffer.java (allocateDirect): Throw exception and tell that direct buffers are not supported yet. From-SVN: r62685 --- libjava/java/nio/Buffer.java | 32 +++++- libjava/java/nio/ByteBuffer.java | 145 ++++++++++++++++++++++++- libjava/java/nio/CharBuffer.java | 9 +- libjava/java/nio/DoubleBuffer.java | 2 +- libjava/java/nio/FloatBuffer.java | 2 +- libjava/java/nio/IntBuffer.java | 2 +- libjava/java/nio/LongBuffer.java | 2 +- libjava/java/nio/MappedByteBuffer.java | 23 ++++ libjava/java/nio/ShortBuffer.java | 2 +- 9 files changed, 202 insertions(+), 17 deletions(-) diff --git a/libjava/java/nio/Buffer.java b/libjava/java/nio/Buffer.java index 9474fb453f0..7d291bedbc1 100644 --- a/libjava/java/nio/Buffer.java +++ b/libjava/java/nio/Buffer.java @@ -39,11 +39,33 @@ package java.nio; public abstract class Buffer { - int cap = 0; - int limit = 0; - int pos = 0; - int mark = -1; - + private int cap = 0; + private int limit = 0; + private int pos = 0; + private int mark = -1; + + // Creates a new Buffer. + // + // Should be package private. + // + Buffer (int capacity, int limit, int position, int mark) + { + if (capacity < 0) + throw new IllegalArgumentException (); + + cap = capacity; + limit (limit); + position (position); + + if (mark > 0) + { + if (mark > pos) + throw new IllegalArgumentException (); + + this.mark = mark; + } + } + /** * Retrieves the capacity of the buffer. */ diff --git a/libjava/java/nio/ByteBuffer.java b/libjava/java/nio/ByteBuffer.java index 874943a8757..380e6c67168 100644 --- a/libjava/java/nio/ByteBuffer.java +++ b/libjava/java/nio/ByteBuffer.java @@ -37,43 +37,178 @@ exception statement from your version. */ package java.nio; -public abstract class ByteBuffer extends Buffer +/** + * @since 1.4 + */ +public abstract class ByteBuffer extends Buffer implements Comparable { + int offset; + boolean readOnly; + byte[] backing_buffer; + + /** + * Allocates a new direct byte buffer. + */ + public static ByteBuffer allocateDirect (int capacity) + { + throw new Error ("direct buffers are not implemented"); + } + + /** + * Allocates a new byte buffer. + */ public static ByteBuffer allocate (int capacity) { return null; } + /** + * Wraps a byte array into a buffer. + * + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold + */ final public static ByteBuffer wrap (byte[] array, int offset, int length) { return null; } + /** + * Wraps a byte array into a buffer. + */ final public static ByteBuffer wrap (byte[] array) { return wrap (array, 0, array.length); } - - final public ByteBuffer put (ByteBuffer src) + + ByteBuffer (int capacity, int limit, int position, int mark) + { + super (capacity, limit, position, mark); + } + + /** + * Writes the content of src into the buffer. + * + * @param src The source data. + * + * @exception BufferOverflowException If there is insufficient space in this + * buffer for the remaining bytes in the source buffer. + * @exception IllegalArgumentException If the source buffer is this buffer. + * @exception ReadOnlyBufferException If this buffer is read only. + */ + public ByteBuffer put (ByteBuffer src) { + if (src == this) + throw new IllegalArgumentException (); + while (src.hasRemaining ()) put (src.get ()); return this; } - - final public ByteBuffer put (byte[] src, int offset, int length) + + /** + * Writes the content of the the array 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 bytes in the source buffer. + * @exception IndexOutOfBoundsException If the preconditions on the offset + * and length parameters do not hold. + * @exception ReadOnlyBufferException If this buffer is read only. + */ + public ByteBuffer put (byte[] src, int offset, int length) { + if ((offset < 0) || + (offset > src.length) || + (length < 0) || + (length > src.length - offset)) + throw new IndexOutOfBoundsException (); + for (int i = offset; i < offset + length; i++) put (src [i]); + return this; } + + /** + * Writes the content of the the array 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 bytes in the source buffer. + * @exception ReadOnlyBufferException If this buffer is read only. + */ public final ByteBuffer put (byte[] src) { return put (src, 0, src.length); } + /** + * Tells whether or not this buffer is backed by an accessible byte array. + */ + public final boolean hasArray () + { + return (backing_buffer != null + && !readOnly); + } + + /** + * Returns the byte array that backs this buffer. + * + * @exception ReadOnlyBufferException If this buffer is backed by an array + * but is read-only. + * @exception UnsupportedOperationException If this buffer is not backed + * by an accessible array. + */ + public final byte[] array () + { + if (backing_buffer == null) + throw new UnsupportedOperationException (); + + if (readOnly) + throw new ReadOnlyBufferException (); + + return backing_buffer; + } + + /** + * Returns the offset within this buffer's backing array of the first element + * of the buffer + * + * @exception ReadOnlyBufferException If this buffer is backed by an array + * but 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 (readOnly) + throw new ReadOnlyBufferException (); + + return offset; + } + + /** + * Relative get method. + */ public abstract byte get (); + /** + * Relative put method. + * + * @exception BufferOverflowException If this buffer's current position is + * not smaller than its limit. + * @exception ReadOnlyBufferException If this buffer is read-only. + */ public abstract ByteBuffer put (byte b); } diff --git a/libjava/java/nio/CharBuffer.java b/libjava/java/nio/CharBuffer.java index 2a21ef93ff5..0e61302c4e9 100644 --- a/libjava/java/nio/CharBuffer.java +++ b/libjava/java/nio/CharBuffer.java @@ -88,6 +88,11 @@ public abstract class CharBuffer extends Buffer { return wrap (array, 0, array.length); } + + CharBuffer (int cap, int lim, int pos, int mark) + { + super (cap, lim, pos, mark); + } /** * @exception BufferUnderflowException FIXME @@ -197,8 +202,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++) { diff --git a/libjava/java/nio/DoubleBuffer.java b/libjava/java/nio/DoubleBuffer.java index 79de54a13ed..72a6c09078d 100644 --- a/libjava/java/nio/DoubleBuffer.java +++ b/libjava/java/nio/DoubleBuffer.java @@ -46,7 +46,7 @@ public abstract class DoubleBuffer extends Buffer implements Comparable public static DoubleBuffer allocateDirect(int capacity) { - return new DoubleBufferImpl(capacity, 0, capacity); + throw new Error ("direct buffers are not implemented"); } public static DoubleBuffer allocate(int capacity) diff --git a/libjava/java/nio/FloatBuffer.java b/libjava/java/nio/FloatBuffer.java index 7cabd964951..b780ff597d8 100644 --- a/libjava/java/nio/FloatBuffer.java +++ b/libjava/java/nio/FloatBuffer.java @@ -46,7 +46,7 @@ public abstract class FloatBuffer extends Buffer implements Comparable public static FloatBuffer allocateDirect(int capacity) { - return new FloatBufferImpl (capacity, 0, capacity); + throw new Error ("direct buffers not implemented"); } public static FloatBuffer allocate(int capacity) diff --git a/libjava/java/nio/IntBuffer.java b/libjava/java/nio/IntBuffer.java index 2aadf350919..aca6e0f1902 100644 --- a/libjava/java/nio/IntBuffer.java +++ b/libjava/java/nio/IntBuffer.java @@ -46,7 +46,7 @@ public abstract class IntBuffer extends Buffer implements Comparable public static IntBuffer allocateDirect(int capacity) { - return new IntBufferImpl (capacity, 0, capacity); + throw new Error ("direct buffers not implemented"); } public static IntBuffer allocate(int capacity) diff --git a/libjava/java/nio/LongBuffer.java b/libjava/java/nio/LongBuffer.java index 62ff390e82d..0ddc7938a49 100644 --- a/libjava/java/nio/LongBuffer.java +++ b/libjava/java/nio/LongBuffer.java @@ -46,7 +46,7 @@ public abstract class LongBuffer extends Buffer implements Comparable public static LongBuffer allocateDirect(int capacity) { - return new LongBufferImpl(capacity, 0, capacity); + throw new Error ("direct buffers not implemented"); } public static LongBuffer allocate(int capacity) diff --git a/libjava/java/nio/MappedByteBuffer.java b/libjava/java/nio/MappedByteBuffer.java index dc1b20d892c..305327b74c5 100644 --- a/libjava/java/nio/MappedByteBuffer.java +++ b/libjava/java/nio/MappedByteBuffer.java @@ -37,6 +37,29 @@ exception statement from your version. */ package java.nio; +/** + * @author Michael Koch + * @since 1.4 + */ public abstract class MappedByteBuffer extends ByteBuffer { + MappedByteBuffer (int capacity, int limit, int position, int mark) + { + super (capacity, limit, position, mark); + } + + public final MappedByteBuffer force () + { + return this; + } + + public final boolean isLoaded () + { + return true; + } + + public final MappedByteBuffer load () + { + return this; + } } diff --git a/libjava/java/nio/ShortBuffer.java b/libjava/java/nio/ShortBuffer.java index 08c05c40404..512c9fd8bec 100644 --- a/libjava/java/nio/ShortBuffer.java +++ b/libjava/java/nio/ShortBuffer.java @@ -46,7 +46,7 @@ public abstract class ShortBuffer extends Buffer implements Comparable public static ShortBuffer allocateDirect(int capacity) { - return new ShortBufferImpl(capacity, 0, capacity); + throw new Error ("direct buffers not implemented"); } public static ShortBuffer allocate(int capacity) -- 2.30.2