deployed 2.0.6.RELEASE,修复集合类型长度域BUG

master
剑器近 2021-09-19 17:52:14 +08:00
parent 44050e72ce
commit d42c6409c8
3 changed files with 11 additions and 10 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>io.github.yezhihao</groupId> <groupId>io.github.yezhihao</groupId>
<artifactId>protostar</artifactId> <artifactId>protostar</artifactId>
<version>2.0.5.RELEASE</version> <version>2.0.6.RELEASE</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Protostar</name> <name>Protostar</name>

View File

@ -33,7 +33,6 @@ public class DynamicTotalField<T> extends BasicField<T> {
public void writeTo(ByteBuf output, Object message) throws Exception { public void writeTo(ByteBuf output, Object message) throws Exception {
Collection value = (Collection) f.get(message); Collection value = (Collection) f.get(message);
if (value != null)
schema.writeTo(output, totalSize, value); schema.writeTo(output, totalSize, value);
} }
@ -63,14 +62,12 @@ public class DynamicTotalField<T> extends BasicField<T> {
public void writeTo(ByteBuf output, Object message) throws Exception { public void writeTo(ByteBuf output, Object message) throws Exception {
Collection value = (Collection) f.get(message); Collection value = (Collection) f.get(message);
if (value != null) {
int total = value.size(); int total = value == null ? 0 : value.size();
String hex = StrUtils.leftPad(Integer.toHexString(total), totalSize << 1, '0'); String hex = StrUtils.leftPad(Integer.toHexString(total), totalSize << 1, '0');
println(this.index, this.field.desc() + "总数", hex, total); println(this.index, this.field.desc() + "总数", hex, total);
schema.writeTo(output, totalSize, value); schema.writeTo(output, totalSize, value);
} }
} }
}
} }

View File

@ -38,6 +38,8 @@ public class CollectionSchema<T> implements Schema<Collection<T>> {
@Override @Override
public Collection<T> readFrom(ByteBuf input, int totalSize) { public Collection<T> readFrom(ByteBuf input, int totalSize) {
int total = ByteBufUtils.readInt(input, totalSize); int total = ByteBufUtils.readInt(input, totalSize);
if (total <= 0)
return null;
Collection<T> list = new ArrayList<>(total); Collection<T> list = new ArrayList<>(total);
for (int i = 0; i < total; i++) { for (int i = 0; i < total; i++) {
T obj = schema.readFrom(input); T obj = schema.readFrom(input);
@ -58,8 +60,10 @@ public class CollectionSchema<T> implements Schema<Collection<T>> {
@Override @Override
public void writeTo(ByteBuf output, int totalSize, Collection<T> list) { public void writeTo(ByteBuf output, int totalSize, Collection<T> list) {
if (list == null || list.isEmpty()) if (list == null || list.isEmpty()) {
ByteBufUtils.writeInt(output, totalSize, 0);
return; return;
}
ByteBufUtils.writeInt(output, totalSize, list.size()); ByteBufUtils.writeInt(output, totalSize, list.size());
for (T obj : list) { for (T obj : list) {