nativeDataStream.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /** @internal */
  2. export class NativeDataStream {
  3. constructor() {
  4. const buffer = new ArrayBuffer(NativeDataStream.DEFAULT_BUFFER_SIZE);
  5. this._uint32s = new Uint32Array(buffer);
  6. this._int32s = new Int32Array(buffer);
  7. this._float32s = new Float32Array(buffer);
  8. this._length = NativeDataStream.DEFAULT_BUFFER_SIZE / 4;
  9. this._position = 0;
  10. this._nativeDataStream = new _native.NativeDataStream(() => {
  11. this._flush();
  12. });
  13. }
  14. /**
  15. * Writes a uint32 to the stream
  16. * @param value the value to write
  17. */
  18. writeUint32(value) {
  19. this._flushIfNecessary(1);
  20. this._uint32s[this._position++] = value;
  21. }
  22. /**
  23. * Writes an int32 to the stream
  24. * @param value the value to write
  25. */
  26. writeInt32(value) {
  27. this._flushIfNecessary(1);
  28. this._int32s[this._position++] = value;
  29. }
  30. /**
  31. * Writes a float32 to the stream
  32. * @param value the value to write
  33. */
  34. writeFloat32(value) {
  35. this._flushIfNecessary(1);
  36. this._float32s[this._position++] = value;
  37. }
  38. /**
  39. * Writes a uint32 array to the stream
  40. * @param values the values to write
  41. */
  42. writeUint32Array(values) {
  43. this._flushIfNecessary(1 + values.length);
  44. this._uint32s[this._position++] = values.length;
  45. this._uint32s.set(values, this._position);
  46. this._position += values.length;
  47. }
  48. /**
  49. * Writes an int32 array to the stream
  50. * @param values the values to write
  51. */
  52. writeInt32Array(values) {
  53. this._flushIfNecessary(1 + values.length);
  54. this._uint32s[this._position++] = values.length;
  55. this._int32s.set(values, this._position);
  56. this._position += values.length;
  57. }
  58. /**
  59. * Writes a float32 array to the stream
  60. * @param values the values to write
  61. */
  62. writeFloat32Array(values) {
  63. this._flushIfNecessary(1 + values.length);
  64. this._uint32s[this._position++] = values.length;
  65. this._float32s.set(values, this._position);
  66. this._position += values.length;
  67. }
  68. /**
  69. * Writes native data to the stream
  70. * @param handle the handle to the native data
  71. */
  72. writeNativeData(handle) {
  73. this._flushIfNecessary(handle.length);
  74. this._uint32s.set(handle, this._position);
  75. this._position += handle.length;
  76. }
  77. /**
  78. * Writes a boolean to the stream
  79. * @param value the value to write
  80. */
  81. writeBoolean(value) {
  82. this.writeUint32(value ? 1 : 0);
  83. }
  84. _flushIfNecessary(required) {
  85. if (this._position + required > this._length) {
  86. this._flush();
  87. }
  88. }
  89. _flush() {
  90. this._nativeDataStream.writeBuffer(this._uint32s.buffer, this._position);
  91. this._position = 0;
  92. }
  93. }
  94. // Must be multiple of 4!
  95. // eslint-disable-next-line @typescript-eslint/naming-convention
  96. NativeDataStream.DEFAULT_BUFFER_SIZE = 65536;
  97. //# sourceMappingURL=nativeDataStream.js.map