vid_us_data_stream_reader.dart 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import 'dart:typed_data';
  2. //Raised when data is not ready.
  3. class NotReadyException implements Exception {}
  4. class StreamErrorException implements Exception {
  5. String _msg;
  6. StreamErrorException(this._msg);
  7. @override
  8. String toString() {
  9. return _msg;
  10. }
  11. }
  12. class VidUsDataStreamReader {
  13. late final List<int> _data = [];
  14. late int _index;
  15. late ByteBuffer _buffer;
  16. late bool _error;
  17. late String _errorMsg;
  18. VidUsDataStreamReader(Stream<Uint8List> stream, [int offset = 0]) {
  19. _buffer = Uint8List.fromList(_data).buffer;
  20. stream.listen(onDataArrived, onError: onStreamError, onDone: onStreamDone);
  21. _index = offset;
  22. }
  23. void onDataArrived(Uint8List data) {
  24. _data.addAll(data);
  25. _buffer = Uint8List.fromList(_data).buffer;
  26. }
  27. void onStreamError(String msg) {
  28. _error = true;
  29. _errorMsg = msg;
  30. }
  31. void onStreamDone() {
  32. //Update the last data into the buffer.
  33. _buffer = Uint8List.fromList(_data).buffer;
  34. }
  35. ///Read int value from binary data(little endian),
  36. int readInt([int index = -1]) {
  37. if (_error) {
  38. throw StreamErrorException(_errorMsg);
  39. }
  40. if (index == -1) {
  41. if ((_buffer.lengthInBytes - _index) < 4) {
  42. throw NotReadyException();
  43. }
  44. var intData = _buffer.asByteData(_index, 4);
  45. _index += 4;
  46. return intData.getInt32(0, Endian.little);
  47. } else {
  48. if ((_buffer.lengthInBytes - index) < 4) {
  49. throw NotReadyException();
  50. }
  51. var intData = _buffer.asByteData(index, 4);
  52. _index = index + 4;
  53. return intData.getInt32(0, Endian.little);
  54. }
  55. }
  56. ///Read string from the binary data, the string format is utf-16, which is unicode in C#
  57. String readString([int index = -1]) {
  58. if (_error) {
  59. throw StreamErrorException(_errorMsg);
  60. }
  61. int dataLength;
  62. if (index == -1) {
  63. dataLength = readInt();
  64. if ((_buffer.lengthInBytes - _index) < dataLength) {
  65. throw NotReadyException();
  66. }
  67. } else {
  68. dataLength = readInt(index);
  69. if ((_buffer.lengthInBytes - index) < dataLength) {
  70. throw NotReadyException();
  71. }
  72. }
  73. var stringData = _buffer.asUint8List(_index, dataLength);
  74. _index += dataLength;
  75. var unicodeStringData = Uint8List.fromList(stringData.toList()).buffer.asUint16List();
  76. return String.fromCharCodes(unicodeStringData);
  77. }
  78. ///Read int16 value from binary data(little endian)
  79. int readInt16([int index = -1]) {
  80. if (_error) {
  81. throw StreamErrorException(_errorMsg);
  82. }
  83. if (index == -1) {
  84. if ((_buffer.lengthInBytes - _index) < 2) {
  85. throw NotReadyException();
  86. }
  87. var intData = _buffer.asByteData(_index, 2);
  88. _index += 2;
  89. return intData.getInt16(0, Endian.little);
  90. } else {
  91. if ((_buffer.lengthInBytes - index) < 2) {
  92. throw NotReadyException();
  93. }
  94. var intData = _buffer.asByteData(index, 2);
  95. _index = index + 2;
  96. return intData.getInt16(0, Endian.little);
  97. }
  98. }
  99. ///Read int64 value from binary data(little endian)
  100. ///this method is not support in web platform use readInt64V2 instead.
  101. int readInt64([int index = -1]) {
  102. if (_error) {
  103. throw StreamErrorException(_errorMsg);
  104. }
  105. if (index == -1) {
  106. if ((_buffer.lengthInBytes - _index) < 8) {
  107. throw NotReadyException();
  108. }
  109. var intData = _buffer.asByteData(_index, 8);
  110. _index += 8;
  111. return intData.getInt64(0, Endian.little);
  112. } else {
  113. if ((_buffer.lengthInBytes - index) < 8) {
  114. throw NotReadyException();
  115. }
  116. var intData = _buffer.asByteData(index, 8);
  117. _index = index + 8;
  118. return intData.getInt64(0, Endian.little);
  119. }
  120. }
  121. ///Read int64 value from binary data(little endian)
  122. ///this method use two int32 to support read int64 on web platform
  123. int readInt64V2([int index = -1]) {
  124. if (_error) {
  125. throw StreamErrorException(_errorMsg);
  126. }
  127. if (index == -1) {
  128. if ((_buffer.lengthInBytes - _index) < 8) {
  129. throw NotReadyException();
  130. }
  131. int low = readInt();
  132. int high = readInt();
  133. int value = high >> 32;
  134. value |= low;
  135. return value;
  136. } else {
  137. if ((_buffer.lengthInBytes - index) < 8) {
  138. throw NotReadyException();
  139. }
  140. int low = readInt(index);
  141. //Read the next int.
  142. int high = readInt();
  143. int value = high >> 32;
  144. value |= low;
  145. return value;
  146. }
  147. }
  148. ///Read float value from binary data(little endian)
  149. double readFloat([int index = -1]) {
  150. if (_error) {
  151. throw StreamErrorException(_errorMsg);
  152. }
  153. if (index == -1) {
  154. if ((_buffer.lengthInBytes - _index) < 4) {
  155. throw NotReadyException();
  156. }
  157. var floatData = _buffer.asByteData(_index, 4);
  158. _index += 4;
  159. return floatData.getFloat32(0, Endian.little);
  160. } else {
  161. if ((_buffer.lengthInBytes - index) < 4) {
  162. throw NotReadyException();
  163. }
  164. var floatData = _buffer.asByteData(index, 4);
  165. _index = index + 4;
  166. return floatData.getFloat32(0, Endian.little);
  167. }
  168. }
  169. ///Read double value from binary data(little endian)
  170. double readDouble([int index = -1]) {
  171. if (_error) {
  172. throw StreamErrorException(_errorMsg);
  173. }
  174. if (index == -1) {
  175. if ((_buffer.lengthInBytes - _index) < 8) {
  176. throw NotReadyException();
  177. }
  178. var floatData = _buffer.asByteData(_index, 8);
  179. _index += 8;
  180. return floatData.getFloat64(0, Endian.little);
  181. } else {
  182. if ((_buffer.lengthInBytes - index) < 8) {
  183. throw NotReadyException();
  184. }
  185. var floatData = _buffer.asByteData(index, 8);
  186. _index = index + 8;
  187. return floatData.getFloat64(0, Endian.little);
  188. }
  189. }
  190. ///Read bool value from binary data
  191. bool readBool([int index = -1]) {
  192. if (_error) {
  193. throw StreamErrorException(_errorMsg);
  194. }
  195. if (index == -1) {
  196. if ((_buffer.lengthInBytes - _index) < 1) {
  197. throw NotReadyException();
  198. }
  199. var boolData = _buffer.asByteData(_index, 1);
  200. _index++;
  201. return boolData.getInt8(0) == 1;
  202. } else {
  203. if ((_buffer.lengthInBytes - index) < 1) {
  204. throw NotReadyException();
  205. }
  206. var boolData = _buffer.asByteData(index, 1);
  207. _index = index + 1;
  208. return boolData.getInt8(0) == 1;
  209. }
  210. }
  211. ///Read byte value from binary data
  212. int readByte([int index = -1]) {
  213. if (_error) {
  214. throw StreamErrorException(_errorMsg);
  215. }
  216. if (index == -1) {
  217. if ((_buffer.lengthInBytes - _index) < 1) {
  218. throw NotReadyException();
  219. }
  220. var byteData = _buffer.asByteData(_index, 1);
  221. _index++;
  222. return byteData.getInt8(0);
  223. } else {
  224. if ((_buffer.lengthInBytes - index) < 1) {
  225. throw NotReadyException();
  226. }
  227. var byteData = _buffer.asByteData(index, 1);
  228. _index = index + 1;
  229. return byteData.getInt8(0);
  230. }
  231. }
  232. ///Read bytes from the binary data.
  233. Uint8List readBytes([int index = -1]) {
  234. if (_error) {
  235. throw StreamErrorException(_errorMsg);
  236. }
  237. var dataLength = readInt(index);
  238. if ((_buffer.lengthInBytes - _index) < dataLength) {
  239. throw NotReadyException();
  240. }
  241. var bytes = _buffer.asUint8List(_index, dataLength);
  242. _index += dataLength;
  243. return bytes;
  244. }
  245. }