Unverified Commit 510c0eea authored by amirh's avatar amirh Committed by GitHub

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

parent 977da4fb
...@@ -41,7 +41,7 @@ class StringCodec implements MessageCodec<String> { ...@@ -41,7 +41,7 @@ class StringCodec implements MessageCodec<String> {
String decodeMessage(ByteData message) { String decodeMessage(ByteData message) {
if (message == null) if (message == null)
return null; return null;
return utf8.decoder.convert(message.buffer.asUint8List()); return utf8.decoder.convert(message.buffer.asUint8List(message.offsetInBytes, message.lengthInBytes));
} }
@override @override
......
...@@ -24,6 +24,19 @@ void main() { ...@@ -24,6 +24,19 @@ void main() {
_checkEncodeDecode<String>(string, 'hello'); _checkEncodeDecode<String>(string, 'hello');
_checkEncodeDecode<String>(string, 'special chars >\u263A\u{1F602}<'); _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', () { group('JSON message codec', () {
const MessageCodec<dynamic> json = JSONMessageCodec(); const MessageCodec<dynamic> json = JSONMessageCodec();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment