Buffer
Edit on GitHubUtilities for working with buffers.
Buffers are data structures that automatically expand as more data is appended. They are useful for storing and operating on an unknown number of bytes. All set or append operations mutate the buffer.
Added in 0.4.0
No other changes yet.
Types
Type declarations included in the Buffer module.
Buffer.Buffer
Values
Functions and constants included in the Buffer module.
Buffer.make
Added in 0.4.0
No other changes yet.
Creates a fresh buffer, initially empty.
The initialSize
parameter is the initial size of the internal byte sequence that holds the buffer contents.
That byte sequence is automatically reallocated when more than initialSize
bytes are stored in the buffer, but shrinks back to initialSize
characters when reset is called.
Parameters:
param | type | description |
---|---|---|
initialSize |
Number |
The initial size of the buffer |
Returns:
type | description |
---|---|
Buffer |
The new buffer |
Throws:
InvalidArgument(String)
- When the
initialSize
is a negative number
Examples:
Buffer.length
Added in 0.4.0
No other changes yet.
Gets the number of bytes currently contained in a buffer.
Parameters:
param | type | description |
---|---|---|
buffer |
Buffer |
The buffer to access |
Returns:
type | description |
---|---|
Number |
The length of the buffer in bytes |
Examples:
Buffer.clear
Added in 0.4.0
No other changes yet.
Clears data in the buffer and sets its length to zero.
This operation does not resize the underlying byte sequence.
Parameters:
param | type | description |
---|---|---|
buffer |
Buffer |
The buffer to clear |
Examples:
1 | let buf = Buffer.make(0) |
Buffer.reset
Added in 0.4.0
No other changes yet.
Empty a buffer and deallocate the internal byte sequence holding the buffer contents.
This operation resizes the underlying byte sequence to the initial size of the buffer.
Parameters:
param | type | description |
---|---|---|
buffer |
Buffer |
The buffer to reset |
Examples:
1 | let buf = Buffer.make(0) |
Buffer.truncate
Added in 0.4.0
No other changes yet.
Shortens a buffer to the given length.
This operation does not resize the underlying byte sequence.
Parameters:
param | type | description |
---|---|---|
length |
Number |
The number of bytes to truncate the buffer to |
buffer |
Buffer |
The buffer to truncate |
Throws:
IndexOutOfBounds
- When the
length
is negative - When the
length
is greater than the buffer size
Examples:
1 | let buf = Buffer.make(0) |
Buffer.toBytes
Added in 0.4.0
No other changes yet.
Returns a copy of the current contents of the buffer as a byte sequence.
Parameters:
param | type | description |
---|---|---|
buffer |
Buffer |
The buffer to copy into a byte sequence |
Returns:
type | description |
---|---|
Bytes |
A byte sequence made from copied buffer data |
Examples:
Buffer.toBytesSlice
Added in 0.4.0
No other changes yet.
Returns a slice of the current contents of the buffer as a byte sequence.
Parameters:
param | type | description |
---|---|---|
start |
Number |
The start index |
length |
Number |
The number of bytes to include after the starting index |
buffer |
Buffer |
The buffer to copy from |
Returns:
type | description |
---|---|
Bytes |
A byte sequence with bytes copied from the buffer |
Throws:
IndexOutOfBounds
- When
start
is negative - When
start
is greater than or equal to the buffer size - When
start + length
is greater than the buffer size
Examples:
1 | let buf = Buffer.make(0) |
Buffer.toString
Added in 0.4.0
No other changes yet.
Returns a copy of the current contents of the buffer as a string.
Parameters:
param | type | description |
---|---|---|
buffer |
Buffer |
The buffer to stringify |
Returns:
type | description |
---|---|
String |
A string made with data copied from the buffer |
Examples:
1 | let buf = Buffer.make(0) |
Buffer.toStringSlice
Added in 0.4.0
No other changes yet.
Returns a copy of a subset of the current contents of the buffer as a string.
Parameters:
param | type | description |
---|---|---|
start |
Number |
The start index |
length |
Number |
The number of bytes to include after the starting index |
buffer |
Buffer |
The buffer to copy from |
Returns:
type | description |
---|---|
String |
A string made with a subset of data copied from the buffer |
Examples:
1 | let buf = Buffer.make(0) |
Buffer.addBytes
Added in 0.4.0
No other changes yet.
Appends a byte sequence to a buffer.
Parameters:
param | type | description |
---|---|---|
bytes |
Bytes |
The byte sequence to append |
buffer |
Buffer |
The buffer to mutate |
Examples:
Buffer.addString
Added in 0.4.0
No other changes yet.
Appends the bytes of a string to a buffer.
Parameters:
param | type | description |
---|---|---|
string |
String |
The string to append |
buffer |
Buffer |
The buffer to mutate |
Examples:
Buffer.addChar
Added in 0.4.0
No other changes yet.
Appends the bytes of a character to a buffer.
Parameters:
param | type | description |
---|---|---|
char |
Char |
The character to append to the buffer |
buffer |
Buffer |
The buffer to mutate |
Examples:
Buffer.addCharFromCodePoint
Added in 0.6.0
No other changes yet.
Appends a character represented by a code point to a buffer.
Parameters:
param | type | description |
---|---|---|
codePoint |
Number |
The code point to append to the buffer |
buffer |
Buffer |
The buffer to mutate |
Examples:
Buffer.addStringSlice
Added in 0.4.0
version | changes |
---|---|
0.5.0 | Now takes the end offset instead of length |
Appends the bytes of a subset of a string to a buffer.
Parameters:
param | type | description |
---|---|---|
start |
Number |
The char offset into the string |
end |
Number |
The end offset into the string |
string |
String |
The string to append |
buffer |
Buffer |
The buffer to mutate |
Examples:
1 | let buf = Buffer.make(0) |
Buffer.addBytesSlice
Added in 0.4.0
No other changes yet.
Appends the bytes of a subset of a byte sequence to a buffer.
Parameters:
param | type | description |
---|---|---|
start |
Number |
The byte offset into the byte sequence |
length |
Number |
The number of bytes to append |
bytes |
Bytes |
The byte sequence to append |
buffer |
Buffer |
The buffer to mutate |
Throws:
IndexOutOfBounds
- When the
start
is negative - When the
start
is greater than or equal to thebytes
size - When the
length
is negative - When the
length
is greater than thebytes
length minusstart
Examples:
1 | let buf = Buffer.make(0) |
Buffer.addBuffer
Added in 0.4.0
No other changes yet.
Appends the bytes of a source buffer to destination buffer.
The source buffer is not mutated by this operation. The destination buffer, however, is mutated.
Parameters:
param | type | description |
---|---|---|
srcBuffer |
Buffer |
The buffer to append |
dstBuffer |
Buffer |
The buffer to mutate |
Examples:
1 | let buf1 = Buffer.make(0) |
Buffer.addBufferSlice
Added in 0.4.0
No other changes yet.
Appends the bytes of a part of a buffer to the end of the buffer
The source buffer is not mutated by this operation. The destination buffer, however, is mutated.
Parameters:
param | type | description |
---|---|---|
start |
Number |
The byte offset into the buffer |
length |
Number |
The number of bytes to append |
srcBuffer |
Buffer |
The buffer to append |
dstBuffer |
Buffer |
The buffer to mutate |
Examples:
Buffer.getInt8
Added in 0.6.0
version | changes |
---|---|
0.4.0 | Originally called `getInt8S`, returning an `Int32` |
Gets a signed 8-bit integer starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to access |
buffer |
Buffer |
The buffer to access |
Returns:
type | description |
---|---|
Int8 |
A signed 8-bit integer that starts at the given index |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 1
is greater than the buffer size
Examples:
Buffer.setInt8
Added in 0.4.0
version | changes |
---|---|
0.6.0 | `value` argument type changed to `Int8` |
Sets a signed 8-bit integer starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to update |
value |
Int8 |
The value to set |
buffer |
Buffer |
The buffer to mutate |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 1
is greater than the buffer size
Examples:
1 | let buf = Buffer.make(32) |
Buffer.addInt8
Added in 0.4.0
version | changes |
---|---|
0.6.0 | `value` argument type changed to `Int8` |
Appends a signed 8-bit integer to a buffer.
Parameters:
param | type | description |
---|---|---|
value |
Int8 |
The value to append |
buffer |
Buffer |
The buffer to mutate |
Examples:
Buffer.getUint8
Added in 0.6.0
version | changes |
---|---|
0.4.0 | Originally called `getInt8U`, returning an `Int32` |
Gets an unsigned 8-bit integer starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to access |
buffer |
Buffer |
The buffer to access |
Returns:
type | description |
---|---|
Uint8 |
An unsigned 8-bit integer that starts at the given index |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 1
is greater than the buffer size
Examples:
Buffer.setUint8
Added in 0.6.0
No other changes yet.
Sets an unsigned 8-bit integer starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to update |
value |
Uint8 |
The value to set |
buffer |
Buffer |
The buffer to mutate |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 1
is greater than the buffer size
Examples:
1 | let buf = Buffer.make(32) |
Buffer.addUint8
Added in 0.6.0
No other changes yet.
Appends an unsigned 8-bit integer to a buffer.
Parameters:
param | type | description |
---|---|---|
value |
Uint8 |
The value to append |
buffer |
Buffer |
The buffer to mutate |
Examples:
Buffer.getInt16
Added in 0.6.0
version | changes |
---|---|
0.4.0 | Originally called `getInt16S`, returning an `Int32` |
Gets a signed 16-bit integer starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to access |
buffer |
Buffer |
The buffer to access |
Returns:
type | description |
---|---|
Int16 |
A signed 16-bit integer that starts at the given index |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 2
is greater than the buffer size
Examples:
Buffer.setInt16
Added in 0.4.0
version | changes |
---|---|
0.6.0 | `value` argument type changed to `Int16` |
Sets a signed 16-bit integer starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to update |
value |
Int16 |
The value to set |
buffer |
Buffer |
The buffer to mutate |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 2
is greater than the buffer size
Examples:
1 | let buf = Buffer.make(32) |
Buffer.addInt16
Added in 0.4.0
version | changes |
---|---|
0.6.0 | `value` argument type changed to `Int16` |
Appends a signed 16-bit integer to a buffer.
Parameters:
param | type | description |
---|---|---|
value |
Int16 |
The value to append |
buffer |
Buffer |
The buffer to mutate |
Examples:
Buffer.getUint16
Added in 0.6.0
version | changes |
---|---|
0.4.0 | Originally called `getInt16U`, returning an `Int32` |
Gets an unsigned 16-bit integer starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to access |
buffer |
Buffer |
The buffer to access |
Returns:
type | description |
---|---|
Uint16 |
An unsigned 16-bit integer that starts at the given index |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 2
is greater than the buffer size
Examples:
Buffer.setUint16
Added in 0.6.0
No other changes yet.
Sets an unsigned 16-bit integer starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to update |
value |
Uint16 |
The value to set |
buffer |
Buffer |
The buffer to mutate |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 2
is greater than the buffer size
Examples:
1 | let buf = Buffer.make(32) |
Buffer.addUint16
Added in 0.6.0
No other changes yet.
Appends an unsigned 16-bit integer to a buffer.
Parameters:
param | type | description |
---|---|---|
value |
Uint16 |
The value to append |
buffer |
Buffer |
The buffer to mutate |
Examples:
Buffer.getInt32
Added in 0.4.0
No other changes yet.
Gets a signed 32-bit integer starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to access |
buffer |
Buffer |
The buffer to access |
Returns:
type | description |
---|---|
Int32 |
A signed 32-bit integer that starts at the given index |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 4
is greater than the buffer size
Examples:
Buffer.setInt32
Added in 0.4.0
No other changes yet.
Sets a signed 32-bit integer starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to update |
value |
Int32 |
The value to set |
buffer |
Buffer |
The buffer to mutate |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 4
is greater than the buffer size
Examples:
1 | let buf = Buffer.make(64) |
Buffer.addInt32
Added in 0.4.0
No other changes yet.
Appends a signed 32-bit integer to a buffer.
Parameters:
param | type | description |
---|---|---|
value |
Int32 |
The value to append |
buffer |
Buffer |
The buffer to mutate |
Examples:
Buffer.getUint32
Added in 0.6.0
No other changes yet.
Gets an unsigned 32-bit integer starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to access |
buffer |
Buffer |
The buffer to access |
Returns:
type | description |
---|---|
Uint32 |
An unsigned 32-bit integer that starts at the given index |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 4
is greater than the buffer size
Examples:
Buffer.setUint32
Added in 0.6.0
No other changes yet.
Sets an unsigned 32-bit integer starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to update |
value |
Uint32 |
The value to set |
buffer |
Buffer |
The buffer to mutate |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 4
is greater than the buffer size
Examples:
1 | let buf = Buffer.make(32) |
Buffer.addUint32
Added in 0.6.0
No other changes yet.
Appends an unsigned 32-bit integer to a buffer.
Parameters:
param | type | description |
---|---|---|
value |
Uint32 |
The value to append |
buffer |
Buffer |
The buffer to mutate |
Examples:
Buffer.getFloat32
Added in 0.4.0
No other changes yet.
Gets a 32-bit float starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to access |
buffer |
Buffer |
The buffer to access |
Returns:
type | description |
---|---|
Float32 |
A 32-bit float that starts at the given index |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 4
is greater than the buffer size
Examples:
Buffer.setFloat32
Added in 0.4.0
No other changes yet.
Sets a 32-bit float starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to update |
value |
Float32 |
The value to set |
buffer |
Buffer |
The buffer to mutate |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 4
is greater than the buffer size
Examples:
1 | let buf = Buffer.make(32) |
Buffer.addFloat32
Added in 0.4.0
No other changes yet.
Appends a 32-bit float to a buffer.
Parameters:
param | type | description |
---|---|---|
value |
Float32 |
The value to append |
buffer |
Buffer |
The buffer to mutate |
Examples:
Buffer.getInt64
Added in 0.4.0
No other changes yet.
Gets a signed 64-bit integer starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to access |
buffer |
Buffer |
The buffer to access |
Returns:
type | description |
---|---|
Int64 |
A signed 64-bit integer that starts at the given index |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 8
is greater than the buffer size
Examples:
Buffer.setInt64
Added in 0.4.0
No other changes yet.
Sets a signed 64-bit integer starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to update |
value |
Int64 |
The value to set |
buffer |
Buffer |
The buffer to mutate |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 8
is greater than the buffer size
Examples:
1 | let buf = Buffer.make(32) |
Buffer.addInt64
Added in 0.4.0
No other changes yet.
Appends a signed 64-bit integer to a buffer.
Parameters:
param | type | description |
---|---|---|
value |
Int64 |
The value to set |
buffer |
Buffer |
The buffer to mutate |
Examples:
Buffer.getUint64
Added in 0.6.0
No other changes yet.
Gets an unsigned 64-bit integer starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to access |
buffer |
Buffer |
The buffer to access |
Returns:
type | description |
---|---|
Uint64 |
An unsigned 64-bit integer that starts at the given index |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 8
is greater than the buffer size
Examples:
Buffer.setUint64
Added in 0.6.0
No other changes yet.
Sets an unsigned 64-bit integer starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to update |
value |
Uint64 |
The value to set |
buffer |
Buffer |
The buffer to mutate |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 8
is greater than the buffer size
Examples:
1 | let buf = Buffer.make(32) |
Buffer.addUint64
Added in 0.6.0
No other changes yet.
Appends an unsigned 64-bit integer to a buffer.
Parameters:
param | type | description |
---|---|---|
value |
Uint64 |
The value to set |
buffer |
Buffer |
The buffer to mutate |
Examples:
Buffer.getFloat64
Added in 0.4.0
No other changes yet.
Gets a 64-bit float starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to access |
buffer |
Buffer |
The buffer to access |
Returns:
type | description |
---|---|
Float64 |
A 64-bit float that starts at the given index |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 8
is greater than the buffer size
Examples:
Buffer.setFloat64
Added in 0.4.0
No other changes yet.
Sets a 64-bit float starting at the given byte index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The byte index to update |
value |
Float64 |
The value to set |
buffer |
Buffer |
The buffer to mutate |
Throws:
IndexOutOfBounds
- When
index
is negative - When
index
is greater than or equal to the buffer size - When
index + 8
is greater than the buffer size
Examples:
1 | let buf = Buffer.make(32) |
Buffer.addFloat64
Added in 0.4.0
No other changes yet.
Appends a 64-bit float to a buffer.
Parameters:
param | type | description |
---|---|---|
value |
Float64 |
The value to append |
buffer |
Buffer |
The buffer to mutate |
Examples: