vid_us_data_reader.dart 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import 'dart:convert';
  2. import 'dart:typed_data';
  3. class VidUsDataReader {
  4. late final ByteBuffer _buffer;
  5. late int _index;
  6. VidUsDataReader(Uint8List data, [int offset = 0]) {
  7. _buffer = data.buffer;
  8. _index = data.offsetInBytes + offset;
  9. }
  10. ///Read int value from binary data(little endian),
  11. int readInt([int index = -1]) {
  12. if (index == -1) {
  13. var intData = _buffer.asByteData(_index, 4);
  14. _index += 4;
  15. return intData.getInt32(0, Endian.little);
  16. } else {
  17. var intData = _buffer.asByteData(index, 4);
  18. _index = index + 4;
  19. return intData.getInt32(0, Endian.little);
  20. }
  21. }
  22. ///Read string from the binary data, the string format is utf8
  23. String readString([int index = -1]) {
  24. if (index == -1) {
  25. var dataLength = readInt();
  26. var stringData = _buffer.asUint8List(_index, dataLength);
  27. _index += dataLength;
  28. return utf8.decode(stringData);
  29. } else {
  30. var dataLength = readInt(index);
  31. var stringData = _buffer.asUint8List(_index, dataLength);
  32. _index += dataLength;
  33. return utf8.decode(stringData);
  34. }
  35. }
  36. ///Read int16 value from binary data(little endian)
  37. int readInt16([int index = -1]) {
  38. if (index == -1) {
  39. var intData = _buffer.asByteData(_index, 2);
  40. _index += 2;
  41. return intData.getInt16(0, Endian.little);
  42. } else {
  43. var intData = _buffer.asByteData(index, 2);
  44. _index = index + 2;
  45. return intData.getInt16(0, Endian.little);
  46. }
  47. }
  48. ///Read int64 value from binary data(little endian)
  49. ///this method is not support in web platform use readInt64V2 instead.
  50. int readInt64([int index = -1]) {
  51. if (index == -1) {
  52. var intData = _buffer.asByteData(_index, 8);
  53. _index += 8;
  54. return intData.getInt64(0, Endian.little);
  55. } else {
  56. var intData = _buffer.asByteData(index, 8);
  57. _index = index + 8;
  58. return intData.getInt64(0, Endian.little);
  59. }
  60. }
  61. ///Read int64 value from binary data(little endian)
  62. ///this method use two int32 to support read int64 on web platform
  63. int readInt64V2([int index = -1]) {
  64. if (index == -1) {
  65. int low = readInt();
  66. int high = readInt();
  67. int value = high >> 32;
  68. value |= low;
  69. return value;
  70. } else {
  71. int low = readInt(index);
  72. int high = readInt();
  73. int value = high >> 32;
  74. value |= low;
  75. return value;
  76. }
  77. }
  78. ///Read float value from binary data(little endian)
  79. double readFloat([int index = -1]) {
  80. if (index == -1) {
  81. var floatData = _buffer.asByteData(_index, 4);
  82. _index += 4;
  83. return floatData.getFloat32(0, Endian.little);
  84. } else {
  85. var floatData = _buffer.asByteData(index, 4);
  86. _index = index + 4;
  87. return floatData.getFloat32(0, Endian.little);
  88. }
  89. }
  90. ///Read double value from binary data(little endian)
  91. double readDouble([int index = -1]) {
  92. if (index == -1) {
  93. var floatData = _buffer.asByteData(_index, 8);
  94. _index += 8;
  95. return floatData.getFloat64(0, Endian.little);
  96. } else {
  97. var floatData = _buffer.asByteData(index, 8);
  98. _index = index + 8;
  99. return floatData.getFloat64(0, Endian.little);
  100. }
  101. }
  102. ///Read bool value from binary data
  103. bool readBool([int index = -1]) {
  104. if (index == -1) {
  105. var boolData = _buffer.asByteData(_index, 1);
  106. _index++;
  107. return boolData.getInt8(0) == 1;
  108. } else {
  109. var boolData = _buffer.asByteData(index, 1);
  110. _index++;
  111. return boolData.getInt8(0) == 1;
  112. }
  113. }
  114. ///Read byte value from binary data
  115. int readByte([int index = -1]) {
  116. if (index == -1) {
  117. var byteData = _buffer.asByteData(_index, 1);
  118. _index++;
  119. return byteData.getInt8(0);
  120. } else {
  121. var byteData = _buffer.asByteData(index, 1);
  122. _index++;
  123. return byteData.getInt8(0);
  124. }
  125. }
  126. ///Read bytes from the binary data.
  127. Uint8List readBytes([int index = -1]) {
  128. var dataLength = readInt(index);
  129. var bytes = _buffer.asUint8List(_index, dataLength);
  130. _index += dataLength;
  131. return bytes;
  132. }
  133. int _readLong(Uint8List longData) {
  134. int low = longData.buffer
  135. .asByteData(longData.offsetInBytes)
  136. .getInt32(0, Endian.little);
  137. int high = longData.buffer
  138. .asByteData(longData.offsetInBytes)
  139. .getInt32(4, Endian.little);
  140. int value = high >> 32;
  141. value |= low;
  142. return value;
  143. }
  144. ///Read a group of int64 values from the binary data.
  145. List<int> readLongs() {
  146. var longData = readBytes();
  147. var longCount = longData.length ~/ 8;
  148. List<int> result = [];
  149. for (var i = 0; i < longCount; i++) {
  150. result.add(_readLong(
  151. longData.buffer.asUint8List(longData.offsetInBytes + i * 8, 8)));
  152. }
  153. return result;
  154. }
  155. }