easy-nio.buffer

Wrapper around java.nio.ByteBuffer.

ByteBuffers are stateful and mutable. All functions that modify buffer state return the buffer itself to support threading via -> or as->.

Terminology follows the java.nio convention: - capacity : total bytes the buffer can hold - limit : the first byte that should not be read or written - position : the current read/write cursor - remaining: (- limit position)

->bytes

(->bytes buf)

Returns a byte-array of the bytes between position and limit without advancing the position. Safe to call multiple times.

->str

(->str buf)

Decodes the bytes between position and limit as a UTF-8 string. Does not advance position.

allocate

(allocate capacity)(allocate capacity direct?)

Allocates a heap ByteBuffer of capacity bytes if direct? is false. Otherwise, allocates a direct (off-heap) ByteBuffer of capacity bytes. The former should be used for general purpose buffers while the latter should be prefverred for I/O operations with NIO channels.

as-read-only

(as-read-only buf)

Returns a read-only view of buf. Writes to the returned buffer throw ReadOnlyBufferException.

big-endian

byte-order

(byte-order buf)

Returns the current ByteOrder of buf.

capacity

(capacity buf)

Returns the total byte capacity of buf.

clear!

(clear! buf)

Resets buf for writing from scratch. Sets position to 0 and limit to capacity. Does not zero out data. Returns buf.

compact!

(compact! buf)

Compacts buf. Copies unread bytes to the start, then positions after the last copied byte ready for more writes. Use instead of clear! when you want to preserve unread data. Returns buf.

direct?

(direct? buf)

Returns true if buf is a direct (off-heap) buffer.

duplicate

(duplicate buf)

Returns a new ByteBuffer sharing the same backing data but with independent position, limit, and mark. Useful for reading the same buffer concurrently without rewinding.

flip!

(flip! buf)

Switches buf from write mode to read mode. Sets limit to current position, then resets position to 0. Returns buf.

from-bytes

(from-bytes ba)

Allocates a direct ByteBuffer containing ba, ready for reading.

from-string

(from-string s)

Allocates a direct ByteBuffer containing the UTF-8 bytes of s, ready for reading (already flipped).

get-byte!

(get-byte! buf)

Reads and returns a single byte at the current position, advancing position by 1.

get-bytes!

(get-bytes! buf n)

Reads n bytes from buf into a new byte-array and returns it. Advances position by n.

get-double!

(get-double! buf)

Reads and returns a double (8 bytes) at the current position.

get-float!

(get-float! buf)

Reads and returns a float (4 bytes) at the current position.

get-int!

(get-int! buf)

Reads and returns an int (4 bytes) at the current position.

get-long!

(get-long! buf)

Reads and returns a long (8 bytes) at the current position.

get-short!

(get-short! buf)

Reads and returns a short (2 bytes) at the current position.

has-remaining?

(has-remaining? buf)

Returns true if there is at least one byte between position and limit.

limit

(limit buf)

Returns the current limit of buf.

little-endian

mark!

(mark! buf)

Marks the current position for a later reset!. Returns buf.

position

(position buf)

Returns the current position of buf.

put-buffer!

(put-buffer! buf src)

Writes all remaining bytes from src ByteBuffer into buf. Returns buf.

put-byte!

(put-byte! buf b)

Writes a single byte b at the current position. Returns buf.

put-bytes!

(put-bytes! buf ba)(put-bytes! buf ba offset length)

Writes all bytes from byte-array ba into buf at the current position. Optionally accepts offset and length to write a sub-range of ba. Returns buf.

put-double!

(put-double! buf n)

Writes an 8-byte double at the current position. Returns buf.

put-float!

(put-float! buf n)

Writes a 4-byte float at the current position. Returns buf.

put-int!

(put-int! buf n)

Writes a 4-byte int at the current position. Returns buf.

put-long!

(put-long! buf n)

Writes an 8-byte long at the current position. Returns buf.

put-short!

(put-short! buf n)

Writes a 2-byte short at the current position. Returns buf.

read-only?

(read-only? buf)

Returns true if buf is read-only.

remaining

(remaining buf)

Returns the number of bytes between position and limit.

reset-to-mark!

(reset-to-mark! buf)

Resets position to the previously marked position. Returns buf.

rewind!

(rewind! buf)

Resets position to 0 without changing the limit. Useful for re-reading a buffer that has already been flipped. Returns buf.

set-byte-order!

(set-byte-order! buf order)

Sets the byte order of buf to order (use big-endian or little-endian). Returns buf.

set-limit!

(set-limit! buf n)

Sets the limit of buf to n. Returns buf.

set-position!

(set-position! buf n)

Sets the position of buf to n. Returns buf.

slice

(slice buf)

Returns a new ByteBuffer sharing the region from the current position to the limit of buf. Changes to the slice’s content are reflected in the original.

wrap

(wrap ba)(wrap ba offset length)

Wraps an existing byte-array into a ByteBuffer. The buffer shares the backing array, mutations to one are visible in the other. Optionally accepts offset and length to wrap a sub-region.