Author: orudyy
Date: Wed Oct 5 11:56:50 2016
New Revision: 1763439
URL: http://svn.apache.org/viewvc?rev=1763439&view=rev
Log:
QPID-6803: [Java Broker] Have SlicedQpidByteBuffer delegate operations to
underlying direct byte buffer
Modified:
qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/QpidByteBuffer.java
qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/QpidByteBufferImpl.java
qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/SlicedQpidByteBuffer.java
qpid/java/trunk/common/src/test/java/org/apache/qpid/bytebuffer/SlicedQpidByteBufferTest.java
Modified:
qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/QpidByteBuffer.java
URL:
http://svn.apache.org/viewvc/qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/QpidByteBuffer.java?rev=1763439&r1=1763438&r2=1763439&view=diff
==============================================================================
---
qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/QpidByteBuffer.java
(original)
+++
qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/QpidByteBuffer.java
Wed Oct 5 11:56:50 2016
@@ -23,6 +23,8 @@ package org.apache.qpid.bytebuffer;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.BufferOverflowException;
+import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.GatheringByteChannel;
@@ -31,6 +33,7 @@ import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.zip.GZIPInputStream;
@@ -130,43 +133,17 @@ public abstract class QpidByteBuffer
public final CharBuffer decode(Charset charset)
{
- ByteBuffer underlyingBuffer = getUnderlyingBuffer();
- try
- {
- return charset.decode(underlyingBuffer);
- }
- finally
- {
- updateFromLastUnderlying();
- }
+ return charset.decode(getUnderlyingBuffer());
}
public final int read(ReadableByteChannel channel) throws IOException
{
- ByteBuffer underlyingBuffer = getUnderlyingBuffer();
- try
- {
- return channel.read(underlyingBuffer);
- }
- finally
- {
- updateFromLastUnderlying();
- }
+ return channel.read(getUnderlyingBuffer());
}
public final SSLEngineResult decryptSSL(SSLEngine engine, QpidByteBuffer
dest) throws SSLException
{
- ByteBuffer underlyingBuffer = getUnderlyingBuffer();
- ByteBuffer destUnderlyingBuffer = dest.getUnderlyingBuffer();
- try
- {
- return engine.unwrap(underlyingBuffer, destUnderlyingBuffer);
- }
- finally
- {
- updateFromLastUnderlying();
- dest.updateFromLastUnderlying();
- }
+ return engine.unwrap(getUnderlyingBuffer(),
dest.getUnderlyingBuffer());
}
@Override
@@ -178,7 +155,10 @@ public abstract class QpidByteBuffer
'}';
}
- public abstract boolean hasRemaining();
+ public final boolean hasRemaining()
+ {
+ return _buffer.hasRemaining();
+ }
public abstract QpidByteBuffer putInt(int index, int value);
@@ -186,49 +166,126 @@ public abstract class QpidByteBuffer
public abstract QpidByteBuffer putChar(int index, char value);
- public abstract QpidByteBuffer put(byte b);
+ public final QpidByteBuffer put(final byte b)
+ {
+ _buffer.put(b);
+ return this;
+ }
public abstract QpidByteBuffer put(int index, byte b);
public abstract short getShort(int index);
- public abstract QpidByteBuffer mark();
+ public final QpidByteBuffer mark()
+ {
+ _buffer.mark();
+ return this;
+ }
- public abstract long getLong();
+ public final long getLong()
+ {
+ return _buffer.getLong();
+ }
public abstract QpidByteBuffer putFloat(int index, float value);
public abstract double getDouble(int index);
- public abstract boolean hasArray();
+ public final boolean hasArray()
+ {
+ return _buffer.hasArray();
+ }
- public abstract double getDouble();
+ public final double getDouble()
+ {
+ return _buffer.getDouble();
+ }
- public abstract QpidByteBuffer putFloat(float value);
+ public final QpidByteBuffer putFloat(final float value)
+ {
+ _buffer.putFloat(value);
+ return this;
+ }
- public abstract QpidByteBuffer putInt(int value);
+ public final QpidByteBuffer putInt(final int value)
+ {
+ _buffer.putInt(value);
+ return this;
+ }
public abstract byte[] array();
- public abstract QpidByteBuffer putShort(short value);
+ public final QpidByteBuffer putShort(final short value)
+ {
+ _buffer.putShort(value);
+ return this;
+ }
public abstract int getInt(int index);
- public abstract int remaining();
+ public final int remaining()
+ {
+ return _buffer.remaining();
+ }
- public abstract QpidByteBuffer put(byte[] src);
+ public final QpidByteBuffer put(final byte[] src)
+ {
+ _buffer.put(src);
+ return this;
+ }
- public abstract QpidByteBuffer put(ByteBuffer src);
+ public final QpidByteBuffer put(final ByteBuffer src)
+ {
+ _buffer.put(src);
+ return this;
+ }
- public abstract QpidByteBuffer put(QpidByteBuffer src);
+ public final QpidByteBuffer put(final QpidByteBuffer src)
+ {
+ int sourceRemaining = src.remaining();
+ if (sourceRemaining > remaining())
+ {
+ throw new BufferOverflowException();
+ }
- public abstract QpidByteBuffer get(byte[] dst, int offset, int length);
+ _buffer.put(src.getUnderlyingBuffer());
+ return this;
+ }
- public abstract QpidByteBuffer get(ByteBuffer dst);
+ public final QpidByteBuffer get(final byte[] dst, final int offset, final
int length)
+ {
+ _buffer.get(dst, offset, length);
+ return this;
+ }
- public abstract void copyTo(ByteBuffer dst);
+ public final QpidByteBuffer get(final ByteBuffer dst)
+ {
+ int destinationRemaining = dst.remaining();
+ int remaining = remaining();
+ if (destinationRemaining < remaining)
+ {
+ throw new BufferUnderflowException();
+ }
+ dst.put(_buffer);
+ return this;
+ }
- public abstract void putCopyOf(QpidByteBuffer buf);
+ public final void copyTo(final ByteBuffer dst)
+ {
+ dst.put(_buffer.duplicate());
+ }
+
+ public final void putCopyOf(final QpidByteBuffer source)
+ {
+ int remaining = remaining();
+ int sourceRemaining = source.remaining();
+ if (sourceRemaining > remaining)
+ {
+ throw new BufferOverflowException();
+ }
+
+ put(source.getUnderlyingBuffer().duplicate());
+ }
public abstract QpidByteBuffer rewind();
@@ -238,7 +295,11 @@ public abstract class QpidByteBuffer
public abstract QpidByteBuffer compact();
- public abstract QpidByteBuffer putDouble(double value);
+ public final QpidByteBuffer putDouble(final double value)
+ {
+ _buffer.putDouble(value);
+ return this;
+ }
public abstract int limit();
@@ -246,15 +307,26 @@ public abstract class QpidByteBuffer
public abstract QpidByteBuffer flip();
- public abstract short getShort();
+ public final short getShort()
+ {
+ return _buffer.getShort();
- public abstract float getFloat();
+ }
+
+ public final float getFloat()
+ {
+ return _buffer.getFloat();
+ }
public abstract QpidByteBuffer limit(int newLimit);
public abstract QpidByteBuffer duplicate();
- public abstract QpidByteBuffer put(byte[] src, int offset, int length);
+ public final QpidByteBuffer put(final byte[] src, final int offset, final
int length)
+ {
+ _buffer.put(src, offset, length);
+ return this;
+ }
public abstract long getLong(int index);
@@ -262,25 +334,53 @@ public abstract class QpidByteBuffer
public abstract char getChar(int index);
- public abstract byte get();
+ public final byte get()
+ {
+ return _buffer.get();
+ }
public abstract byte get(int index);
- public abstract QpidByteBuffer get(byte[] dst);
+ public final QpidByteBuffer get(final byte[] dst)
+ {
+ _buffer.get(dst);
+ return this;
+ }
- public abstract void copyTo(byte[] dst);
+ public final void copyTo(final byte[] dst)
+ {
+ if (remaining() < dst.length)
+ {
+ throw new BufferUnderflowException();
+ }
+ _buffer.duplicate().get(dst);
+ }
- public abstract QpidByteBuffer putChar(char value);
+ public final QpidByteBuffer putChar(final char value)
+ {
+ _buffer.putChar(value);
+ return this;
+ }
public abstract QpidByteBuffer position(int newPosition);
public abstract int arrayOffset();
- public abstract char getChar();
+ public final char getChar()
+ {
+ return _buffer.getChar();
+ }
- public abstract int getInt();
+ public final int getInt()
+ {
+ return _buffer.getInt();
+ }
- public abstract QpidByteBuffer putLong(long value);
+ public final QpidByteBuffer putLong(final long value)
+ {
+ _buffer.putLong(value);
+ return this;
+ }
public abstract float getFloat(int index);
@@ -292,22 +392,8 @@ public abstract class QpidByteBuffer
public abstract QpidByteBuffer putDouble(int index, double value);
- /**
- * Returns an underlying byte buffer for update operations.
- * <p></p>
- * Method {@link #updateFromLastUnderlying()} needs to be invoked to
update the state of {@link QpidByteBuffer}
- *
- * @return ByteBuffer
- */
abstract ByteBuffer getUnderlyingBuffer();
- /**
- * Used to update the state of {@link QpidByteBuffer} after underlying
byte buffer is modified.
- *
- * @throws IllegalStateException when method is invoked without previous
call to {@link #getUnderlyingBuffer()}
- */
- abstract void updateFromLastUnderlying();
-
public static QpidByteBuffer allocate(boolean direct, int size)
{
return direct ? allocateDirect(size) : allocate(size);
@@ -415,25 +501,13 @@ public abstract class QpidByteBuffer
final Collection<QpidByteBuffer>
buffers,
QpidByteBuffer dest) throws
SSLException
{
- List<QpidByteBuffer> qpidBuffers = new ArrayList<>(buffers);
final ByteBuffer[] src = new ByteBuffer[buffers.size()];
+ Iterator<QpidByteBuffer> iterator = buffers.iterator();
for (int i = 0; i < src.length; i++)
{
- src[i] = qpidBuffers.get(i).getUnderlyingBuffer();
- }
- ByteBuffer destinationUnderlyingBuffer = dest.getUnderlyingBuffer();
- try
- {
- return engine.wrap(src, destinationUnderlyingBuffer);
- }
- finally
- {
- for (QpidByteBuffer qpidByteBuffer : qpidBuffers)
- {
- qpidByteBuffer.updateFromLastUnderlying();
- }
- dest.updateFromLastUnderlying();
+ src[i] = iterator.next().getUnderlyingBuffer();
}
+ return engine.wrap(src, dest.getUnderlyingBuffer());
}
public static Collection<QpidByteBuffer>
inflate(Collection<QpidByteBuffer> compressedBuffers) throws IOException
@@ -511,23 +585,13 @@ public abstract class QpidByteBuffer
public static long write(GatheringByteChannel channel,
Collection<QpidByteBuffer> qpidByteBuffers)
throws IOException
{
- List<QpidByteBuffer> qpidBuffers = new ArrayList<>(qpidByteBuffers);
- ByteBuffer[] byteBuffers = new ByteBuffer[qpidBuffers.size()];
+ ByteBuffer[] byteBuffers = new ByteBuffer[qpidByteBuffers.size()];
+ Iterator<QpidByteBuffer> iterator = qpidByteBuffers.iterator();
for (int i = 0; i < byteBuffers.length; i++)
{
- byteBuffers[i] = qpidBuffers.get(i).getUnderlyingBuffer();
- }
- try
- {
- return channel.write(byteBuffers);
- }
- finally
- {
- for (QpidByteBuffer qbb : qpidBuffers)
- {
- qbb.updateFromLastUnderlying();
- }
+ byteBuffers[i] = iterator.next().getUnderlyingBuffer();
}
+ return channel.write(byteBuffers);
}
public static QpidByteBuffer wrap(final ByteBuffer wrap)
Modified:
qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/QpidByteBufferImpl.java
URL:
http://svn.apache.org/viewvc/qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/QpidByteBufferImpl.java?rev=1763439&r1=1763438&r2=1763439&view=diff
==============================================================================
---
qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/QpidByteBufferImpl.java
(original)
+++
qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/QpidByteBufferImpl.java
Wed Oct 5 11:56:50 2016
@@ -20,7 +20,6 @@
*/
package org.apache.qpid.bytebuffer;
-import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
final class QpidByteBufferImpl extends QpidByteBuffer
@@ -38,12 +37,6 @@ final class QpidByteBufferImpl extends Q
}
@Override
- public boolean hasRemaining()
- {
- return _buffer.hasRemaining();
- }
-
- @Override
public QpidByteBuffer putInt(final int index, final int value)
{
_buffer.putInt(index, value);
@@ -65,13 +58,6 @@ final class QpidByteBufferImpl extends Q
}
@Override
- public QpidByteBuffer put(final byte b)
- {
- _buffer.put(b);
- return this;
- }
-
- @Override
public QpidByteBuffer put(final int index, final byte b)
{
_buffer.put(index, b);
@@ -84,20 +70,6 @@ final class QpidByteBufferImpl extends Q
return _buffer.getShort(index);
}
-
- @Override
- public QpidByteBuffer mark()
- {
- _buffer.mark();
- return this;
- }
-
- @Override
- public long getLong()
- {
- return _buffer.getLong();
- }
-
@Override
public QpidByteBuffer putFloat(final int index, final float value)
{
@@ -112,116 +84,18 @@ final class QpidByteBufferImpl extends Q
}
@Override
- public boolean hasArray()
- {
- return _buffer.hasArray();
- }
-
- @Override
- public double getDouble()
- {
- return _buffer.getDouble();
- }
-
- @Override
- public QpidByteBuffer putFloat(final float value)
- {
- _buffer.putFloat(value);
- return this;
- }
-
- @Override
- public QpidByteBuffer putInt(final int value)
- {
- _buffer.putInt(value);
- return this;
- }
-
- @Override
public byte[] array()
{
return _buffer.array();
}
@Override
- public QpidByteBuffer putShort(final short value)
- {
- _buffer.putShort(value);
- return this;
- }
-
- @Override
public int getInt(final int index)
{
return _buffer.getInt(index);
}
@Override
- public int remaining()
- {
- return _buffer.remaining();
- }
-
- @Override
- public QpidByteBuffer put(final byte[] src)
- {
- _buffer.put(src);
- return this;
- }
-
- @Override
- public QpidByteBuffer put(final ByteBuffer src)
- {
- _buffer.put(src);
- return this;
- }
-
- @Override
- public QpidByteBuffer put(final QpidByteBuffer src)
- {
- ByteBuffer underlyingBuffer = src.getUnderlyingBuffer();
- _buffer.put(underlyingBuffer);
- src.updateFromLastUnderlying();
- return this;
- }
-
- @Override
- public QpidByteBuffer get(final byte[] dst, final int offset, final int
length)
- {
- _buffer.get(dst, offset, length);
- return this;
- }
-
- @Override
- public QpidByteBuffer get(final ByteBuffer dst)
- {
- int destinationRemaining = dst.remaining();
- int remaining = remaining();
- if (destinationRemaining < remaining)
- {
- throw new BufferUnderflowException();
- }
- dst.put(_buffer);
- return this;
- }
-
- @Override
- public void copyTo(final ByteBuffer dst)
- {
- dst.put(_buffer.duplicate());
- }
-
- @Override
- public void putCopyOf(final QpidByteBuffer buf)
- {
- _buffer.put(buf.getUnderlyingBuffer().duplicate());
- if (buf instanceof SlicedQpidByteBuffer)
- {
- ((SlicedQpidByteBuffer)buf).clearLastUnderlyingBuffer();
- }
- }
-
- @Override
public QpidByteBuffer rewind()
{
_buffer.rewind();
@@ -250,13 +124,6 @@ final class QpidByteBufferImpl extends Q
}
@Override
- public QpidByteBuffer putDouble(final double value)
- {
- _buffer.putDouble(value);
- return this;
- }
-
- @Override
public int limit()
{
return _buffer.limit();
@@ -277,18 +144,6 @@ final class QpidByteBufferImpl extends Q
}
@Override
- public short getShort()
- {
- return _buffer.getShort();
- }
-
- @Override
- public float getFloat()
- {
- return _buffer.getFloat();
- }
-
- @Override
public QpidByteBuffer limit(final int newLimit)
{
_buffer.limit(newLimit);
@@ -302,13 +157,6 @@ final class QpidByteBufferImpl extends Q
}
@Override
- public QpidByteBuffer put(final byte[] src, final int offset, final int
length)
- {
- _buffer.put(src, offset, length);
- return this;
- }
-
- @Override
public long getLong(final int index)
{
return _buffer.getLong(index);
@@ -327,38 +175,12 @@ final class QpidByteBufferImpl extends Q
}
@Override
- public byte get()
- {
- return _buffer.get();
- }
-
- @Override
public byte get(final int index)
{
return _buffer.get(index);
}
@Override
- public QpidByteBuffer get(final byte[] dst)
- {
- _buffer.get(dst);
- return this;
- }
-
- @Override
- public void copyTo(final byte[] dst)
- {
- _buffer.duplicate().get(dst);
- }
-
- @Override
- public QpidByteBuffer putChar(final char value)
- {
- _buffer.putChar(value);
- return this;
- }
-
- @Override
public QpidByteBuffer position(final int newPosition)
{
_buffer.position(newPosition);
@@ -372,25 +194,6 @@ final class QpidByteBufferImpl extends Q
}
@Override
- public char getChar()
- {
- return _buffer.getChar();
- }
-
- @Override
- public int getInt()
- {
- return _buffer.getInt();
- }
-
- @Override
- public QpidByteBuffer putLong(final long value)
- {
- _buffer.putLong(value);
- return this;
- }
-
- @Override
public float getFloat(final int index)
{
return _buffer.getFloat(index);
@@ -446,9 +249,4 @@ final class QpidByteBufferImpl extends Q
return _buffer;
}
- @Override
- void updateFromLastUnderlying()
- {
- // noop
- }
}
Modified:
qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/SlicedQpidByteBuffer.java
URL:
http://svn.apache.org/viewvc/qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/SlicedQpidByteBuffer.java?rev=1763439&r1=1763438&r2=1763439&view=diff
==============================================================================
---
qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/SlicedQpidByteBuffer.java
(original)
+++
qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/SlicedQpidByteBuffer.java
Wed Oct 5 11:56:50 2016
@@ -20,10 +20,7 @@
package org.apache.qpid.bytebuffer;
-import java.nio.BufferOverflowException;
-import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
-import java.nio.InvalidMarkException;
final class SlicedQpidByteBuffer extends QpidByteBuffer
{
@@ -38,18 +35,13 @@ final class SlicedQpidByteBuffer extends
private final int _capacity;
private final int _offset;
- private int _mark = -1;
- private int _position = 0;
- private int _limit;
- private ByteBuffer _lastUnderlyingBuffer;
-
SlicedQpidByteBuffer(final int position,
final int limit,
final int capacity,
final int offset,
final ByteBufferRef ref)
{
- super(ref, ref.getBuffer());
+ super(ref, ref instanceof PooledByteBufferRef ? ref.getBuffer() :
ref.getBuffer().duplicate());
if (capacity < 0)
{
@@ -71,17 +63,17 @@ final class SlicedQpidByteBuffer extends
throw new IllegalArgumentException("Offset cannot be negative");
}
+ if (offset >= _buffer.capacity())
+ {
+ throw new IllegalArgumentException("Offset exceeds capacity");
+ }
+
_capacity = capacity;
- _position = position;
- _limit = limit;
_offset = offset;
_ref.incrementRef();
- }
- @Override
- public boolean hasRemaining()
- {
- return _position < _limit;
+ _buffer.limit(offset + limit);
+ _buffer.position(offset + position);
}
@Override
@@ -117,19 +109,9 @@ final class SlicedQpidByteBuffer extends
}
@Override
- public QpidByteBuffer put(final byte b)
- {
- checkOverflow(SIZE_BYTE);
- put(_position, b);
- _position++;
- return this;
- }
-
- @Override
public QpidByteBuffer putDouble(final int index, final double value)
{
checkIndexBounds(index, SIZE_DOUBLE);
-
_buffer.putDouble(_offset + index, value);
return this;
}
@@ -143,27 +125,9 @@ final class SlicedQpidByteBuffer extends
}
@Override
- public QpidByteBuffer mark()
- {
- _mark = _position;
- return this;
- }
-
- @Override
- public long getLong()
- {
- checkUnderflow(SIZE_LONG);
-
- long value = getLong(_position);
- _position += SIZE_LONG;
- return value;
- }
-
- @Override
public QpidByteBuffer putFloat(final int index, final float value)
{
checkIndexBounds(index, SIZE_FLOAT);
-
_buffer.putFloat(_offset + index, value);
return this;
}
@@ -176,55 +140,10 @@ final class SlicedQpidByteBuffer extends
}
@Override
- public boolean hasArray()
- {
- return _buffer.hasArray();
- }
-
- @Override
- public double getDouble()
- {
- checkUnderflow(SIZE_DOUBLE);
-
- double value = getDouble(_position);
- _position += SIZE_DOUBLE;
- return value;
- }
-
- @Override
- public QpidByteBuffer putFloat(final float value)
- {
- checkOverflow(SIZE_FLOAT);
-
- putFloat(position(), value);
- _position += SIZE_FLOAT;
- return this;
- }
-
- @Override
- public QpidByteBuffer putInt(final int value)
- {
- checkOverflow(SIZE_INT);
-
- putInt(position(), value);
- _position += SIZE_INT;
- return this;
- }
-
- @Override
- public byte[] array()
- {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public QpidByteBuffer putShort(final short value)
+ public long getLong(final int index)
{
- checkOverflow(SIZE_SHORT);
-
- putShort(position(), value);
- _position += SIZE_SHORT;
- return this;
+ checkIndexBounds(index, SIZE_LONG);
+ return _buffer.getLong(index + _offset);
}
@Override
@@ -235,129 +154,52 @@ final class SlicedQpidByteBuffer extends
}
@Override
- public int remaining()
- {
- return _limit - _position;
- }
-
- @Override
- public QpidByteBuffer put(final byte[] src)
- {
- return put(src, 0, src.length);
- }
-
- @Override
- public QpidByteBuffer put(final ByteBuffer src)
- {
- int sourceRemaining = src.remaining();
- if (sourceRemaining > remaining())
- {
- throw new BufferOverflowException();
- }
-
- final int length = src.remaining();
- ByteBuffer dup = getDuplicateForBulk();
- dup.put(src);
- _position += length;
-
- return this;
- }
-
- @Override
- public QpidByteBuffer put(final QpidByteBuffer src)
+ public QpidByteBuffer putLong(final int index, final long value)
{
- if (src == this)
- {
- throw new IllegalArgumentException();
- }
-
- int sourceRemaining = src.remaining();
- if (sourceRemaining > remaining())
- {
- throw new BufferOverflowException();
- }
-
- put(src.getUnderlyingBuffer());
- src.updateFromLastUnderlying();
-
+ checkIndexBounds(index, SIZE_LONG);
+ _buffer.putLong(_offset + index, value);
return this;
}
@Override
- public QpidByteBuffer get(final byte[] dst, final int offset, final int
length)
+ public char getChar(final int index)
{
- checkBounds(dst, offset, length);
-
- if (length > remaining())
- {
- throw new BufferUnderflowException();
- }
-
- ByteBuffer dup = getDuplicateForBulk();
- dup.get(dst, offset, length);
- _position += length;
- return this;
+ checkIndexBounds(index, SIZE_CHAR);
+ return _buffer.getChar(index + _offset);
}
@Override
- public QpidByteBuffer get(final ByteBuffer dst)
+ public byte get(final int index)
{
- int remaining = remaining();
- copyTo(dst);
- _position += remaining;
- return this;
+ checkIndexBounds(index, SIZE_BYTE);
+ return _buffer.get(index + _offset);
}
@Override
- public void copyTo(final ByteBuffer dst)
+ public float getFloat(final int index)
{
- int destinationRemaining = dst.remaining();
- int remaining = remaining();
- if (destinationRemaining < remaining)
- {
- throw new BufferUnderflowException();
- }
-
- ByteBuffer dup = getDuplicateForBulk();
- dst.put(dup);
+ checkIndexBounds(index, SIZE_FLOAT);
+ return _buffer.getFloat(index + _offset);
}
@Override
- public void putCopyOf(final QpidByteBuffer source)
+ public byte[] array()
{
- int remaining = remaining();
- int sourceRemaining = source.remaining();
- if (sourceRemaining > remaining)
- {
- throw new BufferOverflowException();
- }
-
- put(source.getUnderlyingBuffer().duplicate());
+ throw new UnsupportedOperationException();
}
@Override
public QpidByteBuffer rewind()
{
- _position = 0;
- _mark = -1;
+ _buffer.position(_offset);
return this;
}
@Override
public QpidByteBuffer clear()
{
- _position = 0;
- _limit = _capacity;
- _mark = -1;
- return this;
- }
-
- @Override
- public QpidByteBuffer putLong(final int index, final long value)
- {
- checkIndexBounds(index, SIZE_LONG);
-
- _buffer.putLong(_offset + index, value);
+ _buffer.position(_offset);
+ _buffer.limit(_offset + _capacity);
return this;
}
@@ -365,123 +207,63 @@ final class SlicedQpidByteBuffer extends
public QpidByteBuffer compact()
{
int remaining = remaining();
- if (_position > 0 && _position < _limit)
+ if (_buffer.position() > _offset)
{
- getUnderlyingBuffer().compact();
- _lastUnderlyingBuffer = null;
- }
- _position = remaining;
- _limit = _capacity;
- _mark = -1;
- return this;
- }
+ ByteBuffer buffer = _buffer.duplicate();
+ buffer.position(_offset);
+ buffer.limit(_offset + _capacity);
- @Override
- public QpidByteBuffer putDouble(final double value)
- {
- checkOverflow(SIZE_DOUBLE);
+ buffer = buffer.slice();
+ buffer.position(position());
+ buffer.limit(limit());
- putDouble(position(), value);
- _position += SIZE_DOUBLE;
+ buffer.compact();
+ }
+
+ _buffer.limit(_offset + _capacity);
+ _buffer.position(_offset + remaining);
return this;
}
@Override
public int limit()
{
- return _limit;
+ return _buffer.limit() - _offset;
}
@Override
public QpidByteBuffer reset()
{
- if (_mark < 0)
- {
- throw new InvalidMarkException();
- }
- _position = _mark;
+ _buffer.reset();
return this;
}
@Override
public QpidByteBuffer flip()
{
- _limit = _position;
- _position = 0;
- _mark = -1;
+ final int position = _buffer.position();
+ _buffer.position(_offset);
+ _buffer.limit(position);
return this;
}
@Override
- public short getShort()
- {
- checkUnderflow(SIZE_SHORT);
-
- short value = getShort(_position);
- _position += SIZE_SHORT;
- return value;
- }
-
- @Override
- public float getFloat()
- {
- checkUnderflow(SIZE_FLOAT);
-
- float value = getFloat(_position);
- _position += SIZE_FLOAT;
- return value;
- }
-
- @Override
public QpidByteBuffer limit(final int newLimit)
{
if (newLimit > _capacity || newLimit < 0)
{
throw new IllegalArgumentException();
}
- _limit = newLimit;
- if (_position > _limit)
- {
- _position = _limit;
- }
- if (_mark > _limit)
- {
- _mark = -1;
- }
+ _buffer.limit(_offset + newLimit);
return this;
}
@Override
public QpidByteBuffer duplicate()
{
- SlicedQpidByteBuffer duplicate = new SlicedQpidByteBuffer(_position,
_limit, _capacity, _offset, _ref);
- duplicate._mark = _mark;
- return duplicate;
+ return new SlicedQpidByteBuffer(position(), limit(), _capacity,
_offset, _ref);
}
- @Override
- public QpidByteBuffer put(final byte[] src, final int offset, final int
length)
- {
- checkBounds(src, offset, length);
-
- if (length > remaining())
- {
- throw new BufferOverflowException();
- }
-
- ByteBuffer dup = getDuplicateForBulk();
- dup.put(src, offset, length);
- _position += length;
-
- return this;
- }
-
- @Override
- public long getLong(final int index)
- {
- checkIndexBounds(index, SIZE_LONG);
- return _buffer.getLong(index + _offset);
- }
@Override
public int capacity()
@@ -490,69 +272,13 @@ final class SlicedQpidByteBuffer extends
}
@Override
- public char getChar(final int index)
- {
- checkIndexBounds(index, SIZE_CHAR);
- return _buffer.getChar(index + _offset);
- }
-
- @Override
- public byte get()
- {
- checkUnderflow(SIZE_BYTE);
-
- byte value = get(_position);
- _position += SIZE_BYTE;
- return value;
- }
-
- @Override
- public byte get(final int index)
- {
- checkIndexBounds(index, SIZE_BYTE);
- return _buffer.get(index + _offset);
- }
-
- @Override
- public QpidByteBuffer get(final byte[] dst)
- {
- return get(dst, 0, dst.length);
- }
-
- @Override
- public void copyTo(final byte[] dst)
- {
- if (remaining() < dst.length)
- {
- throw new BufferUnderflowException();
- }
-
- ByteBuffer dup = getDuplicateForBulk();
- dup.get(dst);
- }
-
- @Override
- public QpidByteBuffer putChar(final char value)
- {
- checkOverflow(SIZE_CHAR);
-
- putChar(position(), value);
- _position += SIZE_CHAR;
- return this;
- }
-
- @Override
public QpidByteBuffer position(final int newPosition)
{
- if (newPosition > _limit || newPosition < 0)
+ if (newPosition > limit() || newPosition < 0)
{
throw new IllegalArgumentException();
}
- _position = newPosition;
- if (_mark > _position)
- {
- _mark = -1;
- }
+ _buffer.position(_offset + newPosition);
return this;
}
@@ -563,46 +289,9 @@ final class SlicedQpidByteBuffer extends
}
@Override
- public char getChar()
- {
- checkUnderflow(SIZE_CHAR);
-
- char value = getChar(_position);
- _position += SIZE_CHAR;
- return value;
- }
-
- @Override
- public int getInt()
- {
- checkUnderflow(SIZE_INT);
-
- int value = getInt(_position);
- _position += SIZE_INT;
- return value;
- }
-
- @Override
- public QpidByteBuffer putLong(final long value)
- {
- checkOverflow(SIZE_LONG);
-
- putLong(position(), value);
- _position += SIZE_LONG;
- return this;
- }
-
- @Override
- public float getFloat(final int index)
- {
- checkIndexBounds(index, SIZE_FLOAT);
- return _buffer.getFloat(index + _offset);
- }
-
- @Override
public QpidByteBuffer slice()
{
- return new SlicedQpidByteBuffer(0, remaining(), remaining(), _offset +
_position, _ref);
+ return new SlicedQpidByteBuffer(0, remaining(), remaining(),
_buffer.position(), _ref);
}
@@ -610,13 +299,13 @@ final class SlicedQpidByteBuffer extends
public QpidByteBuffer view(final int offset, final int length)
{
int newCapacity = Math.min(length, remaining() - offset);
- return new SlicedQpidByteBuffer(0, newCapacity, newCapacity, _offset +
_position + offset, _ref);
+ return new SlicedQpidByteBuffer(0, newCapacity, newCapacity,
_buffer.position() + offset, _ref);
}
@Override
public int position()
{
- return _position;
+ return _buffer.position() - _offset;
}
@Override
@@ -625,81 +314,21 @@ final class SlicedQpidByteBuffer extends
return "SlicedQpidByteBuffer{" +
"_capacity=" + _capacity +
", _offset=" + _offset +
- ", _mark=" + _mark +
- ", _position=" + _position +
- ", _limit=" + _limit +
+ ", _buffer=" + _buffer +
'}';
}
@Override
ByteBuffer getUnderlyingBuffer()
{
- ByteBuffer buffer = _buffer.duplicate();
- buffer.position(_offset);
- buffer.limit(_offset + _capacity);
-
- buffer = buffer.slice();
- buffer.position(_position);
- buffer.limit(_limit);
- _lastUnderlyingBuffer = buffer;
- return buffer;
- }
-
- @Override
- void updateFromLastUnderlying()
- {
- if (_lastUnderlyingBuffer == null)
- {
- throw new IllegalStateException("No last underlying ByteBuffer
recorded for " + this);
- }
- _position = _lastUnderlyingBuffer.position();
- _limit = _lastUnderlyingBuffer.limit();
- _lastUnderlyingBuffer = null;
- }
-
- void clearLastUnderlyingBuffer()
- {
- _lastUnderlyingBuffer = null;
- }
-
- private ByteBuffer getDuplicateForBulk()
- {
- ByteBuffer dup = _buffer.duplicate();
- dup.position(_offset + _position);
- dup.limit(_offset + _limit);
- return dup;
- }
-
- private void checkBounds(final byte[] array, final int offset, final int
length)
- {
- if (offset < 0 || (offset > 0 && offset > array.length - 1) || length
< 0 || length > array.length)
- {
- throw new IndexOutOfBoundsException();
- }
+ return _buffer;
}
private void checkIndexBounds(int index, int size)
{
- if (index < 0 || size > _limit - index)
+ if (index < 0 || size > limit() - index)
{
throw new IndexOutOfBoundsException();
}
}
-
- private void checkOverflow(final int size)
- {
- if (_limit - _position < size)
- {
- throw new BufferOverflowException();
- }
- }
-
- private void checkUnderflow(final int size)
- {
- if (_limit - _position < size)
- {
- throw new BufferUnderflowException();
- }
- }
-
}
Modified:
qpid/java/trunk/common/src/test/java/org/apache/qpid/bytebuffer/SlicedQpidByteBufferTest.java
URL:
http://svn.apache.org/viewvc/qpid/java/trunk/common/src/test/java/org/apache/qpid/bytebuffer/SlicedQpidByteBufferTest.java?rev=1763439&r1=1763438&r2=1763439&view=diff
==============================================================================
---
qpid/java/trunk/common/src/test/java/org/apache/qpid/bytebuffer/SlicedQpidByteBufferTest.java
(original)
+++
qpid/java/trunk/common/src/test/java/org/apache/qpid/bytebuffer/SlicedQpidByteBufferTest.java
Wed Oct 5 11:56:50 2016
@@ -615,21 +615,20 @@ public class SlicedQpidByteBufferTest ex
public void testAsByteBuffer() throws Exception
{
_slicedBuffer = createSlice();
- byte[] source = getTestBytes(_slicedBuffer.remaining());
- _slicedBuffer.put(source);
_slicedBuffer.position(1);
_slicedBuffer.limit(_slicedBuffer.limit() - 1);
+ _slicedBuffer.mark();
+ int remaining = _slicedBuffer.remaining();
+ byte[] source = getTestBytes(remaining);
+ _slicedBuffer.put(source);
+ _slicedBuffer.reset();
ByteBuffer buffer = _slicedBuffer.asByteBuffer();
- assertEquals("Unexpected capacity", _slicedBuffer.capacity(),
buffer.capacity());
- assertEquals("Unexpected position", _slicedBuffer.position(),
buffer.position());
- assertEquals("Unexpected limit", _slicedBuffer.limit(),
buffer.limit());
+ assertEquals("Unexpected remaining", remaining, buffer.remaining());
- buffer.clear();
-
- byte[] target = new byte[source.length];
+ byte[] target = new byte[remaining];
buffer.get(target);
Assert.assertArrayEquals("Unexpected asByteBuffer result", source,
target);
}
@@ -675,6 +674,10 @@ public class SlicedQpidByteBufferTest ex
_slicedBuffer = _parent.slice();
_parent.limit(_parent.capacity());
+ assertEquals("Unexpected position ", 0, _slicedBuffer.position());
+ assertEquals("Unexpected limit ", size, _slicedBuffer.limit());
+ assertEquals("Unexpected capacity ", size, _slicedBuffer.capacity());
+
String methodSuffix = getMethodSuffix(primitiveTargetClass, unsigned);
Method put = _slicedBuffer.getClass().getMethod("put" + methodSuffix,
Primitives.primitiveTypeOf(value.getClass()));
Method get = _slicedBuffer.getClass().getMethod("get" + methodSuffix);
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]