ByteBuffer.java (shiftDown): New helper method.

* java/nio/ByteBuffer.java (shiftDown):  New helper method.
	* java/nio/natDirectByteBufferImpl.cc (shiftDown):  New implementation.
	* java/nio/ByteBufferImpl.java (compact):  Use new shiftDown method.
	* sava/nio/ByteBufferHelper.java:  Remove redundant 'final' specifiers.
	Pass ByteOrder parameter to most methods, since the underlying
	ByteBuffer's order isn't always what we should use.
	* java/nio/ByteBufferImpl.java:  Pass byte-order various places.
	* java/nio/DirectByteBufferImpl.java:  Likewise.
	Use ByteBufferHelper methods.
	* java/nio/MappedByteBufferImpl.java:  Likewise.
	(compact):  Use shiftDown.
	* java/nio/CharViewBufferImpl.java (<init>):  Pass byte-order.
	(get, put):  Use ByteBufferHelper.
	(compact):  Use new shiftDown method.
	(duplicate(boolean)):  New helper method.
	(duplicate, asReadOnlyBuffer):  Use it.
	(order):  Return endian field.
	* java/nio/DoubleViewBufferImpl.java:  Likewise.
	* java/nio/FloatViewBufferImpl.java:  Likewise.
	* java/nio/IntViewBufferImpl.java:  Likewise.
	* java/nio/LongViewBufferImpl.java:  Likewise.
	* java/nio/ShortViewBufferImpl.java:  Likewise.
	* java/nio/CharViewBufferImpl.java (subsequence):  Redundant test.
	* java/nio/DirectByteBufferImpl.java (shiftDown):  New native method.
	(compact):  Re-implement using shiftDown.

From-SVN: r77501
This commit is contained in:
Per Bothner 2004-02-08 13:02:53 -08:00 committed by Per Bothner
parent b46b8fb40c
commit 40c23042f4
13 changed files with 493 additions and 552 deletions

View file

@ -1,3 +1,31 @@
2004-02-08 Per Bothner <per@bothner.com>
* java/nio/ByteBuffer.java (shiftDown): New helper method.
* java/nio/natDirectByteBufferImpl.cc (shiftDown): New implementation.
* java/nio/ByteBufferImpl.java (compact): Use new shiftDown method.
* sava/nio/ByteBufferHelper.java: Remove redundant 'final' specifiers.
Pass ByteOrder parameter to most methods, since the underlying
ByteBuffer's order isn't always what we should use.
* java/nio/ByteBufferImpl.java: Pass byte-order various places.
* java/nio/DirectByteBufferImpl.java: Likewise.
Use ByteBufferHelper methods.
* java/nio/MappedByteBufferImpl.java: Likewise.
(compact): Use shiftDown.
* java/nio/CharViewBufferImpl.java (<init>): Pass byte-order.
(get, put): Use ByteBufferHelper.
(compact): Use new shiftDown method.
(duplicate(boolean)): New helper method.
(duplicate, asReadOnlyBuffer): Use it.
(order): Return endian field.
* java/nio/DoubleViewBufferImpl.java: Likewise.
* java/nio/FloatViewBufferImpl.java: Likewise.
* java/nio/IntViewBufferImpl.java: Likewise.
* java/nio/LongViewBufferImpl.java: Likewise.
* java/nio/ShortViewBufferImpl.java: Likewise.
* java/nio/CharViewBufferImpl.java (subsequence): Redundant test.
* java/nio/DirectByteBufferImpl.java (shiftDown): New native method.
(compact): Re-implement using shiftDown.
2004-02-08 Andreas Jaeger <aj@suse.de> 2004-02-08 Andreas Jaeger <aj@suse.de>
* include/x86_64-signal.h: Fix typo. * include/x86_64-signal.h: Fix typo.

View file

@ -1,5 +1,5 @@
/* ByteBuffer.java -- /* ByteBuffer.java --
Copyright (C) 2002, 2003 Free Software Foundation, Inc. Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
@ -382,8 +382,14 @@ public abstract class ByteBuffer extends Buffer
*/ */
public abstract ByteBuffer compact (); public abstract ByteBuffer compact ();
void shiftDown (int dst_offset, int src_offset, int count)
{
for (int i = 0; i < count; i++)
put(dst_offset + i, get(src_offset + i));
}
/** /**
* Tells wether or not this buffer is direct. * Tells whether or not this buffer is direct.
*/ */
public abstract boolean isDirect (); public abstract boolean isDirect ();

View file

