diff --git a/src/main/java/io/github/yezhihao/protostar/converter/MapConverter.java b/src/main/java/io/github/yezhihao/protostar/converter/MapConverter.java index 5cb14b9..73816a9 100644 --- a/src/main/java/io/github/yezhihao/protostar/converter/MapConverter.java +++ b/src/main/java/io/github/yezhihao/protostar/converter/MapConverter.java @@ -27,9 +27,16 @@ public abstract class MapConverter extends PrepareLoadStrategy implements Map map = new TreeMap<>(); do { K key = readKey(input); - int len = ByteBufUtils.readInt(input, valueSize()); - Object value = readValue(key, input.readSlice(len)); - map.put(key, (V) value); + int length = ByteBufUtils.readInt(input, valueSize()); + + if (input.isReadable(length)) { + int writerIndex = input.writerIndex(); + input.writerIndex(input.readerIndex() + length); + map.put(key, (V) readValue(key, input)); + input.writerIndex(writerIndex); + } else { + map.put(key, (V) readValue(key, input)); + } } while (input.isReadable()); return map; } diff --git a/src/main/java/io/github/yezhihao/protostar/schema/StringSchema.java b/src/main/java/io/github/yezhihao/protostar/schema/StringSchema.java index 01478e8..f5e4525 100644 --- a/src/main/java/io/github/yezhihao/protostar/schema/StringSchema.java +++ b/src/main/java/io/github/yezhihao/protostar/schema/StringSchema.java @@ -8,6 +8,7 @@ import io.netty.util.internal.StringUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.Arrays; @@ -58,24 +59,25 @@ public class StringSchema { @Override public void writeTo(ByteBuf output, int length, String value) { - byte[] bytes = value.getBytes(charset); + ByteBuffer buffer = charset.encode(value); if (length > 0) { - int srcPos = length - bytes.length; + int srcPos = length - buffer.limit(); if (srcPos > 0) { byte[] pads = new byte[srcPos]; if (pad != 0x00) Arrays.fill(pads, pad); output.writeBytes(pads); - output.writeBytes(bytes); + output.writeBytes(buffer); } else if (srcPos < 0) { - output.writeBytes(bytes, -srcPos, length); - log.info("字符长度超出限制: 长度[{}],数据长度[{}],{}", length, bytes.length, value); + buffer.position(-srcPos); + output.writeBytes(buffer); + log.info("字符长度超出限制: 长度[{}],数据长度[{}],{}", length, buffer.limit(), value); } else { - output.writeBytes(bytes); + output.writeBytes(buffer); } } else { - output.writeBytes(bytes); + output.writeBytes(buffer); } } }