vid_us_data_http_reader.dart 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. import 'dart:typed_data';
  2. import 'package:dio/dio.dart';
  3. //Raised when data is not ready.
  4. class NotReadyException implements Exception {}
  5. //Raised when errro happened during the downloading.
  6. class DownloadErrorException implements Exception {
  7. String _message;
  8. String get message => _message;
  9. DownloadErrorException(this._message);
  10. }
  11. typedef DownloadCallback = void Function(double progress);
  12. class VidUsDataHttpReader {
  13. late Uint8List _underlyingData = Uint8List(0);
  14. late int _bufferSize = 0;
  15. late int _index;
  16. late bool _error;
  17. late String _errorMsg;
  18. late String _url;
  19. late int _chunkSize;
  20. late Dio _dio;
  21. DownloadCallback? downloadCallback;
  22. VidUsDataHttpReader(String url, {this.downloadCallback, int chunkSize = 65536, int connectTimeout = 30000, int receiveTimeout = 30000}) {
  23. _chunkSize = chunkSize;
  24. _url = url;
  25. _error = false;
  26. _errorMsg = '';
  27. _index = 0;
  28. _dio = Dio();
  29. _dio.options.connectTimeout = connectTimeout;
  30. _dio.options.receiveTimeout = receiveTimeout;
  31. _startDownload();
  32. }
  33. void _startDownload() async {
  34. try {
  35. //get file info
  36. var fileSize = await _getFileSize();
  37. if (!_error) {
  38. _underlyingData = Uint8List(fileSize);
  39. if (fileSize < _chunkSize) {
  40. await _downloadChunk(0, fileSize -1);
  41. if (!_error && downloadCallback != null) {
  42. var progress = _bufferSize / fileSize;
  43. downloadCallback!(progress);
  44. }
  45. } else {
  46. var last = fileSize % _chunkSize;
  47. var chunkCount = fileSize ~/ _chunkSize;
  48. for (var i = 0; i < chunkCount; i++) {
  49. var start = i * _chunkSize;
  50. var end = start + _chunkSize - 1;
  51. await _downloadChunk(start, end);
  52. if (_error) {
  53. break;
  54. }
  55. if (downloadCallback != null) {
  56. var progress = _bufferSize / fileSize;
  57. downloadCallback!(progress);
  58. }
  59. }
  60. if (!_error && last > 0) {
  61. var start = chunkCount * _chunkSize;
  62. var end = start + last - 1;
  63. await _downloadChunk(start, end);
  64. if (!_error && downloadCallback != null) {
  65. var progress = _bufferSize / fileSize;
  66. downloadCallback!(progress);
  67. }
  68. }
  69. }
  70. }
  71. } catch (ex) {
  72. _error = true;
  73. _errorMsg = ex.toString();
  74. }
  75. }
  76. Future<int> _getFileSize() async {
  77. var response = await _dio.head(_url);
  78. if (response.statusCode != 200) {
  79. _error = true;
  80. _errorMsg = 'Get file info error.';
  81. return 0;
  82. } else {
  83. var contentLength = response.headers.value(Headers.contentLengthHeader);
  84. if (contentLength == null) {
  85. _error = true;
  86. _errorMsg = 'No Content-Length from headers.';
  87. return 0;
  88. } else {
  89. var fileSize = int.parse(contentLength);
  90. return fileSize;
  91. }
  92. }
  93. }
  94. Future _downloadChunk(int chunkStart, int chunkEnd) async {
  95. var response = await _dio.get<List<int>>(_url, options: Options(responseType: ResponseType.bytes, headers: {"range": "bytes=$chunkStart-$chunkEnd"}));
  96. if (response.statusCode == 206) {
  97. var downloadedData = response.data;
  98. if (downloadedData == null) {
  99. _error = true;
  100. _errorMsg = 'Download chunk error.';
  101. } else {
  102. //Update the buffer.
  103. _underlyingData.setAll(_bufferSize, downloadedData);
  104. _bufferSize += downloadedData.length;
  105. }
  106. } else {
  107. _error = true;
  108. _errorMsg = 'Target url does not support download with chunks.';
  109. }
  110. }
  111. //Close the reader and its http connection.
  112. void close() {
  113. _dio.close(force: true);
  114. }
  115. ///Read int value from binary data(little endian),
  116. int readInt([int index = -1]) {
  117. if (_error) {
  118. throw DownloadErrorException(_errorMsg);
  119. }
  120. if (index == -1) {
  121. if ((_bufferSize - _index) < 4) {
  122. throw NotReadyException();
  123. }
  124. var intData = _underlyingData.buffer.asByteData(_index, 4);
  125. _index += 4;
  126. return intData.getInt32(0, Endian.little);
  127. } else {
  128. if ((_bufferSize - index) < 4) {
  129. throw NotReadyException();
  130. }
  131. var intData = _underlyingData.buffer.asByteData(index, 4);
  132. _index = index + 4;
  133. return intData.getInt32(0, Endian.little);
  134. }
  135. }
  136. ///Read string from the binary data, the string format is utf-16, which is unicode in C#
  137. String readString([int index = -1]) {
  138. if (_error) {
  139. throw DownloadErrorException(_errorMsg);
  140. }
  141. int dataLength;
  142. if (index == -1) {
  143. dataLength = readInt();
  144. if ((_bufferSize - _index) < dataLength) {
  145. throw NotReadyException();
  146. }
  147. } else {
  148. dataLength = readInt(index);
  149. if ((_bufferSize - index) < dataLength) {
  150. throw NotReadyException();
  151. }
  152. }
  153. var stringData = _underlyingData.buffer.asUint8List(_index, dataLength);
  154. _index += dataLength;
  155. var unicodeStringData = Uint8List.fromList(stringData.toList()).buffer.asUint16List();
  156. return String.fromCharCodes(unicodeStringData);
  157. }
  158. ///Read int16 value from binary data(little endian)
  159. int readInt16([int index = -1]) {
  160. if (_error) {
  161. throw DownloadErrorException(_errorMsg);
  162. }
  163. if (index == -1) {
  164. if ((_bufferSize - _index) < 2) {
  165. throw NotReadyException();
  166. }
  167. var intData = _underlyingData.buffer.asByteData(_index, 2);
  168. _index += 2;
  169. return intData.getInt16(0, Endian.little);
  170. } else {
  171. if ((_bufferSize - index) < 2) {
  172. throw NotReadyException();
  173. }
  174. var intData = _underlyingData.buffer.asByteData(index, 2);
  175. _index = index + 2;
  176. return intData.getInt16(0, Endian.little);
  177. }
  178. }
  179. ///Read int64 value from binary data(little endian)
  180. ///this method is not support in web platform use readInt64V2 instead.
  181. int readInt64([int index = -1]) {
  182. if (_error) {
  183. throw DownloadErrorException(_errorMsg);
  184. }
  185. if (index == -1) {
  186. if ((_bufferSize - _index) < 8) {
  187. throw NotReadyException();
  188. }
  189. var intData = _underlyingData.buffer.asByteData(_index, 8);
  190. _index += 8;
  191. return intData.getInt64(0, Endian.little);
  192. } else {
  193. if ((_bufferSize - index) < 8) {
  194. throw NotReadyException();
  195. }
  196. var intData = _underlyingData.buffer.asByteData(index, 8);
  197. _index = index + 8;
  198. return intData.getInt64(0, Endian.little);
  199. }
  200. }
  201. ///Read int64 value from binary data(little endian)
  202. ///this method use two int32 to support read int64 on web platform
  203. int readInt64V2([int index = -1]) {
  204. if (_error) {
  205. throw DownloadErrorException(_errorMsg);
  206. }
  207. if (index == -1) {
  208. if ((_bufferSize - _index) < 8) {
  209. throw NotReadyException();
  210. }
  211. int low = readInt();
  212. int high = readInt();
  213. int value = high >> 32;
  214. value |= low;
  215. return value;
  216. } else {
  217. if ((_bufferSize - index) < 8) {
  218. throw NotReadyException();
  219. }
  220. int low = readInt(index);
  221. //Read the next int.
  222. int high = readInt();
  223. int value = high >> 32;
  224. value |= low;
  225. return value;
  226. }
  227. }
  228. ///Read float value from binary data(little endian)
  229. double readFloat([int index = -1]) {
  230. if (_error) {
  231. throw DownloadErrorException(_errorMsg);
  232. }
  233. if (index == -1) {
  234. if ((_bufferSize - _index) < 4) {
  235. throw NotReadyException();
  236. }
  237. var floatData = _underlyingData.buffer.asByteData(_index, 4);
  238. _index += 4;
  239. return floatData.getFloat32(0, Endian.little);
  240. } else {
  241. if ((_bufferSize - index) < 4) {
  242. throw NotReadyException();
  243. }
  244. var floatData = _underlyingData.buffer.asByteData(index, 4);
  245. _index = index + 4;
  246. return floatData.getFloat32(0, Endian.little);
  247. }
  248. }
  249. ///Read double value from binary data(little endian)
  250. double readDouble([int index = -1]) {
  251. if (_error) {
  252. throw DownloadErrorException(_errorMsg);
  253. }
  254. if (index == -1) {
  255. if ((_bufferSize - _index) < 8) {
  256. throw NotReadyException();
  257. }
  258. var floatData = _underlyingData.buffer.asByteData(_index, 8);
  259. _index += 8;
  260. return floatData.getFloat64(0, Endian.little);
  261. } else {
  262. if ((_bufferSize - index) < 8) {
  263. throw NotReadyException();
  264. }
  265. var floatData = _underlyingData.buffer.asByteData(index, 8);
  266. _index = index + 8;
  267. return floatData.getFloat64(0, Endian.little);
  268. }
  269. }
  270. ///Read bool value from binary data
  271. bool readBool([int index = -1]) {
  272. if (_error) {
  273. throw DownloadErrorException(_errorMsg);
  274. }
  275. if (index == -1) {
  276. if ((_bufferSize - _index) < 1) {
  277. throw NotReadyException();
  278. }
  279. var boolData = _underlyingData.buffer.asByteData(_index, 1);
  280. _index++;
  281. return boolData.getInt8(0) == 1;
  282. } else {
  283. if ((_bufferSize - index) < 1) {
  284. throw NotReadyException();
  285. }
  286. var boolData = _underlyingData.buffer.asByteData(index, 1);
  287. _index = index + 1;
  288. return boolData.getInt8(0) == 1;
  289. }
  290. }
  291. ///Read byte value from binary data
  292. int readByte([int index = -1]) {
  293. if (_error) {
  294. throw DownloadErrorException(_errorMsg);
  295. }
  296. if (index == -1) {
  297. if ((_bufferSize - _index) < 1) {
  298. throw NotReadyException();
  299. }
  300. var byteData = _underlyingData.buffer.asByteData(_index, 1);
  301. _index++;
  302. return byteData.getInt8(0);
  303. } else {
  304. if ((_bufferSize - index) < 1) {
  305. throw NotReadyException();
  306. }
  307. var byteData = _underlyingData.buffer.asByteData(index, 1);
  308. _index = index + 1;
  309. return byteData.getInt8(0);
  310. }
  311. }
  312. ///Read bytes from the binary data.
  313. Uint8List readBytes([int index = -1]) {
  314. if (_error) {
  315. throw DownloadErrorException(_errorMsg);
  316. }
  317. var dataLength = readInt(index);
  318. if ((_bufferSize - _index) < dataLength) {
  319. throw NotReadyException();
  320. }
  321. var bytes = _underlyingData.buffer.asUint8List(_index, dataLength);
  322. _index += dataLength;
  323. return bytes;
  324. }
  325. }