diff --git a/protocol/src/main/resources/dart/ByteBuffer.dart b/protocol/src/main/resources/dart/ByteBuffer.dart index 5b3f6e65..a7efebbb 100644 --- a/protocol/src/main/resources/dart/ByteBuffer.dart +++ b/protocol/src/main/resources/dart/ByteBuffer.dart @@ -1,5 +1,6 @@ import 'dart:convert'; import 'dart:typed_data'; + import 'IByteBuffer.dart'; import 'ProtocolManager.dart'; @@ -8,6 +9,30 @@ class ByteBuffer implements IByteBuffer { int writeOffset = 0; int readOffset = 0; + @override + void adjustPadding(int predictionLength, int beforeWriteIndex) { + var currentWriteIndex = getWriteOffset(); + var predictionCount = writeIntCount(predictionLength); + var length = currentWriteIndex - beforeWriteIndex - predictionCount; + var lengthCount = writeIntCount(length); + var padding = lengthCount - predictionCount; + if (padding == 0) { + setWriteOffset(beforeWriteIndex); + writeInt(length); + setWriteOffset(currentWriteIndex); + } else { + var retainedByteBuf = buffer.sublist(currentWriteIndex - length, currentWriteIndex); + setWriteOffset(beforeWriteIndex); + writeInt(length); + writeBytes(retainedByteBuf); + } + } + + @override + bool compatibleRead(int beforeReadIndex, int length) { + return length != -1 && getReadOffset() < length + beforeReadIndex; + } + @override Int8List getBuffer() { return buffer; @@ -129,6 +154,29 @@ class ByteBuffer implements IByteBuffer { return value; } + @override + int writeIntCount(int value) { + value = (value << 1) ^ (value >> 31); + + if (value >>> 7 == 0) { + return 1; + } + + if (value >>> 14 == 0) { + return 2; + } + + if (value >>> 21 == 0) { + return 3; + } + + if (value >>> 28 == 0) { + return 4; + } + + return 5; + } + @override void writeInt(int value) { writeLong(value); @@ -323,4 +371,3 @@ class ByteBuffer implements IByteBuffer { return protocol.read(this); } } - diff --git a/protocol/src/main/resources/dart/IByteBuffer.dart b/protocol/src/main/resources/dart/IByteBuffer.dart index e4ee0e98..2af297f1 100644 --- a/protocol/src/main/resources/dart/IByteBuffer.dart +++ b/protocol/src/main/resources/dart/IByteBuffer.dart @@ -1,6 +1,10 @@ import 'dart:typed_data'; abstract class IByteBuffer { + void adjustPadding(int predictionLength, int beforeWriteIndex); + + bool compatibleRead(int beforeReadIndex, int length); + Int8List getBuffer(); int getWriteOffset(); @@ -33,6 +37,8 @@ abstract class IByteBuffer { int readShort(); + int writeIntCount(int value); + void writeRawInt(int value); int readRawInt();