reader.dart 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import 'dart:typed_data';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:vid/us/vid_us_data_http_reader.dart';
  4. typedef AsyncVidDataReaderBytesProxy = Uint8List Function();
  5. abstract class AsyncVidDataReaderBase {
  6. Uint8List _chunkedData = Uint8List(0);
  7. int _bufferSize = 0;
  8. int _index = 0;
  9. bool _error = false;
  10. String _errorMsg = '';
  11. double _downloadProgress = 0;
  12. String url;
  13. DownloadCallback? downloadCallback;
  14. AsyncVidDataReaderBytesProxy? _bytesProxy;
  15. AsyncVidDataReaderBase(this.url, {this.downloadCallback}) {
  16. startDownload();
  17. }
  18. Uint8List get _underlyingData {
  19. if (_bytesProxy != null) {
  20. return _bytesProxy!.call();
  21. }
  22. return _chunkedData;
  23. }
  24. int get downloadedSize => _bufferSize;
  25. int get totalSize => _underlyingData.length;
  26. void startDownload();
  27. void initChunk(int size) {
  28. _chunkedData = Uint8List(size);
  29. }
  30. void appendChunk(Uint8List chunk) {
  31. _chunkedData.setAll(_bufferSize, chunk);
  32. setBufferSize(_bufferSize + chunk.length);
  33. }
  34. @protected
  35. void setBufferSize(int size) {
  36. _bufferSize = size;
  37. }
  38. void setError(bool hasErr, [String msg = '']) {
  39. _error = hasErr;
  40. _errorMsg = hasErr ? msg : '';
  41. if (_error) {
  42. downloadCallback?.call(_downloadProgress, DownloadErrorException(msg));
  43. }
  44. }
  45. void updateProgress(double progress, [DownloadErrorException? err]) {
  46. _downloadProgress = progress;
  47. downloadCallback?.call(_downloadProgress, err);
  48. }
  49. /// 通过代理从其他渠道读取字节数组
  50. ///
  51. /// 仅用于接入[VidUsDataHttpReader]
  52. @protected
  53. void setBytesProxy(AsyncVidDataReaderBytesProxy proxy) {
  54. _bytesProxy = proxy;
  55. }
  56. Uint8List toBytes() {
  57. return _underlyingData;
  58. }
  59. //Close the reader and its overhead.
  60. void close();
  61. ///Read int value from binary data(little endian),
  62. int readInt([int index = -1]) {
  63. if (_error) {
  64. throw DownloadErrorException(_errorMsg);
  65. }
  66. if (index == -1) {
  67. if ((_bufferSize - _index) < 4) {
  68. throw NotReadyException();
  69. }
  70. var intData = _underlyingData.buffer.asByteData(_index, 4);
  71. _index += 4;
  72. return intData.getInt32(0, Endian.little);
  73. } else {
  74. if ((_bufferSize - index) < 4) {
  75. throw NotReadyException();
  76. }
  77. var intData = _underlyingData.buffer.asByteData(index, 4);
  78. _index = index + 4;
  79. return intData.getInt32(0, Endian.little);
  80. }
  81. }
  82. ///Read string from the binary data, the string format is utf-16, which is unicode in C#
  83. String readString([int index = -1]) {
  84. if (_error) {
  85. throw DownloadErrorException(_errorMsg);
  86. }
  87. int dataLength;
  88. if (index == -1) {
  89. dataLength = readInt();
  90. if ((_bufferSize - _index) < dataLength) {
  91. throw NotReadyException();
  92. }
  93. } else {
  94. dataLength = readInt(index);
  95. if ((_bufferSize - index) < dataLength) {
  96. throw NotReadyException();
  97. }
  98. }
  99. var stringData = _underlyingData.buffer.asUint8List(_index, dataLength);
  100. _index += dataLength;
  101. var unicodeStringData =
  102. Uint8List.fromList(stringData.toList()).buffer.asUint16List();
  103. return String.fromCharCodes(unicodeStringData);
  104. }
  105. ///Read int16 value from binary data(little endian)
  106. int readInt16([int index = -1]) {
  107. if (_error) {
  108. throw DownloadErrorException(_errorMsg);
  109. }
  110. if (index == -1) {
  111. if ((_bufferSize - _index) < 2) {
  112. throw NotReadyException();
  113. }
  114. var intData = _underlyingData.buffer.asByteData(_index, 2);
  115. _index += 2;
  116. return intData.getInt16(0, Endian.little);
  117. } else {
  118. if ((_bufferSize - index) < 2) {
  119. throw NotReadyException();
  120. }
  121. var intData = _underlyingData.buffer.asByteData(index, 2);
  122. _index = index + 2;
  123. return intData.getInt16(0, Endian.little);
  124. }
  125. }
  126. ///Read int64 value from binary data(little endian)
  127. ///this method is not support in web platform use readInt64V2 instead.
  128. int readInt64([int index = -1]) {
  129. if (_error) {
  130. throw DownloadErrorException(_errorMsg);
  131. }
  132. if (index == -1) {
  133. if ((_bufferSize - _index) < 8) {
  134. throw NotReadyException();
  135. }
  136. var intData = _underlyingData.buffer.asByteData(_index, 8);
  137. _index += 8;
  138. return intData.getInt64(0, Endian.little);
  139. } else {
  140. if ((_bufferSize - index) < 8) {
  141. throw NotReadyException();
  142. }
  143. var intData = _underlyingData.buffer.asByteData(index, 8);
  144. _index = index + 8;
  145. return intData.getInt64(0, Endian.little);
  146. }
  147. }
  148. ///Read int64 value from binary data(little endian)
  149. ///this method use two int32 to support read int64 on web platform
  150. int readInt64V2([int index = -1]) {
  151. if (_error) {
  152. throw DownloadErrorException(_errorMsg);
  153. }
  154. if (index == -1) {
  155. if ((_bufferSize - _index) < 8) {
  156. throw NotReadyException();
  157. }
  158. int low = readInt();
  159. int high = readInt();
  160. int value = high >> 32;
  161. value |= low;
  162. return value;
  163. } else {
  164. if ((_bufferSize - index) < 8) {
  165. throw NotReadyException();
  166. }
  167. int low = readInt(index);
  168. //Read the next int.
  169. int high = readInt();
  170. int value = high >> 32;
  171. value |= low;
  172. return value;
  173. }
  174. }
  175. ///Read float value from binary data(little endian)
  176. double readFloat([int index = -1]) {
  177. if (_error) {
  178. throw DownloadErrorException(_errorMsg);
  179. }
  180. if (index == -1) {
  181. if ((_bufferSize - _index) < 4) {
  182. throw NotReadyException();
  183. }
  184. var floatData = _underlyingData.buffer.asByteData(_index, 4);
  185. _index += 4;
  186. return floatData.getFloat32(0, Endian.little);
  187. } else {
  188. if ((_bufferSize - index) < 4) {
  189. throw NotReadyException();
  190. }
  191. var floatData = _underlyingData.buffer.asByteData(index, 4);
  192. _index = index + 4;
  193. return floatData.getFloat32(0, Endian.little);
  194. }
  195. }
  196. ///Read double value from binary data(little endian)
  197. double readDouble([int index = -1]) {
  198. if (_error) {
  199. throw DownloadErrorException(_errorMsg);
  200. }
  201. if (index == -1) {
  202. if ((_bufferSize - _index) < 8) {
  203. throw NotReadyException();
  204. }
  205. var floatData = _underlyingData.buffer.asByteData(_index, 8);
  206. _index += 8;
  207. return floatData.getFloat64(0, Endian.little);
  208. } else {
  209. if ((_bufferSize - index) < 8) {
  210. throw NotReadyException();
  211. }
  212. var floatData = _underlyingData.buffer.asByteData(index, 8);
  213. _index = index + 8;
  214. return floatData.getFloat64(0, Endian.little);
  215. }
  216. }
  217. ///Read bool value from binary data
  218. bool readBool([int index = -1]) {
  219. if (_error) {
  220. throw DownloadErrorException(_errorMsg);
  221. }
  222. if (index == -1) {
  223. if ((_bufferSize - _index) < 1) {
  224. throw NotReadyException();
  225. }
  226. var boolData = _underlyingData.buffer.asByteData(_index, 1);
  227. _index++;
  228. return boolData.getInt8(0) == 1;
  229. } else {
  230. if ((_bufferSize - index) < 1) {
  231. throw NotReadyException();
  232. }
  233. var boolData = _underlyingData.buffer.asByteData(index, 1);
  234. _index = index + 1;
  235. return boolData.getInt8(0) == 1;
  236. }
  237. }
  238. ///Read byte value from binary data
  239. int readByte([int index = -1]) {
  240. if (_error) {
  241. throw DownloadErrorException(_errorMsg);
  242. }
  243. if (index == -1) {
  244. if ((_bufferSize - _index) < 1) {
  245. throw NotReadyException();
  246. }
  247. var byteData = _underlyingData.buffer.asByteData(_index, 1);
  248. _index++;
  249. return byteData.getInt8(0);
  250. } else {
  251. if ((_bufferSize - index) < 1) {
  252. throw NotReadyException();
  253. }
  254. var byteData = _underlyingData.buffer.asByteData(index, 1);
  255. _index = index + 1;
  256. return byteData.getInt8(0);
  257. }
  258. }
  259. ///Read bytes from the binary data.
  260. Uint8List readBytes([int index = -1]) {
  261. if (_error) {
  262. throw DownloadErrorException(_errorMsg);
  263. }
  264. var dataLength = readInt(index);
  265. if ((_bufferSize - _index) < dataLength) {
  266. throw NotReadyException();
  267. }
  268. var bytes = _underlyingData.buffer.asUint8List(_index, dataLength);
  269. _index += dataLength;
  270. return bytes;
  271. }
  272. }