@ -1,5 +1,5 @@
/* ByteBufferImpl.java -- /* ByteBufferImpl.java --
Copyright (C) 2003 Free Software Foundation, Inc. Copyright (C) 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
@ -42,58 +42,58 @@ package java.nio;
*/ */
final class ByteBufferHelper final class ByteBufferHelper
{ {
private static final void checkRemainingForRead (ByteBuffer buffer, int bytes) private static void checkRemainingForRead (ByteBuffer buffer, int bytes)
{ {
if (buffer.remaining() < bytes) if (buffer.remaining() < bytes)
throw new BufferUnderflowException(); throw new BufferUnderflowException();
} }
private static final void checkRemainingForWrite (ByteBuffer buffer, int bytes) private static void checkRemainingForWrite (ByteBuffer buffer, int bytes)
{ {
if (buffer.remaining() < bytes) if (buffer.remaining() < bytes)
throw new BufferOverflowException(); throw new BufferOverflowException();
} }
private static final void checkAvailableForRead (ByteBuffer buffer, private static void checkAvailableForRead (ByteBuffer buffer,
int index, int bytes) int index, int bytes)
{ {
if (buffer.limit() < (index + bytes)) if (buffer.limit() < (index + bytes))
throw new BufferUnderflowException(); throw new BufferUnderflowException();
} }
private static final void checkAvailableForWrite (ByteBuffer buffer, private static void checkAvailableForWrite (ByteBuffer buffer,
int index, int bytes) int index, int bytes)
{ {
if (buffer.limit() < (index + bytes)) if (buffer.limit() < (index + bytes))
throw new BufferOverflowException(); throw new BufferOverflowException();
} }
public static final char getChar (ByteBuffer buffer) public static char getChar (ByteBuffer buffer, ByteOrder order)
{ {
return (char) getShort (buffer); return (char) getShort (buffer, order);
} }
public static final ByteBuffer putChar (ByteBuffer buffer, char value) public static void putChar (ByteBuffer buffer, char value, ByteOrder order)
{ {
return putShort (buffer, (short) value); putShort (buffer, (short) value, order);
} }
public static final char getChar (ByteBuffer buffer, int index) public static char getChar (ByteBuffer buffer, int index, ByteOrder order)
{ {
return (char) getShort (buffer, index); return (char) getShort (buffer, index, order);
} }
public static final ByteBuffer putChar (ByteBuffer buffer, int index, public static void putChar (ByteBuffer buffer, int index,
char value) char value, ByteOrder order)
{ {
return putShort (buffer, index, (short) value); putShort (buffer, index, (short) value, order);
} }
public static final short getShort (ByteBuffer buffer) public static short getShort (ByteBuffer buffer, ByteOrder order)
{ {
checkRemainingForRead (buffer, 2); checkRemainingForRead (buffer, 2);
if (buffer.order() == ByteOrder.LITTLE_ENDIAN) if (order == ByteOrder.LITTLE_ENDIAN)
{ {
return (short) ((buffer.get() & 0xff) return (short) ((buffer.get() & 0xff)
+ (buffer.get() << 8)); + (buffer.get() << 8));
@ -103,11 +103,11 @@ final class ByteBufferHelper
+ (buffer.get() & 0xff)); + (buffer.get() & 0xff));
} }
public static final ByteBuffer putShort (ByteBuffer buffer, short value) public static void putShort (ByteBuffer buffer, short value, ByteOrder order)
{ {
checkRemainingForWrite (buffer, 2); checkRemainingForWrite (buffer, 2);
if (buffer.order() == ByteOrder.LITTLE_ENDIAN) if (order == ByteOrder.LITTLE_ENDIAN)
{ {
buffer.put ((byte) value); buffer.put ((byte) value);
buffer.put ((byte) (value >> 8)); buffer.put ((byte) (value >> 8));
@ -117,15 +117,14 @@ final class ByteBufferHelper
buffer.put ((byte) (value >> 8)); buffer.put ((byte) (value >> 8));
buffer.put ((byte) value); buffer.put ((byte) value);
} }
return buffer;
} }
public static final short getShort (ByteBuffer buffer, int index) public static short getShort (ByteBuffer buffer,
int index, ByteOrder order)
{ {
checkAvailableForRead (buffer, index, 2); checkAvailableForRead (buffer, index, 2);
if (buffer.order() == ByteOrder.LITTLE_ENDIAN) if (order == ByteOrder.LITTLE_ENDIAN)
{ {
return (short) ((buffer.get (index) & 0xff) return (short) ((buffer.get (index) & 0xff)
+ (buffer.get (++index) << 8)); + (buffer.get (++index) << 8));
@ -135,12 +134,12 @@ final class ByteBufferHelper
+ (buffer.get (++index) & 0xff)); + (buffer.get (++index) & 0xff));
} }
public static final ByteBuffer putShort (ByteBuffer buffer, int index, public static void putShort (ByteBuffer buffer, int index,
short value) short value, ByteOrder order)
{ {
checkAvailableForWrite (buffer, index, 2); checkAvailableForWrite (buffer, index, 2);
if (buffer.order() == ByteOrder.LITTLE_ENDIAN) if (order == ByteOrder.LITTLE_ENDIAN)
{ {
buffer.put (index, (byte) value); buffer.put (index, (byte) value);
buffer.put (++index, (byte) (value >> 8)); buffer.put (++index, (byte) (value >> 8));
@ -150,15 +149,13 @@ final class ByteBufferHelper
buffer.put (index, (byte) (value >> 8)); buffer.put (index, (byte) (value >> 8));
buffer.put (++index, (byte) value); buffer.put (++index, (byte) value);
} }
return buffer;
} }
public static final int getInt (ByteBuffer buffer) public static int getInt (ByteBuffer buffer, ByteOrder order)
{ {
checkRemainingForRead (buffer, 4); checkRemainingForRead (buffer, 4);
if (buffer.order() == ByteOrder.LITTLE_ENDIAN) if (order == ByteOrder.LITTLE_ENDIAN)
{ {
return ((buffer.get() & 0xff) return ((buffer.get() & 0xff)
+ ((buffer.get() & 0xff) << 8) + ((buffer.get() & 0xff) << 8)
@ -172,11 +169,11 @@ final class ByteBufferHelper
+ (buffer.get() & 0xff)); + (buffer.get() & 0xff));
} }
public static final ByteBuffer putInt (ByteBuffer buffer, int value) public static void putInt (ByteBuffer buffer, int value, ByteOrder order)
{ {
checkRemainingForWrite (buffer, 4); checkRemainingForWrite (buffer, 4);
if (buffer.order() == ByteOrder.LITTLE_ENDIAN) if (order == ByteOrder.LITTLE_ENDIAN)
{ {
buffer.put ((byte) value); buffer.put ((byte) value);
buffer.put ((byte) (value >> 8)); buffer.put ((byte) (value >> 8));
@ -190,15 +187,13 @@ final class ByteBufferHelper
buffer.put ((byte) (value >> 8)); buffer.put ((byte) (value >> 8));
buffer.put ((byte) value); buffer.put ((byte) value);
} }
return buffer;
} }
public static final int getInt (ByteBuffer buffer, int index) public static int getInt (ByteBuffer buffer, int index, ByteOrder order)
{ {
checkAvailableForRead (buffer, index, 4); checkAvailableForRead (buffer, index, 4);
if (buffer.order() == ByteOrder.LITTLE_ENDIAN) if (order == ByteOrder.LITTLE_ENDIAN)
{ {
return ((buffer.get (index) & 0xff) return ((buffer.get (index) & 0xff)
+ ((buffer.get (++index) & 0xff) << 8) + ((buffer.get (++index) & 0xff) << 8)
@ -212,12 +207,12 @@ final class ByteBufferHelper
+ (buffer.get (++index) & 0xff)); + (buffer.get (++index) & 0xff));
} }
public static final ByteBuffer putInt (ByteBuffer buffer, int index, public static void putInt (ByteBuffer buffer, int index,
int value) int value, ByteOrder order)
{ {
checkAvailableForWrite (buffer, index, 4); checkAvailableForWrite (buffer, index, 4);
if (buffer.order() == ByteOrder.LITTLE_ENDIAN) if (order == ByteOrder.LITTLE_ENDIAN)
{ {
buffer.put (index, (byte) value); buffer.put (index, (byte) value);
buffer.put (++index, (byte) (value >> 8)); buffer.put (++index, (byte) (value >> 8));
@ -231,15 +226,13 @@ final class ByteBufferHelper
buffer.put (++index, (byte) (value >> 8)); buffer.put (++index, (byte) (value >> 8));
buffer.put (++index, (byte) value); buffer.put (++index, (byte) value);
} }
return buffer;
} }
public static final long getLong (ByteBuffer buffer) public static long getLong (ByteBuffer buffer, ByteOrder order)
{ {
checkRemainingForRead (buffer, 8); checkRemainingForRead (buffer, 8);
if (buffer.order() == ByteOrder.LITTLE_ENDIAN) if (order == ByteOrder.LITTLE_ENDIAN)
{ {
return ((buffer.get() & 0xff) return ((buffer.get() & 0xff)
+ (((buffer.get() & 0xff)) << 8) + (((buffer.get() & 0xff)) << 8)
@ -261,11 +254,11 @@ final class ByteBufferHelper
+ (buffer.get() & 0xff)); + (buffer.get() & 0xff));
} }
public static final ByteBuffer putLong (ByteBuffer buffer, long value) public static void putLong (ByteBuffer buffer, long value, ByteOrder order)
{ {
checkRemainingForWrite (buffer, 8); checkRemainingForWrite (buffer, 8);
if (buffer.order() == ByteOrder.LITTLE_ENDIAN) if (order == ByteOrder.LITTLE_ENDIAN)
{ {
buffer.put ((byte) value); buffer.put ((byte) value);
buffer.put ((byte) (value >> 8)); buffer.put ((byte) (value >> 8));
@ -287,15 +280,13 @@ final class ByteBufferHelper
buffer.put ((byte) (value >> 8)); buffer.put ((byte) (value >> 8));
buffer.put ((byte) value); buffer.put ((byte) value);
} }
return buffer;
} }
public static final long getLong (ByteBuffer buffer, int index) public static long getLong (ByteBuffer buffer, int index, ByteOrder order)
{ {
checkAvailableForRead (buffer, index, 8); checkAvailableForRead (buffer, index, 8);
if (buffer.order() == ByteOrder.LITTLE_ENDIAN) if (order == ByteOrder.LITTLE_ENDIAN)
{ {
return ((buffer.get (index) & 0xff) return ((buffer.get (index) & 0xff)
+ ((buffer.get (++index) & 0xff) << 8) + ((buffer.get (++index) & 0xff) << 8)
@ -317,12 +308,12 @@ final class ByteBufferHelper
+ (buffer.get (++index) & 0xff)); + (buffer.get (++index) & 0xff));
} }
public static final ByteBuffer putLong (ByteBuffer buffer, int index, public static void putLong (ByteBuffer buffer, int index,
long value) long value, ByteOrder order)
{ {
checkAvailableForWrite (buffer, index, 8); checkAvailableForWrite (buffer, index, 8);
if (buffer.order() == ByteOrder.LITTLE_ENDIAN) if (order == ByteOrder.LITTLE_ENDIAN)
{ {
buffer.put (index, (byte) value); buffer.put (index, (byte) value);
buffer.put (++index, (byte) (value >> 8)); buffer.put (++index, (byte) (value >> 8));
@ -344,50 +335,47 @@ final class ByteBufferHelper
buffer.put (++index, (byte) (value >> 8)); buffer.put (++index, (byte) (value >> 8));
buffer.put (++index, (byte) value); buffer.put (++index, (byte) value);
} }
return buffer;
} }
public static final float getFloat (ByteBuffer buffer) public static float getFloat (ByteBuffer buffer, ByteOrder order)
{ {
return Float.intBitsToFloat (getInt (buffer)); return Float.intBitsToFloat (getInt (buffer, order));
} }
public static final ByteBuffer putFloat (ByteBuffer buffer, float value) public static void putFloat (ByteBuffer buffer, float value, ByteOrder order)
{ {
return putInt (buffer, Float.floatToRawIntBits (value)); putInt (buffer, Float.floatToRawIntBits (value), order);
} }
public static final float getFloat (ByteBuffer buffer, int index) public static float getFloat (ByteBuffer buffer, int index, ByteOrder order)
{ {
return Float.intBitsToFloat (getInt (buffer, index)); return Float.intBitsToFloat (getInt (buffer, index, order));
} }
public static final ByteBuffer putFloat (ByteBuffer buffer, int index, public static void putFloat (ByteBuffer buffer, int index,
float value) float value, ByteOrder order)
{ {
return putInt (buffer, index, Float.floatToRawIntBits (value)); putInt (buffer, index, Float.floatToRawIntBits (value), order);
} }
public static final double getDouble (ByteBuffer buffer) public static double getDouble (ByteBuffer buffer, ByteOrder order)
{ {
return Double.longBitsToDouble (getLong (buffer)); return Double.longBitsToDouble (getLong (buffer, order));
} }
public static final ByteBuffer putDouble (ByteBuffer buffer, double value) public static void putDouble (ByteBuffer buffer, double value, ByteOrder order)
{ {
return putLong (buffer, Double.doubleToLongBits (value)); putLong (buffer, Double.doubleToLongBits (value), order);
} }
public static final double getDouble (ByteBuffer buffer, int index) public static double getDouble (ByteBuffer buffer, int index, ByteOrder order)
{ {
return Double.longBitsToDouble (getLong (buffer, index)); return Double.longBitsToDouble (getLong (buffer, index, order));
} }
public static final ByteBuffer putDouble (ByteBuffer buffer, int index, public static void putDouble (ByteBuffer buffer, int index,
double value) double value, ByteOrder order)
{ {
return putLong (buffer, index, Double.doubleToLongBits (value)); putLong (buffer, index, Double.doubleToLongBits (value), order);
} }
} // ByteBufferHelper } // ByteBufferHelper

View file

@ -1,5 +1,5 @@
/* ByteBufferImpl.java -- /* ByteBufferImpl.java --
Copyright (C) 2002, 2003 Free Software Foundation, Inc. Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
@ -58,32 +58,32 @@ final class ByteBufferImpl extends ByteBuffer
public CharBuffer asCharBuffer () public CharBuffer asCharBuffer ()
{ {
return new CharViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ()); return new CharViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
} }
public ShortBuffer asShortBuffer () public ShortBuffer asShortBuffer ()
{ {
return new ShortViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ()); return new ShortViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
} }
public IntBuffer asIntBuffer () public IntBuffer asIntBuffer ()
{ {
return new IntViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ()); return new IntViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
} }
public LongBuffer asLongBuffer () public LongBuffer asLongBuffer ()
{ {
return new LongViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ()); return new LongViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
} }
public FloatBuffer asFloatBuffer () public FloatBuffer asFloatBuffer ()
{ {
return new FloatViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ()); return new FloatViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
} }
public DoubleBuffer asDoubleBuffer () public DoubleBuffer asDoubleBuffer ()
{ {
return new DoubleViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ()); return new DoubleViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
} }
public boolean isReadOnly () public boolean isReadOnly ()
@ -108,15 +108,14 @@ final class ByteBufferImpl extends ByteBuffer
public ByteBuffer compact () public ByteBuffer compact ()
{ {
int copied = 0; int pos = position();
if (pos > 0)
while (remaining () > 0)
{ {
put (copied, get ()); int count = remaining();
copied++; shiftDown(0, pos, count);
position(count);
limit(capacity());
} }
position (copied);
return this; return this;
} }
@ -182,121 +181,133 @@ final class ByteBufferImpl extends ByteBuffer
final public char getChar () final public char getChar ()
{ {
return ByteBufferHelper.getChar (this); return ByteBufferHelper.getChar(this, order());
} }
final public ByteBuffer putChar (char value) final public ByteBuffer putChar (char value)
{ {
return ByteBufferHelper.putChar (this, value); ByteBufferHelper.putChar(this, value, order());
return this;
} }
final public char getChar (int index) final public char getChar (int index)
{ {
return ByteBufferHelper.getChar (this, index); return ByteBufferHelper.getChar(this, index, order());
} }
final public ByteBuffer putChar (int index, char value) final public ByteBuffer putChar (int index, char value)
{ {
return ByteBufferHelper.putChar (this, index, value); ByteBufferHelper.putChar(this, index, value, order());
return this;
} }
final public short getShort () final public short getShort ()
{ {
return ByteBufferHelper.getShort (this); return ByteBufferHelper.getShort(this, order());
} }
final public ByteBuffer putShort (short value) final public ByteBuffer putShort (short value)
{ {
return ByteBufferHelper.putShort (this, value); ByteBufferHelper.putShort(this, value, order());
return this;
} }
final public short getShort (int index) final public short getShort (int index)
{ {
return ByteBufferHelper.getShort (this, index); return ByteBufferHelper.getShort(this, index, order());
} }
final public ByteBuffer putShort (int index, short value) final public ByteBuffer putShort (int index, short value)
{ {
return ByteBufferHelper.putShort (this, index, value); ByteBufferHelper.putShort(this, index, value, order());
return this;
} }
final public int getInt () final public int getInt ()
{ {
return ByteBufferHelper.getInt (this); return ByteBufferHelper.getInt(this, order());
} }
final public ByteBuffer putInt (int value) final public ByteBuffer putInt (int value)
{ {
return ByteBufferHelper.putInt (this, value); ByteBufferHelper.putInt(this, value, order());
return this;
} }
final public int getInt (int index) final public int getInt (int index)
{ {
return ByteBufferHelper.getInt (this, index); return ByteBufferHelper.getInt(this, index, order());
} }
final public ByteBuffer putInt (int index, int value) final public ByteBuffer putInt (int index, int value)
{ {
return ByteBufferHelper.putInt (this, index, value); ByteBufferHelper.putInt(this, index, value, order());
return this;
} }
final public long getLong () final public long getLong ()
{ {
return ByteBufferHelper.getLong (this); return ByteBufferHelper.getLong(this, order());
} }
final public ByteBuffer putLong (long value) final public ByteBuffer putLong (long value)
{ {
return ByteBufferHelper.putLong (this, value); ByteBufferHelper.putLong (this, value, order());
return this;
} }
final public long getLong (int index) final public long getLong (int index)
{ {
return ByteBufferHelper.getLong (this, index); return ByteBufferHelper.getLong (this, index, order());
} }
final public ByteBuffer putLong (int index, long value) final public ByteBuffer putLong (int index, long value)
{ {
return ByteBufferHelper.putLong (this, index, value); ByteBufferHelper.putLong (this, index, value, order());
return this;
} }
final public float getFloat () final public float getFloat ()
{ {
return ByteBufferHelper.getFloat (this); return ByteBufferHelper.getFloat (this, order());
} }
final public ByteBuffer putFloat (float value) final public ByteBuffer putFloat (float value)
{ {
return ByteBufferHelper.putFloat (this, value); ByteBufferHelper.putFloat (this, value, order());
return this;
} }
final public float getFloat (int index) public final float getFloat (int index)
{ {
return ByteBufferHelper.getFloat (this, index); return ByteBufferHelper.getFloat (this, index, order());
} }
public final ByteBuffer putFloat (int index, float value) final public ByteBuffer putFloat (int index, float value)
{ {
return ByteBufferHelper.putFloat (this, index, value); ByteBufferHelper.putFloat (this, index, value, order());
return this;
} }
final public double getDouble () final public double getDouble ()
{ {
return ByteBufferHelper.getDouble (this); return ByteBufferHelper.getDouble (this, order());
} }
final public ByteBuffer putDouble (double value) final public ByteBuffer putDouble (double value)
{ {
return ByteBufferHelper.putDouble (this, value); ByteBufferHelper.putDouble (this, value, order());
return this;
} }
final public double getDouble (int index) final public double getDouble (int index)
{ {
return ByteBufferHelper.getDouble (this, index); return ByteBufferHelper.getDouble (this, index, order());
} }
final public ByteBuffer putDouble (int index, double value) final public ByteBuffer putDouble (int index, double value)
{ {
return ByteBufferHelper.putDouble (this, index, value); ByteBufferHelper.putDouble (this, index, value, order());
return this;
} }
} }

View file

@ -1,5 +1,5 @@
/* CharViewBufferImpl.java -- /* CharViewBufferImpl.java --
Copyright (C) 2003 Free Software Foundation, Inc. Copyright (C) 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
@ -40,54 +40,47 @@ package java.nio;
class CharViewBufferImpl extends CharBuffer class CharViewBufferImpl extends CharBuffer
{ {
private boolean readOnly; /** Position in bb (i.e. a byte offset) where this buffer starts. */
private int offset; private int offset;
private ByteBuffer bb; private ByteBuffer bb;
private boolean readOnly;
private ByteOrder endian; private ByteOrder endian;
public CharViewBufferImpl (ByteBuffer bb, boolean readOnly)
{
super (bb.remaining () >> 1, bb.remaining () >> 1, bb.position (), 0);
this.bb = bb;
this.readOnly = readOnly;
// FIXME: What if this is called from CharByteBufferImpl and ByteBuffer has changed its endianess ?
this.endian = bb.order ();
}
public CharViewBufferImpl (ByteBuffer bb, int offset, int capacity, public CharViewBufferImpl (ByteBuffer bb, int offset, int capacity,
int limit, int position, int mark, int limit, int position, int mark,
boolean readOnly) boolean readOnly, ByteOrder endian)
{ {
super (limit >> 1, limit >> 1, position >> 1, mark >> 1); super (limit >> 1, limit >> 1, position >> 1, mark >> 1);
this.bb = bb; this.bb = bb;
this.offset = offset; this.offset = offset;
this.readOnly = readOnly; this.readOnly = readOnly;
// FIXME: What if this is called from CharViewBufferImpl and ByteBuffer has changed its endianess ? this.endian = endian;
this.endian = bb.order ();
} }
public char get () public char get ()
{ {
char result = bb.getChar ((position () << 1) + offset); int p = position();
position (position () + 1); char result = ByteBufferHelper.getChar(bb, (p << 1) + offset, endian);
position(p + 1);
return result; return result;
} }
public char get (int index) public char get (int index)
{ {
return bb.getChar ((index << 1) + offset); return ByteBufferHelper.getChar(bb, (index << 1) + offset, endian);
} }
public CharBuffer put (char value) public CharBuffer put (char value)
{ {
bb.putChar ((position () << 1) + offset, value); int p = position();
position (position () + 1); ByteBufferHelper.putChar(bb, (p << 1) + offset, value, endian);
position(p + 1);
return this; return this;
} }
public CharBuffer put (int index, char value) public CharBuffer put (int index, char value)
{ {
bb.putChar ((index << 1) + offset, value); ByteBufferHelper.putChar(bb, (index << 1) + offset, value, endian);
return this; return this;
} }
@ -95,59 +88,54 @@ class CharViewBufferImpl extends CharBuffer
{ {
if (position () > 0) if (position () > 0)
{ {
// Copy all data from position() to limit() to the beginning of the
// buffer, set position to end of data and limit to capacity
// XXX: This can surely be optimized, for direct and non-direct buffers
int count = limit () - position (); int count = limit () - position ();
bb.shiftDown(offset, offset + 2 * position(), 2 * count);
for (int i = 0; i < count; i++)
{
bb.putChar ((i >> 1) + offset,
bb.getChar (((i + position ()) >> 1) + offset));
}
position (count); position (count);
limit (capacity ()); limit (capacity ());
} }
return this; return this;
} }
public CharBuffer duplicate ()
{
// Create a copy of this object that shares its content
// FIXME: mark is not correct
return new CharViewBufferImpl (bb, offset, capacity (), limit (),
position (), -1, isReadOnly ());
}
public CharBuffer slice () public CharBuffer slice ()
{ {
// Create a sliced copy of this object that shares its content. // Create a sliced copy of this object that shares its content.
return new CharViewBufferImpl (bb, (position () >> 1) + offset, return new CharViewBufferImpl (bb, (position () >> 1) + offset,
remaining (), remaining (), 0, -1, remaining (), remaining (), 0, -1,
isReadOnly ()); isReadOnly (), endian);
} }
public CharSequence subSequence (int start, int end) CharBuffer duplicate (boolean readOnly)
{ {
if (start < 0 int pos = position();
|| start > length () reset();
|| end < start int mark = position();
|| end > length ()) position(pos);
throw new IndexOutOfBoundsException (); return new CharViewBufferImpl (bb, offset, capacity(), limit(),
pos, mark, readOnly, endian);
return new CharViewBufferImpl (bb, array_offset, capacity (), position () + end, position () + start, -1, isReadOnly ()); }
public CharBuffer duplicate ()
{
return duplicate(readOnly);
} }
public CharBuffer asReadOnlyBuffer () public CharBuffer asReadOnlyBuffer ()
{ {
// Create a copy of this object that shares its content and is read-only return duplicate(true);
return new CharViewBufferImpl (bb, (position () >> 1) + offset,
remaining (), remaining (), 0, -1, true);
} }
public CharSequence subSequence (int start, int end)
{
if (start < 0
|| end < start
|| end > length ())
throw new IndexOutOfBoundsException ();
return new CharViewBufferImpl (bb, array_offset, capacity (),
position () + end, position () + start,
-1, isReadOnly (), endian);
}
public boolean isReadOnly () public boolean isReadOnly ()
{ {
return readOnly; return readOnly;
@ -160,6 +148,6 @@ class CharViewBufferImpl extends CharBuffer
public ByteOrder order () public ByteOrder order ()
{ {
return ByteOrder.LITTLE_ENDIAN; return endian;
} }
} }

View file

@ -1,5 +1,5 @@
/* DirectByteBufferImpl.java -- /* DirectByteBufferImpl.java --
Copyright (C) 2003 Free Software Foundation, Inc. Copyright (C) 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
@ -117,18 +117,18 @@ class DirectByteBufferImpl extends ByteBuffer
return this; return this;
} }
native void shiftDown (int dst_offset, int src_offset, int count);
public ByteBuffer compact () public ByteBuffer compact ()
{ {
// FIXME this can sure be optimized using memcpy() int pos = position();
int copied = 0; if (pos > 0)
while (remaining () > 0)
{ {
put (copied, get ()); int count = remaining();
copied++; shiftDown(0, pos, count);
position(count);
limit(capacity());
} }
position (copied);
return this; return this;
} }
@ -161,197 +161,163 @@ class DirectByteBufferImpl extends ByteBuffer
public CharBuffer asCharBuffer () public CharBuffer asCharBuffer ()
{ {
return new CharViewBufferImpl (this, position () + offset, remaining (), remaining (), 0, -1, isReadOnly ()); return new CharViewBufferImpl (this, position (), remaining (), remaining (), 0, -1, isReadOnly (), order());
} }
public DoubleBuffer asDoubleBuffer () public DoubleBuffer asDoubleBuffer ()
{ {
return new DoubleViewBufferImpl (this, position () + offset, remaining (), remaining (), 0, -1, isReadOnly ()); return new DoubleViewBufferImpl (this, position (), remaining (), remaining (), 0, -1, isReadOnly (), order());
} }
public FloatBuffer asFloatBuffer () public FloatBuffer asFloatBuffer ()
{ {
return new FloatViewBufferImpl (this, position () + offset, remaining (), remaining (), 0, -1, isReadOnly ()); return new FloatViewBufferImpl (this, position (), remaining (), remaining (), 0, -1, isReadOnly (), order());
} }
public IntBuffer asIntBuffer () public IntBuffer asIntBuffer ()
{ {
return new IntViewBufferImpl (this, position () + offset, remaining (), remaining (), 0, -1, isReadOnly ()); return new IntViewBufferImpl (this, position (), remaining (), remaining (), 0, -1, isReadOnly (), order());
} }
public LongBuffer asLongBuffer () public LongBuffer asLongBuffer ()
{ {
return new LongViewBufferImpl (this, position () + offset, remaining (), remaining (), 0, -1, isReadOnly ()); return new LongViewBufferImpl (this, position (), remaining (), remaining (), 0, -1, isReadOnly (), order());
} }
public ShortBuffer asShortBuffer () public ShortBuffer asShortBuffer ()
{ {
return new ShortViewBufferImpl (this, position () + offset, remaining (), remaining (), 0, -1, isReadOnly ()); return new ShortViewBufferImpl (this, position (), remaining (), remaining (), 0, -1, isReadOnly (), order());
} }
final public char getChar () final public char getChar ()
{ {
// FIXME: this handles little endian only return ByteBufferHelper.getChar(this, order());
return (char) (((get () & 0xff) << 8)
+ (get () & 0xff));
} }
final public ByteBuffer putChar (char value) final public ByteBuffer putChar (char value)
{ {
// FIXME: this handles little endian only ByteBufferHelper.putChar(this, value, order());
put ((byte) ((((int) value) & 0xff00) >> 8));
put ((byte) (((int) value) & 0x00ff));
return this; return this;
} }
final public char getChar (int index) final public char getChar (int index)
{ {
// FIXME: this handles little endian only return ByteBufferHelper.getChar(this, index, order());
return (char) (((get (index) & 0xff) << 8)
+ (get (index + 1) & 0xff));
} }
final public ByteBuffer putChar (int index, char value) final public ByteBuffer putChar (int index, char value)
{ {
// FIXME: this handles little endian only ByteBufferHelper.putChar(this, index, value, order());
put (index, (byte) ((((int) value) & 0xff00) >> 8));
put (index + 1, (byte) (((int) value) & 0x00ff));
return this; return this;
} }
final public short getShort () final public short getShort ()
{ {
// FIXME: this handles little endian only return ByteBufferHelper.getShort(this, order());
return (short) (((get () & 0xff) << 8)
+ (get () & 0xff));
} }
final public ByteBuffer putShort (short value) final public ByteBuffer putShort (short value)
{ {
// FIXME: this handles little endian only ByteBufferHelper.putShort(this, value, order());
put ((byte) ((((int) value) & 0xff00) >> 8));
put ((byte) (((int) value) & 0x00ff));
return this; return this;
} }
final public short getShort (int index) final public short getShort (int index)
{ {
// FIXME: this handles little endian only return ByteBufferHelper.getShort(this, index, order());
return (short) (((get (index) & 0xff) << 8)
+ (get (index + 1) & 0xff));
} }
final public ByteBuffer putShort (int index, short value) final public ByteBuffer putShort (int index, short value)
{ {
// FIXME: this handles little endian only ByteBufferHelper.putShort(this, index, value, order());
put (index, (byte) ((((int) value) & 0xff00) >> 8));
put (index + 1, (byte) (((int) value) & 0x00ff));
return this; return this;
} }
final public int getInt () final public int getInt ()
{ {
// FIXME: this handles little endian only return ByteBufferHelper.getInt(this, order());
return (int) (((get () & 0xff) << 24)
+ ((get () & 0xff) << 16)
+ ((get () & 0xff) << 8)
+ (get () & 0xff));
} }
final public ByteBuffer putInt (int value) final public ByteBuffer putInt (int value)
{ {
// FIXME: this handles little endian only ByteBufferHelper.putInt(this, value, order());
put ((byte) ((((int) value) & 0xff000000) >> 24));
put ((byte) ((((int) value) & 0x00ff0000) >> 16));
put ((byte) ((((int) value) & 0x0000ff00) >> 8));
put ((byte) (((int) value) & 0x000000ff));
return this; return this;
} }
final public int getInt (int index) final public int getInt (int index)
{ {
// FIXME: this handles little endian only return ByteBufferHelper.getInt(this, index, order());
return (int) (((get (index) & 0xff) << 24)
+ ((get (index + 1) & 0xff) << 16)
+ ((get (index + 2) & 0xff) << 8)
+ (get (index + 3) & 0xff));
} }
final public ByteBuffer putInt (int index, int value) final public ByteBuffer putInt (int index, int value)
{ {
// FIXME: this handles little endian only ByteBufferHelper.putInt(this, index, value, order());
put (index, (byte) ((((int) value) & 0xff000000) >> 24));
put (index + 1, (byte) ((((int) value) & 0x00ff0000) >> 16));
put (index + 2, (byte) ((((int) value) & 0x0000ff00) >> 8));
put (index + 3, (byte) (((int) value) & 0x000000ff));
return this; return this;
} }
final public long getLong () final public long getLong ()
{ {
// FIXME: this handles little endian only return ByteBufferHelper.getLong(this, order());
return (long) (((get () & 0xff) << 56)
+ ((get () & 0xff) << 48)
+ ((get () & 0xff) << 40)
+ ((get () & 0xff) << 32)
+ ((get () & 0xff) << 24)
+ ((get () & 0xff) << 16)
+ ((get () & 0xff) << 8)
+ (get () & 0xff));
} }
final public ByteBuffer putLong (long value) final public ByteBuffer putLong (long value)
{ {
return ByteBufferHelper.putLong (this, value); ByteBufferHelper.putLong (this, value, order());
return this;
} }
final public long getLong (int index) final public long getLong (int index)
{ {
return ByteBufferHelper.getLong (this, index); return ByteBufferHelper.getLong (this, index, order());
} }
final public ByteBuffer putLong (int index, long value) final public ByteBuffer putLong (int index, long value)
{ {
return ByteBufferHelper.putLong (this, index, value); ByteBufferHelper.putLong (this, index, value, order());
return this;
} }
final public float getFloat () final public float getFloat ()
{ {
return ByteBufferHelper.getFloat (this); return ByteBufferHelper.getFloat (this, order());
} }
final public ByteBuffer putFloat (float value) final public ByteBuffer putFloat (float value)
{ {
return ByteBufferHelper.putFloat (this, value); ByteBufferHelper.putFloat (this, value, order());
return this;
} }
public final float getFloat (int index) public final float getFloat (int index)
{ {
return ByteBufferHelper.getFloat (this, index); return ByteBufferHelper.getFloat (this, index, order());
} }
final public ByteBuffer putFloat (int index, float value) final public ByteBuffer putFloat (int index, float value)
{ {
return ByteBufferHelper.putFloat (this, index, value); ByteBufferHelper.putFloat (this, index, value, order());
return this;
} }
final public double getDouble () final public double getDouble ()
{ {
return ByteBufferHelper.getDouble (this); return ByteBufferHelper.getDouble (this, order());
} }
final public ByteBuffer putDouble (double value) final public ByteBuffer putDouble (double value)
{ {
return ByteBufferHelper.putDouble (this, value); ByteBufferHelper.putDouble (this, value, order());
return this;
} }
final public double getDouble (int index) final public double getDouble (int index)
{ {
return ByteBufferHelper.getDouble (this, index); return ByteBufferHelper.getDouble (this, index, order());
} }
final public ByteBuffer putDouble (int index, double value) final public ByteBuffer putDouble (int index, double value)
{ {
return ByteBufferHelper.putDouble (this, index, value); ByteBufferHelper.putDouble (this, index, value, order());
return this;
} }
} }

View file

@ -1,5 +1,5 @@
/* DoubleViewBufferImpl.java -- /* DoubleViewBufferImpl.java --
Copyright (C) 2003 Free Software Foundation, Inc. Copyright (C) 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
@ -40,54 +40,47 @@ package java.nio;
class DoubleViewBufferImpl extends DoubleBuffer class DoubleViewBufferImpl extends DoubleBuffer
{ {
private boolean readOnly; /** Position in bb (i.e. a byte offset) where this buffer starts. */
private int offset; private int offset;
private ByteBuffer bb; private ByteBuffer bb;
private boolean readOnly;
private ByteOrder endian; private ByteOrder endian;
public DoubleViewBufferImpl (ByteBuffer bb, boolean readOnly)
{
super (bb.remaining () >> 3, bb.remaining () >> 3, bb.position (), 0);
this.bb = bb;
this.readOnly = readOnly;
// FIXME: What if this is called from DoubleByteBufferImpl and ByteBuffer has changed its endianess ?
this.endian = bb.order ();
}
public DoubleViewBufferImpl (ByteBuffer bb, int offset, int capacity, public DoubleViewBufferImpl (ByteBuffer bb, int offset, int capacity,
int limit, int position, int mark, int limit, int position, int mark,
boolean readOnly) boolean readOnly, ByteOrder endian)
{ {
super (limit >> 3, limit >> 3, position >> 3, mark >> 3); super (limit >> 3, limit >> 3, position >> 3, mark >> 3);
this.bb = bb; this.bb = bb;
this.offset = offset; this.offset = offset;
this.readOnly = readOnly; this.readOnly = readOnly;
// FIXME: What if this is called from DoubleViewBufferImpl and ByteBuffer has changed its endianess ? this.endian = endian;
this.endian = bb.order ();
} }
public double get () public double get ()
{ {
double result = bb.getDouble ((position () << 3) + offset); int p = position();
position (position () + 1); double result = ByteBufferHelper.getDouble(bb, (p << 3) + offset, endian);
position(p + 1);
return result; return result;
} }
public double get (int index) public double get (int index)
{ {
return bb.getDouble ((index << 3) + offset); return ByteBufferHelper.getDouble(bb, (index << 3) + offset, endian);
} }
public DoubleBuffer put (double value) public DoubleBuffer put (double value)
{ {
bb.putDouble ((position () << 3) + offset, value); int p = position();
position (position () + 1); ByteBufferHelper.putDouble(bb, (p << 3) + offset, value, endian);
position(p + 1);
return this; return this;
} }
public DoubleBuffer put (int index, double value) public DoubleBuffer put (int index, double value)
{ {
bb.putDouble ((index << 3) + offset, value); ByteBufferHelper.putDouble(bb, (index << 3) + offset, value, endian);
return this; return this;
} }
@ -95,48 +88,41 @@ class DoubleViewBufferImpl extends DoubleBuffer
{ {
if (position () > 0) if (position () > 0)
{ {
// Copy all data from position() to limit() to the beginning of the
// buffer, set position to end of data and limit to capacity
// XXX: This can surely be optimized, for direct and non-direct buffers
int count = limit () - position (); int count = limit () - position ();
bb.shiftDown(offset, offset + 8 * position(), 8 * count);
for (int i = 0; i < count; i++)
{
bb.putDouble ((i >> 3) + offset,
bb.getDouble (((i + position ()) >> 3) + offset));
}
position (count); position (count);
limit (capacity ()); limit (capacity ());
} }
return this; return this;
} }
public DoubleBuffer duplicate ()
{
// Create a copy of this object that shares its content
// FIXME: mark is not correct
return new DoubleViewBufferImpl (bb, offset, capacity (), limit (),
position (), -1, isReadOnly ());
}
public DoubleBuffer slice () public DoubleBuffer slice ()
{ {
// Create a sliced copy of this object that shares its content.
return new DoubleViewBufferImpl (bb, (position () >> 3) + offset, return new DoubleViewBufferImpl (bb, (position () >> 3) + offset,
remaining (), remaining (), 0, -1, remaining(), remaining(), 0, -1,
isReadOnly ()); readOnly, endian);
} }
DoubleBuffer duplicate (boolean readOnly)
{
int pos = position();
reset();
int mark = position();
position(pos);
return new DoubleViewBufferImpl (bb, offset, capacity(), limit(),
pos, mark, readOnly, endian);
}
public DoubleBuffer duplicate ()
{
return duplicate(readOnly);
}
public DoubleBuffer asReadOnlyBuffer () public DoubleBuffer asReadOnlyBuffer ()
{ {
// Create a copy of this object that shares its content and is read-only return duplicate(true);
return new DoubleViewBufferImpl (bb, (position () >> 3) + offset,
remaining (), remaining (), 0, -1, true);
} }
public boolean isReadOnly () public boolean isReadOnly ()
{ {
return readOnly; return readOnly;
@ -149,6 +135,6 @@ class DoubleViewBufferImpl extends DoubleBuffer
public ByteOrder order () public ByteOrder order ()
{ {
return ByteOrder.LITTLE_ENDIAN; return endian;
} }
} }

View file

@ -1,5 +1,5 @@
/* FloatViewBufferImpl.java -- /* FloatViewBufferImpl.java --
Copyright (C) 2003 Free Software Foundation, Inc. Copyright (C) 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
@ -40,54 +40,47 @@ package java.nio;
class FloatViewBufferImpl extends FloatBuffer class FloatViewBufferImpl extends FloatBuffer
{ {
private boolean readOnly; /** Position in bb (i.e. a byte offset) where this buffer starts. */
private int offset; private int offset;
private ByteBuffer bb; private ByteBuffer bb;
private boolean readOnly;
private ByteOrder endian; private ByteOrder endian;
public FloatViewBufferImpl (ByteBuffer bb, boolean readOnly)
{
super (bb.remaining () >> 2, bb.remaining () >> 2, bb.position (), 0);
this.bb = bb;
this.readOnly = readOnly;
// FIXME: What if this is called from FloatByteBufferImpl and ByteBuffer has changed its endianess ?
this.endian = bb.order ();
}
public FloatViewBufferImpl (ByteBuffer bb, int offset, int capacity, public FloatViewBufferImpl (ByteBuffer bb, int offset, int capacity,
int limit, int position, int mark, int limit, int position, int mark,
boolean readOnly) boolean readOnly, ByteOrder endian)
{ {
super (limit >> 2, limit >> 2, position >> 2, mark >> 2); super (limit >> 2, limit >> 2, position >> 2, mark >> 2);
this.bb = bb; this.bb = bb;
this.offset = offset; this.offset = offset;
this.readOnly = readOnly; this.readOnly = readOnly;
// FIXME: What if this is called from FloatViewBufferImpl and ByteBuffer has changed its endianess ? this.endian = endian;
this.endian = bb.order ();
} }
public float get () public float get ()
{ {
float result = bb.getFloat ((position () << 2) + offset); int p = position();
position (position () + 1); float result = ByteBufferHelper.getFloat(bb, (p << 2) + offset, endian);
position(p + 1);
return result; return result;
} }
public float get (int index) public float get (int index)
{ {
return bb.getFloat ((index << 2) + offset); return ByteBufferHelper.getFloat(bb, (index << 2) + offset, endian);
} }
public FloatBuffer put (float value) public FloatBuffer put (float value)
{ {
bb.putFloat ((position () << 2) + offset, value); int p = position();
position (position () + 1); ByteBufferHelper.putFloat(bb, (p << 2) + offset, value, endian);
position(p + 1);
return this; return this;
} }
public FloatBuffer put (int index, float value) public FloatBuffer put (int index, float value)
{ {
bb.putFloat ((index << 2) + offset, value); ByteBufferHelper.putFloat(bb, (index << 2) + offset, value, endian);
return this; return this;
} }
@ -95,48 +88,42 @@ class FloatViewBufferImpl extends FloatBuffer
{ {
if (position () > 0) if (position () > 0)
{ {
// Copy all data from position() to limit() to the beginning of the
// buffer, set position to end of data and limit to capacity
// XXX: This can surely be optimized, for direct and non-direct buffers
int count = limit () - position (); int count = limit () - position ();
bb.shiftDown(offset, offset + 4 * position(), 4 * count);
for (int i = 0; i < count; i++)
{
bb.putFloat ((i >> 2) + offset,
bb.getFloat (((i + position ()) >> 2) + offset));
}
position (count); position (count);
limit (capacity ()); limit (capacity ());
} }
return this; return this;
} }
public FloatBuffer duplicate ()
{
// Create a copy of this object that shares its content
// FIXME: mark is not correct
return new FloatViewBufferImpl (bb, offset, capacity (), limit (),
position (), -1, isReadOnly ());
}
public FloatBuffer slice () public FloatBuffer slice ()
{ {
// Create a sliced copy of this object that shares its content. // Create a sliced copy of this object that shares its content.
return new FloatViewBufferImpl (bb, (position () >> 2) + offset, return new FloatViewBufferImpl (bb, (position () >> 2) + offset,
remaining (), remaining (), 0, -1, remaining(), remaining(), 0, -1,
isReadOnly ()); readOnly, endian);
} }
FloatBuffer duplicate (boolean readOnly)
{
int pos = position();
reset();
int mark = position();
position(pos);
return new FloatViewBufferImpl (bb, offset, capacity(), limit(),
pos, mark, readOnly, endian);
}
public FloatBuffer duplicate ()
{
return duplicate(readOnly);
}
public FloatBuffer asReadOnlyBuffer () public FloatBuffer asReadOnlyBuffer ()
{ {
// Create a copy of this object that shares its content and is read-only return duplicate(true);
return new FloatViewBufferImpl (bb, (position () >> 2) + offset,
remaining (), remaining (), 0, -1, true);
} }
public boolean isReadOnly () public boolean isReadOnly ()
{ {
return readOnly; return readOnly;
@ -149,6 +136,6 @@ class FloatViewBufferImpl extends FloatBuffer
public ByteOrder order () public ByteOrder order ()
{ {
return ByteOrder.LITTLE_ENDIAN; return endian;
} }
} }

View file

@ -1,5 +1,5 @@
/* IntViewBufferImpl.java -- /* IntViewBufferImpl.java --
Copyright (C) 2003 Free Software Foundation, Inc. Copyright (C) 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
@ -40,54 +40,47 @@ package java.nio;
class IntViewBufferImpl extends IntBuffer class IntViewBufferImpl extends IntBuffer
{ {
private boolean readOnly; /** Position in bb (i.e. a byte offset) where this buffer starts. */
private int offset; private int offset;
private ByteBuffer bb; private ByteBuffer bb;
private boolean readOnly;
private ByteOrder endian; private ByteOrder endian;
public IntViewBufferImpl (ByteBuffer bb, boolean readOnly)
{
super (bb.remaining () >> 2, bb.remaining () >> 2, bb.position (), 0);
this.bb = bb;
this.readOnly = readOnly;
// FIXME: What if this is called from IntByteBufferImpl and ByteBuffer has changed its endianess ?
this.endian = bb.order ();
}
public IntViewBufferImpl (ByteBuffer bb, int offset, int capacity, public IntViewBufferImpl (ByteBuffer bb, int offset, int capacity,
int limit, int position, int mark, int limit, int position, int mark,
boolean readOnly) boolean readOnly, ByteOrder endian)
{ {
super (limit >> 2, limit >> 2, position >> 2, mark >> 2); super (limit >> 2, limit >> 2, position >> 2, mark >> 2);
this.bb = bb; this.bb = bb;
this.offset = offset; this.offset = offset;
this.readOnly = readOnly; this.readOnly = readOnly;
// FIXME: What if this is called from IntViewBufferImpl and ByteBuffer has changed its endianess ? this.endian = endian;
this.endian = bb.order ();
} }
public int get () public int get ()
{ {
int result = bb.getInt ((position () << 2) + offset); int p = position();
position (position () + 1); int result = ByteBufferHelper.getInt(bb, (p << 2) + offset, endian);
position(p + 1);
return result; return result;
} }
public int get (int index) public int get (int index)
{ {
return bb.getInt ((index << 2) + offset); return ByteBufferHelper.getInt(bb, (index << 2) + offset, endian);
} }
public IntBuffer put (int value) public IntBuffer put (int value)
{ {
bb.putInt ((position () << 2) + offset, value); int p = position();
position (position () + 1); ByteBufferHelper.putInt(bb, (p << 2) + offset, value, endian);
position(p + 1);
return this; return this;
} }
public IntBuffer put (int index, int value) public IntBuffer put (int index, int value)
{ {
bb.putInt ((index << 2) + offset, value); ByteBufferHelper.putInt(bb, (index << 2) + offset, value, endian);
return this; return this;
} }
@ -95,48 +88,42 @@ class IntViewBufferImpl extends IntBuffer
{ {
if (position () > 0) if (position () > 0)
{ {
// Copy all data from position() to limit() to the beginning of the
// buffer, set position to end of data and limit to capacity
// XXX: This can surely be optimized, for direct and non-direct buffers
int count = limit () - position (); int count = limit () - position ();
bb.shiftDown(offset, offset + 4 * position(), 4 * count);
for (int i = 0; i < count; i++)
{
bb.putInt ((i >> 2) + offset,
bb.getInt (((i + position ()) >> 2) + offset));
}
position (count); position (count);
limit (capacity ()); limit (capacity ());
} }
return this; return this;
} }
public IntBuffer duplicate ()
{
// Create a copy of this object that shares its content
// FIXME: mark is not correct
return new IntViewBufferImpl (bb, offset, capacity (), limit (),
position (), -1, isReadOnly ());
}
public IntBuffer slice () public IntBuffer slice ()
{ {
// Create a sliced copy of this object that shares its content. // Create a sliced copy of this object that shares its content.
return new IntViewBufferImpl (bb, (position () >> 2) + offset, return new IntViewBufferImpl (bb, (position () >> 2) + offset,
remaining (), remaining (), 0, -1, remaining(), remaining(), 0, -1,
isReadOnly ()); readOnly, endian);
} }
IntBuffer duplicate (boolean readOnly)
{
int pos = position();
reset();
int mark = position();
position(pos);
return new IntViewBufferImpl (bb, offset, capacity(), limit(),
pos, mark, readOnly, endian);
}
public IntBuffer duplicate ()
{
return duplicate(readOnly);
}
public IntBuffer asReadOnlyBuffer () public IntBuffer asReadOnlyBuffer ()
{ {
// Create a copy of this object that shares its content and is read-only return duplicate(true);
return new IntViewBufferImpl (bb, (position () >> 2) + offset,
remaining (), remaining (), 0, -1, true);
} }
public boolean isReadOnly () public boolean isReadOnly ()
{ {
return readOnly; return readOnly;
@ -149,6 +136,6 @@ class IntViewBufferImpl extends IntBuffer
public ByteOrder order () public ByteOrder order ()
{ {
return ByteOrder.LITTLE_ENDIAN; return endian;
} }
} }

View file

@ -1,5 +1,5 @@
/* LongViewBufferImpl.java -- /* LongViewBufferImpl.java --
Copyright (C) 2003 Free Software Foundation, Inc. Copyright (C) 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
@ -40,54 +40,47 @@ package java.nio;
class LongViewBufferImpl extends LongBuffer class LongViewBufferImpl extends LongBuffer
{ {
private boolean readOnly; /** Position in bb (i.e. a byte offset) where this buffer starts. */
private int offset; private int offset;
private ByteBuffer bb; private ByteBuffer bb;
private boolean readOnly;
private ByteOrder endian; private ByteOrder endian;
public LongViewBufferImpl (ByteBuffer bb, boolean readOnly)
{
super (bb.remaining () >> 3, bb.remaining () >> 3, bb.position (), 0);
this.bb = bb;
this.readOnly = readOnly;
// FIXME: What if this is called from LongByteBufferImpl and ByteBuffer has changed its endianess ?
this.endian = bb.order ();
}
public LongViewBufferImpl (ByteBuffer bb, int offset, int capacity, public LongViewBufferImpl (ByteBuffer bb, int offset, int capacity,
int limit, int position, int mark, int limit, int position, int mark,
boolean readOnly) boolean readOnly, ByteOrder endian)
{ {
super (limit >> 3, limit >> 3, position >> 3, mark >> 3); super (limit >> 3, limit >> 3, position >> 3, mark >> 3);
this.bb = bb; this.bb = bb;
this.offset = offset; this.offset = offset;
this.readOnly = readOnly; this.readOnly = readOnly;
// FIXME: What if this is called from LongViewBufferImpl and ByteBuffer has changed its endianess ? this.endian = endian;
this.endian = bb.order ();
} }
public long get () public long get ()
{ {
long result = bb.getLong ((position () << 3) + offset); int p = position();
position (position () + 1); long result = ByteBufferHelper.getLong(bb, (p << 3) + offset, endian);
position(p + 1);
return result; return result;
} }
public long get (int index) public long get (int index)
{ {
return bb.getLong ((index << 3) + offset); return ByteBufferHelper.getLong(bb, (index << 3) + offset, endian);
} }
public LongBuffer put (long value) public LongBuffer put (long value)
{ {
bb.putLong ((position () << 3) + offset, value); int p = position();
position (position () + 1); ByteBufferHelper.putLong(bb, (p << 3) + offset, value, endian);
position(p + 1);
return this; return this;
} }
public LongBuffer put (int index, long value) public LongBuffer put (int index, long value)
{ {
bb.putLong ((index << 3) + offset, value); ByteBufferHelper.putLong(bb, (index << 3) + offset, value, endian);
return this; return this;
} }
@ -95,48 +88,42 @@ class LongViewBufferImpl extends LongBuffer
{ {
if (position () > 0) if (position () > 0)
{ {
// Copy all data from position() to limit() to the beginning of the
// buffer, set position to end of data and limit to capacity
// XXX: This can surely be optimized, for direct and non-direct buffers
int count = limit () - position (); int count = limit () - position ();
bb.shiftDown(offset, offset + 8 * position(), 8 * count);
for (int i = 0; i < count; i++)
{
bb.putLong ((i >> 3) + offset,
bb.getLong (((i + position ()) >> 3) + offset));
}
position (count); position (count);
limit (capacity ()); limit (capacity ());
} }
return this; return this;
} }
public LongBuffer duplicate ()
{
// Create a copy of this object that shares its content
// FIXME: mark is not correct
return new LongViewBufferImpl (bb, offset, capacity (), limit (),
position (), -1, isReadOnly ());
}
public LongBuffer slice () public LongBuffer slice ()
{ {
// Create a sliced copy of this object that shares its content. // Create a sliced copy of this object that shares its content.
return new LongViewBufferImpl (bb, (position () >> 3) + offset, return new LongViewBufferImpl (bb, (position () >> 3) + offset,
remaining (), remaining (), 0, -1, remaining(), remaining(), 0, -1,
isReadOnly ()); readOnly, endian);
} }
LongBuffer duplicate (boolean readOnly)
{
int pos = position();
reset();
int mark = position();
position(pos);
return new LongViewBufferImpl (bb, offset, capacity(), limit(),
pos, mark, readOnly, endian);
}
public LongBuffer duplicate ()
{
return duplicate(readOnly);
}
public LongBuffer asReadOnlyBuffer () public LongBuffer asReadOnlyBuffer ()
{ {
// Create a copy of this object that shares its content and is read-only return duplicate(true);
return new LongViewBufferImpl (bb, (position () >> 3) + offset,
remaining (), remaining (), 0, -1, true);
} }
public boolean isReadOnly () public boolean isReadOnly ()
{ {
return readOnly; return readOnly;
@ -149,6 +136,6 @@ class LongViewBufferImpl extends LongBuffer
public ByteOrder order () public ByteOrder order ()
{ {
return ByteOrder.LITTLE_ENDIAN; return endian;
} }
} }

View file

@ -1,5 +1,5 @@
/* MappedByteBufferImpl.java -- /* MappedByteBufferImpl.java --
Copyright (C) 2002, 2003 Free Software Foundation, Inc. Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
@ -111,15 +111,14 @@ public class MappedByteBufferImpl extends MappedByteBuffer
public ByteBuffer compact () public ByteBuffer compact ()
{ {
int copied = 0; int pos = position();
if (pos > 0)
while (remaining () > 0)
{ {
put (copied, get ()); int count = remaining();
copied++; shiftDown(0, pos, count);
position(count);
limit(capacity());
} }
position (copied);
return this; return this;
} }
@ -145,151 +144,163 @@ public class MappedByteBufferImpl extends MappedByteBuffer
public CharBuffer asCharBuffer () public CharBuffer asCharBuffer ()
{ {
return new CharViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ()); return new CharViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
} }
public ShortBuffer asShortBuffer () public ShortBuffer asShortBuffer ()
{ {
return new ShortViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ()); return new ShortViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
} }
public IntBuffer asIntBuffer () public IntBuffer asIntBuffer ()
{ {
return new IntViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ()); return new IntViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
} }
public LongBuffer asLongBuffer () public LongBuffer asLongBuffer ()
{ {
return new LongViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ()); return new LongViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
} }
public FloatBuffer asFloatBuffer () public FloatBuffer asFloatBuffer ()
{ {
return new FloatViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ()); return new FloatViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
} }
public DoubleBuffer asDoubleBuffer () public DoubleBuffer asDoubleBuffer ()
{ {
return new DoubleViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ()); return new DoubleViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
} }
public final char getChar() final public char getChar ()
{ {
return ByteBufferHelper.getChar (this); return ByteBufferHelper.getChar(this, order());
} }
public final ByteBuffer putChar (char value) final public ByteBuffer putChar (char value)
{ {
return ByteBufferHelper.putChar (this, value); ByteBufferHelper.putChar(this, value, order());
return this;
} }
public final char getChar (int index) final public char getChar (int index)
{ {
return ByteBufferHelper.getChar (this, index); return ByteBufferHelper.getChar(this, index, order());
} }
public final ByteBuffer putChar (int index, char value) final public ByteBuffer putChar (int index, char value)
{ {
return ByteBufferHelper.putChar (this, index, value); ByteBufferHelper.putChar(this, index, value, order());
return this;
} }
public final short getShort() final public short getShort ()
{ {
return ByteBufferHelper.getShort (this); return ByteBufferHelper.getShort(this, order());
} }
public final ByteBuffer putShort (short value) final public ByteBuffer putShort (short value)
{ {
return ByteBufferHelper.putShort (this, value); ByteBufferHelper.putShort(this, value, order());
return this;
} }
public final short getShort (int index) final public short getShort (int index)
{ {
return ByteBufferHelper.getShort (this, index); return ByteBufferHelper.getShort(this, index, order());
} }
public final ByteBuffer putShort (int index, short value) final public ByteBuffer putShort (int index, short value)
{ {
return ByteBufferHelper.putShort (this, index, value); ByteBufferHelper.putShort(this, index, value, order());
return this;
} }
public final int getInt() final public int getInt ()
{ {
return ByteBufferHelper.getInt (this); return ByteBufferHelper.getInt(this, order());
} }
public final ByteBuffer putInt (int value) final public ByteBuffer putInt (int value)
{ {
return ByteBufferHelper.putInt (this, value); ByteBufferHelper.putInt(this, value, order());
return this;
} }
public final int getInt (int index) final public int getInt (int index)
{ {
return ByteBufferHelper.getInt (this, index); return ByteBufferHelper.getInt(this, index, order());
} }
public final ByteBuffer putInt (int index, int value) final public ByteBuffer putInt (int index, int value)
{ {
return ByteBufferHelper.putInt (this, index, value); ByteBufferHelper.putInt(this, index, value, order());
return this;
} }
public final long getLong() final public long getLong ()
{ {
return ByteBufferHelper.getLong (this); return ByteBufferHelper.getLong(this, order());
} }
public final ByteBuffer putLong (long value) final public ByteBuffer putLong (long value)
{ {
return ByteBufferHelper.putLong (this, value); ByteBufferHelper.putLong (this, value, order());
return this;
} }
public final long getLong (int index) final public long getLong (int index)
{ {
return ByteBufferHelper.getLong (this, index); return ByteBufferHelper.getLong (this, index, order());
} }
public final ByteBuffer putLong (int index, long value) final public ByteBuffer putLong (int index, long value)
{ {
return ByteBufferHelper.putLong (this, index, value); ByteBufferHelper.putLong (this, index, value, order());
return this;
} }
public final float getFloat() final public float getFloat ()
{ {
return ByteBufferHelper.getFloat (this); return ByteBufferHelper.getFloat (this, order());
} }
public final ByteBuffer putFloat (float value) final public ByteBuffer putFloat (float value)
{ {
return ByteBufferHelper.putFloat (this, value); ByteBufferHelper.putFloat (this, value, order());
return this;
} }
public final float getFloat (int index) public final float getFloat (int index)
{ {
return ByteBufferHelper.getFloat (this, index); return ByteBufferHelper.getFloat (this, index, order());
} }
public final ByteBuffer putFloat (int index, float value) final public ByteBuffer putFloat (int index, float value)
{ {
return ByteBufferHelper.putFloat (this, index, value); ByteBufferHelper.putFloat (this, index, value, order());
return this;
} }
public final double getDouble() final public double getDouble ()
{ {
return ByteBufferHelper.getDouble (this); return ByteBufferHelper.getDouble (this, order());
} }
public final ByteBuffer putDouble (double value) final public ByteBuffer putDouble (double value)
{ {
return ByteBufferHelper.putDouble (this, value); ByteBufferHelper.putDouble (this, value, order());
return this;
} }
public final double getDouble (int index) final public double getDouble (int index)
{ {
return ByteBufferHelper.getDouble (this, index); return ByteBufferHelper.getDouble (this, index, order());
} }
public final ByteBuffer putDouble (int index, double value) final public ByteBuffer putDouble (int index, double value)
{ {
return ByteBufferHelper.putDouble (this, index, value); ByteBufferHelper.putDouble (this, index, value, order());
return this;
} }
} }

View file

@ -1,5 +1,5 @@
/* ShortViewBufferImpl.java -- /* ShortViewBufferImpl.java --
Copyright (C) 2003 Free Software Foundation, Inc. Copyright (C) 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
@ -40,54 +40,47 @@ package java.nio;
class ShortViewBufferImpl extends ShortBuffer class ShortViewBufferImpl extends ShortBuffer
{ {
private boolean readOnly; /** Position in bb (i.e. a byte offset) where this buffer starts. */
private int offset; private int offset;
private ByteBuffer bb; private ByteBuffer bb;
private boolean readOnly;
private ByteOrder endian; private ByteOrder endian;
public ShortViewBufferImpl (ByteBuffer bb, boolean readOnly)
{
super (bb.remaining () >> 1, bb.remaining () >> 1, bb.position (), 0);
this.bb = bb;
this.readOnly = readOnly;
// FIXME: What if this is called from ShortByteBufferImpl and ByteBuffer has changed its endianess ?
this.endian = bb.order ();
}
public ShortViewBufferImpl (ByteBuffer bb, int offset, int capacity, public ShortViewBufferImpl (ByteBuffer bb, int offset, int capacity,
int limit, int position, int mark, int limit, int position, int mark,
boolean readOnly) boolean readOnly, ByteOrder endian)
{ {
super (limit >> 1, limit >> 1, position >> 1, mark >> 1); super (limit >> 1, limit >> 1, position >> 1, mark >> 1);
this.bb = bb; this.bb = bb;
this.offset = offset; this.offset = offset;
this.readOnly = readOnly; this.readOnly = readOnly;
// FIXME: What if this is called from ShortViewBufferImpl and ByteBuffer has changed its endianess ? this.endian = endian;
this.endian = bb.order ();
} }
public short get () public short get ()
{ {
short result = bb.getShort ((position () << 1) + offset); int p = position();
position (position () + 1); short result = ByteBufferHelper.getShort(bb, (p << 1) + offset, endian);
position(p + 1);
return result; return result;
} }
public short get (int index) public short get (int index)
{ {
return bb.getShort ((index << 1) + offset); return ByteBufferHelper.getShort(bb, (index << 1) + offset, endian);
} }
public ShortBuffer put (short value) public ShortBuffer put (short value)
{ {
bb.putShort ((position () << 1) + offset, value); int p = position();
position (position () + 1); ByteBufferHelper.putShort(bb, (p << 1) + offset, value, endian);
position(p + 1);
return this; return this;
} }
public ShortBuffer put (int index, short value) public ShortBuffer put (int index, short value)
{ {
bb.putShort ((index << 1) + offset, value); ByteBufferHelper.putShort(bb, (index << 1) + offset, value, endian);
return this; return this;
} }
@ -95,48 +88,42 @@ class ShortViewBufferImpl extends ShortBuffer
{ {
if (position () > 0) if (position () > 0)
{ {
// Copy all data from position() to limit() to the beginning of the
// buffer, set position to end of data and limit to capacity
// XXX: This can surely be optimized, for direct and non-direct buffers
int count = limit () - position (); int count = limit () - position ();
bb.shiftDown(offset, offset + 2 * position(), 2 * count);
for (int i = 0; i < count; i++)
{
bb.putShort ((i >> 1) + offset,
bb.getShort (((i + position ()) >> 1) + offset));
}
position (count); position (count);
limit (capacity ()); limit (capacity ());
} }
return this; return this;
} }
public ShortBuffer duplicate ()
{
// Create a copy of this object that shares its content
// FIXME: mark is not correct
return new ShortViewBufferImpl (bb, offset, capacity (), limit (),
position (), -1, isReadOnly ());
}
public ShortBuffer slice () public ShortBuffer slice ()
{ {
// Create a sliced copy of this object that shares its content. // Create a sliced copy of this object that shares its content.
return new ShortViewBufferImpl (bb, (position () >> 1) + offset, return new ShortViewBufferImpl (bb, (position () >> 1) + offset,
remaining (), remaining (), 0, -1, remaining(), remaining(), 0, -1,
isReadOnly ()); readOnly, endian);
} }
ShortBuffer duplicate (boolean readOnly)
{
int pos = position();
reset();
int mark = position();
position(pos);
return new ShortViewBufferImpl (bb, offset, capacity(), limit(),
pos, mark, readOnly, endian);
}
public ShortBuffer duplicate ()
{
return duplicate(readOnly);
}
public ShortBuffer asReadOnlyBuffer () public ShortBuffer asReadOnlyBuffer ()
{ {
// Create a copy of this object that shares its content and is read-only return duplicate(true);
return new ShortViewBufferImpl (bb, (position () >> 1) + offset,
remaining (), remaining (), 0, -1, true);
} }
public boolean isReadOnly () public boolean isReadOnly ()
{ {
return readOnly; return readOnly;
@ -149,6 +136,6 @@ class ShortViewBufferImpl extends ShortBuffer
public ByteOrder order () public ByteOrder order ()
{ {
return ByteOrder.LITTLE_ENDIAN; return endian;
} }
} }

View file

@ -43,3 +43,12 @@ java::nio::DirectByteBufferImpl::putImpl (jint index, jbyte value)
jbyte* pointer = reinterpret_cast<jbyte*> (address) + offset + index; jbyte* pointer = reinterpret_cast<jbyte*> (address) + offset + index;
*pointer = value; *pointer = value;
} }
void
java::nio::DirectByteBufferImpl::shiftDown
(jint dst_offset, jint src_offset, jint count)
{
jbyte* dst = reinterpret_cast<jbyte*> (address) + offset + dst_offset;
jbyte* src = reinterpret_cast<jbyte*> (address) + offset + src_offset;
::memmove(dst, src, count);
}