Streamed Buffers - .NET's BinaryReader facsimile
npm install streambufstreambuf offers a low-level API that similar to C++'s fstream/iostream/etc. or .NET's BinaryReader/BinaryWriter.
streambuf, check out node-structor.
$ npm i streambuf
`
Usage
`js
const fs = require('fs')
const { StreamBuffer } = require('streambuf')
let buffer = StreamBuffer.from(fs.readFileSync('hiscore.dat'))
let nameLength = buffer.readUInt32LE()
let name = buffer.readString(nameLength)
buffer.skip(-nameLength) // Go back to the beginning of the name
buffer.writeString(name.toUpperCase()) // Overwrite the name in the buffer with something else
`
Refer to Buffer for a list of available read and write methods supported by StreamBuffer (omit the offset param).
API
StreamBuffer(Buffer)
Constructor: initialize with a Buffer object.
StreamBuffer(StreamBuffer)
---
Constructor: initialize with another StreamBuffer object's underlying Buffer.
StreamBuffer numeric methods
readInt8, readInt16LE, readInt16BE, readInt32LE, readInt32BE, readIntLE, readIntBE,
readUInt8, readUInt16LE, readUInt16BE, readUInt32LE, readUInt32BE, readUIntLE, readUIntBE,
readFloatLE, readFloatBE, readDoubleLE, readDoubleBE
writeInt8, writeInt16LE, writeInt16BE, writeInt32LE, writeInt32BE, writeIntLE, writeIntBE,
writeUInt8, writeUInt16LE, writeUInt16BE, writeUInt32LE, writeUInt32BE,
writeFloatLE, writeFloatBE, writeDoubleLE, writeDoubleBE
StreamBuffer BigInt methods
readBigInt64LE, readBigInt64BE, readBigUInt64LE, readBigUInt64BE,
writeBigInt64LE, writeBigInt64BE, writeBigUInt64LE, writeBigUInt64BE
.buffer
Provides raw access to the underlying Buffer object (read-only)
.read(numBytes)
Returns a new StreamBuffer that references the same Buffer as the original, but cropped by offset and offset + numBytes.
.write(buffer: Buffer)
Writes the contents of another Buffer.
.readByte()
Alias for .readUInt8()
.readSByte()
Alias for .readInt8()
.writeByte()
Alias for .writeUInt8()
.writeSByte()
Alias for .writeInt8()
.read7BitInt()
Reads a 7 bit encoded integer, like those used by .NET.aspx>)
.write7BitInt()
Writes a 7 bit encoded integer, like those used by .NET.aspx>)
.readChar([encoding])
Reads a single character from the buffer according to the specified character encoding. Multi-byte characters are not read - use readString for that instead.
'encoding' defaults to utf8.
.writeChar([encoding])
Writes a single character to the buffer according to the specified character encoding. Multi-byte characters are not written - use writeString for that instead.
'encoding' defaults to utf8.
.readChars(length, [encoding])
Reads a fixed number of characters from the buffer according to the specified character encoding. The resulting string strips any trailing null characters.
'encoding' defaults to utf8.
.writeChars(str, length, [encoding])
Writes a string to the underlying buffer with the specified encoding, padding with null characters (0) if the string is shorter than length.
'encoding' defaults to utf8.
.readString([length, [encoding]])
Decodes to a string according to the specified character encoding in encoding and length.
'encoding' defaults to utf8.
'length' is optional. If left undefined, it will use the first occurrence of a zero (0) byte as the end of the string.
.writeString(str, [encoding])
Writes a string to the underlying buffer with the specified encoding.
'encoding' defaults to utf8.
.peekString([length, [encoding]])
Functions the same way as .readString(), but does not update the offset.
.readString0([encoding])
Reads a string from the buffer according to the specified character encoding, stopping at the first zero (0) byte. Similar to .readString() or .readString(undefined, encoding), but more implicit.
'encoding' defaults to utf8.
$3
`js
// A buffer containing two zero-terminated strings: "Hello" and "World"
const buf = Buffer.from('Hello\0World\0')
const sb = new StreamBuffer(buf)
console.log(sb.readString0()) // "Hello"
console.log(sb.readString0()) // "World"
``