bit-buffer
Advanced tools
Comparing version
@@ -35,3 +35,3 @@ declare module 'bit-buffer' { | ||
writeBoolean(value: number); | ||
writeBoolean(value: boolean); | ||
@@ -65,3 +65,7 @@ writeInt8(value: number); | ||
readArrayBuffer(byteLength: number): Uint8Array; | ||
writeBitStream(stream: BitStream, length?: number); | ||
writeArrayBuffer(buffer: BitStream, length?: number); | ||
} | ||
} |
@@ -418,2 +418,16 @@ (function (root) { | ||
}; | ||
BitStream.prototype.writeBitStream = function(stream, length) { | ||
if (!length) { | ||
length = stream.bitsLeft; | ||
} | ||
var bitsToWrite; | ||
while (length > 0) { | ||
bitsToWrite = Math.min(length, 32); | ||
this.writeBits(stream.readBits(bitsToWrite), bitsToWrite); | ||
length -= bitsToWrite; | ||
} | ||
}; | ||
BitStream.prototype.readArrayBuffer = function(byteLength) { | ||
@@ -425,2 +439,6 @@ var buffer = this._view.getArrayBuffer(this._index, byteLength); | ||
BitStream.prototype.writeArrayBuffer = function(buffer, byteLength) { | ||
this.writeBitStream(new BitStream(buffer), byteLength * 8); | ||
}; | ||
// AMD / RequireJS | ||
@@ -427,0 +445,0 @@ if (typeof define !== 'undefined' && define.amd) { |
{ | ||
"name": "bit-buffer", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Bit-level reads and writes for ArrayBuffers", | ||
@@ -5,0 +5,0 @@ "main": "bit-buffer.js", |
47
test.js
@@ -297,2 +297,34 @@ var assert = require('assert'), | ||
test('writeBitStream', function () { | ||
var sourceStream = new BitStream(new ArrayBuffer(64)); | ||
sourceStream.writeBits(0xF0, 8); //0b11110000 | ||
sourceStream.writeBits(0xF1, 8); //0b11110001 | ||
sourceStream.index = 0; | ||
sourceStream.readBits(3); //offset | ||
bsr.writeBitStream(sourceStream, 8); | ||
assert.equal(8, bsr.index); | ||
bsr.index = 0; | ||
assert.equal(bsr.readBits(6), 0x3E); //0b00111110 | ||
assert.equal(11, sourceStream.index); | ||
}); | ||
test('writeBitStream long', function () { | ||
var sourceStream = new BitStream(new ArrayBuffer(64)); | ||
sourceStream.writeBits(0xF0, 8); | ||
sourceStream.writeBits(0xF1, 8); | ||
sourceStream.writeBits(0xF1, 8); | ||
sourceStream.writeBits(0xF1, 8); | ||
sourceStream.writeBits(0xF1, 8); | ||
sourceStream.index = 0; | ||
sourceStream.readBits(3); //offset | ||
bsr.index = 3; | ||
bsr.writeBitStream(sourceStream, 35); | ||
assert.equal(38, bsr.index); | ||
bsr.index = 3; | ||
assert.equal(bsr.readBits(35), 1044266558); | ||
assert.equal(38, sourceStream.index); | ||
}); | ||
test('readArrayBuffer', function () { | ||
@@ -312,2 +344,17 @@ bsw.writeBits(0xF0, 8); //0b11110000 | ||
test('writeArrayBuffer', function () { | ||
var source = new Uint8Array(4); | ||
source[0]=0xF0; | ||
source[1]=0xF1; | ||
source[2]=0xF1; | ||
bsr.readBits(3); //offset | ||
bsr.writeArrayBuffer(source.buffer, 2); | ||
assert.equal(19, bsr.index); | ||
bsr.index = 0; | ||
assert.equal(bsr.readBits(8), 128); | ||
}); | ||
test('Get buffer from view', function() { | ||
@@ -314,0 +361,0 @@ bv.setBits(0, 0xFFFFFFFF, 32); |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
80962
2.34%718
8.3%0
-100%