123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- import 'dart:convert';
- import 'dart:typed_data';
- class VidUsDataReader {
- late final ByteBuffer _buffer;
- late int _index;
- VidUsDataReader(Uint8List data, [int offset = 0]) {
- _buffer = data.buffer;
- _index = data.offsetInBytes + offset;
- }
- ///Read int value from binary data(little endian),
- int readInt([int index = -1]) {
- if (index == -1) {
- var intData = _buffer.asByteData(_index, 4);
- _index += 4;
- return intData.getInt32(0, Endian.little);
- } else {
- var intData = _buffer.asByteData(index, 4);
- _index = index + 4;
- return intData.getInt32(0, Endian.little);
- }
- }
- ///Read string from the binary data, the string format is utf8
- String readString([int index = -1]) {
- if (index == -1) {
- var dataLength = readInt();
- var stringData = _buffer.asUint8List(_index, dataLength);
- _index += dataLength;
- return utf8.decode(stringData);
- } else {
- var dataLength = readInt(index);
- var stringData = _buffer.asUint8List(_index, dataLength);
- _index += dataLength;
- return utf8.decode(stringData);
- }
- }
- ///Read int16 value from binary data(little endian)
- int readInt16([int index = -1]) {
- if (index == -1) {
- var intData = _buffer.asByteData(_index, 2);
- _index += 2;
- return intData.getInt16(0, Endian.little);
- } else {
- var intData = _buffer.asByteData(index, 2);
- _index = index + 2;
- return intData.getInt16(0, Endian.little);
- }
- }
- ///Read int64 value from binary data(little endian)
- ///this method is not support in web platform use readInt64V2 instead.
- int readInt64([int index = -1]) {
- if (index == -1) {
- var intData = _buffer.asByteData(_index, 8);
- _index += 8;
- return intData.getInt64(0, Endian.little);
- } else {
- var intData = _buffer.asByteData(index, 8);
- _index = index + 8;
- return intData.getInt64(0, Endian.little);
- }
- }
- ///Read int64 value from binary data(little endian)
- ///this method use two int32 to support read int64 on web platform
- int readInt64V2([int index = -1]) {
- if (index == -1) {
- int low = readInt();
- int high = readInt();
- int value = high >> 32;
- value |= low;
- return value;
- } else {
- int low = readInt(index);
- int high = readInt();
- int value = high >> 32;
- value |= low;
- return value;
- }
- }
- ///Read float value from binary data(little endian)
- double readFloat([int index = -1]) {
- if (index == -1) {
- var floatData = _buffer.asByteData(_index, 4);
- _index += 4;
- return floatData.getFloat32(0, Endian.little);
- } else {
- var floatData = _buffer.asByteData(index, 4);
- _index = index + 4;
- return floatData.getFloat32(0, Endian.little);
- }
- }
- ///Read double value from binary data(little endian)
- double readDouble([int index = -1]) {
- if (index == -1) {
- var floatData = _buffer.asByteData(_index, 8);
- _index += 8;
- return floatData.getFloat64(0, Endian.little);
- } else {
- var floatData = _buffer.asByteData(index, 8);
- _index = index + 8;
- return floatData.getFloat64(0, Endian.little);
- }
- }
- ///Read bool value from binary data
- bool readBool([int index = -1]) {
- if (index == -1) {
- var boolData = _buffer.asByteData(_index, 1);
- _index++;
- return boolData.getInt8(0) == 1;
- } else {
- var boolData = _buffer.asByteData(index, 1);
- _index++;
- return boolData.getInt8(0) == 1;
- }
- }
- ///Read byte value from binary data
- int readByte([int index = -1]) {
- if (index == -1) {
- var byteData = _buffer.asByteData(_index, 1);
- _index++;
- return byteData.getInt8(0);
- } else {
- var byteData = _buffer.asByteData(index, 1);
- _index++;
- return byteData.getInt8(0);
- }
- }
- ///Read bytes from the binary data.
- Uint8List readBytes([int index = -1]) {
- var dataLength = readInt(index);
- var bytes = _buffer.asUint8List(_index, dataLength);
- _index += dataLength;
- return bytes;
- }
- int _readLong(Uint8List longData) {
- int low = longData.buffer
- .asByteData(longData.offsetInBytes)
- .getInt32(0, Endian.little);
- int high = longData.buffer
- .asByteData(longData.offsetInBytes)
- .getInt32(4, Endian.little);
- int value = high >> 32;
- value |= low;
- return value;
- }
- ///Read a group of int64 values from the binary data.
- List<int> readLongs() {
- var longData = readBytes();
- var longCount = longData.length ~/ 8;
- List<int> result = [];
- for (var i = 0; i < longCount; i++) {
- result.add(_readLong(
- longData.buffer.asUint8List(longData.offsetInBytes + i * 8, 8)));
- }
- return result;
- }
- }
|