Don't ignore the ByteData buffer offset in StringCodec.decode(). (#21011)

This commit is contained in:
amirh
2018-08-24 17:19:30 -07:00
committed by GitHub
parent 977da4fb20
commit 510c0eeaff
2 changed files with 14 additions and 1 deletions

View File

@@ -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

View File

@@ -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();