Index: java/nio/ByteBuffer.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/nio/ByteBuffer.java,v retrieving revision 1.17 diff -u -b -B -r1.17 ByteBuffer.java --- java/nio/ByteBuffer.java 4 Nov 2003 10:00:28 -0000 1.17 +++ java/nio/ByteBuffer.java 11 Feb 2004 19:04:06 -0000 @@ -1,5 +1,5 @@ /* 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. @@ -382,8 +382,14 @@ */ 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 (); Index: java/nio/ByteBufferHelper.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/nio/ByteBufferHelper.java,v retrieving revision 1.3 diff -u -b -B -r1.3 ByteBufferHelper.java --- java/nio/ByteBufferHelper.java 6 Oct 2003 13:54:30 -0000 1.3 +++ java/nio/ByteBufferHelper.java 11 Feb 2004 19:04:06 -0000 @@ -1,5 +1,5 @@ /* ByteBufferImpl.java -- - Copyright (C) 2003 Free Software Foundation, Inc. + Copyright (C) 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -42,58 +42,58 @@ */ final class ByteBufferHelper { - private static final void checkRemainingForRead (ByteBuffer buffer, int bytes) + private static void checkRemainingForRead (ByteBuffer buffer, int bytes) { if (buffer.remaining() < bytes) throw new BufferUnderflowException(); } - private static final void checkRemainingForWrite (ByteBuffer buffer, int bytes) + private static void checkRemainingForWrite (ByteBuffer buffer, int bytes) { if (buffer.remaining() < bytes) throw new BufferOverflowException(); } - private static final void checkAvailableForRead (ByteBuffer buffer, + private static void checkAvailableForRead (ByteBuffer buffer, int index, int bytes) { if (buffer.limit() < (index + bytes)) throw new BufferUnderflowException(); } - private static final void checkAvailableForWrite (ByteBuffer buffer, + private static void checkAvailableForWrite (ByteBuffer buffer, int index, int bytes) { if (buffer.limit() < (index + bytes)) 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, - char value) + public static void putChar (ByteBuffer buffer, int index, + 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); - if (buffer.order() == ByteOrder.LITTLE_ENDIAN) + if (order == ByteOrder.LITTLE_ENDIAN) { return (short) ((buffer.get() & 0xff) + (buffer.get() << 8)); @@ -103,11 +103,11 @@ + (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); - if (buffer.order() == ByteOrder.LITTLE_ENDIAN) + if (order == ByteOrder.LITTLE_ENDIAN) { buffer.put ((byte) value); buffer.put ((byte) (value >> 8)); @@ -117,15 +117,14 @@ buffer.put ((byte) (value >> 8)); 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); - if (buffer.order() == ByteOrder.LITTLE_ENDIAN) + if (order == ByteOrder.LITTLE_ENDIAN) { return (short) ((buffer.get (index) & 0xff) + (buffer.get (++index) << 8)); @@ -135,12 +134,12 @@ + (buffer.get (++index) & 0xff)); } - public static final ByteBuffer putShort (ByteBuffer buffer, int index, - short value) + public static void putShort (ByteBuffer buffer, int index, + short value, ByteOrder order) { 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 >> 8)); @@ -150,15 +149,13 @@ buffer.put (index, (byte) (value >> 8)); 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); - if (buffer.order() == ByteOrder.LITTLE_ENDIAN) + if (order == ByteOrder.LITTLE_ENDIAN) { return ((buffer.get() & 0xff) + ((buffer.get() & 0xff) << 8) @@ -172,11 +169,11 @@ + (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); - if (buffer.order() == ByteOrder.LITTLE_ENDIAN) + if (order == ByteOrder.LITTLE_ENDIAN) { buffer.put ((byte) value); buffer.put ((byte) (value >> 8)); @@ -190,15 +187,13 @@ buffer.put ((byte) (value >> 8)); 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); - if (buffer.order() == ByteOrder.LITTLE_ENDIAN) + if (order == ByteOrder.LITTLE_ENDIAN) { return ((buffer.get (index) & 0xff) + ((buffer.get (++index) & 0xff) << 8) @@ -212,12 +207,12 @@ + (buffer.get (++index) & 0xff)); } - public static final ByteBuffer putInt (ByteBuffer buffer, int index, - int value) + public static void putInt (ByteBuffer buffer, int index, + int value, ByteOrder order) { 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 >> 8)); @@ -231,15 +226,13 @@ buffer.put (++index, (byte) (value >> 8)); 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); - if (buffer.order() == ByteOrder.LITTLE_ENDIAN) + if (order == ByteOrder.LITTLE_ENDIAN) { return ((buffer.get() & 0xff) + (((buffer.get() & 0xff)) << 8) @@ -261,11 +254,11 @@ + (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); - if (buffer.order() == ByteOrder.LITTLE_ENDIAN) + if (order == ByteOrder.LITTLE_ENDIAN) { buffer.put ((byte) value); buffer.put ((byte) (value >> 8)); @@ -287,15 +280,13 @@ buffer.put ((byte) (value >> 8)); 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); - if (buffer.order() == ByteOrder.LITTLE_ENDIAN) + if (order == ByteOrder.LITTLE_ENDIAN) { return ((buffer.get (index) & 0xff) + ((buffer.get (++index) & 0xff) << 8) @@ -317,12 +308,12 @@ + (buffer.get (++index) & 0xff)); } - public static final ByteBuffer putLong (ByteBuffer buffer, int index, - long value) + public static void putLong (ByteBuffer buffer, int index, + long value, ByteOrder order) { 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 >> 8)); @@ -344,50 +335,47 @@ buffer.put (++index, (byte) (value >> 8)); 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, - float value) + public static void putFloat (ByteBuffer buffer, int index, + 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, - double value) + public static void putDouble (ByteBuffer buffer, int index, + double value, ByteOrder order) { - return putLong (buffer, index, Double.doubleToLongBits (value)); + putLong (buffer, index, Double.doubleToLongBits (value), order); } - } // ByteBufferHelper Index: java/nio/ByteBufferImpl.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/nio/ByteBufferImpl.java,v retrieving revision 1.3 diff -u -b -B -r1.3 ByteBufferImpl.java --- java/nio/ByteBufferImpl.java 25 Sep 2003 14:01:18 -0000 1.3 +++ java/nio/ByteBufferImpl.java 11 Feb 2004 19:04:06 -0000 @@ -1,5 +1,5 @@ /* 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. @@ -58,32 +58,32 @@ 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 () { - return new ShortViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ()); + return new ShortViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order()); } 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 () { - return new LongViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ()); + return new LongViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order()); } 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 () { - return new DoubleViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ()); + return new DoubleViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order()); } public boolean isReadOnly () @@ -108,15 +108,14 @@ public ByteBuffer compact () { - int copied = 0; - - while (remaining () > 0) + int pos = position(); + if (pos > 0) { - put (copied, get ()); - copied++; + int count = remaining(); + shiftDown(0, pos, count); + position(count); + limit(capacity()); } - - position (copied); return this; } @@ -182,121 +181,133 @@ final public char getChar () { - return ByteBufferHelper.getChar (this); + return ByteBufferHelper.getChar(this, order()); } final public ByteBuffer putChar (char value) { - return ByteBufferHelper.putChar (this, value); + ByteBufferHelper.putChar(this, value, order()); + return this; } 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) { - return ByteBufferHelper.putChar (this, index, value); + ByteBufferHelper.putChar(this, index, value, order()); + return this; } final public short getShort () { - return ByteBufferHelper.getShort (this); + return ByteBufferHelper.getShort(this, order()); } final public ByteBuffer putShort (short value) { - return ByteBufferHelper.putShort (this, value); + ByteBufferHelper.putShort(this, value, order()); + return this; } 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) { - return ByteBufferHelper.putShort (this, index, value); + ByteBufferHelper.putShort(this, index, value, order()); + return this; } final public int getInt () { - return ByteBufferHelper.getInt (this); + return ByteBufferHelper.getInt(this, order()); } final public ByteBuffer putInt (int value) { - return ByteBufferHelper.putInt (this, value); + ByteBufferHelper.putInt(this, value, order()); + return this; } 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) { - return ByteBufferHelper.putInt (this, index, value); + ByteBufferHelper.putInt(this, index, value, order()); + return this; } final public long getLong () { - return ByteBufferHelper.getLong (this); + return ByteBufferHelper.getLong(this, order()); } final public ByteBuffer putLong (long value) { - return ByteBufferHelper.putLong (this, value); + ByteBufferHelper.putLong (this, value, order()); + return this; } 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) { - return ByteBufferHelper.putLong (this, index, value); + ByteBufferHelper.putLong (this, index, value, order()); + return this; } final public float getFloat () { - return ByteBufferHelper.getFloat (this); + return ByteBufferHelper.getFloat (this, order()); } 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 () { - return ByteBufferHelper.getDouble (this); + return ByteBufferHelper.getDouble (this, order()); } final public ByteBuffer putDouble (double value) { - return ByteBufferHelper.putDouble (this, value); + ByteBufferHelper.putDouble (this, value, order()); + return this; } 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) { - return ByteBufferHelper.putDouble (this, index, value); + ByteBufferHelper.putDouble (this, index, value, order()); + return this; } } Index: java/nio/ByteOrder.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/nio/ByteOrder.java,v retrieving revision 1.6 diff -u -b -B -r1.6 ByteOrder.java --- java/nio/ByteOrder.java 20 Oct 2003 15:32:56 -0000 1.6 +++ java/nio/ByteOrder.java 11 Feb 2004 19:04:06 -0000 @@ -63,7 +63,7 @@ */ public static ByteOrder nativeOrder () { - return (System.getProperty ("gnu.cpu.endian") == "big" + return (System.getProperty ("gnu.cpu.endian").equals("big") ? BIG_ENDIAN : LITTLE_ENDIAN); } Index: java/nio/CharViewBufferImpl.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/nio/CharViewBufferImpl.java,v retrieving revision 1.1 diff -u -b -B -r1.1 CharViewBufferImpl.java --- java/nio/CharViewBufferImpl.java 19 May 2003 09:05:13 -0000 1.1 +++ java/nio/CharViewBufferImpl.java 11 Feb 2004 19:04:06 -0000 @@ -1,5 +1,5 @@ /* CharViewBufferImpl.java -- - Copyright (C) 2003 Free Software Foundation, Inc. + Copyright (C) 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -40,54 +40,47 @@ class CharViewBufferImpl extends CharBuffer { - private boolean readOnly; + /** Position in bb (i.e. a byte offset) where this buffer starts. */ private int offset; private ByteBuffer bb; + private boolean readOnly; 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, int limit, int position, int mark, - boolean readOnly) + boolean readOnly, ByteOrder endian) { super (limit >> 1, limit >> 1, position >> 1, mark >> 1); this.bb = bb; this.offset = offset; this.readOnly = readOnly; - // FIXME: What if this is called from CharViewBufferImpl and ByteBuffer has changed its endianess ? - this.endian = bb.order (); + this.endian = endian; } public char get () { - char result = bb.getChar ((position () << 1) + offset); - position (position () + 1); + int p = position(); + char result = ByteBufferHelper.getChar(bb, (p << 1) + offset, endian); + position(p + 1); return result; } public char get (int index) { - return bb.getChar ((index << 1) + offset); + return ByteBufferHelper.getChar(bb, (index << 1) + offset, endian); } public CharBuffer put (char value) { - bb.putChar ((position () << 1) + offset, value); - position (position () + 1); + int p = position(); + ByteBufferHelper.putChar(bb, (p << 1) + offset, value, endian); + position(p + 1); return this; } public CharBuffer put (int index, char value) { - bb.putChar ((index << 1) + offset, value); + ByteBufferHelper.putChar(bb, (index << 1) + offset, value, endian); return this; } @@ -95,18 +88,8 @@ { 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 (); - - for (int i = 0; i < count; i++) - { - bb.putChar ((i >> 1) + offset, - bb.getChar (((i + position ()) >> 1) + offset)); - } - + bb.shiftDown(offset, offset + 2 * position(), 2 * count); position (count); limit (capacity ()); } @@ -114,38 +96,44 @@ 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 () { // Create a sliced copy of this object that shares its content. return new CharViewBufferImpl (bb, (position () >> 1) + offset, remaining (), remaining (), 0, -1, - isReadOnly ()); + isReadOnly (), endian); + } + + CharBuffer duplicate (boolean readOnly) + { + int pos = position(); + reset(); + int mark = position(); + position(pos); + return new CharViewBufferImpl (bb, offset, capacity(), limit(), + pos, mark, readOnly, endian); + } + + public CharBuffer duplicate () + { + return duplicate(readOnly); + } + + public CharBuffer asReadOnlyBuffer () + { + return duplicate(true); } public CharSequence subSequence (int start, int end) { if (start < 0 - || start > length () || end < start || end > length ()) throw new IndexOutOfBoundsException (); - return new CharViewBufferImpl (bb, array_offset, capacity (), position () + end, position () + start, -1, isReadOnly ()); - } - - public CharBuffer asReadOnlyBuffer () - { - // Create a copy of this object that shares its content and is read-only - return new CharViewBufferImpl (bb, (position () >> 1) + offset, - remaining (), remaining (), 0, -1, true); + return new CharViewBufferImpl (bb, array_offset, capacity (), + position () + end, position () + start, + -1, isReadOnly (), endian); } public boolean isReadOnly () @@ -160,6 +148,6 @@ public ByteOrder order () { - return ByteOrder.LITTLE_ENDIAN; + return endian; } } Index: java/nio/DirectByteBufferImpl.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/nio/DirectByteBufferImpl.java,v retrieving revision 1.6 diff -u -b -B -r1.6 DirectByteBufferImpl.java --- java/nio/DirectByteBufferImpl.java 20 Oct 2003 15:32:56 -0000 1.6 +++ java/nio/DirectByteBufferImpl.java 11 Feb 2004 19:04:06 -0000 @@ -1,5 +1,5 @@ /* DirectByteBufferImpl.java -- - Copyright (C) 2003 Free Software Foundation, Inc. + Copyright (C) 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -117,18 +117,18 @@ return this; } + native void shiftDown (int dst_offset, int src_offset, int count); + public ByteBuffer compact () { - // FIXME this can sure be optimized using memcpy() - int copied = 0; - - while (remaining () > 0) + int pos = position(); + if (pos > 0) { - put (copied, get ()); - copied++; + int count = remaining(); + shiftDown(0, pos, count); + position(count); + limit(capacity()); } - - position (copied); return this; } @@ -161,197 +161,163 @@ 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 () { - 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 () { - 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 () { - 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 () { - 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 () { - 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 () { - // FIXME: this handles little endian only - return (char) (((get () & 0xff) << 8) - + (get () & 0xff)); + return ByteBufferHelper.getChar(this, order()); } final public ByteBuffer putChar (char value) { - // FIXME: this handles little endian only - put ((byte) ((((int) value) & 0xff00) >> 8)); - put ((byte) (((int) value) & 0x00ff)); + ByteBufferHelper.putChar(this, value, order()); return this; } final public char getChar (int index) { - // FIXME: this handles little endian only - return (char) (((get (index) & 0xff) << 8) - + (get (index + 1) & 0xff)); + return ByteBufferHelper.getChar(this, index, order()); } final public ByteBuffer putChar (int index, char value) { - // FIXME: this handles little endian only - put (index, (byte) ((((int) value) & 0xff00) >> 8)); - put (index + 1, (byte) (((int) value) & 0x00ff)); + ByteBufferHelper.putChar(this, index, value, order()); return this; } final public short getShort () { - // FIXME: this handles little endian only - return (short) (((get () & 0xff) << 8) - + (get () & 0xff)); + return ByteBufferHelper.getShort(this, order()); } final public ByteBuffer putShort (short value) { - // FIXME: this handles little endian only - put ((byte) ((((int) value) & 0xff00) >> 8)); - put ((byte) (((int) value) & 0x00ff)); + ByteBufferHelper.putShort(this, value, order()); return this; } final public short getShort (int index) { - // FIXME: this handles little endian only - return (short) (((get (index) & 0xff) << 8) - + (get (index + 1) & 0xff)); + return ByteBufferHelper.getShort(this, index, order()); } final public ByteBuffer putShort (int index, short value) { - // FIXME: this handles little endian only - put (index, (byte) ((((int) value) & 0xff00) >> 8)); - put (index + 1, (byte) (((int) value) & 0x00ff)); + ByteBufferHelper.putShort(this, index, value, order()); return this; } final public int getInt () { - // FIXME: this handles little endian only - return (int) (((get () & 0xff) << 24) - + ((get () & 0xff) << 16) - + ((get () & 0xff) << 8) - + (get () & 0xff)); + return ByteBufferHelper.getInt(this, order()); } final public ByteBuffer putInt (int value) { - // FIXME: this handles little endian only - put ((byte) ((((int) value) & 0xff000000) >> 24)); - put ((byte) ((((int) value) & 0x00ff0000) >> 16)); - put ((byte) ((((int) value) & 0x0000ff00) >> 8)); - put ((byte) (((int) value) & 0x000000ff)); + ByteBufferHelper.putInt(this, value, order()); return this; } final public int getInt (int index) { - // FIXME: this handles little endian only - return (int) (((get (index) & 0xff) << 24) - + ((get (index + 1) & 0xff) << 16) - + ((get (index + 2) & 0xff) << 8) - + (get (index + 3) & 0xff)); + return ByteBufferHelper.getInt(this, index, order()); } final public ByteBuffer putInt (int index, int value) { - // FIXME: this handles little endian only - 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)); + ByteBufferHelper.putInt(this, index, value, order()); return this; } final public long getLong () { - // FIXME: this handles little endian only - 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)); + return ByteBufferHelper.getLong(this, order()); } final public ByteBuffer putLong (long value) { - return ByteBufferHelper.putLong (this, value); + ByteBufferHelper.putLong (this, value, order()); + return this; } 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) { - return ByteBufferHelper.putLong (this, index, value); + ByteBufferHelper.putLong (this, index, value, order()); + return this; } final public float getFloat () { - return ByteBufferHelper.getFloat (this); + return ByteBufferHelper.getFloat (this, order()); } final public ByteBuffer putFloat (float value) { - return ByteBufferHelper.putFloat (this, value); + ByteBufferHelper.putFloat (this, value, order()); + return this; } 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) { - return ByteBufferHelper.putFloat (this, index, value); + ByteBufferHelper.putFloat (this, index, value, order()); + return this; } final public double getDouble () { - return ByteBufferHelper.getDouble (this); + return ByteBufferHelper.getDouble (this, order()); } final public ByteBuffer putDouble (double value) { - return ByteBufferHelper.putDouble (this, value); + ByteBufferHelper.putDouble (this, value, order()); + return this; } 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) { - return ByteBufferHelper.putDouble (this, index, value); + ByteBufferHelper.putDouble (this, index, value, order()); + return this; } } Index: java/nio/DoubleViewBufferImpl.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/nio/DoubleViewBufferImpl.java,v retrieving revision 1.1 diff -u -b -B -r1.1 DoubleViewBufferImpl.java --- java/nio/DoubleViewBufferImpl.java 19 May 2003 09:05:13 -0000 1.1 +++ java/nio/DoubleViewBufferImpl.java 11 Feb 2004 19:04:06 -0000 @@ -1,5 +1,5 @@ /* DoubleViewBufferImpl.java -- - Copyright (C) 2003 Free Software Foundation, Inc. + Copyright (C) 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -40,54 +40,47 @@ class DoubleViewBufferImpl extends DoubleBuffer { - private boolean readOnly; + /** Position in bb (i.e. a byte offset) where this buffer starts. */ private int offset; private ByteBuffer bb; + private boolean readOnly; 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, int limit, int position, int mark, - boolean readOnly) + boolean readOnly, ByteOrder endian) { super (limit >> 3, limit >> 3, position >> 3, mark >> 3); this.bb = bb; this.offset = offset; this.readOnly = readOnly; - // FIXME: What if this is called from DoubleViewBufferImpl and ByteBuffer has changed its endianess ? - this.endian = bb.order (); + this.endian = endian; } public double get () { - double result = bb.getDouble ((position () << 3) + offset); - position (position () + 1); + int p = position(); + double result = ByteBufferHelper.getDouble(bb, (p << 3) + offset, endian); + position(p + 1); return result; } public double get (int index) { - return bb.getDouble ((index << 3) + offset); + return ByteBufferHelper.getDouble(bb, (index << 3) + offset, endian); } public DoubleBuffer put (double value) { - bb.putDouble ((position () << 3) + offset, value); - position (position () + 1); + int p = position(); + ByteBufferHelper.putDouble(bb, (p << 3) + offset, value, endian); + position(p + 1); return this; } public DoubleBuffer put (int index, double value) { - bb.putDouble ((index << 3) + offset, value); + ByteBufferHelper.putDouble(bb, (index << 3) + offset, value, endian); return this; } @@ -95,18 +88,8 @@ { 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 (); - - for (int i = 0; i < count; i++) - { - bb.putDouble ((i >> 3) + offset, - bb.getDouble (((i + position ()) >> 3) + offset)); - } - + bb.shiftDown(offset, offset + 8 * position(), 8 * count); position (count); limit (capacity ()); } @@ -114,27 +96,31 @@ return this; } - public DoubleBuffer duplicate () + public DoubleBuffer slice () { - // 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 ()); + return new DoubleViewBufferImpl (bb, (position () >> 3) + offset, + remaining(), remaining(), 0, -1, + readOnly, endian); } - public DoubleBuffer slice () + DoubleBuffer duplicate (boolean readOnly) { - // Create a sliced copy of this object that shares its content. - return new DoubleViewBufferImpl (bb, (position () >> 3) + offset, - remaining (), remaining (), 0, -1, - isReadOnly ()); + 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 () { - // Create a copy of this object that shares its content and is read-only - return new DoubleViewBufferImpl (bb, (position () >> 3) + offset, - remaining (), remaining (), 0, -1, true); + return duplicate(true); } public boolean isReadOnly () @@ -149,6 +135,6 @@ public ByteOrder order () { - return ByteOrder.LITTLE_ENDIAN; + return endian; } } Index: java/nio/FloatViewBufferImpl.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/nio/FloatViewBufferImpl.java,v retrieving revision 1.1 diff -u -b -B -r1.1 FloatViewBufferImpl.java --- java/nio/FloatViewBufferImpl.java 19 May 2003 09:05:13 -0000 1.1 +++ java/nio/FloatViewBufferImpl.java 11 Feb 2004 19:04:06 -0000 @@ -1,5 +1,5 @@ /* FloatViewBufferImpl.java -- - Copyright (C) 2003 Free Software Foundation, Inc. + Copyright (C) 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -40,54 +40,47 @@ class FloatViewBufferImpl extends FloatBuffer { - private boolean readOnly; + /** Position in bb (i.e. a byte offset) where this buffer starts. */ private int offset; private ByteBuffer bb; + private boolean readOnly; 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, int limit, int position, int mark, - boolean readOnly) + boolean readOnly, ByteOrder endian) { super (limit >> 2, limit >> 2, position >> 2, mark >> 2); this.bb = bb; this.offset = offset; this.readOnly = readOnly; - // FIXME: What if this is called from FloatViewBufferImpl and ByteBuffer has changed its endianess ? - this.endian = bb.order (); + this.endian = endian; } public float get () { - float result = bb.getFloat ((position () << 2) + offset); - position (position () + 1); + int p = position(); + float result = ByteBufferHelper.getFloat(bb, (p << 2) + offset, endian); + position(p + 1); return result; } public float get (int index) { - return bb.getFloat ((index << 2) + offset); + return ByteBufferHelper.getFloat(bb, (index << 2) + offset, endian); } public FloatBuffer put (float value) { - bb.putFloat ((position () << 2) + offset, value); - position (position () + 1); + int p = position(); + ByteBufferHelper.putFloat(bb, (p << 2) + offset, value, endian); + position(p + 1); return this; } public FloatBuffer put (int index, float value) { - bb.putFloat ((index << 2) + offset, value); + ByteBufferHelper.putFloat(bb, (index << 2) + offset, value, endian); return this; } @@ -95,18 +88,8 @@ { 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 (); - - for (int i = 0; i < count; i++) - { - bb.putFloat ((i >> 2) + offset, - bb.getFloat (((i + position ()) >> 2) + offset)); - } - + bb.shiftDown(offset, offset + 4 * position(), 4 * count); position (count); limit (capacity ()); } @@ -114,27 +96,32 @@ 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 () { // Create a sliced copy of this object that shares its content. return new FloatViewBufferImpl (bb, (position () >> 2) + offset, - remaining (), remaining (), 0, -1, - isReadOnly ()); + remaining(), remaining(), 0, -1, + 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 () { - // Create a copy of this object that shares its content and is read-only - return new FloatViewBufferImpl (bb, (position () >> 2) + offset, - remaining (), remaining (), 0, -1, true); + return duplicate(true); } public boolean isReadOnly () @@ -149,6 +136,6 @@ public ByteOrder order () { - return ByteOrder.LITTLE_ENDIAN; + return endian; } } Index: java/nio/IntViewBufferImpl.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/nio/IntViewBufferImpl.java,v retrieving revision 1.1 diff -u -b -B -r1.1 IntViewBufferImpl.java --- java/nio/IntViewBufferImpl.java 19 May 2003 09:05:13 -0000 1.1 +++ java/nio/IntViewBufferImpl.java 11 Feb 2004 19:04:06 -0000 @@ -1,5 +1,5 @@ /* IntViewBufferImpl.java -- - Copyright (C) 2003 Free Software Foundation, Inc. + Copyright (C) 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -40,54 +40,47 @@ class IntViewBufferImpl extends IntBuffer { - private boolean readOnly; + /** Position in bb (i.e. a byte offset) where this buffer starts. */ private int offset; private ByteBuffer bb; + private boolean readOnly; 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, int limit, int position, int mark, - boolean readOnly) + boolean readOnly, ByteOrder endian) { super (limit >> 2, limit >> 2, position >> 2, mark >> 2); this.bb = bb; this.offset = offset; this.readOnly = readOnly; - // FIXME: What if this is called from IntViewBufferImpl and ByteBuffer has changed its endianess ? - this.endian = bb.order (); + this.endian = endian; } public int get () { - int result = bb.getInt ((position () << 2) + offset); - position (position () + 1); + int p = position(); + int result = ByteBufferHelper.getInt(bb, (p << 2) + offset, endian); + position(p + 1); return result; } public int get (int index) { - return bb.getInt ((index << 2) + offset); + return ByteBufferHelper.getInt(bb, (index << 2) + offset, endian); } public IntBuffer put (int value) { - bb.putInt ((position () << 2) + offset, value); - position (position () + 1); + int p = position(); + ByteBufferHelper.putInt(bb, (p << 2) + offset, value, endian); + position(p + 1); return this; } public IntBuffer put (int index, int value) { - bb.putInt ((index << 2) + offset, value); + ByteBufferHelper.putInt(bb, (index << 2) + offset, value, endian); return this; } @@ -95,18 +88,8 @@ { 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 (); - - for (int i = 0; i < count; i++) - { - bb.putInt ((i >> 2) + offset, - bb.getInt (((i + position ()) >> 2) + offset)); - } - + bb.shiftDown(offset, offset + 4 * position(), 4 * count); position (count); limit (capacity ()); } @@ -114,27 +96,32 @@ 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 () { // Create a sliced copy of this object that shares its content. return new IntViewBufferImpl (bb, (position () >> 2) + offset, - remaining (), remaining (), 0, -1, - isReadOnly ()); + remaining(), remaining(), 0, -1, + 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 () { - // Create a copy of this object that shares its content and is read-only - return new IntViewBufferImpl (bb, (position () >> 2) + offset, - remaining (), remaining (), 0, -1, true); + return duplicate(true); } public boolean isReadOnly () @@ -149,6 +136,6 @@ public ByteOrder order () { - return ByteOrder.LITTLE_ENDIAN; + return endian; } } Index: java/nio/LongViewBufferImpl.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/nio/LongViewBufferImpl.java,v retrieving revision 1.1 diff -u -b -B -r1.1 LongViewBufferImpl.java --- java/nio/LongViewBufferImpl.java 19 May 2003 09:05:13 -0000 1.1 +++ java/nio/LongViewBufferImpl.java 11 Feb 2004 19:04:06 -0000 @@ -1,5 +1,5 @@ /* LongViewBufferImpl.java -- - Copyright (C) 2003 Free Software Foundation, Inc. + Copyright (C) 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -40,54 +40,47 @@ class LongViewBufferImpl extends LongBuffer { - private boolean readOnly; + /** Position in bb (i.e. a byte offset) where this buffer starts. */ private int offset; private ByteBuffer bb; + private boolean readOnly; 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, int limit, int position, int mark, - boolean readOnly) + boolean readOnly, ByteOrder endian) { super (limit >> 3, limit >> 3, position >> 3, mark >> 3); this.bb = bb; this.offset = offset; this.readOnly = readOnly; - // FIXME: What if this is called from LongViewBufferImpl and ByteBuffer has changed its endianess ? - this.endian = bb.order (); + this.endian = endian; } public long get () { - long result = bb.getLong ((position () << 3) + offset); - position (position () + 1); + int p = position(); + long result = ByteBufferHelper.getLong(bb, (p << 3) + offset, endian); + position(p + 1); return result; } public long get (int index) { - return bb.getLong ((index << 3) + offset); + return ByteBufferHelper.getLong(bb, (index << 3) + offset, endian); } public LongBuffer put (long value) { - bb.putLong ((position () << 3) + offset, value); - position (position () + 1); + int p = position(); + ByteBufferHelper.putLong(bb, (p << 3) + offset, value, endian); + position(p + 1); return this; } public LongBuffer put (int index, long value) { - bb.putLong ((index << 3) + offset, value); + ByteBufferHelper.putLong(bb, (index << 3) + offset, value, endian); return this; } @@ -95,18 +88,8 @@ { 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 (); - - for (int i = 0; i < count; i++) - { - bb.putLong ((i >> 3) + offset, - bb.getLong (((i + position ()) >> 3) + offset)); - } - + bb.shiftDown(offset, offset + 8 * position(), 8 * count); position (count); limit (capacity ()); } @@ -114,27 +96,32 @@ 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 () { // Create a sliced copy of this object that shares its content. return new LongViewBufferImpl (bb, (position () >> 3) + offset, - remaining (), remaining (), 0, -1, - isReadOnly ()); + remaining(), remaining(), 0, -1, + 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 () { - // Create a copy of this object that shares its content and is read-only - return new LongViewBufferImpl (bb, (position () >> 3) + offset, - remaining (), remaining (), 0, -1, true); + return duplicate(true); } public boolean isReadOnly () @@ -149,6 +136,6 @@ public ByteOrder order () { - return ByteOrder.LITTLE_ENDIAN; + return endian; } } Index: java/nio/MappedByteBufferImpl.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/nio/MappedByteBufferImpl.java,v retrieving revision 1.5 diff -u -b -B -r1.5 MappedByteBufferImpl.java --- java/nio/MappedByteBufferImpl.java 25 Sep 2003 14:01:18 -0000 1.5 +++ java/nio/MappedByteBufferImpl.java 11 Feb 2004 19:04:06 -0000 @@ -1,5 +1,5 @@ /* 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. @@ -111,15 +111,14 @@ public ByteBuffer compact () { - int copied = 0; - - while (remaining () > 0) + int pos = position(); + if (pos > 0) { - put (copied, get ()); - copied++; + int count = remaining(); + shiftDown(0, pos, count); + position(count); + limit(capacity()); } - - position (copied); return this; } @@ -145,151 +144,163 @@ 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 () { - return new ShortViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ()); + return new ShortViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order()); } 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 () { - return new LongViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ()); + return new LongViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order()); } 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 () { - 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) { - 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; } } Index: java/nio/ShortViewBufferImpl.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/nio/ShortViewBufferImpl.java,v retrieving revision 1.1 diff -u -b -B -r1.1 ShortViewBufferImpl.java --- java/nio/ShortViewBufferImpl.java 19 May 2003 09:05:13 -0000 1.1 +++ java/nio/ShortViewBufferImpl.java 11 Feb 2004 19:04:06 -0000 @@ -1,5 +1,5 @@ /* ShortViewBufferImpl.java -- - Copyright (C) 2003 Free Software Foundation, Inc. + Copyright (C) 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -40,54 +40,47 @@ class ShortViewBufferImpl extends ShortBuffer { - private boolean readOnly; + /** Position in bb (i.e. a byte offset) where this buffer starts. */ private int offset; private ByteBuffer bb; + private boolean readOnly; 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, int limit, int position, int mark, - boolean readOnly) + boolean readOnly, ByteOrder endian) { super (limit >> 1, limit >> 1, position >> 1, mark >> 1); this.bb = bb; this.offset = offset; this.readOnly = readOnly; - // FIXME: What if this is called from ShortViewBufferImpl and ByteBuffer has changed its endianess ? - this.endian = bb.order (); + this.endian = endian; } public short get () { - short result = bb.getShort ((position () << 1) + offset); - position (position () + 1); + int p = position(); + short result = ByteBufferHelper.getShort(bb, (p << 1) + offset, endian); + position(p + 1); return result; } public short get (int index) { - return bb.getShort ((index << 1) + offset); + return ByteBufferHelper.getShort(bb, (index << 1) + offset, endian); } public ShortBuffer put (short value) { - bb.putShort ((position () << 1) + offset, value); - position (position () + 1); + int p = position(); + ByteBufferHelper.putShort(bb, (p << 1) + offset, value, endian); + position(p + 1); return this; } public ShortBuffer put (int index, short value) { - bb.putShort ((index << 1) + offset, value); + ByteBufferHelper.putShort(bb, (index << 1) + offset, value, endian); return this; } @@ -95,18 +88,8 @@ { 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 (); - - for (int i = 0; i < count; i++) - { - bb.putShort ((i >> 1) + offset, - bb.getShort (((i + position ()) >> 1) + offset)); - } - + bb.shiftDown(offset, offset + 2 * position(), 2 * count); position (count); limit (capacity ()); } @@ -114,27 +96,32 @@ 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 () { // Create a sliced copy of this object that shares its content. return new ShortViewBufferImpl (bb, (position () >> 1) + offset, - remaining (), remaining (), 0, -1, - isReadOnly ()); + remaining(), remaining(), 0, -1, + 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 () { - // Create a copy of this object that shares its content and is read-only - return new ShortViewBufferImpl (bb, (position () >> 1) + offset, - remaining (), remaining (), 0, -1, true); + return duplicate(true); } public boolean isReadOnly () @@ -149,6 +136,6 @@ public ByteOrder order () { - return ByteOrder.LITTLE_ENDIAN; + return endian; } } Index: include/java_nio_DirectByteBufferImpl.h =================================================================== RCS file: /cvsroot/classpath/classpath/include/java_nio_DirectByteBufferImpl.h,v retrieving revision 1.1 diff -u -b -B -r1.1 java_nio_DirectByteBufferImpl.h --- include/java_nio_DirectByteBufferImpl.h 2 Jul 2003 09:16:52 -0000 1.1 +++ include/java_nio_DirectByteBufferImpl.h 11 Feb 2004 19:04:06 -0000 @@ -14,6 +14,7 @@ extern JNIEXPORT void JNICALL Java_java_nio_DirectByteBufferImpl_freeImpl (JNIEnv *env, jclass, jobject); extern JNIEXPORT jbyte JNICALL Java_java_nio_DirectByteBufferImpl_getImpl (JNIEnv *env, jobject, jint); extern JNIEXPORT void JNICALL Java_java_nio_DirectByteBufferImpl_putImpl (JNIEnv *env, jobject, jint, jbyte); +extern JNIEXPORT void JNICALL Java_java_nio_DirectByteBufferImpl_shiftDown (JNIEnv *env, jobject, jint, jint, jint); #ifdef __cplusplus } Index: native/jni/java-nio/java_nio_DirectByteBufferImpl.c =================================================================== RCS file: /cvsroot/classpath/classpath/native/jni/java-nio/java_nio_DirectByteBufferImpl.c,v retrieving revision 1.1 diff -u -b -B -r1.1 java_nio_DirectByteBufferImpl.c --- native/jni/java-nio/java_nio_DirectByteBufferImpl.c 2 Jul 2003 09:16:52 -0000 1.1 +++ native/jni/java-nio/java_nio_DirectByteBufferImpl.c 11 Feb 2004 19:04:06 -0000 @@ -71,3 +71,8 @@ JCL_ThrowException (env, IO_EXCEPTION, "java.nio.DirectByteBufferImpl.putImpl(): not implemented"); } +JNIEXPORT void JNICALL +Java_java_nio_DirectByteBufferImpl_shiftDown (JNIEnv *env, jobject obj, jint src_offset, jint dst_offset, jint count); +{ + JCL_ThrowException (env, IO_EXCEPTION, "java.nio.DirectByteBufferImpl.shiftDown(): not implemented"); +}