BytesBuffer.hx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package haxe.io;
  23. @:coreApi
  24. class BytesBuffer {
  25. var buffer:js.lib.ArrayBuffer;
  26. var view:js.lib.DataView;
  27. var u8:js.lib.Uint8Array;
  28. var pos:Int;
  29. var size:Int;
  30. public var length(get, never):Int;
  31. public function new() {
  32. pos = 0;
  33. size = 0;
  34. }
  35. inline function get_length():Int {
  36. return pos;
  37. }
  38. public function addByte(byte:Int):Void {
  39. if (pos == size)
  40. grow(1);
  41. view.setUint8(pos++, byte);
  42. }
  43. public function add(src:Bytes):Void {
  44. if (pos + src.length > size)
  45. grow(src.length);
  46. if (size == 0)
  47. return;
  48. var sub = new js.lib.Uint8Array(@:privateAccess src.b.buffer, @:privateAccess src.b.byteOffset, src.length);
  49. u8.set(sub, pos);
  50. pos += src.length;
  51. }
  52. public function addString(v:String, ?encoding:Encoding):Void {
  53. add(Bytes.ofString(v, encoding));
  54. }
  55. public function addInt32(v:Int):Void {
  56. if (pos + 4 > size)
  57. grow(4);
  58. view.setInt32(pos, v, true);
  59. pos += 4;
  60. }
  61. public function addInt64(v:haxe.Int64):Void {
  62. if (pos + 8 > size)
  63. grow(8);
  64. view.setInt32(pos, v.low, true);
  65. view.setInt32(pos + 4, v.high, true);
  66. pos += 8;
  67. }
  68. public function addFloat(v:Float):Void {
  69. if (pos + 4 > size)
  70. grow(4);
  71. view.setFloat32(pos, v, true);
  72. pos += 4;
  73. }
  74. public function addDouble(v:Float):Void {
  75. if (pos + 8 > size)
  76. grow(8);
  77. view.setFloat64(pos, v, true);
  78. pos += 8;
  79. }
  80. public function addBytes(src:Bytes, pos:Int, len:Int):Void {
  81. if (pos < 0 || len < 0 || pos + len > src.length)
  82. throw Error.OutsideBounds;
  83. if (this.pos + len > size)
  84. grow(len);
  85. if (size == 0)
  86. return;
  87. var sub = new js.lib.Uint8Array(@:privateAccess src.b.buffer, @:privateAccess src.b.byteOffset + pos, len);
  88. u8.set(sub, this.pos);
  89. this.pos += len;
  90. }
  91. function grow(delta:Int):Void {
  92. var req = pos + delta;
  93. var nsize = size == 0 ? 16 : size;
  94. while (nsize < req)
  95. nsize = (nsize * 3) >> 1;
  96. var nbuf = new js.lib.ArrayBuffer(nsize);
  97. var nu8 = new js.lib.Uint8Array(nbuf);
  98. if (size > 0)
  99. nu8.set(u8);
  100. size = nsize;
  101. buffer = nbuf;
  102. u8 = nu8;
  103. view = new js.lib.DataView(buffer);
  104. }
  105. public function getBytes():Bytes@:privateAccess {
  106. if (size == 0)
  107. return haxe.io.Bytes.alloc(0);
  108. var b = new Bytes(buffer);
  109. b.length = pos;
  110. return b;
  111. }
  112. }