DataViewReader.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // Simple wrapper around DataView that auto-advances the read offset and provides
  3. // a few common data type conversions specific to this app
  4. //
  5. DataViewReader = (function ()
  6. {
  7. function DataViewReader(data_view, offset)
  8. {
  9. this.DataView = data_view;
  10. this.Offset = offset;
  11. }
  12. DataViewReader.prototype.GetUInt32 = function ()
  13. {
  14. var v = this.DataView.getUint32(this.Offset, true);
  15. this.Offset += 4;
  16. return v;
  17. }
  18. DataViewReader.prototype.GetUInt64 = function ()
  19. {
  20. var v = this.DataView.getFloat64(this.Offset, true);
  21. this.Offset += 8;
  22. return v;
  23. }
  24. DataViewReader.prototype.GetStringOfLength = function (string_length)
  25. {
  26. var string = "";
  27. for (var i = 0; i < string_length; i++)
  28. {
  29. string += String.fromCharCode(this.DataView.getInt8(this.Offset));
  30. this.Offset++;
  31. }
  32. return string;
  33. }
  34. DataViewReader.prototype.GetString = function ()
  35. {
  36. var string_length = this.GetUInt32();
  37. return this.GetStringOfLength(string_length);
  38. }
  39. return DataViewReader;
  40. })();