UInt8Array.hx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. typedef UInt8ArrayData = ArrayBufferView.ArrayBufferViewData;
  24. abstract UInt8Array(UInt8ArrayData) {
  25. public static inline var BYTES_PER_ELEMENT = 1;
  26. public var length(get, never):Int;
  27. public var view(get, never):ArrayBufferView;
  28. public inline function new(elements:Int) {
  29. this = new ArrayBufferView(elements * BYTES_PER_ELEMENT).getData();
  30. }
  31. inline function get_length() {
  32. return this.byteLength;
  33. }
  34. public inline function get_view():ArrayBufferView {
  35. return ArrayBufferView.fromData(this);
  36. }
  37. @:arrayAccess public inline function get(index:Int) {
  38. return this.bytes.get(index + this.byteOffset);
  39. }
  40. @:arrayAccess public inline function set(index:Int, value:Int):Int {
  41. if (index >= 0 && index < length) {
  42. this.bytes.set(index + this.byteOffset, value);
  43. return value;
  44. }
  45. return 0;
  46. }
  47. public inline function sub(begin:Int, ?length:Int):UInt8Array {
  48. return fromData(this.sub(begin, length));
  49. }
  50. public inline function subarray(?begin:Int, ?end:Int):UInt8Array {
  51. return fromData(this.subarray(begin, end));
  52. }
  53. public inline function getData():UInt8ArrayData {
  54. return this;
  55. }
  56. public static function fromData(d:UInt8ArrayData):UInt8Array {
  57. return cast d;
  58. }
  59. public static function fromArray(a:Array<Int>, pos = 0, ?length:Int):UInt8Array {
  60. if (length == null)
  61. length = a.length - pos;
  62. if (pos < 0 || length < 0 || pos + length > a.length)
  63. throw Error.OutsideBounds;
  64. var i = new UInt8Array(a.length);
  65. for (idx in 0...length)
  66. i[idx] = a[idx + pos];
  67. return i;
  68. }
  69. public static function fromBytes(bytes:haxe.io.Bytes, bytePos:Int = 0, ?length:Int):UInt8Array {
  70. return fromData(ArrayBufferView.fromBytes(bytes, bytePos, length).getData());
  71. }
  72. }