From 71be789c62d750d7e74c037c33d84c037fb1f0c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=89=91=E5=99=A8=E8=BF=91?= Date: Fri, 23 Jul 2021 09:29:41 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BC=98=E5=8C=96String=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=86=99=E5=85=A5=E6=80=A7=E8=83=BD=EF=BC=9B2.=E6=8F=90?= =?UTF-8?q?=E5=8D=87MapConverter=E5=AE=B9=E9=94=99=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../protostar/converter/MapConverter.java | 13 ++++++++++--- .../yezhihao/protostar/schema/StringSchema.java | 16 +++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) 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); } } }