Javascript Binary Reader to read and manipulate binary data.
This library supports:
- little/big endian
- seek and tell
- strings and chars in different encodings
- blobs and chunks
- really fast, because it avoids copying underlaying buffer
- takes care of byteOffset
- chainable :D
Install with yarn: yarn add opusonline-binary.js
let binary = new Binary(16);
binary.setString('hello world');
binary.setByte(33);
binary.skip();
binary.setByte(32);
binary.setChar(':');
binary.setChar('D');
binary.tell();
// gives 16
binary.seek(0).getBytes(4);
// gives [104, 101, 108, 108]
binary.skip(10).getUint16();
// gives 14916
let blob = binary.getBlob('text/plain');
// gives Blob {type: "text/plain", size: 16, …}
Same, but chained:
new Binary(16)
.setString('hello world')
.setByte(33)
.skip()
.setByte(32)
.setChar(':')
.setChar('D')
.seek(0)
.getString();
// gives "hello world �:D"
input
can be:
- Array
- TypedArray like DataView or Uint8Array etc.
- ArrayBuffer
- number as the size for a new binary
Binary Object has variables length
, position
, littleEndian
.
getByte
, getUint8
, getInt8
, getUint16
, getInt16
, getUint32
, getInt32
, getFloat32
, getFloat64
, getBytes
, getChar
, getString
, getBlob
, getChunk
, getBuffer
, toString
setByte
, setUint8
, setInt8
, setUint16
, setInt16
, setUint32
, setInt32
, setFloat32
, setFloat64
, setBytes
, setChar
, setString
, fill
, copy
, setLittleEndian
, setBigEndian
seek
, tell
, skip
Returns 1 unsigned byte.
Sets value as 1 unsigned byte.
Reads 2 unsigned bytes and returns as 1 number.
If littleEndian is not provided, the property of the constructed binary object is taken.
- getInt8() / setInt8(value)
- getInt16([littleEndian = Binary.littleEndian]) / setInt16(value)
- getUint32([littleEndian = Binary.littleEndian]) / setUint32(value)
- getInt32([littleEndian = Binary.littleEndian]) / setInt32(value)
- getFloat32([littleEndian = Binary.littleEndian]) / setFloat32(value)
- getFloat64([littleEndian = Binary.littleEndian]) / setFloat64(value)
Returns a new Uint8Array.
If length
is not provided, the full length until the end is assumed.
If copy
is true
the Uint8Array holds a copy of the ArrayBuffer in the desired length.
Returns a character value.
If TextDecoder
is available you can choose every encoding.
Otherwise encoding
supports only utf-8
and binary
(raw).
Returns a string.
If length
is not provided, the full length until the end is assumed.
If TextDecoder
is available you can choose every encoding.
Otherwise encoding
supports only utf-8
and binary
(raw).
Returns a string.
If length
is not provided, the full length until the end is assumed.
If TextDecoder
is available you can choose every encoding.
Otherwise encoding
supports only utf-8
and binary
(raw).
Returns a new Blob generated from the complete underlaying ArrayBuffer.
Returns a new UintArray with a copy of the ArrayBuffer in the desired length.
With from
and to
the offset is given. With length
it starts from current position.
Returns the complete underlaying ArrayBuffer.
input
can be:
- Array
- TypedArray like DataView or Uint8Array etc.
- ArrayBuffer
Sets a character value at current position.
If TextDecoder
is available you can choose every encoding.
Otherwise encoding
supports only utf-8
and binary
(raw).
Sets a string.
If TextDecoder
is available you can choose every encoding.
Otherwise encoding
supports only utf-8
and binary
(raw).
value
can be
- any number, but will be cut of as uint8
- any character or string, but only first character will be used and cut of as uint8
Fills binary with provided value from current position for length
bytes.
If length
is not provided, the full length until the end is assumed.
Copies given bytes strictly (blind) to target
binary.
target
must be another binary.
length
defaults to length of source bytes from sourceStart
. If source length is higher than target size starting from targetFrom
an Error
is thrown.
Changes binary object to desired mode.
Sets position for next operations on binary object.
Returns current position.
Shifts the current operation position by given length.