Skip to content

Instantly share code, notes, and snippets.

@BonsaiDen
Created January 12, 2012 15:46
Show Gist options
  • Save BonsaiDen/1601201 to your computer and use it in GitHub Desktop.
Save BonsaiDen/1601201 to your computer and use it in GitHub Desktop.
BitStream
/*
* Copyright (c) 2011 Ivo Wetzel.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
var BitStream = (function() {
var chrTable = new Array(255);
for(var i = 0; i < 256; i++) {
chrTable[i] = String.fromCharCode(i);
}
var maskTable = new Array(8),
powTable = new Array(8);
for(var i = 0; i < 9; i++) {
maskTable[i] = ~((powTable[i] = Math.pow(2, i) - 1) ^ 0xFF);
}
var stream = '',
value = 0,
left = 8;
return {
open: function(data) {
left = 8;
if (data !== undefined) {
index = 0;
stream = data;
value = stream.charCodeAt(index);
} else {
value = 0;
stream = '';
}
},
end: function() {
if (value > 0) {
stream += chrTable[value];
}
return stream;
},
write: function write(val, count) {
var overflow = count - left,
use = left < count ? left : count,
shift = left - use;
if (overflow > 0) {
value += val >> overflow << shift;
} else {
value += val << shift;
}
left -= use;
if (left === 0) {
stream += chrTable[value];
left = 8;
value = 0;
if (overflow > 0) {
write(val & powTable[overflow], overflow);
}
}
},
read: function read(count) {
var overflow = count - left,
use = left < count ? left : count,
shift = left - use;
var val = (value & maskTable[left]) >> shift;
left -= use;
if (left === 0) {
value = stream.charCodeAt(++index);
left = 8;
if (overflow > 0) {
console.log('Wrap')
val = val << overflow | read(overflow);
}
}
console.log(val)
return val;
}
};
})();
function bin(value, len) {
var bin = value.toString(2);
return new Array((len + 1) - bin.length).join('0') + bin;
}
BitStream.open();
BitStream.write(1, 2)
BitStream.write(2, 4)
BitStream.write(15, 4)
BitStream.write(15, 4)
BitStream.write(3, 2)
BitStream.write(1, 1)
var data = BitStream.end();
BitStream.open(data);
BitStream.read(2);
BitStream.read(4);
BitStream.read(4);
BitStream.read(4);
BitStream.read(2);
BitStream.read(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment