ByteStream is a library making possible to manipulates single bytes and bits on pure JavaScript
npm install bytestreamjs   
_ByteStream_ class is the major class for all others. It provides many useful function for making search for patterns and data transformation. Optimized for fast as possible data processing.
Method | Description
-------|--------------
_clear_|Clear existing stream
_fromArrayBuffer_|Initialize "Stream" object from existing "ArrayBuffer"
_fromUint8Array_|Initialize "Stream" object from existing "Uint8Array"
_fromString_|Initialize "Stream" object from existing string
_toString_|Represent "Stream" object content as a string
_fromHexString_|Initialize "Stream" object from existing hexdecimal string
_toHexString_|Represent "Stream" object content as a hexdecimal string
_copy_|Return copy of existing "Stream"
_slice_|Return slice of existing "Stream"
_realloc_|Change size of existing "Stream"
_append_|Append a new "Stream" content to the current "Stream"
_insert_|Insert "Stream" content to the current "Stream" at specific position
_isEqual_|Check that two "Stream" objects has equal content
_isEqualView_|Check that current "Stream" objects has equal content with input "Uint8Array"
_findPattern_|Find any byte pattern in "Stream"
_findFirstIn_|Find first position of any pattern from input array
_findAllIn_|Find all positions of any pattern from input array
_findAllPatternIn_|Find all positions of a pattern
_findFirstNotIn_|Find first position of data, not included in patterns from input array
_findAllNotIn_|Find all positions of data, not included in patterns from input array
_findFirstSequence_|Find position of a sequence of any patterns from input array
_findAllSequences_|Find all positions of a sequence of any patterns from input array
_findPairedPatterns_|Find all paired patterns in the stream
_findPairedArrays_|Find all paired patterns in the stream
_replacePattern_|Replace one patter with other
_skipPatterns_|Skip any pattern from input array
_skipNotPatterns_|Skip any pattern not from input array
_SeqStream_ class is the aux class for sequential reading/writing data from/to _ByteStream_ underline class.
Method | Description
-------|--------------
_resetPosition_|Reset current position of the "SeqStream"
_findPattern_|Find any byte pattern in "ByteStream"
_findFirstIn_|Find first position of any pattern from input array
_findAllIn_|Find all positions of any pattern from input array
_findFirstNotIn_|Find first position of data, not included in patterns from input array
_findAllNotIn_|Find all positions of data, not included in patterns from input array
_findFirstSequence_|Find position of a sequence of any patterns from input array
_findAllSequences_|Find position of a sequence of any patterns from input array
_findPairedPatterns_|Find all paired patterns in the stream
_findPairedArrays_|Find all paired patterns in the stream
_replacePattern_|Replace one patter with other
_skipPatterns_|Skip of any pattern from input array
_skipNotPatterns_|Skip of any pattern from input array
_append_|Append a new "Stream" content to the current "Stream"
_appendView_|Append a "view" content to the current "Stream"
_appendChar_|Append a new char to the current "Stream"
_getBlock_|Get a block of data
_getUint32_|Get 4-byte unsigned integer value
Main purpose of the _BitStream_ is manipulating of each bit inside any _ByteStream_ data.
Method | Description
-------|--------------
_clear_|Clear existing stream
_fromByteStream_|Initialize "BitStream" by data from existing "ByteStream"
_fromArrayBuffer_|Initialize "BitStream" object from existing "ArrayBuffer"
_fromUint8Array_|Initialize "BitStream" object from existing "Uint8Array"
_fromString_|Initialize "BitStream" object from existing bit string
_toString_|Represent "BitStream" object content as a string
_shiftRight_|Shift entire "BitStream" value right to number of bits
_shiftLeft_|Shift entire "BitStream" value left to number of bits
_slice_|Return slice of existing "BitStream"
_copy_|Return copy of existing "BitStream"
_shrink_|Shrink unnecessary bytes in current stream accordingly to "bitsCount" value
_reverseBytes_|Reverse bits order in each byte in the stream
_reverseValue_|Reverse all bits in entire "BitStream"
_getNumberValue_|Trying to represent entire "BitStream" as an unsigned integer
_findPattern_|Find any bit pattern in "BitStream"
_findFirstIn_|Find first position of any pattern from input array
_findAllIn_|Find all positions of any pattern from input array
_findAllPatternIn_|Find all positions of a pattern
_findFirstNotIn_|Find first position of data, not included in patterns from input array
_findAllNotIn_|Find all positions of data, not included in patterns from input array
_findFirstSequence_|Find position of a sequence of any patterns from input array
_findAllSequences_|Find position of a sequence of any patterns from input array
_findPairedPatterns_|Find all paired patterns in the stream
_findPairedArrays_|Find all paired patterns in the stream
_replacePattern_|Replace one pattern with other
_skipPatterns_|Skip any pattern from input array
_skipNotPatterns_|Skip any pattern not from input array
_append_|Append a new "BitStream" content to the current "BitStream"
_SeqBitStream_ class is the aux class for sequential reading/writing data from/to _BitStream_ underline class.
Method | Description
-------|--------------
_getBits_|Get next "length" bits from the stream
_getBitsString_|Get string representation for the next "length" bits from the stream
_getBitsReversedValue_|Get number value representation of the next "length" bits from the stream, preliminary reversed
_toString_|Represent remaining bits in "BitStream" as a string
The parseByteMap function is intended to parse and check byte streams with determinated structure.
Example of _map_ used as a kind of _template_. Exactly this _map_ is using for parsing PDF _xref_ table:
``javascript
let map = [
{
type: "string",
name: "type",
minlength: 1,
maxlength: 1,
func: function(array){
let result = {
status: (-1),
length: 1
};
switch(array[0])
{
case 0x6E: // "n"
result.value = "n";
break;
case 0x66: // "f"
result.value = "f";
break;
default:
return result;
}
result.status = 1;
return result;
}
},
{
type: "check",
minlength: 1,
maxlength: 2,
func: function(array){
let position = (-1);
if(array[0] == 0x0A)
position = 1;
if(array[1] == 0x0A)
position = 2;
return {
status: (position > 0) ? 1 : (-1),
length: position
};
}
}
];
``
Copyright (c) 2016-2022, Peculiar Ventures
All rights reserved.
Author 2016-2018 Yury Strozhevsky.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.*