# Module: `nupp.bytes`
Typed binary reads and writes over a string.buffer.Buffer.
A Buffer already moves raw bytes with put/get and reserve/commit/ref; Reader and
Writer add the missing piece, a sized integer or float landing on those bytes
without hand-rolling the ffi.cast each time.
Both read and write host-endian, the same as every other cast in this codebase
(nupp.compiler.build.hash): LuaJIT itself only ships little-endian, so that is
the honest default. A format that has to be byte-order-fixed casts and swaps
explicitly; this module carries no LE/BE variants until something needs one.
A short read raises, matching Buffer:get. Buffer:skip is the one method that
clamps instead of raising, and Reader does not inherit that.
## Constructors
### `bytes.newReader` _constructor_
Wraps `data` for typed reads.
A string is copied into a fresh buffer. Passing an existing buffer instead lets a
caller mix `buf:get(...)` and `reader:readInt32()` over the same bytes.
```nupp
function bytes.newReader(data: string | string.buffer.Buffer): bytes.Reader
```
#### Arguments
| Name | Type | Description |
| --- | --- | --- |
| `data` | `string | string.buffer.Buffer` | the bytes to read, or the buffer to read from |
#### Returns
| Type | Description |
| --- | --- |
| `bytes.Reader` | the reader |
### `bytes.newWriter` _constructor_
Wraps `buf` for typed writes, or starts a fresh buffer when omitted.
```nupp
function bytes.newWriter(buf: string.buffer.Buffer?): bytes.Writer
```
#### Arguments
| Name | Type | Description |
| --- | --- | --- |
| `buf` | `string.buffer.Buffer?` | the buffer to append to |
#### Returns
| Type | Description |
| --- | --- |
| `bytes.Writer` | the writer |
## Types
### `Reader` _record_
A cursor for typed reads over a buffer's unconsumed data. Consumes as it reads, the
same direction as Buffer:get.
```nupp
record bytes.Reader
buf: string.buffer.Buffer
end
```
#### Methods
##### `remaining`
How many unread bytes remain.
```nupp
remaining: function bytes.Reader:remaining(): integer
```
###### Returns
| Type | Description |
| --- | --- |
| `integer` | |
##### `skip`
Discards `n` unread bytes. Raises if fewer remain.
```nupp
skip: function bytes.Reader:skip(n: integer): bytes.Reader
```
###### Arguments
| Name | Type | Description |
| --- | --- | --- |
| `n` | `integer` | |
###### Returns
| Type | Description |
| --- | --- |
| `bytes.Reader` | |
##### `readBytes`
Reads `n` raw bytes. Raises if fewer remain.
```nupp
readBytes: function bytes.Reader:readBytes(n: integer): string
```
###### Arguments
| Name | Type | Description |
| --- | --- | --- |
| `n` | `integer` | |
###### Returns
| Type | Description |
| --- | --- |
| `string` | |
##### `readUint8`
Reads one unsigned byte.
```nupp
readUint8: function bytes.Reader:readUint8(): uint8
```
###### Returns
| Type | Description |
| --- | --- |
| `uint8` | |
##### `readInt8`
Reads one signed byte.
```nupp
readInt8: function bytes.Reader:readInt8(): int8
```
###### Returns
| Type | Description |
| --- | --- |
| `int8` | |
##### `readUint16`
Reads a host-endian unsigned 16-bit integer.
```nupp
readUint16: function bytes.Reader:readUint16(): uint16
```
###### Returns
| Type | Description |
| --- | --- |
| `uint16` | |
##### `readInt16`
Reads a host-endian signed 16-bit integer.
```nupp
readInt16: function bytes.Reader:readInt16(): int16
```
###### Returns
| Type | Description |
| --- | --- |
| `int16` | |
##### `readUint32`
Reads a host-endian unsigned 32-bit integer.
```nupp
readUint32: function bytes.Reader:readUint32(): uint32
```
###### Returns
| Type | Description |
| --- | --- |
| `uint32` | |
##### `readInt32`
Reads a host-endian signed 32-bit integer.
```nupp
readInt32: function bytes.Reader:readInt32(): int32
```
###### Returns
| Type | Description |
| --- | --- |
| `int32` | |
##### `readUint64`
Reads a host-endian unsigned 64-bit integer.
```nupp
readUint64: function bytes.Reader:readUint64(): uint64
```
###### Returns
| Type | Description |
| --- | --- |
| `uint64` | |
##### `readInt64`
Reads a host-endian signed 64-bit integer.
```nupp
readInt64: function bytes.Reader:readInt64(): int64
```
###### Returns
| Type | Description |
| --- | --- |
| `int64` | |
##### `readFloat32`
Reads a host-endian 32-bit float.
```nupp
readFloat32: function bytes.Reader:readFloat32(): float
```
###### Returns
| Type | Description |
| --- | --- |
| `float` | |
##### `readFloat64`
Reads a host-endian 64-bit float.
```nupp
readFloat64: function bytes.Reader:readFloat64(): number
```
###### Returns
| Type | Description |
| --- | --- |
| `number` | |
#### Fields
| Name | Type | Description |
| --- | --- | --- |
| `buf` | `string.buffer.Buffer` | |
### `Writer` _record_
A cursor for typed writes, appending to a buffer.
```nupp
record bytes.Writer
buf: string.buffer.Buffer
end
```
#### Methods
##### `writeBytes`
Appends `s` unchanged.
```nupp
writeBytes: function bytes.Writer:writeBytes(s: string): bytes.Writer
```
###### Arguments
| Name | Type | Description |
| --- | --- | --- |
| `s` | `string` | |
###### Returns
| Type | Description |
| --- | --- |
| `bytes.Writer` | |
##### `writeUint8`
Writes one unsigned byte.
```nupp
writeUint8: function bytes.Writer:writeUint8(v: uint8): bytes.Writer
```
###### Arguments
| Name | Type | Description |
| --- | --- | --- |
| `v` | `uint8` | |
###### Returns
| Type | Description |
| --- | --- |
| `bytes.Writer` | |
##### `writeInt8`
Writes one signed byte.
```nupp
writeInt8: function bytes.Writer:writeInt8(v: int8): bytes.Writer
```
###### Arguments
| Name | Type | Description |
| --- | --- | --- |
| `v` | `int8` | |
###### Returns
| Type | Description |
| --- | --- |
| `bytes.Writer` | |
##### `writeUint16`
Writes a host-endian unsigned 16-bit integer.
```nupp
writeUint16: function bytes.Writer:writeUint16(v: uint16): bytes.Writer
```
###### Arguments
| Name | Type | Description |
| --- | --- | --- |
| `v` | `uint16` | |
###### Returns
| Type | Description |
| --- | --- |
| `bytes.Writer` | |
##### `writeInt16`
Writes a host-endian signed 16-bit integer.
```nupp
writeInt16: function bytes.Writer:writeInt16(v: int16): bytes.Writer
```
###### Arguments
| Name | Type | Description |
| --- | --- | --- |
| `v` | `int16` | |
###### Returns
| Type | Description |
| --- | --- |
| `bytes.Writer` | |
##### `writeUint32`
Writes a host-endian unsigned 32-bit integer.
```nupp
writeUint32: function bytes.Writer:writeUint32(v: uint32): bytes.Writer
```
###### Arguments
| Name | Type | Description |
| --- | --- | --- |
| `v` | `uint32` | |
###### Returns
| Type | Description |
| --- | --- |
| `bytes.Writer` | |
##### `writeInt32`
Writes a host-endian signed 32-bit integer.
```nupp
writeInt32: function bytes.Writer:writeInt32(v: int32): bytes.Writer
```
###### Arguments
| Name | Type | Description |
| --- | --- | --- |
| `v` | `int32` | |
###### Returns
| Type | Description |
| --- | --- |
| `bytes.Writer` | |
##### `writeUint64`
Writes a host-endian unsigned 64-bit integer.
```nupp
writeUint64: function bytes.Writer:writeUint64(v: uint64): bytes.Writer
```
###### Arguments
| Name | Type | Description |
| --- | --- | --- |
| `v` | `uint64` | |
###### Returns
| Type | Description |
| --- | --- |
| `bytes.Writer` | |
##### `writeInt64`
Writes a host-endian signed 64-bit integer.
```nupp
writeInt64: function bytes.Writer:writeInt64(v: int64): bytes.Writer
```
###### Arguments
| Name | Type | Description |
| --- | --- | --- |
| `v` | `int64` | |
###### Returns
| Type | Description |
| --- | --- |
| `bytes.Writer` | |
##### `writeFloat32`
Writes a host-endian 32-bit float.
```nupp
writeFloat32: function bytes.Writer:writeFloat32(v: float): bytes.Writer
```
###### Arguments
| Name | Type | Description |
| --- | --- | --- |
| `v` | `float` | |
###### Returns
| Type | Description |
| --- | --- |
| `bytes.Writer` | |
##### `writeFloat64`
Writes a host-endian 64-bit float.
```nupp
writeFloat64: function bytes.Writer:writeFloat64(v: number): bytes.Writer
```
###### Arguments
| Name | Type | Description |
| --- | --- | --- |
| `v` | `number` | |
###### Returns
| Type | Description |
| --- | --- |
| `bytes.Writer` | |
##### `buffer`
The buffer being written to, for `:tostring()`, passing to a C call, or handing off
to `bytes.newReader`.
```nupp
buffer: function bytes.Writer:buffer(): string.buffer.Buffer
```
###### Returns
| Type | Description |
| --- | --- |
| `string.buffer.Buffer` | |
#### Fields
| Name | Type | Description |
| --- | --- | --- |
| `buf` | `string.buffer.Buffer` | |