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.
	(<init>):  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.
	(<init>):  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
This commit is contained in:
Per Bothner 2004-02-16 12:00:33 -08:00 committed by Per Bothner
parent 5693912a7d
commit 35d0b14df6
6 changed files with 201 additions and 104 deletions

View file

@ -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++)
{