MemStream.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * 内存数据流,用于数据流解析。
  3. * @param buffer
  4. * @constructor
  5. */
  6. function MemStream(buffer) {
  7. this.curOffset = 0;
  8. this.data = buffer;
  9. this.bLittleEndian = this.isLittleEndian();
  10. };
  11. MemStream.prototype.TWO_POW_MINUS23 = Math.pow(2, -23);
  12. MemStream.prototype.TWO_POW_MINUS126 = Math.pow(2, -126);
  13. MemStream.prototype.isLittleEndian = function(){
  14. var buffer = new ArrayBuffer(2),
  15. bytes = new Uint8Array(buffer),
  16. ints = new Uint16Array(buffer);
  17. bytes[0] = 1;
  18. return ints[0] === 1;
  19. };
  20. MemStream.prototype.readByte_1 = function(){
  21. return this.data[this.curOffset ++] & 0xff;
  22. };
  23. MemStream.prototype.readUChar8_1 = function(){
  24. return this.data[this.curOffset ++] & 0xff;
  25. };
  26. MemStream.prototype.readUInt32_1 = function( ) {
  27. var i = this.readByte();
  28. i |= this.readByte() << 8;
  29. i |= this.readByte() << 16;
  30. return i | (this.readByte() << 24);
  31. };
  32. MemStream.prototype.readInt32_1 = function( ) {
  33. var i = this.readByte();
  34. i |= this.readByte() << 8;
  35. i |= this.readByte() << 16;
  36. return i | (this.readByte() << 24);
  37. };
  38. MemStream.prototype.readFloat_1 = function( ) {
  39. var m = this.readByte();
  40. m += this.readByte() << 8;
  41. var b1 = this.readByte();
  42. var b2 = this.readByte();
  43. m += (b1 & 0x7f) << 16;
  44. var e = ( (b2 & 0x7f) << 1) | ( (b1 & 0x80) >>> 7);
  45. var s = b2 & 0x80? -1: 1;
  46. if (e === 255){
  47. return m !== 0? NaN: s * Infinity;
  48. }
  49. if (e > 0){
  50. return s * (1 + (m * this.TWO_POW_MINUS23) ) * Math.pow(2, e - 127);
  51. }
  52. if (m !== 0){
  53. return s * m * this.TWO_POW_MINUS126;
  54. }
  55. return s * 0;
  56. };
  57. MemStream.prototype.readUChar8 = function( ) {
  58. var charArray = new Uint8Array( this.data, this.curOffset, 1 );
  59. this.curOffset +=1;
  60. return charArray[ 0 ];
  61. };
  62. MemStream.prototype.readUInt32 = function( ) {
  63. var dataView = new DataView(this.data, this.curOffset, 4 );
  64. var v2 = dataView.getUint32(0,true); // 以大端读出,默认是小端
  65. this.curOffset +=4;
  66. return v2;
  67. };
  68. MemStream.prototype.readInt32 = function( ) {
  69. var dataView = new DataView(this.data, this.curOffset, 4 );
  70. this.curOffset +=4;
  71. return dataView.getInt32(0,true);
  72. };
  73. MemStream.prototype.readInt16 = function( ) {
  74. var dataView = new DataView(this.data, this.curOffset, 2 );
  75. this.curOffset +=2;
  76. return dataView.getInt16(0,true);
  77. };
  78. MemStream.prototype.readUInt16 = function( ) {
  79. var dataView = new DataView(this.data, this.curOffset, 2 );
  80. this.curOffset +=2;
  81. return dataView.getUint16(0,true);
  82. };
  83. MemStream.prototype.readFloat = function( ) {
  84. var dataView = new DataView(this.data, this.curOffset, 4);
  85. this.curOffset += 4;
  86. return dataView.getFloat32(0,true);
  87. };
  88. MemStream.prototype.readDouble = function( ) {
  89. var dataView = new DataView(this.data, this.curOffset, 8);
  90. this.curOffset += 8;
  91. return dataView.getFloat64(0,true);
  92. };
  93. MemStream.prototype.readFloat32Array = function(nLen) {
  94. var nByteLen = nLen * 4;
  95. var dataView = new DataView(this.data, this.curOffset, nByteLen);
  96. this.curOffset += nByteLen;
  97. return dataView;
  98. };
  99. MemStream.prototype.readFloat64Array = function(nLen) {
  100. var nByteLen = nLen * 8;
  101. var dataView = new DataView(this.data,this.curOffset, nByteLen);
  102. this.curOffset += nByteLen;
  103. return dataView;
  104. };
  105. MemStream.prototype.readUInt16Array = function(nLen) {
  106. var nByteLen = nLen * 2;
  107. var dataView = new DataView(this.data, this.curOffset, nByteLen);
  108. this.curOffset += nByteLen;
  109. return dataView;
  110. };
  111. MemStream.prototype.readUInt32Array = function(nLen) {
  112. var nByteLen = nLen * 4;
  113. //var nByteLen2 = nLen * Uint32Array.BYTES_PER_ELEMENT;
  114. var dataView = new DataView(this.data, this.curOffset, nByteLen);
  115. this.curOffset += nByteLen;
  116. return dataView;
  117. };
  118. MemStream.prototype.readUChar8Array = function(nLen) {
  119. var nByteLen = nLen * 1;
  120. var dataView = new DataView(this.data, this.curOffset, nByteLen);
  121. this.curOffset += nByteLen;
  122. return dataView;
  123. };
  124. MemStream.prototype.readUChar8Array2 = function(nLen) {
  125. var charArray = new Uint8Array( this.data, this.curOffset, nLen );
  126. this.curOffset +=nLen;
  127. return charArray;
  128. };
  129. MemStream.prototype.readString = function() {
  130. var strLen = this.readUInt32();
  131. var charArray = new Uint8Array( this.data, this.curOffset, strLen );
  132. var text = "";
  133. for ( var i = 0; i < strLen; i ++ ) {
  134. text += String.fromCharCode( charArray[ i ] );
  135. }
  136. this.curOffset += strLen;
  137. return text;
  138. };