xrtlibrary-bufferutilities
v1.0.4
Published
Buffer utilities module of XRT library.
Downloads
3
Readme
XRTLibrary-BufferUtilities
Introduction
This library provides following buffer utilities:
- Buffer fetcher (fetch several bytes synchronously from a Buffer object).
- Blocked buffer fetcher (fetch several bytes synchronously from a Buffer array).
- Buffer merger (merge a buffer array into one).
Installation
To install this package, you can use NPM by typing following command:
npm install xrtlibrary-bufferutilities --save
Then you can import this library in your JavaScript code:
const XRTLibBufferUtilities = require("xrtlibrary-bufferutilities");
API
(Class) BufferFetcher
Buffer fetcher.
new BufferFetcher(buffer)
Construct a new object.
Parameter(s):
- buffer (Buffer): The buffer.
fetcher.isEnd()
Get whether the cursor reached the end.
Return value:
- (Boolean) True if ended.
fetcher.reset()
Reset the cursor.
fetcher.fetch()
Fetch one byte.
Exception(s):
- BufferFetcher.EndOfStreamError: Raised if the stream was already ended.
Return value:
- (Number) The byte.
Example:
let fetcher = new XRTLibBufferUtilities.BufferFetcher(
Buffer.from([10, 9, 8, 7])
);
while (!fetcher.isEnd()) {
console.log(fetcher.fetch());
}
// Output:
// 10
// 9
// 8
// 7
fetcher.fetchTo(output)
Fetch bytes to output buffer.
Parameter(s):
- output (Buffer): The output buffer.
Return value:
- (Number) The count of copied bytes.
Example:
let fetcher = new XRTLibBufferUtilities.BufferFetcher(
Buffer.from([1, 2, 3, 4])
);
while (!fetcher.isEnd()) {
let output = Buffer.alloc(2);
fetcher.fetchTo(output);
console.log(output);
}
// Output:
// <Buffer 01 02>
// <Buffer 03 04>
fetcher.fetchToWithStart(output, outputStart)
Fetch bytes to output buffer (with start position).
Parameter(s):
- output (Buffer): The output buffer.
- outputStart (Number): The start position of the output buffer.
Return value:
- (Number) The count of copied bytes.
fetcher.fetchAll()
Fetch all bytes.
Return value:
- (Buffer): The bytes.
Example:
let fetcher = new XRTLibBufferUtilities.BufferFetcher(
Buffer.from([1, 2, 3, 4])
);
console.log(fetcher.fetch());
console.log(fetcher.fetchAll());
// Output:
// 1
// <Buffer 02 03 04>
fetcher.fetchBytes(count)
Fetch specific bytes.
Parameter(s):
- count (Number): The bytes count.
Return value:
- (Buffer) The bytes.
fetcher.skip(steps)
Skip specific bytes.
Parameter(s):
- steps (Number): The count of bytes to be skipped.
Return value:
- (Number): The count of skipped bytes.
Example:
let fetcher = new XRTLibBufferUtilities.BufferFetcher(
Buffer.from([1, 2, 3, 4])
);
console.log(fetcher.fetch());
fetcher.skip(2);
console.log(fetcher.fetch());
// Output:
// 1
// 4
fetcher.getRemainingCount()
Get the count of remaining bytes.
Return value:
- (Number): The count.
fetcher.reinitialize(newBuffer)
Parameter(s):
- newBuffer (Buffer): New buffer.
(Class) BufferFetcher.Error
Buffer fetcher error.
Extend(s):
- Error
(Class) BufferFetcher.EndOfStreamError
Buffer fetcher end of stream error.
Extend(s):
- BufferFetcher.Error
(Class) BlockBufferFetcher
Block buffer fetcher.
new BlockBufferFetcher(blocks)
Construct a new object.
Parameter(s):
- blocks (Buffer[]): The buffer blocks.
blockfetcher.isEnd()
Get whether the cursor reached the end.
Return value:
- (Boolean) True if ended.
blockfetcher.reset()
Reset the cursor.
blockfetcher.fetch()
Fetch one byte.
Exception(s):
- BlockBufferFetcher.EndOfStreamError: Raised if the stream was already ended.
Return value:
- (Number) The byte.
blockfetcher.fetchTo(output)
Fetch bytes to output buffer.
Parameter(s):
- output (Buffer): The output buffer.
Return value:
- (Number) The count of copied bytes.
blockfetcher.fetchAll()
Fetch all bytes.
Return value:
- (Buffer) The bytes.
blockfetcher.fetchAllAsBlocks([outBytesCount = new OutputParameter()])
Fetch all remaining bytes as blocks (faster).
Parameter(s):
- outBytesCount (OutputParameter): (Optional, Out) The size of output buffer blocks.
Return value:
- (Buffer[]) The blocks.
blockfetcher.fetchBytes(count)
Fetch specific bytes.
Parameter(s):
- count (Number): The bytes count.
Return value:
- (Buffer) The bytes.
blockfetcher.fetchBytesAsBlocks(count)
Fetch specific bytes as blocks.
Warning(s):
- This method has been deprecated (but still usable).
- Use blockfetcher.fetchBytesAsBlocks2() instead.
Parameter(s):
- count (Number): The bytes count.
Return value:
- (Object) The fetch result.
- buffers (Buffer[]): The buffer array.
- length (Number): The length of bytes fetched.
blockfetcher.fetchBytesAsBlocks2(count, [outBytesCount = new OutputParameter()])
Fetch specific bytes as blocks.
Parameter(s):
- count (Number): The bytes count.
- outBytesCount (OutputParameter): (Optional, Out) The size of output buffer blocks.
Return value:
- (Buffer[]) The buffer blocks.
blockfetcher.skip(steps)
Skip specific bytes.
Parameter(s):
- steps (Number): The count of bytes to be skipped.
Return value:
- (Number) The count of skipped bytes.
blockfetcher.reinitialize(newBlocks)
Reinitialize the object.
Parameter(s):
- newBlocks (Buffer[]): New blocks.
(Class) BlockBufferFetcher.Error
Block buffer fetcher error.
Extend(s):
- Error
(Class) BlockBufferFetcher.EndOfStreamError
Block buffer fetcher end of stream error.
Extend(s):
- BlockBufferFetcher.Error
(Class) BufferMerger
Buffer merger.
new BufferMerger()
Construct a new object.
merger.push(data)
Push a buffer.
Parameter(s):
- data (Buffer): The buffer.
merger.merge()
Merge pushed buffers.
Return value:
- (Buffer) The concatencated buffer.
merger.reset()
Clear pushed buffers.
(Class) BufferEmitter
Buffer emitter.
Extend(s):
- BufferMerger
new BufferEmitter()
Construct a new object.
emitter.writeUInt8(value)
Write an 8-bit unsigned integer.
Parameter(s):
- value (Number): The integer value.
emitter.writeInt8(value)
Write an 8-bit signed integer.
Parameter(s):
- value (Number): The integer value.
emitter.writeUInt16LE(value)
Write an 16-bit little endian unsigned integer.
Parameter(s):
- value (Number): The integer value.
emitter.writeUInt16BE(value)
Write an 16-bit big endian unsigned integer.
Parameter(s):
- value (Number): The integer value.
emitter.writeInt16LE(value)
Write an 16-bit little endian signed integer.
Parameter(s):
- value (Number): The integer value.
emitter.writeInt16BE(value)
Write an 16-bit big endian signed integer.
Parameter(s):
- value (Number): The integer value.
emitter.writeUInt32LE(value)
Write an 32-bit little endian unsigned integer.
Parameter(s):
- value (Number): The integer value.
emitter.writeUInt32BE(value)
Write an 32-bit big endian unsigned integer.
Parameter(s):
- value (Number): The integer value.
emitter.writeInt32LE(value)
Write an 32-bit little endian signed integer.
Parameter(s):
- value (Number): The integer value.
emitter.writeInt32BE(value)
Write an 32-bit big endian signed integer.
Parameter(s):
- value (Number): The integer value.
emitter.writeUIntLE(value, byteLength)
Write an custom byte length little endian unsigned integer.
Parameter(s):
- value (Number): The integer value.
- byteLength (Number): The byte size.
emitter.writeUIntBE(value, byteLength)
Write an custom byte length big endian unsigned integer.
Parameter(s):
- value (Number): The integer value.
- byteLength (Number): The byte size.
emitter.writeIntLE(value, byteLength)
Write an custom byte length little endian signed integer.
Parameter(s):
- value (Number): The integer value.
- byteLength (Number): The byte size.
emitter.writeIntBE(value, byteLength)
Write an custom byte length big endian signed integer.
Parameter(s):
- value (Number): The integer value.
- byteLength (Number): The byte size.
emitter.writeFloatLE(value)
Write a little-endian float.
Parameter(s):
- value (Number): The float value.
emitter.writeFloatBE(value)
Write a big-endian float.
Parameter(s):
- value (Number): The float value.
emitter.writeDoubleLE(value)
Write a little-endian double.
Parameter(s):
- value (Number): The float value.
emitter.writeDoubleBE(value)
Write a big-endian double.
Parameter(s):
- value (Number): The float value.
MergeBufferBlocks(blocks)
Merge buffer blocks to one buffer.
Parameter(s):
- blocks (Buffer[]): The blocks to be concatencated.
Return value:
- (Buffer) The concatencated buffer.