Update ByteCursor method names and related javadocs

This commit is contained in:
Chris Vest 2020-12-07 14:56:03 +01:00
parent b2d0231f27
commit a7701c04b5
6 changed files with 168 additions and 170 deletions

View File

@ -295,27 +295,27 @@ public interface Buf extends Rc<Buf>, BufAccessors {
}
/**
* Iterate the readable bytes of this buffer. The {@linkplain #readerOffset() reader offset} and
* {@linkplain #writerOffset() witer offset} are not modified by the iterator.
* Open a cursor to iterate the readable bytes of this buffer. The {@linkplain #readerOffset() reader offset} and
* {@linkplain #writerOffset() witer offset} are not modified by the cursor.
* <p>
* Care should be taken to ensure that the buffers lifetime extends beyond the iteration, and the
* {@linkplain #readerOffset() reader offset} and {@linkplain #writerOffset() writer offset} are not modified while
* the iteration takes place. Otherwise unpredictable behaviour might result.
* Care should be taken to ensure that the buffers lifetime extends beyond the cursor and the iteration, and that
* the {@linkplain #readerOffset() reader offset} and {@linkplain #writerOffset() writer offset} are not modified
* while the iteration takes place. Otherwise unpredictable behaviour might result.
*
* @return A {@link ByteCursor} for the readable bytes of this buffer.
* @return A {@link ByteCursor} for iterating the readable bytes of this buffer.
*/
default ByteCursor iterate() {
return iterate(readerOffset(), readableBytes());
default ByteCursor openCursor() {
return openCursor(readerOffset(), readableBytes());
}
/**
* Iterate the given number bytes of this buffer, starting at the given offset.
* Open a cursor to iterate the given number bytes of this buffer, starting at the given offset.
* The {@linkplain #readerOffset() reader offset} and {@linkplain #writerOffset() witer offset} are not modified by
* the iterator.
* the cursor.
* <p>
* Care should be taken to ensure that the buffers lifetime extends beyond the iteration, and the
* {@linkplain #readerOffset() reader offset} and {@linkplain #writerOffset() writer offset} are not modified while
* the iteration takes place. Otherwise unpredictable behaviour might result.
* Care should be taken to ensure that the buffers lifetime extends beyond the cursor and the iteration, and that
* the {@linkplain #readerOffset() reader offset} and {@linkplain #writerOffset() writer offset} are not modified
* while the iteration takes place. Otherwise unpredictable behaviour might result.
*
* @param fromOffset The offset into the buffer where iteration should start.
* The first byte read from the iterator will be the byte at this offset.
@ -324,31 +324,32 @@ public interface Buf extends Rc<Buf>, BufAccessors {
* @throws IllegalArgumentException if the length is negative, or if the region given by the {@code fromOffset} and
* the {@code length} reaches outside of the bounds of this buffer.
*/
ByteCursor iterate(int fromOffset, int length);
ByteCursor openCursor(int fromOffset, int length);
/**
* Iterate the readable bytes of this buffer, in reverse. The {@linkplain #readerOffset() reader offset} and
* {@linkplain #writerOffset() witer offset} are not modified by the iterator.
* Open a cursor to iterate the readable bytes of this buffer, in reverse.
* The {@linkplain #readerOffset() reader offset} and {@linkplain #writerOffset() witer offset} are not modified by
* the cursor.
* <p>
* Care should be taken to ensure that the buffers lifetime extends beyond the iteration, and the
* {@linkplain #readerOffset() reader offset} and {@linkplain #writerOffset() writer offset} are not modified while
* the iteration takes place. Otherwise unpredictable behaviour might result.
* Care should be taken to ensure that the buffers lifetime extends beyond the cursor and the iteration, and that
* the {@linkplain #readerOffset() reader offset} and {@linkplain #writerOffset() writer offset} are not modified
* while the iteration takes place. Otherwise unpredictable behaviour might result.
*
* @return A {@link ByteCursor} for the readable bytes of this buffer.
*/
default ByteCursor iterateReverse() {
default ByteCursor openReverseCursor() {
int woff = writerOffset();
return iterateReverse(woff == 0? 0 : woff - 1, readableBytes());
return openReverseCursor(woff == 0? 0 : woff - 1, readableBytes());
}
/**
* Iterate the given number bytes of this buffer, in reverse, starting at the given offset.
* Open a cursor to iterate the given number bytes of this buffer, in reverse, starting at the given offset.
* The {@linkplain #readerOffset() reader offset} and {@linkplain #writerOffset() witer offset} are not modified by
* the iterator.
* the cursor.
* <p>
* Care should be taken to ensure that the buffers lifetime extends beyond the iteration, and the
* {@linkplain #readerOffset() reader offset} and {@linkplain #writerOffset() writer offset} are not modified while
* the iteration takes place. Otherwise unpredictable behaviour might result.
* Care should be taken to ensure that the buffers lifetime extends beyond the cursor and the iteration, and that
* the {@linkplain #readerOffset() reader offset} and {@linkplain #writerOffset() writer offset} are not modified
* while the iteration takes place. Otherwise unpredictable behaviour might result.
*
* @param fromOffset The offset into the buffer where iteration should start.
* The first byte read from the iterator will be the byte at this offset.
@ -357,7 +358,7 @@ public interface Buf extends Rc<Buf>, BufAccessors {
* @throws IllegalArgumentException if the length is negative, or if the region given by the {@code fromOffset} and
* the {@code length} reaches outside of the bounds of this buffer.
*/
ByteCursor iterateReverse(int fromOffset, int length);
ByteCursor openReverseCursor(int fromOffset, int length);
/**
* Ensure that this buffer has {@linkplain #writableBytes() available space for writing} the given number of

View File

@ -29,10 +29,10 @@ public interface ByteCursor {
* Check if the iterator has at least 8 bytes left, and if so, read those 8 bytes and move the cursor forward.
* The bytes are packed as a {@code long} value in big-endian format, such that the highest-order byte
* in the long, is the byte that would otherwise have been returned by the next call to {@link #getByte()},
* after a call to {@link #nextByte()}.
* after a call to {@link #readByte()}.
* The bytes (as a {@code long}) will then be available through the {@link #getLong()} method.
* <p>
* Note that when this method returns {@code false}, the {@link #nextByte()} can still return {@code true}.
* Note that when this method returns {@code false}, the {@link #readByte()} can still return {@code true}.
* It is recommended to have any long-processing loop be followed by a byte-processing loop for the 7 or fewer
* bytes that might form a tail in the cursor.
* <p>
@ -40,14 +40,14 @@ public interface ByteCursor {
*
* @return {@code true} if the cursor read 8 bytes and moved forward, otherwise {@code false}.
*/
boolean nextLong();
boolean readLong();
/**
* Return the last 8 bytes read by {@link #nextLong()}.
* If {@link #nextLong()} has not been called on this cursor before, then {@code -1} is returned.
* Return the last 8 bytes read by {@link #readLong()}.
* If {@link #readLong()} has not been called on this cursor before, then {@code -1} is returned.
*
* @return The 8 bytes, in big-endian format, that was read by the most recent successful call to
* {@link #nextLong()}.
* {@link #readLong()}.
*/
long getLong();
@ -59,13 +59,13 @@ public interface ByteCursor {
*
* @return {@code true} if the cursor read a byte and moved forward, otherwise {@code false}.
*/
boolean nextByte();
boolean readByte();
/**
* Return the last byte that was read by {@link #nextByte()}.
* If {@link #nextByte()} has not been called on this cursor before, then {@code -1} is returned.
* Return the last byte that was read by {@link #readByte()}.
* If {@link #readByte()} has not been called on this cursor before, then {@code -1} is returned.
*
* @return The next byte that was read by the most recent successful call to {@link #nextByte()}.
* @return The next byte that was read by the most recent successful call to {@link #readByte()}.
*/
byte getByte();
@ -95,7 +95,7 @@ public interface ByteCursor {
default int process(ByteProcessor processor) {
boolean requestMore = true;
int count = 0;
while (nextByte() && (requestMore = processor.process(getByte()))) {
while (readByte() && (requestMore = processor.process(getByte()))) {
count++;
}
return requestMore? -1 : count;

View File

@ -18,7 +18,6 @@ package io.netty.buffer.api;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Objects;
final class CompositeBuf extends RcSupport<Buf, CompositeBuf> implements Buf {
@ -293,16 +292,16 @@ final class CompositeBuf extends RcSupport<Buf, CompositeBuf> implements Buf {
// Iterate in reverse to account for src and dest buffer overlap.
// todo optimise by delegating to constituent buffers.
var cursor = iterateReverse(srcPos + length - 1, length);
var cursor = openReverseCursor(srcPos + length - 1, length);
ByteOrder prevOrder = dest.order();
// We read longs in BE, in reverse, so they need to be flipped for writing.
dest.order(ByteOrder.LITTLE_ENDIAN);
try {
while (cursor.nextLong()) {
while (cursor.readLong()) {
length -= Long.BYTES;
dest.setLong(destPos + length, cursor.getLong());
}
while (cursor.nextByte()) {
while (cursor.readByte()) {
dest.setByte(destPos + --length, cursor.getByte());
}
} finally {
@ -311,7 +310,7 @@ final class CompositeBuf extends RcSupport<Buf, CompositeBuf> implements Buf {
}
@Override
public ByteCursor iterate(int fromOffset, int length) {
public ByteCursor openCursor(int fromOffset, int length) {
if (fromOffset < 0) {
throw new IllegalArgumentException("The fromOffset cannot be negative: " + fromOffset + '.');
}
@ -325,7 +324,7 @@ final class CompositeBuf extends RcSupport<Buf, CompositeBuf> implements Buf {
int startBufferIndex = searchOffsets(fromOffset);
int off = fromOffset - offsets[startBufferIndex];
Buf startBuf = bufs[startBufferIndex];
ByteCursor startCursor = startBuf.iterate(off, Math.min(startBuf.capacity() - off, length));
ByteCursor startCursor = startBuf.openCursor(off, Math.min(startBuf.capacity() - off, length));
return new ByteCursor() {
int index = fromOffset;
final int end = fromOffset + length;
@ -335,13 +334,13 @@ final class CompositeBuf extends RcSupport<Buf, CompositeBuf> implements Buf {
byte byteValue = -1;
@Override
public boolean nextLong() {
public boolean readLong() {
if (bytesLeft() >= Long.BYTES) {
if (cursor.nextLong()) {
if (cursor.readLong()) {
longValue = cursor.getLong();
index += Long.BYTES;
} else {
longValue = nextLongFromBytes(); // Leave index increments to 'nextByte'
longValue = nextLongFromBytes(); // Leave index increments to 'readByte'
}
return true;
}
@ -351,7 +350,7 @@ final class CompositeBuf extends RcSupport<Buf, CompositeBuf> implements Buf {
private long nextLongFromBytes() {
long val = 0;
for (int i = 0; i < 8; i++) {
nextByte();
readByte();
val <<= 8;
val |= getByte();
}
@ -364,13 +363,13 @@ final class CompositeBuf extends RcSupport<Buf, CompositeBuf> implements Buf {
}
@Override
public boolean nextByte() {
public boolean readByte() {
if (index < end) {
if (!cursor.nextByte()) {
if (!cursor.readByte()) {
bufferIndex++;
Buf nextBuf = bufs[bufferIndex];
cursor = nextBuf.iterate(0, Math.min(nextBuf.capacity(), bytesLeft()));
cursor.nextByte();
cursor = nextBuf.openCursor(0, Math.min(nextBuf.capacity(), bytesLeft()));
cursor.readByte();
}
byteValue = cursor.getByte();
index++;
@ -397,7 +396,7 @@ final class CompositeBuf extends RcSupport<Buf, CompositeBuf> implements Buf {
}
@Override
public ByteCursor iterateReverse(int fromOffset, int length) {
public ByteCursor openReverseCursor(int fromOffset, int length) {
if (fromOffset < 0) {
throw new IllegalArgumentException("The fromOffset cannot be negative: " + fromOffset + '.');
}
@ -411,7 +410,7 @@ final class CompositeBuf extends RcSupport<Buf, CompositeBuf> implements Buf {
int startBufferIndex = searchOffsets(fromOffset);
int off = fromOffset - offsets[startBufferIndex];
Buf startBuf = bufs[startBufferIndex];
ByteCursor startCursor = startBuf.iterateReverse(off, Math.min(off + 1, length));
ByteCursor startCursor = startBuf.openReverseCursor(off, Math.min(off + 1, length));
return new ByteCursor() {
int index = fromOffset;
final int end = fromOffset - length;
@ -421,13 +420,13 @@ final class CompositeBuf extends RcSupport<Buf, CompositeBuf> implements Buf {
byte byteValue = -1;
@Override
public boolean nextLong() {
public boolean readLong() {
if (bytesLeft() >= Long.BYTES) {
if (cursor.nextLong()) {
if (cursor.readLong()) {
index -= Long.BYTES;
longValue = cursor.getLong();
} else {
longValue = nextLongFromBytes(); // Leave index increments to 'nextByte'
longValue = nextLongFromBytes(); // Leave index increments to 'readByte'
}
return true;
}
@ -437,7 +436,7 @@ final class CompositeBuf extends RcSupport<Buf, CompositeBuf> implements Buf {
private long nextLongFromBytes() {
long val = 0;
for (int i = 0; i < 8; i++) {
nextByte();
readByte();
val <<= 8;
val |= getByte();
}
@ -450,14 +449,14 @@ final class CompositeBuf extends RcSupport<Buf, CompositeBuf> implements Buf {
}
@Override
public boolean nextByte() {
public boolean readByte() {
if (index > end) {
if (!cursor.nextByte()) {
if (!cursor.readByte()) {
bufferIndex--;
Buf nextBuf = bufs[bufferIndex];
int length = Math.min(nextBuf.capacity(), bytesLeft());
cursor = nextBuf.iterateReverse(nextBuf.capacity() - 1, length);
cursor.nextByte();
cursor = nextBuf.openReverseCursor(nextBuf.capacity() - 1, length);
cursor.readByte();
}
byteValue = cursor.getByte();
index--;

View File

@ -26,7 +26,6 @@ import jdk.incubator.foreign.MemorySegment;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.NoSuchElementException;
import static jdk.incubator.foreign.MemoryAccess.getByteAtOffset;
import static jdk.incubator.foreign.MemoryAccess.getCharAtOffset;
@ -158,17 +157,17 @@ class MemSegBuf extends RcSupport<Buf, MemSegBuf> implements Buf {
public void copyInto(int srcPos, Buf dest, int destPos, int length) {
// todo optimise: specialise for MemSegBuf.
// Iterate in reverse to account for src and dest buffer overlap.
var itr = iterateReverse(srcPos + length - 1, length);
var itr = openReverseCursor(srcPos + length - 1, length);
ByteOrder prevOrder = dest.order();
// We read longs in BE, in reverse, so they need to be flipped for writing.
dest.order(ByteOrder.LITTLE_ENDIAN);
try {
while (itr.nextLong()) {
while (itr.readLong()) {
long val = itr.getLong();
length -= Long.BYTES;
dest.setLong(destPos + length, val);
}
while (itr.nextByte()) {
while (itr.readByte()) {
dest.setByte(destPos + --length, itr.getByte());
}
} finally {
@ -177,7 +176,7 @@ class MemSegBuf extends RcSupport<Buf, MemSegBuf> implements Buf {
}
@Override
public ByteCursor iterate(int fromOffset, int length) {
public ByteCursor openCursor(int fromOffset, int length) {
if (fromOffset < 0) {
throw new IllegalArgumentException("The fromOffset cannot be negative: " + fromOffset + '.');
}
@ -196,7 +195,7 @@ class MemSegBuf extends RcSupport<Buf, MemSegBuf> implements Buf {
byte byteValue = -1;
@Override
public boolean nextLong() {
public boolean readLong() {
if (index + Long.BYTES <= end) {
longValue = getLongAtOffset(segment, index, ByteOrder.BIG_ENDIAN);
index += Long.BYTES;
@ -211,7 +210,7 @@ class MemSegBuf extends RcSupport<Buf, MemSegBuf> implements Buf {
}
@Override
public boolean nextByte() {
public boolean readByte() {
if (index < end) {
byteValue = getByteAtOffset(segment, index);
index++;
@ -238,7 +237,7 @@ class MemSegBuf extends RcSupport<Buf, MemSegBuf> implements Buf {
}
@Override
public ByteCursor iterateReverse(int fromOffset, int length) {
public ByteCursor openReverseCursor(int fromOffset, int length) {
if (fromOffset < 0) {
throw new IllegalArgumentException("The fromOffset cannot be negative: " + fromOffset + '.');
}
@ -260,7 +259,7 @@ class MemSegBuf extends RcSupport<Buf, MemSegBuf> implements Buf {
byte byteValue = -1;
@Override
public boolean nextLong() {
public boolean readLong() {
if (index - Long.BYTES >= end) {
index -= 7;
longValue = getLongAtOffset(segment, index, ByteOrder.LITTLE_ENDIAN);
@ -276,7 +275,7 @@ class MemSegBuf extends RcSupport<Buf, MemSegBuf> implements Buf {
}
@Override
public boolean nextByte() {
public boolean readByte() {
if (index > end) {
byteValue = getByteAtOffset(segment, index);
index--;

View File

@ -32,7 +32,6 @@ import java.nio.ByteOrder;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -1051,9 +1050,9 @@ public class BufTest {
}
private static void checkByteIteration(Buf buf) {
var cursor = buf.iterate();
assertFalse(cursor.nextByte());
assertFalse(cursor.nextLong());
var cursor = buf.openCursor();
assertFalse(cursor.readByte());
assertFalse(cursor.readLong());
assertEquals(0, cursor.bytesLeft());
assertEquals((byte) -1, cursor.getByte());
assertEquals(-1L, cursor.getLong());
@ -1063,64 +1062,64 @@ public class BufTest {
}
int roff = buf.readerOffset();
int woff = buf.writerOffset();
cursor = buf.iterate();
cursor = buf.openCursor();
assertEquals(0x27, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x01, cursor.getByte());
assertEquals((byte) 0x01, cursor.getByte());
assertTrue(cursor.nextLong());
assertTrue(cursor.readLong());
assertEquals(0x0203040506070809L, cursor.getLong());
assertEquals(0x0203040506070809L, cursor.getLong());
assertEquals(0x1E, cursor.bytesLeft());
assertTrue(cursor.nextLong());
assertTrue(cursor.readLong());
assertEquals(0x0A0B0C0D0E0F1011L, cursor.getLong());
assertEquals(0x16, cursor.bytesLeft());
assertTrue(cursor.nextLong());
assertTrue(cursor.readLong());
assertEquals(0x1213141516171819L, cursor.getLong());
assertEquals(0x0E, cursor.bytesLeft());
assertTrue(cursor.nextLong());
assertTrue(cursor.readLong());
assertEquals(0x1A1B1C1D1E1F2021L, cursor.getLong());
assertEquals(6, cursor.bytesLeft());
assertFalse(cursor.nextLong());
assertFalse(cursor.readLong());
assertEquals(6, cursor.bytesLeft());
assertEquals(0x1A1B1C1D1E1F2021L, cursor.getLong());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x22, cursor.getByte());
assertEquals((byte) 0x22, cursor.getByte());
assertEquals(5, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x23, cursor.getByte());
assertEquals(4, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x24, cursor.getByte());
assertEquals(3, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x25, cursor.getByte());
assertEquals(2, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x26, cursor.getByte());
assertEquals(1, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x27, cursor.getByte());
assertEquals(0, cursor.bytesLeft());
assertFalse(cursor.nextByte());
assertFalse(cursor.readByte());
assertEquals((byte) 0x27, cursor.getByte());
assertEquals((byte) 0x27, cursor.getByte());
assertFalse(cursor.nextLong());
assertFalse(cursor.readLong());
assertEquals(roff, buf.readerOffset());
assertEquals(woff, buf.writerOffset());
}
private static void checkByteIterationOfRegion(Buf buf) {
assertThrows(IllegalArgumentException.class, () -> buf.iterate(-1, 1));
assertThrows(IllegalArgumentException.class, () -> buf.iterate(1, -1));
assertThrows(IllegalArgumentException.class, () -> buf.iterate(buf.capacity(), 1));
assertThrows(IllegalArgumentException.class, () -> buf.iterate(buf.capacity() - 1, 2));
assertThrows(IllegalArgumentException.class, () -> buf.iterate(buf.capacity() - 2, 3));
assertThrows(IllegalArgumentException.class, () -> buf.openCursor(-1, 1));
assertThrows(IllegalArgumentException.class, () -> buf.openCursor(1, -1));
assertThrows(IllegalArgumentException.class, () -> buf.openCursor(buf.capacity(), 1));
assertThrows(IllegalArgumentException.class, () -> buf.openCursor(buf.capacity() - 1, 2));
assertThrows(IllegalArgumentException.class, () -> buf.openCursor(buf.capacity() - 2, 3));
var cursor = buf.iterate(1, 0);
assertFalse(cursor.nextByte());
assertFalse(cursor.nextLong());
var cursor = buf.openCursor(1, 0);
assertFalse(cursor.readByte());
assertFalse(cursor.readLong());
assertEquals(0, cursor.bytesLeft());
assertEquals((byte) -1, cursor.getByte());
assertEquals(-1L, cursor.getLong());
@ -1130,55 +1129,55 @@ public class BufTest {
}
int roff = buf.readerOffset();
int woff = buf.writerOffset();
cursor = buf.iterate(buf.readerOffset() + 1, buf.readableBytes() - 2);
cursor = buf.openCursor(buf.readerOffset() + 1, buf.readableBytes() - 2);
assertEquals(0x25, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x02, cursor.getByte());
assertEquals((byte) 0x02, cursor.getByte());
assertTrue(cursor.nextLong());
assertTrue(cursor.readLong());
assertEquals(0x030405060708090AL, cursor.getLong());
assertEquals(0x1C, cursor.bytesLeft());
assertTrue(cursor.nextLong());
assertTrue(cursor.readLong());
assertEquals(0x0B0C0D0E0F101112L, cursor.getLong());
assertEquals(0x14, cursor.bytesLeft());
assertTrue(cursor.nextLong());
assertTrue(cursor.readLong());
assertEquals(0x131415161718191AL, cursor.getLong());
assertEquals(0x0C, cursor.bytesLeft());
assertTrue(cursor.nextLong());
assertTrue(cursor.readLong());
assertEquals(0x1B1C1D1E1F202122L, cursor.getLong());
assertEquals(4, cursor.bytesLeft());
assertFalse(cursor.nextLong());
assertFalse(cursor.readLong());
assertEquals(4, cursor.bytesLeft());
assertEquals(0x1B1C1D1E1F202122L, cursor.getLong());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x23, cursor.getByte());
assertEquals(3, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x24, cursor.getByte());
assertEquals(2, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x25, cursor.getByte());
assertEquals(1, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x26, cursor.getByte());
assertEquals(0, cursor.bytesLeft());
assertFalse(cursor.nextByte());
assertFalse(cursor.readByte());
assertEquals(0, cursor.bytesLeft());
assertEquals(0x1B1C1D1E1F202122L, cursor.getLong());
assertEquals((byte) 0x26, cursor.getByte());
cursor = buf.iterate(buf.readerOffset() + 1, 2);
cursor = buf.openCursor(buf.readerOffset() + 1, 2);
assertEquals(2, cursor.bytesLeft());
assertFalse(cursor.nextLong());
assertTrue(cursor.nextByte());
assertFalse(cursor.readLong());
assertTrue(cursor.readByte());
assertEquals((byte) 0x02, cursor.getByte());
assertEquals(1, cursor.bytesLeft());
assertFalse(cursor.nextLong());
assertTrue(cursor.nextByte());
assertFalse(cursor.readLong());
assertTrue(cursor.readByte());
assertEquals((byte) 0x03, cursor.getByte());
assertEquals(0, cursor.bytesLeft());
assertFalse(cursor.nextByte());
assertFalse(cursor.nextLong());
assertFalse(cursor.readByte());
assertFalse(cursor.readLong());
assertEquals(roff, buf.readerOffset());
assertEquals(woff, buf.writerOffset());
}
@ -1208,9 +1207,9 @@ public class BufTest {
}
private static void checkReverseByteIteration(Buf buf) {
var cursor = buf.iterateReverse();
assertFalse(cursor.nextByte());
assertFalse(cursor.nextLong());
var cursor = buf.openReverseCursor();
assertFalse(cursor.readByte());
assertFalse(cursor.readLong());
assertEquals(0, cursor.bytesLeft());
assertEquals((byte) -1, cursor.getByte());
assertEquals(-1L, cursor.getLong());
@ -1220,47 +1219,47 @@ public class BufTest {
}
int roff = buf.readerOffset();
int woff = buf.writerOffset();
cursor = buf.iterateReverse();
cursor = buf.openReverseCursor();
assertEquals(0x27, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x27, cursor.getByte());
assertEquals((byte) 0x27, cursor.getByte());
assertTrue(cursor.nextLong());
assertTrue(cursor.readLong());
assertEquals(0x262524232221201FL, cursor.getLong());
assertEquals(0x1E, cursor.bytesLeft());
assertTrue(cursor.nextLong());
assertTrue(cursor.readLong());
assertEquals(0x1E1D1C1B1A191817L, cursor.getLong());
assertEquals(0x16, cursor.bytesLeft());
assertTrue(cursor.nextLong());
assertTrue(cursor.readLong());
assertEquals(0x161514131211100FL, cursor.getLong());
assertEquals(0x0E, cursor.bytesLeft());
assertTrue(cursor.nextLong());
assertTrue(cursor.readLong());
assertEquals(0x0E0D0C0B0A090807L, cursor.getLong());
assertEquals(6, cursor.bytesLeft());
assertFalse(cursor.nextLong());
assertFalse(cursor.readLong());
assertEquals(6, cursor.bytesLeft());
assertEquals(0x0E0D0C0B0A090807L, cursor.getLong());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x06, cursor.getByte());
assertEquals(5, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x05, cursor.getByte());
assertEquals(4, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x04, cursor.getByte());
assertEquals(3, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x03, cursor.getByte());
assertEquals(2, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x02, cursor.getByte());
assertEquals(1, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x01, cursor.getByte());
assertEquals(0, cursor.bytesLeft());
assertFalse(cursor.nextByte());
assertFalse(cursor.readByte());
assertEquals((byte) 0x01, cursor.getByte());
assertFalse(cursor.nextByte());
assertFalse(cursor.readByte());
assertEquals(0, cursor.bytesLeft());
assertEquals(0x0E0D0C0B0A090807L, cursor.getLong());
assertEquals(roff, buf.readerOffset());
@ -1268,15 +1267,15 @@ public class BufTest {
}
private static void checkReverseByteIterationOfRegion(Buf buf) {
assertThrows(IllegalArgumentException.class, () -> buf.iterateReverse(-1, 0));
assertThrows(IllegalArgumentException.class, () -> buf.iterateReverse(0, -1));
assertThrows(IllegalArgumentException.class, () -> buf.iterateReverse(0, 2));
assertThrows(IllegalArgumentException.class, () -> buf.iterateReverse(1, 3));
assertThrows(IllegalArgumentException.class, () -> buf.iterateReverse(buf.capacity(), 0));
assertThrows(IllegalArgumentException.class, () -> buf.openReverseCursor(-1, 0));
assertThrows(IllegalArgumentException.class, () -> buf.openReverseCursor(0, -1));
assertThrows(IllegalArgumentException.class, () -> buf.openReverseCursor(0, 2));
assertThrows(IllegalArgumentException.class, () -> buf.openReverseCursor(1, 3));
assertThrows(IllegalArgumentException.class, () -> buf.openReverseCursor(buf.capacity(), 0));
var cursor = buf.iterateReverse(1, 0);
assertFalse(cursor.nextByte());
assertFalse(cursor.nextLong());
var cursor = buf.openReverseCursor(1, 0);
assertFalse(cursor.readByte());
assertFalse(cursor.readLong());
assertEquals(0, cursor.bytesLeft());
assertEquals((byte) -1, cursor.getByte());
assertEquals(-1L, cursor.getLong());
@ -1286,56 +1285,56 @@ public class BufTest {
}
int roff = buf.readerOffset();
int woff = buf.writerOffset();
cursor = buf.iterateReverse(buf.writerOffset() - 2, buf.readableBytes() - 2);
cursor = buf.openReverseCursor(buf.writerOffset() - 2, buf.readableBytes() - 2);
assertEquals(0x25, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x26, cursor.getByte());
assertEquals((byte) 0x26, cursor.getByte());
assertTrue(cursor.nextLong());
assertTrue(cursor.readLong());
assertEquals(0x2524232221201F1EL, cursor.getLong());
assertEquals(0x1C, cursor.bytesLeft());
assertTrue(cursor.nextLong());
assertTrue(cursor.readLong());
assertEquals(0x1D1C1B1A19181716L, cursor.getLong());
assertEquals(0x14, cursor.bytesLeft());
assertTrue(cursor.nextLong());
assertTrue(cursor.readLong());
assertEquals(0x1514131211100F0EL, cursor.getLong());
assertEquals(0x0C, cursor.bytesLeft());
assertTrue(cursor.nextLong());
assertTrue(cursor.readLong());
assertEquals(0x0D0C0B0A09080706L, cursor.getLong());
assertEquals(4, cursor.bytesLeft());
assertFalse(cursor.nextLong());
assertFalse(cursor.readLong());
assertEquals(4, cursor.bytesLeft());
assertEquals(0x0D0C0B0A09080706L, cursor.getLong());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x05, cursor.getByte());
assertEquals(3, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x04, cursor.getByte());
assertEquals(2, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x03, cursor.getByte());
assertEquals(1, cursor.bytesLeft());
assertTrue(cursor.nextByte());
assertTrue(cursor.readByte());
assertEquals((byte) 0x02, cursor.getByte());
assertEquals(0, cursor.bytesLeft());
assertFalse(cursor.nextByte());
assertFalse(cursor.readByte());
assertEquals((byte) 0x02, cursor.getByte());
assertFalse(cursor.nextByte());
assertFalse(cursor.readByte());
assertEquals(0, cursor.bytesLeft());
assertEquals(0x0D0C0B0A09080706L, cursor.getLong());
cursor = buf.iterateReverse(buf.readerOffset() + 2, 2);
cursor = buf.openReverseCursor(buf.readerOffset() + 2, 2);
assertEquals(2, cursor.bytesLeft());
assertFalse(cursor.nextLong());
assertTrue(cursor.nextByte());
assertFalse(cursor.readLong());
assertTrue(cursor.readByte());
assertEquals((byte) 0x03, cursor.getByte());
assertEquals(1, cursor.bytesLeft());
assertFalse(cursor.nextLong());
assertTrue(cursor.nextByte());
assertFalse(cursor.readLong());
assertTrue(cursor.readByte());
assertEquals((byte) 0x02, cursor.getByte());
assertEquals(0, cursor.bytesLeft());
assertFalse(cursor.nextByte());
assertFalse(cursor.nextLong());
assertFalse(cursor.readByte());
assertFalse(cursor.readLong());
assertEquals(roff, buf.readerOffset());
assertEquals(woff, buf.writerOffset());
}

View File

@ -92,12 +92,12 @@ public class ByteIterationBenchmark {
@Benchmark
public long sum() {
var itr = buf.iterate();
var itr = buf.openCursor();
long sum = 0;
while (itr.nextLong()) {
while (itr.readLong()) {
sum += itr.getLong();
}
while (itr.nextByte()) {
while (itr.readByte()) {
sum += itr.getByte();
}
return sum;
@ -105,12 +105,12 @@ public class ByteIterationBenchmark {
@Benchmark
public long sumReverse() {
var itr = buf.iterateReverse();
var itr = buf.openReverseCursor();
long sum = 0;
while (itr.nextLong()) {
while (itr.readLong()) {
sum += itr.getLong();
}
while (itr.nextByte()) {
while (itr.readByte()) {
sum += itr.getByte();
}
return sum;