diff --git a/pom.xml b/pom.xml index 330f944..d24f9f4 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 io.github.yezhihao protostar - 2.0.5.RELEASE + 2.0.6.RELEASE jar Protostar diff --git a/src/main/java/io/github/yezhihao/protostar/field/DynamicTotalField.java b/src/main/java/io/github/yezhihao/protostar/field/DynamicTotalField.java index 2ebbdf2..eaa24e5 100644 --- a/src/main/java/io/github/yezhihao/protostar/field/DynamicTotalField.java +++ b/src/main/java/io/github/yezhihao/protostar/field/DynamicTotalField.java @@ -33,8 +33,7 @@ public class DynamicTotalField extends BasicField { public void writeTo(ByteBuf output, Object message) throws Exception { Collection value = (Collection) f.get(message); - if (value != null) - schema.writeTo(output, totalSize, value); + schema.writeTo(output, totalSize, value); } @Override @@ -63,14 +62,12 @@ public class DynamicTotalField extends BasicField { public void writeTo(ByteBuf output, Object message) throws Exception { Collection value = (Collection) f.get(message); - if (value != null) { - int total = value.size(); - String hex = StrUtils.leftPad(Integer.toHexString(total), totalSize << 1, '0'); - println(this.index, this.field.desc() + "总数", hex, total); + int total = value == null ? 0 : value.size(); + String hex = StrUtils.leftPad(Integer.toHexString(total), totalSize << 1, '0'); + println(this.index, this.field.desc() + "总数", hex, total); - schema.writeTo(output, totalSize, value); - } + schema.writeTo(output, totalSize, value); } } } \ No newline at end of file diff --git a/src/main/java/io/github/yezhihao/protostar/schema/CollectionSchema.java b/src/main/java/io/github/yezhihao/protostar/schema/CollectionSchema.java index 71771c2..ec73325 100644 --- a/src/main/java/io/github/yezhihao/protostar/schema/CollectionSchema.java +++ b/src/main/java/io/github/yezhihao/protostar/schema/CollectionSchema.java @@ -38,6 +38,8 @@ public class CollectionSchema implements Schema> { @Override public Collection readFrom(ByteBuf input, int totalSize) { int total = ByteBufUtils.readInt(input, totalSize); + if (total <= 0) + return null; Collection list = new ArrayList<>(total); for (int i = 0; i < total; i++) { T obj = schema.readFrom(input); @@ -58,8 +60,10 @@ public class CollectionSchema implements Schema> { @Override public void writeTo(ByteBuf output, int totalSize, Collection list) { - if (list == null || list.isEmpty()) + if (list == null || list.isEmpty()) { + ByteBufUtils.writeInt(output, totalSize, 0); return; + } ByteBufUtils.writeInt(output, totalSize, list.size()); for (T obj : list) {