From 40c23042f4594368c96e1af7dc65b72590d584b0 Mon Sep 17 00:00:00 2001 From: Per Bothner Date: Sun, 8 Feb 2004 13:02:53 -0800 Subject: 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 (): 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 --- libjava/java/nio/LongViewBufferImpl.java | 83 ++++++++++++++------------------ 1 file changed, 35 insertions(+), 48 deletions(-) (limited to 'libjava/java/nio/LongViewBufferImpl.java') diff --git a/libjava/java/nio/LongViewBufferImpl.java b/libjava/java/nio/LongViewBufferImpl.java index c7ada48..a405ec8 100644 --- a/libjava/java/nio/LongViewBufferImpl.java +++ b/libjava/java/nio/LongViewBufferImpl.java @@ -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 @@ package java.nio; 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) + int limit, int position, int mark, + 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,48 +88,42 @@ class LongViewBufferImpl extends LongBuffer { 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 ()); } - 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); } - public LongBuffer asReadOnlyBuffer () + LongBuffer duplicate (boolean readOnly) { - // 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); + 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 () + { + return duplicate(true); + } + public boolean isReadOnly () { return readOnly; @@ -149,6 +136,6 @@ class LongViewBufferImpl extends LongBuffer public ByteOrder order () { - return ByteOrder.LITTLE_ENDIAN; + return endian; } } -- cgit v1.1