Data
Description
A chunk of bytes.
Once created, you can't alter a byte in it, but you can cut it short or enlarge it by joining to other Data objects.
Conversion from/to strings
To convert from a string, you can construct a Data object or call Data.from() with the string as parameter, plus an optional encoding parameter. The following encodings are supported:
- "utf8" (default) - As a UTF-8 encoded string.
- "hex" - As hexadecimal numbers.
- "base64" - Base64 representation.
- "base64url" - Base64url representation.
To convert to a string, call toString with one of the above encodings as the optional encoding parameter.
Create binary data using script
To make raw binary data from scratch, first, create an array of numbers, each representing a byte, and then construct a Data object with the array as parameter.
After a Data object is created, you can also call push() to concatenate it with other Data objects.
Constructor
new Data()
Creates an instance of Data.
new Data(bytes)
Creates an instance of Data from an array of bytes.
new Data(text, encoding?)
Creates an instance of Data from a string.
new Data(data)
Creates an instance of Data by copying bytes from another Data object.
Static methods
push(bytes)
Appends bytes.
shift(count)
Removes bytes from the beginning.
shiftTo(callback)
Removes bytes from the beginning up to the first byte where user callback returns true.
shiftWhile(callback)
Removes bytes from the beginning until user callback returns false.
toArray()
Converts to an array of bytes.
toString(encoding?)
Converts to a string.
Properties
sizeNumber of bytes.
Type of the event.
Methods
push(bytes)
Appends bytes.
shift(count)
Removes bytes from the beginning.
shiftTo(callback)
Removes bytes from the beginning up to the first byte where user callback returns true.
shiftWhile(callback)
Removes bytes from the beginning until user callback returns false.
toArray()
Converts to an array of bytes.
toString(encoding?)
Converts to a string.