Skip to content

Commit

Permalink
Merge branch 'master' into osa1/pubignore
Browse files Browse the repository at this point in the history
  • Loading branch information
osa1 committed Oct 27, 2023
2 parents 46cc612 + 42436cd commit 91a1061
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
39 changes: 29 additions & 10 deletions protobuf/lib/src/protobuf/coded_buffer_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class CodedBufferReader {
static const int DEFAULT_SIZE_LIMIT = 64 << 20;

final Uint8List _buffer;

/// [ByteData] of [_buffer], created once to be able to decode fixed-size
/// integers and floats without having to allocate a [ByteData] every time.
late final ByteData _byteData = ByteData.sublistView(_buffer);

int _bufferPos = 0;
int _currentLimit = -1;
int _lastTag = 0;
Expand Down Expand Up @@ -123,9 +128,20 @@ class CodedBufferReader {
Int64 readUint64() => _readRawVarint64();
int readSint32() => _decodeZigZag32(readUint32());
Int64 readSint64() => _decodeZigZag64(readUint64());
int readFixed32() => _readByteData(4).getUint32(0, Endian.little);

int readFixed32() {
final pos = _bufferPos;
_checkLimit(4);
return _byteData.getUint32(pos, Endian.little);
}

Int64 readFixed64() => readSfixed64();
int readSfixed32() => _readByteData(4).getInt32(0, Endian.little);

int readSfixed32() {
final pos = _bufferPos;
_checkLimit(4);
return _byteData.getInt32(pos, Endian.little);
}

Int64 readSfixed64() {
final pos = _bufferPos;
Expand Down Expand Up @@ -158,8 +174,17 @@ class CodedBufferReader {
.convert(_buffer, stringPos, stringPos + length);
}

double readFloat() => _readByteData(4).getFloat32(0, Endian.little);
double readDouble() => _readByteData(8).getFloat64(0, Endian.little);
double readFloat() {
final pos = _bufferPos;
_checkLimit(4);
return _byteData.getFloat32(pos, Endian.little);
}

double readDouble() {
final pos = _bufferPos;
_checkLimit(8);
return _byteData.getFloat64(pos, Endian.little);
}

int readTag() {
if (isAtEnd()) {
Expand Down Expand Up @@ -270,10 +295,4 @@ class CodedBufferReader {
}
throw InvalidProtocolBufferException.malformedVarint();
}

ByteData _readByteData(int sizeInBytes) {
_checkLimit(sizeInBytes);
return ByteData.view(_buffer.buffer,
_buffer.offsetInBytes + _bufferPos - sizeInBytes, sizeInBytes);
}
}
25 changes: 21 additions & 4 deletions protobuf/lib/src/protobuf/coded_buffer_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,11 @@ class CodedBufferWriter {
/// Add a [Uint8List] splice, without copying. These bytes will be directly
/// copied into the output buffer by [writeTo].
void writeRawBytes(Uint8List value) {
final length = value.lengthInBytes;
if (length == 0) return;
_commitSplice();
_splices.add(value);
_bytesTotal += value.lengthInBytes;
_bytesTotal += length;
}

/// Start writing a length-delimited data.
Expand Down Expand Up @@ -340,11 +342,22 @@ class CodedBufferWriter {
_writeVarint32(value ? 1 : 0);
break;
case PbFieldType._BYTES_BIT:
_writeBytesNoTag(
value is Uint8List ? value : Uint8List.fromList(value));
final List<int> bytes = value;
if (bytes is Uint8List) {
_writeBytesNoTag(bytes);
} else if (bytes.isEmpty) {
_writeEmptyBytes();
} else {
_writeBytesNoTag(Uint8List.fromList(bytes));
}
break;
case PbFieldType._STRING_BIT:
_writeBytesNoTag(const Utf8Encoder().convert(value));
final String string = value;
if (string.isEmpty) {
_writeEmptyBytes();
} else {
_writeBytesNoTag(const Utf8Encoder().convert(string));
}
break;
case PbFieldType._DOUBLE_BIT:
_writeDouble(value);
Expand Down Expand Up @@ -405,6 +418,10 @@ class CodedBufferWriter {
writeRawBytes(value);
}

void _writeEmptyBytes() {
writeInt32NoTag(0);
}

void _writeTag(int fieldNumber, int wireFormat) {
writeInt32NoTag(makeTag(fieldNumber, wireFormat));
}
Expand Down

0 comments on commit 91a1061

Please sign in to comment.