@Test public void testCompact() throws Exception { DynamicByteBuffer readBuffer = DynamicByteBuffer.allocate(512); byte[] bytes = new byte[521]; DynamicByteBuffer data = DynamicByteBuffer.wrap(bytes); readBuffer.putBuffer(data); readBuffer.limit(data.position()); int numberOfBytesToBeCopiedDuringCompact = readBuffer.remaining(); readBuffer.compact(); Assert.assertEquals(numberOfBytesToBeCopiedDuringCompact, readBuffer.position()); byte[] bytes1 = new byte[10]; DynamicByteBuffer data1 = DynamicByteBuffer.wrap(bytes1); readBuffer.putBuffer(data1); readBuffer.limit(data1.position()); numberOfBytesToBeCopiedDuringCompact = readBuffer.remaining(); readBuffer.compact(); Assert.assertEquals(numberOfBytesToBeCopiedDuringCompact, readBuffer.position()); readBuffer.position(10); numberOfBytesToBeCopiedDuringCompact = readBuffer.remaining(); readBuffer.compact(); Assert.assertEquals(numberOfBytesToBeCopiedDuringCompact, readBuffer.position()); }
public DynamicByteBuffer compact() { int remaining = remaining(); int capacity = capacity(); if (capacity == 0) { return this; } if (remaining <= capacity >>> 2 && capacity > _minimumCapacity) { int newCapacity = capacity; int minCapacity = max(_minimumCapacity, remaining << 1); for (;;) { if (newCapacity >>> 1 < minCapacity) { break; } newCapacity >>>= 1; } newCapacity = max(minCapacity, newCapacity); if (newCapacity == capacity) { if (_buf.remaining() == 0) { _buf.position(0); _buf.limit(_buf.capacity()); } else { java.nio.ByteBuffer dup = _buf.duplicate(); _buf.position(0); _buf.limit(_buf.capacity()); _buf.put(dup); } return this; } ByteOrder bo = order(); if (remaining > newCapacity) { throw new IllegalStateException("The amount of the remaining bytes is greater than " + "the new capacity."); } java.nio.ByteBuffer oldBuf = _buf; java.nio.ByteBuffer newBuf = java.nio.ByteBuffer.allocate(newCapacity); newBuf.put(oldBuf); _buf = newBuf; _buf.order(bo); } else { _buf.compact(); } return this; }
DynamicByteBuffer { public DynamicByteBuffer compact() { int remaining = remaining(); int capacity = capacity(); if (capacity == 0) { return this; } if (remaining <= capacity >>> 2 && capacity > _minimumCapacity) { int newCapacity = capacity; int minCapacity = max(_minimumCapacity, remaining << 1); for (;;) { if (newCapacity >>> 1 < minCapacity) { break; } newCapacity >>>= 1; } newCapacity = max(minCapacity, newCapacity); if (newCapacity == capacity) { if (_buf.remaining() == 0) { _buf.position(0); _buf.limit(_buf.capacity()); } else { java.nio.ByteBuffer dup = _buf.duplicate(); _buf.position(0); _buf.limit(_buf.capacity()); _buf.put(dup); } return this; } ByteOrder bo = order(); if (remaining > newCapacity) { throw new IllegalStateException("The amount of the remaining bytes is greater than " + "the new capacity."); } java.nio.ByteBuffer oldBuf = _buf; java.nio.ByteBuffer newBuf = java.nio.ByteBuffer.allocate(newCapacity); newBuf.put(oldBuf); _buf = newBuf; _buf.order(bo); } else { _buf.compact(); } return this; } }
DynamicByteBuffer { public DynamicByteBuffer compact() { int remaining = remaining(); int capacity = capacity(); if (capacity == 0) { return this; } if (remaining <= capacity >>> 2 && capacity > _minimumCapacity) { int newCapacity = capacity; int minCapacity = max(_minimumCapacity, remaining << 1); for (;;) { if (newCapacity >>> 1 < minCapacity) { break; } newCapacity >>>= 1; } newCapacity = max(minCapacity, newCapacity); if (newCapacity == capacity) { if (_buf.remaining() == 0) { _buf.position(0); _buf.limit(_buf.capacity()); } else { java.nio.ByteBuffer dup = _buf.duplicate(); _buf.position(0); _buf.limit(_buf.capacity()); _buf.put(dup); } return this; } ByteOrder bo = order(); if (remaining > newCapacity) { throw new IllegalStateException("The amount of the remaining bytes is greater than " + "the new capacity."); } java.nio.ByteBuffer oldBuf = _buf; java.nio.ByteBuffer newBuf = java.nio.ByteBuffer.allocate(newCapacity); newBuf.put(oldBuf); _buf = newBuf; _buf.order(bo); } else { _buf.compact(); } return this; } DynamicByteBuffer();   DynamicByteBuffer(int capacity);  DynamicByteBuffer(byte[] bytes);   DynamicByteBuffer(byte[] bytes, int offset, int length);  DynamicByteBuffer(java.nio.ByteBuffer buf); }
DynamicByteBuffer { public DynamicByteBuffer compact() { int remaining = remaining(); int capacity = capacity(); if (capacity == 0) { return this; } if (remaining <= capacity >>> 2 && capacity > _minimumCapacity) { int newCapacity = capacity; int minCapacity = max(_minimumCapacity, remaining << 1); for (;;) { if (newCapacity >>> 1 < minCapacity) { break; } newCapacity >>>= 1; } newCapacity = max(minCapacity, newCapacity); if (newCapacity == capacity) { if (_buf.remaining() == 0) { _buf.position(0); _buf.limit(_buf.capacity()); } else { java.nio.ByteBuffer dup = _buf.duplicate(); _buf.position(0); _buf.limit(_buf.capacity()); _buf.put(dup); } return this; } ByteOrder bo = order(); if (remaining > newCapacity) { throw new IllegalStateException("The amount of the remaining bytes is greater than " + "the new capacity."); } java.nio.ByteBuffer oldBuf = _buf; java.nio.ByteBuffer newBuf = java.nio.ByteBuffer.allocate(newCapacity); newBuf.put(oldBuf); _buf = newBuf; _buf.order(bo); } else { _buf.compact(); } return this; } DynamicByteBuffer();   DynamicByteBuffer(int capacity);  DynamicByteBuffer(byte[] bytes);   DynamicByteBuffer(byte[] bytes, int offset, int length);  DynamicByteBuffer(java.nio.ByteBuffer buf); java.nio.ByteBuffer getNioByteBuffer(); final DynamicByteBuffer setAutoExpand(boolean autoExpand); final boolean isAutoExpand(); final int arrayOffset(); final byte[] array(); static DynamicByteBuffer allocate(int capacity); static DynamicByteBuffer wrap(byte[] bytes); static DynamicByteBuffer wrap(byte[] bytes, int offset, int length); static DynamicByteBuffer wrap(java.nio.ByteBuffer in); ByteOrder order(); @Deprecated ByteOrder order(ByteOrder o); DynamicByteBuffer mark(); DynamicByteBuffer reset(); DynamicByteBuffer compact(); DynamicByteBuffer duplicate(); DynamicByteBuffer fill(int size); DynamicByteBuffer fillWith(byte b, int size); int indexOf(byte b); DynamicByteBuffer put(byte v); DynamicByteBuffer put(byte[] v, int offset, int length); DynamicByteBuffer putAt(int index, byte v); DynamicByteBuffer putUnsigned(int v); DynamicByteBuffer putUnsignedAt(int index, int v); DynamicByteBuffer putShort(short v); DynamicByteBuffer putShortAt(int index, short v); DynamicByteBuffer putUnsignedShort(int v); DynamicByteBuffer putUnsignedShortAt(int index, int v); DynamicByteBuffer putInt(int v); DynamicByteBuffer putIntAt(int index, int v); DynamicByteBuffer putUnsignedInt(long value); DynamicByteBuffer putUnsignedIntAt(int index, long value); DynamicByteBuffer putLong(long v); DynamicByteBuffer putLongAt(int index, long v); DynamicByteBuffer putString(String v, Charset cs); DynamicByteBuffer putPrefixedString(int fieldSize, String v, Charset cs); DynamicByteBuffer putBytes(byte[] b); DynamicByteBuffer putBytesAt(int index, byte[] b); DynamicByteBuffer putBuffer(DynamicByteBuffer v); DynamicByteBuffer putBufferAt(int index, DynamicByteBuffer v); byte get(); DynamicByteBuffer get(byte[] dst, int offset, int length); DynamicByteBuffer get(byte[] dst); byte getAt(int index); int getUnsigned(); int getUnsignedAt(int index); byte[] getBytes(int size); byte[] getBytesAt(int index, int size); short getShort(); short getShortAt(int index); int getUnsignedShort(); int getUnsignedShortAt(int index); int getUnsignedMediumInt(); int getInt(); int getIntAt(int index); long getUnsignedInt(); long getUnsignedIntAt(int index); long getLong(); long getLongAt(int index); String getPrefixedString(int fieldSize, Charset cs); String getString(Charset cs); DynamicByteBuffer slice(); DynamicByteBuffer flip(); DynamicByteBuffer rewind(); DynamicByteBuffer clear(); int remaining(); int capacity(); int position(); DynamicByteBuffer position(int position); int limit(); DynamicByteBuffer limit(int limit); boolean hasRemaining(); DynamicByteBuffer skip(int size); String getHexDump(); @Override String toString(); @Override boolean equals(Object obj); @Override int hashCode(); }
DynamicByteBuffer { public DynamicByteBuffer compact() { int remaining = remaining(); int capacity = capacity(); if (capacity == 0) { return this; } if (remaining <= capacity >>> 2 && capacity > _minimumCapacity) { int newCapacity = capacity; int minCapacity = max(_minimumCapacity, remaining << 1); for (;;) { if (newCapacity >>> 1 < minCapacity) { break; } newCapacity >>>= 1; } newCapacity = max(minCapacity, newCapacity); if (newCapacity == capacity) { if (_buf.remaining() == 0) { _buf.position(0); _buf.limit(_buf.capacity()); } else { java.nio.ByteBuffer dup = _buf.duplicate(); _buf.position(0); _buf.limit(_buf.capacity()); _buf.put(dup); } return this; } ByteOrder bo = order(); if (remaining > newCapacity) { throw new IllegalStateException("The amount of the remaining bytes is greater than " + "the new capacity."); } java.nio.ByteBuffer oldBuf = _buf; java.nio.ByteBuffer newBuf = java.nio.ByteBuffer.allocate(newCapacity); newBuf.put(oldBuf); _buf = newBuf; _buf.order(bo); } else { _buf.compact(); } return this; } DynamicByteBuffer();   DynamicByteBuffer(int capacity);  DynamicByteBuffer(byte[] bytes);   DynamicByteBuffer(byte[] bytes, int offset, int length);  DynamicByteBuffer(java.nio.ByteBuffer buf); java.nio.ByteBuffer getNioByteBuffer(); final DynamicByteBuffer setAutoExpand(boolean autoExpand); final boolean isAutoExpand(); final int arrayOffset(); final byte[] array(); static DynamicByteBuffer allocate(int capacity); static DynamicByteBuffer wrap(byte[] bytes); static DynamicByteBuffer wrap(byte[] bytes, int offset, int length); static DynamicByteBuffer wrap(java.nio.ByteBuffer in); ByteOrder order(); @Deprecated ByteOrder order(ByteOrder o); DynamicByteBuffer mark(); DynamicByteBuffer reset(); DynamicByteBuffer compact(); DynamicByteBuffer duplicate(); DynamicByteBuffer fill(int size); DynamicByteBuffer fillWith(byte b, int size); int indexOf(byte b); DynamicByteBuffer put(byte v); DynamicByteBuffer put(byte[] v, int offset, int length); DynamicByteBuffer putAt(int index, byte v); DynamicByteBuffer putUnsigned(int v); DynamicByteBuffer putUnsignedAt(int index, int v); DynamicByteBuffer putShort(short v); DynamicByteBuffer putShortAt(int index, short v); DynamicByteBuffer putUnsignedShort(int v); DynamicByteBuffer putUnsignedShortAt(int index, int v); DynamicByteBuffer putInt(int v); DynamicByteBuffer putIntAt(int index, int v); DynamicByteBuffer putUnsignedInt(long value); DynamicByteBuffer putUnsignedIntAt(int index, long value); DynamicByteBuffer putLong(long v); DynamicByteBuffer putLongAt(int index, long v); DynamicByteBuffer putString(String v, Charset cs); DynamicByteBuffer putPrefixedString(int fieldSize, String v, Charset cs); DynamicByteBuffer putBytes(byte[] b); DynamicByteBuffer putBytesAt(int index, byte[] b); DynamicByteBuffer putBuffer(DynamicByteBuffer v); DynamicByteBuffer putBufferAt(int index, DynamicByteBuffer v); byte get(); DynamicByteBuffer get(byte[] dst, int offset, int length); DynamicByteBuffer get(byte[] dst); byte getAt(int index); int getUnsigned(); int getUnsignedAt(int index); byte[] getBytes(int size); byte[] getBytesAt(int index, int size); short getShort(); short getShortAt(int index); int getUnsignedShort(); int getUnsignedShortAt(int index); int getUnsignedMediumInt(); int getInt(); int getIntAt(int index); long getUnsignedInt(); long getUnsignedIntAt(int index); long getLong(); long getLongAt(int index); String getPrefixedString(int fieldSize, Charset cs); String getString(Charset cs); DynamicByteBuffer slice(); DynamicByteBuffer flip(); DynamicByteBuffer rewind(); DynamicByteBuffer clear(); int remaining(); int capacity(); int position(); DynamicByteBuffer position(int position); int limit(); DynamicByteBuffer limit(int limit); boolean hasRemaining(); DynamicByteBuffer skip(int size); String getHexDump(); @Override String toString(); @Override boolean equals(Object obj); @Override int hashCode();  }