From 35d0b14df6995e79e84201c45d49ef986f9e0b77 Mon Sep 17 00:00:00 2001 From: Per Bothner Date: Mon, 16 Feb 2004 12:00:33 -0800 Subject: ByteBuffer.java (endian): Make non-private so other java.nio classes can inherit it. * java/nio/ByteBuffer.java (endian): Make non-private so other java.nio classes can inherit it. (): Don't bother clearing array_offset. * java/nio/ByteBuffer.java (allocate): Re-implement using wrap. * java/nio/ByteBuffer.java (get(byte[],int,int)): Check underflow. Remove redundant test. * java/nio/ByteBufferImpl.java (asCharBuffer, asShortBuffer, asIntBuffer, asLongBuffer, asFloatBuffer, asDoubleBuffer): Use new XxxViewBufferImpl constructors. * java/nio/MappedByteBufferImpl.java: Likewise. * java/nio/DirectByteBufferImpl.java: Likewise. * java/nio/ByteBufferImpl.java: Remove one constructor. Inline super in remaining constructor. * java/nio/ByteBuffer.java: Remove unused constructor. * java/nio/ByteBufferImpl.java (shiftDown): New optimized method. * java/nio/ByteBufferImpl.java (get, put): Add array_offset. * java/nio/DirectByteBufferImpl.java (owner): New field. (offset): Remove unused field. (): Modify one and add another constructor. Change callers. (allocateDirect): Removed - not used. (getImpl, putImpl): Make static and pass address explicitly, to make them useful for MappedByteBufferImpl. (get, put): Check for underflow. Modify for new getImpl. (getImpl): New native method where target is array. (get(byte[],int,int)): Use the above. (adjustAddress): New static native method. (slice, duplicate, asReadOnly): New implementations. * java/nio/natDirectByteBufferImpl.cc (getImpl, putImpl, shiftDown, adjustAddress): New or updated native methods. From-SVN: r77919 --- libjava/java/nio/ByteBuffer.java | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'libjava/java/nio/ByteBuffer.java') diff --git a/libjava/java/nio/ByteBuffer.java b/libjava/java/nio/ByteBuffer.java index 9ca20618c45..276b2dbcb81 100644 --- a/libjava/java/nio/ByteBuffer.java +++ b/libjava/java/nio/ByteBuffer.java @@ -44,7 +44,7 @@ package java.nio; public abstract class ByteBuffer extends Buffer implements Comparable { - private ByteOrder endian = ByteOrder.BIG_ENDIAN; + ByteOrder endian = ByteOrder.BIG_ENDIAN; int array_offset; byte[] backing_buffer; @@ -52,16 +52,8 @@ public abstract class ByteBuffer extends Buffer ByteBuffer (int capacity, int limit, int position, int mark) { super (capacity, limit, position, mark); - array_offset = 0; } - ByteBuffer (byte[] 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 direct byte buffer. */ @@ -75,7 +67,7 @@ public abstract class ByteBuffer extends Buffer */ public static ByteBuffer allocate (int capacity) { - return new ByteBufferImpl (capacity); + return wrap(new byte[capacity], 0, capacity); } /** @@ -87,6 +79,14 @@ public abstract class ByteBuffer extends Buffer */ final public static ByteBuffer wrap (byte[] array, int offset, int length) { + // FIXME: In GCJ and other implementations where arrays may not + // move we might consider, at least when offset==0: + // return new DirectByteBufferImpl(array, + // address_of_data(array) + offset, + // length, length, 0, false); + // This may be more efficient, mainly because we can then use the + // same logic for all ByteBuffers. + return new ByteBufferImpl (array, 0, array.length, offset + length, offset, -1, false); } @@ -116,11 +116,10 @@ public abstract class ByteBuffer extends Buffer */ public ByteBuffer get (byte[] dst, int offset, int length) { - if ((offset < 0) - || (offset > dst.length) - || (length < 0) - || (length > (dst.length - offset))) + if (offset < 0 || length < 0 || offset + length > dst.length) throw new IndexOutOfBoundsException (); + if (length > remaining()) + throw new BufferUnderflowException(); for (int i = offset; i < offset + length; i++) { -- cgit v1.2.3