select

expect abstract fun select(options: Options): Int

Finds the first byte string in options that is a prefix of this buffer, consumes it from this source, and returns its index. If no byte string in options is a prefix of this buffer this returns -1 and no bytes are consumed.

This can be used as an alternative to readByteString or even readUtf8 if the set of expected values is known in advance.

Options FIELDS = Options.of(
ByteString.encodeUtf8("depth="),
ByteString.encodeUtf8("height="),
ByteString.encodeUtf8("width="));

Buffer buffer = new Buffer()
.writeUtf8("width=640\n")
.writeUtf8("height=480\n");

assertEquals(2, buffer.select(FIELDS));
assertEquals(640, buffer.readDecimalLong());
assertEquals('\n', buffer.readByte());
assertEquals(1, buffer.select(FIELDS));
assertEquals(480, buffer.readDecimalLong());
assertEquals('\n', buffer.readByte());

expect abstract fun <T : Any> select(options: TypedOptions<T>): T?

Finds the first item in options whose encoding is a prefix of this buffer, consumes it from this buffer, and returns it. If no item in options is a prefix of this source, this function returns null and no bytes are consumed.

This can be used as an alternative to readByteString or even readUtf8 if the set of expected values is known in advance.

TypedOptions<Direction> options = TypedOptions.of(
Arrays.asList(Direction.values()),
(direction) -> ByteString.encodeUtf8(direction.name().toLowerCase(Locale.ROOT))
);

Buffer buffer = new Buffer()
.writeUtf8("north:100\n")
.writeUtf8("east:50\n");

assertEquals(Direction.NORTH, buffer.select(options));
assertEquals(':', buffer.readByte());
assertEquals(100L, buffer.readDecimalLong());
assertEquals('\n', buffer.readByte());

assertEquals(Direction.EAST, buffer.select(options));
assertEquals(':', buffer.readByte());
assertEquals(50L, buffer.readDecimalLong());
assertEquals('\n', buffer.readByte());
actual abstract fun select(options: Options): Int
actual abstract fun <T : Any> select(options: TypedOptions<T>): T?
actual abstract fun select(options: Options): Int
actual abstract fun <T : Any> select(options: TypedOptions<T>): T?