Skip to content

Instantly share code, notes, and snippets.

@lukaskollmer
Forked from TooTallNate/ref-buffer-type.js
Created January 2, 2019 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaskollmer/ae50b12987a80586af21fe676a94026e to your computer and use it in GitHub Desktop.
Save lukaskollmer/ae50b12987a80586af21fe676a94026e to your computer and use it in GitHub Desktop.
Fixed length "Buffer" type, for use in `ref-struct` type definitions.
var ref = require('ref');
module.exports = BufferType;
/**
* Fixed length "Buffer" type, for use in Struct type definitions.
*
* Optionally setting the `encoding` param will force to call
* `toString(encoding)` on the buffer returning a String instead.
*/
function BufferType (length, encoding) {
if (!(this instanceof BufferType)) return new BufferType(length);
this.size = length | 0;
this.encoding = encoding || null;
}
BufferType.prototype = Object.create(ref.types.byte, {
constructor: {
value: BufferType,
enumerable: false,
writable: true,
configurable: true
}
});
BufferType.prototype.get = function (buffer, offset) {
var buf = buffer.slice(offset, offset + this.size);
if (this.encoding !== null) {
buf = buf.toString(this.encoding);
}
return buf;
};
BufferType.prototype.set = function (buffer, offset, value) {
if ('string' === typeof value || Array.isArray(value)) {
value = new Buffer(value, this.encoding);
} else if (!Buffer.isBuffer(value)) {
throw new TypeError('Buffer instance expected');
}
if (value.length > this.size) {
throw new Error('Buffer given is ' + value.length + ' bytes, but only '
+ this.size + ' bytes available');
}
value.copy(buffer, offset);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment