bufferstream.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. function BufferStream(buffer, offset, length, bigEndian) {
  2. this.buffer = buffer;
  3. this.offset = offset || 0;
  4. length = typeof length === 'number' ? length : buffer.length;
  5. this.endPosition = this.offset + length;
  6. this.setBigEndian(bigEndian);
  7. }
  8. BufferStream.prototype = {
  9. setBigEndian: function(bigEndian) {
  10. this.bigEndian = !!bigEndian;
  11. },
  12. nextUInt8: function() {
  13. var value = this.buffer.readUInt8(this.offset);
  14. this.offset += 1;
  15. return value;
  16. },
  17. nextInt8: function() {
  18. var value = this.buffer.readInt8(this.offset);
  19. this.offset += 1;
  20. return value;
  21. },
  22. nextUInt16: function() {
  23. var value = this.bigEndian ? this.buffer.readUInt16BE(this.offset) : this.buffer.readUInt16LE(this.offset);
  24. this.offset += 2;
  25. return value;
  26. },
  27. nextUInt32: function() {
  28. var value = this.bigEndian ? this.buffer.readUInt32BE(this.offset) : this.buffer.readUInt32LE(this.offset);
  29. this.offset += 4;
  30. return value;
  31. },
  32. nextInt16: function() {
  33. var value = this.bigEndian ? this.buffer.readInt16BE(this.offset) : this.buffer.readInt16LE(this.offset);
  34. this.offset += 2;
  35. return value;
  36. },
  37. nextInt32: function() {
  38. var value = this.bigEndian ? this.buffer.readInt32BE(this.offset) : this.buffer.readInt32LE(this.offset);
  39. this.offset += 4;
  40. return value;
  41. },
  42. nextFloat: function() {
  43. var value = this.bigEndian ? this.buffer.readFloatBE(this.offset) : this.buffer.readFloatLE(this.offset);
  44. this.offset += 4;
  45. return value;
  46. },
  47. nextDouble: function() {
  48. var value = this.bigEndian ? this.buffer.readDoubleBE(this.offset) : this.buffer.readDoubleLE(this.offset);
  49. this.offset += 8;
  50. return value;
  51. },
  52. nextBuffer: function(length) {
  53. var value = this.buffer.slice(this.offset, this.offset + length);
  54. this.offset += length;
  55. return value;
  56. },
  57. remainingLength: function() {
  58. return this.endPosition - this.offset;
  59. },
  60. nextString: function(length) {
  61. var value = this.buffer.toString('utf8', this.offset, this.offset + length);
  62. this.offset += length;
  63. return value;
  64. },
  65. mark: function() {
  66. var self = this;
  67. return {
  68. openWithOffset: function(offset) {
  69. offset = (offset || 0) + this.offset;
  70. return new BufferStream(self.buffer, offset, self.endPosition - offset, self.bigEndian);
  71. },
  72. offset: this.offset
  73. };
  74. },
  75. offsetFrom: function(marker) {
  76. return this.offset - marker.offset;
  77. },
  78. skip: function(amount) {
  79. this.offset += amount;
  80. },
  81. branch: function(offset, length) {
  82. length = typeof length === 'number' ? length : this.endPosition - (this.offset + offset);
  83. return new BufferStream(this.buffer, this.offset + offset, length, this.bigEndian);
  84. }
  85. };
  86. module.exports = BufferStream;