class binary.ByteArray
Includes
Methods
Constructor
new binary.ByteArray(length)
new binary.ByteArray(bytes)
new binary.ByteArray(string[, charset="UTF-8"])
-
length(Number) – length of new string -
bytes(Array|binary.Binary) – contents of new blob.
Create a new ByteArray. length creates a new ByteArray of this length where every byte is 0. bytes is either another blob to copy, or an array of byte values. The third form is constructed by encoding string to bytes using charset via the encodings module.
ByteArrays are mutable, both in terms of length and of the data contained within.
Instance methods
-
append #
binary.ByteArray#append(args...) ⇒ Number-
args(binary.Binary|Byte|Array) – data to append to this one
Concatenate the provided arguments onto the end of the existing blob.
Each argument must be a Byte, an Array of Bytes or another blob.
Return the new length of the blob.
Aliased as:
binary.ByteArray#push -
-
count #
binary.ByteArray#count(callback[, thisobj]) ⇒ Number-
callback(Function) – callback -
thisobj(?) – invocant forcallback
Calls the provided
callbackfunction once for each byte in the blob, in order, returns the number of bytes for whichcallbackreturned a truthful value.The callback is called with arguments of:
- byte (
Number): the current byte - index (
Number): the byte offset from the start of the array - blob (
binary.ByteArray): the blob
-
-
displace #
binary.ByteArray#displace(begin[, end[, args...]]) ⇒ Number-
begin(Number) – start of range -
end(Number) – end of range -
args(binary.Binary|Byte|Array) – data to insert atbegin.
Remove the data in the range [
begin,end), and optionally insert new data started atbegin. Original data afterendis preserved.Returns the new length of the blob. If you want the deleted data instead, use splice
Note This method is perhaps badly named.
-
-
erase #
binary.ByteArray#erase(begin[, end]) ⇒ Number-
begin(Number) – start of range -
end(Number) – end of range
Erase a range of bytes [
begin,end) form this blob. Bytes followingendwill be shifted down to start at indexbeginand the length will be reduced.Returns the new length of the blob.
-
-
every #
binary.ByteArray#every(callback[, thisobj]) ⇒ Boolean-
callback(Function) – callback -
thisobj(?) – invocant forcallback
Invoke
callback(with an invocant ofthisobjif provided) and return true iff the callback returns a truthful value for every byte in the array.The callback is called with arguments of:
- byte (
Number): the current byte - index (
Number): the byte offset from the start of the array - blob (
binary.ByteArray): the blob
-
-
filter #
binary.ByteArray#filter(callback[, thisobj]) ⇒ binary.ByteArray-
callback(Function) – filter callback function -
thisobj(?) – optional invocant forcallback
Calls the provided
callbackfunction for each byte in the blob, in order, and constructs a new blob containing only the bytes for which the callback returned a truthful value.The callback is called with arguments of:
- byte (
Number): the current byte - index (
Number): the byte offset from the start of the array - blob (
binary.ByteArray): the blob
-
-
forEach #
binary.ByteArray#forEach(callback[, thisobj]) ⇒ undefined-
callback(Function) – callback -
thisobj(?) – invocant forcallback
Call the
callbackfunction once for each byte, in order, with the invocant optionally set tothisobj. The return value of the callback is ignored.The callback is called with arguments of:
- byte (
Number): the current byte - index (
Number): the byte offset from the start of the array - blob (
binary.ByteArray): the blob
-
-
insert #
binary.ByteArray#insert(pos, args...) ⇒ Number-
pos(Number) – position -
args(binary.Binary|Byte|Array) – data to insert
Insert data into the byte array before
pos. Equivalent to callingblob.displace(pos,pos,args...)Returns the new length of the blob.
-
-
map #
binary.ByteArray#map(callback[, thisobj]) ⇒ binary.ByteArray-
callback(Function) – callback -
thisobj(?) – invocant forcallback
Calls the provided
callbackfunction once for each byte in the blob, in order, and constructs a new blob from the results. The callback must return something that could be passed to binary.ByteArray.append which means that the new byte array could be longer or shorter then the original.The callback is called with arguments of:
- byte (
Number): the current byte - index (
Number): the byte offset from the start of the array - blob (
binary.ByteArray): the blob
-
-
pop #
binary.ByteArray#pop() ⇒ ByteRemove the final byte from the blob and return it. If the blob is empty an exception will be thrown.
This doesn’t make a huge amount of sense on a ByteArray.
-
prepend #
binary.ByteArray#prepend(args...) ⇒ Number-
args(binary.Binary|Byte|Array) – data to append to this one
Prepend the concatenation of the provided arguments to the contents of this blob.
Return the new length of the blob.
Aliased as:
binary.ByteArray#unshift -
-
push #
binary.ByteArray#push(args...) ⇒ Number-
args(binary.Binary|Byte|Array) – data to append to this blob
Provided for API compatibility with Array.
Alias of:
binary.ByteArray#append -
-
reduce #
binary.ByteArray#reduce(callback[, initialValue]) ⇒ ?-
callback(Function) – callback -
initialValue(?) – initial value passed tocallback
Apply a function against an accumulator and each byte of the blob (from left-to-right) as to reduce it to a single value.
Executes the
callbackfunction once for each blob in the blob, receiving four arguments: the initial value (or value from the previous callback call), the current byte, the current index, and the blob over which iteration is occurring.The return value is whaterver is returned from the last iteration.
The callback is called with arguments of:
- value (
?): value returned from previous callback, orinitialValue. - byte (
Number): the current byte - index (
Number): the byte offset from the start of the array - blob (
binary.ByteArray): the blob
-
-
reduceRight #
binary.ByteArray#reduceRight(callback[, initialValue]) ⇒ ?Apply a function against an accumulator and each byte of the blob (from right-to-left) as to reduce it to a single value. See reduce for a more detailed description.
-
reverse #
binary.ByteArray#reverse() ⇒ binary.ByteArrayReverse the order of the bytes of this array in-place, and return it.
This behaves the same as Array#reverse.
This doesn’t make a huge amount of sense on a ByteArray.
-
shift #
binary.ByteArray#shift() ⇒ NumberRemove and return the first byte of the blob. Will throw an exception if it is empty.
This doesn’t make a huge amount of sense on a ByteArray.
-
some #
binary.ByteArray#some(callback[, thisobj]) ⇒ Boolean-
callback(Function) – callback -
thisobj(?) – invocant forcallback
Calls the provided
callbackfunction for each byte in the blob, in order, and returns true as soon as the callback returns a truthful value. Will return false if no true value is returned.The callback is called with arguments of:
- byte (
Number): the current byte - index (
Number): the byte offset from the start of the array - blob (
binary.ByteArray): the blob
-
-
sort #
binary.ByteArray#sort([sorter]) ⇒ binary.ByteArray-
sorter(Function) – comparision function
This behaves the same as Array#sort.
This doesn’t make a huge amount of sense on a ByteArray.
-
-
splice #
binary.ByteArray#splice(begin[, end[, args...]]) ⇒ binary.ByteArray-
begin(Number) – start of range -
end(Number) – end of range -
args(binary.Binary|Byte|Array) – data to insert
Remove the data in the range [
begin,end), and optionally insert new data started atbegin. Original data afterendis preserved.This method is similar to displace but it returns the removed bytes instead of the new length.
-
-
unshift #
binary.ByteArray#unshift(args...) ⇒ Number-
args(binary.Binary|Byte|Array) – data to prepend to this one
Insert new data before the start of the current data
Return the new length of the blob.
Alias of:
binary.ByteArray#prepend -