bits.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. if (!dojo._hasResource["dojox.encoding.bits"]) { // _hasResource checks added
  2. // by build. Do not use
  3. // _hasResource directly in
  4. // your code.
  5. dojo._hasResource["dojox.encoding.bits"] = true;
  6. dojo.provide("dojox.encoding.bits");
  7. dojox.encoding.bits.OutputStream = function() {
  8. this.reset();
  9. };
  10. dojo.extend(dojox.encoding.bits.OutputStream, {
  11. reset : function() {
  12. this.buffer = [];
  13. this.accumulator = 0;
  14. this.available = 8;
  15. },
  16. putBits : function(value, width) {
  17. while (width) {
  18. var w = Math.min(width, this.available);
  19. var v = (w <= width ? value >>> (width - w) : value) << (this.available - w);
  20. this.accumulator |= v & (255 >>> (8 - this.available));
  21. this.available -= w;
  22. if (!this.available) {
  23. this.buffer.push(this.accumulator);
  24. this.accumulator = 0;
  25. this.available = 8;
  26. }
  27. width -= w;
  28. }
  29. },
  30. getWidth : function() {
  31. return this.buffer.length * 8 + (8 - this.available);
  32. },
  33. getBuffer : function() {
  34. var b = this.buffer;
  35. if (this.available < 8) {
  36. b.push(this.accumulator & (255 << this.available));
  37. }
  38. this.reset();
  39. return b;
  40. }
  41. });
  42. dojox.encoding.bits.InputStream = function(buffer, width) {
  43. this.buffer = buffer;
  44. this.width = width;
  45. this.bbyte = this.bit = 0;
  46. };
  47. dojo.extend(dojox.encoding.bits.InputStream, {
  48. getBits : function(width) {
  49. var r = 0;
  50. while (width) {
  51. var w = Math.min(width, 8 - this.bit);
  52. var v = this.buffer[this.bbyte] >>> (8 - this.bit - w);
  53. r <<= w;
  54. r |= v & ~(~0 << w);
  55. this.bit += w;
  56. if (this.bit == 8) {
  57. ++this.bbyte;
  58. this.bit = 0;
  59. }
  60. width -= w;
  61. }
  62. return r;
  63. },
  64. getWidth : function() {
  65. return this.width - this.bbyte * 8 - this.bit;
  66. }
  67. });
  68. }