Don't ignore the ByteData buffer offset in StringCodec.decode(). (#21011)
This commit is contained in:
@@ -41,7 +41,7 @@ class StringCodec implements MessageCodec<String> {
|
||||
String decodeMessage(ByteData message) {
|
||||
if (message == null)
|
||||
return null;
|
||||
return utf8.decoder.convert(message.buffer.asUint8List());
|
||||
return utf8.decoder.convert(message.buffer.asUint8List(message.offsetInBytes, message.lengthInBytes));
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -24,6 +24,19 @@ void main() {
|
||||
_checkEncodeDecode<String>(string, 'hello');
|
||||
_checkEncodeDecode<String>(string, 'special chars >\u263A\u{1F602}<');
|
||||
});
|
||||
test('ByteData with offset', () {
|
||||
const MessageCodec<String> string = StringCodec();
|
||||
final ByteData helloWorldByteData = string.encodeMessage('hello world');
|
||||
final ByteData helloByteData = string.encodeMessage('hello');
|
||||
|
||||
final ByteData offsetByteData = new ByteData.view(
|
||||
helloWorldByteData.buffer,
|
||||
helloByteData.lengthInBytes,
|
||||
helloWorldByteData.lengthInBytes - helloByteData.lengthInBytes
|
||||
);
|
||||
|
||||
expect(string.decodeMessage(offsetByteData), ' world');
|
||||
});
|
||||
});
|
||||
group('JSON message codec', () {
|
||||
const MessageCodec<dynamic> json = JSONMessageCodec();
|
||||
|
||||
Reference in New Issue
Block a user