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:
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 buf)Returns a byte-array of the bytes between position and limit without advancing the position. Safe to call multiple times.
Returns a byte-array of the bytes between position and limit without advancing the position. Safe to call multiple times.
(->str buf)Decodes the bytes between position and limit as a UTF-8 string. Does not advance position.
Decodes the bytes between position and limit as a UTF-8 string. Does not advance position.
(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.
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 buf)Returns a read-only view of buf. Writes to the returned buffer
throw ReadOnlyBufferException.
Returns a read-only view of `buf`. Writes to the returned buffer throw ReadOnlyBufferException.
(byte-order buf)Returns the current ByteOrder of buf.
Returns the current ByteOrder of `buf`.
(capacity buf)Returns the total byte capacity of buf.
Returns the total byte capacity of `buf`.
(clear! buf)Resets buf for writing from scratch.
Sets position to 0 and limit to capacity. Does not zero out data.
Returns buf.
Resets `buf` for writing from scratch. Sets position to 0 and limit to capacity. Does not zero out data. Returns `buf`.
(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.
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? buf)Returns true if buf is a direct (off-heap) buffer.
Returns true if `buf` is a direct (off-heap) buffer.
(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.
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! buf)Switches buf from write mode to read mode.
Sets limit to current position, then resets position to 0.
Returns buf.
Switches `buf` from write mode to read mode. Sets limit to current position, then resets position to 0. Returns `buf`.
(from-bytes ba)Allocates a direct ByteBuffer containing ba, ready for reading.
Allocates a direct ByteBuffer containing `ba`, ready for reading.
(from-string s)Allocates a direct ByteBuffer containing the UTF-8 bytes of s,
ready for reading (already flipped).
Allocates a direct ByteBuffer containing the UTF-8 bytes of `s`, ready for reading (already flipped).
(get-byte! buf)Reads and returns a single byte at the current position, advancing position by 1.
Reads and returns a single byte at the current position, advancing position by 1.
(get-bytes! buf n)Reads n bytes from buf into a new byte-array and returns it.
Advances position by n.
Reads `n` bytes from `buf` into a new byte-array and returns it. Advances position by `n`.
(get-double! buf)Reads and returns a double (8 bytes) at the current position.
Reads and returns a double (8 bytes) at the current position.
(get-float! buf)Reads and returns a float (4 bytes) at the current position.
Reads and returns a float (4 bytes) at the current position.
(get-int! buf)Reads and returns an int (4 bytes) at the current position.
Reads and returns an int (4 bytes) at the current position.
(get-long! buf)Reads and returns a long (8 bytes) at the current position.
Reads and returns a long (8 bytes) at the current position.
(get-short! buf)Reads and returns a short (2 bytes) at the current position.
Reads and returns a short (2 bytes) at the current position.
(has-remaining? buf)Returns true if there is at least one byte between position and limit.
Returns true if there is at least one byte between position and limit.
(limit buf)Returns the current limit of buf.
Returns the current limit of `buf`.
(mark! buf)Marks the current position for a later reset!. Returns buf.
Marks the current position for a later reset!. Returns `buf`.
(position buf)Returns the current position of buf.
Returns the current position of `buf`.
(put-buffer! buf src)Writes all remaining bytes from src ByteBuffer into buf.
Returns buf.
Writes all remaining bytes from `src` ByteBuffer into `buf`. Returns `buf`.
(put-byte! buf b)Writes a single byte b at the current position. Returns buf.
Writes a single byte `b` at the current position. Returns `buf`.
(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.
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! buf n)Writes an 8-byte double at the current position. Returns buf.
Writes an 8-byte double at the current position. Returns `buf`.
(put-float! buf n)Writes a 4-byte float at the current position. Returns buf.
Writes a 4-byte float at the current position. Returns `buf`.
(put-int! buf n)Writes a 4-byte int at the current position. Returns buf.
Writes a 4-byte int at the current position. Returns `buf`.
(put-long! buf n)Writes an 8-byte long at the current position. Returns buf.
Writes an 8-byte long at the current position. Returns `buf`.
(put-short! buf n)Writes a 2-byte short at the current position. Returns buf.
Writes a 2-byte short at the current position. Returns `buf`.
(read-only? buf)Returns true if buf is read-only.
Returns true if `buf` is read-only.
(remaining buf)Returns the number of bytes between position and limit.
Returns the number of bytes between position and limit.
(reset-to-mark! buf)Resets position to the previously marked position. Returns buf.
Resets position to the previously marked position. Returns `buf`.
(rewind! buf)Resets position to 0 without changing the limit.
Useful for re-reading a buffer that has already been flipped.
Returns 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! buf order)Sets the byte order of buf to order (use big-endian or
little-endian). Returns buf.
Sets the byte order of `buf` to `order` (use `big-endian` or `little-endian`). Returns `buf`.
(set-limit! buf n)Sets the limit of buf to n. Returns buf.
Sets the limit of `buf` to `n`. Returns `buf`.
(set-position! buf n)Sets the position of buf to n. Returns buf.
Sets the position of `buf` to `n`. Returns `buf`.
(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.
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 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.
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.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |