diff --git a/pom.xml b/pom.xml
index 669496d..8cba8cd 100644
--- a/pom.xml
+++ b/pom.xml
@@ -3,7 +3,7 @@
4.0.0
io.github.yezhihao
protostar
- 2.0.3.RELEASE
+ 2.0.4.RELEASE
jar
Protostar
@@ -62,7 +62,7 @@
io.netty
netty-buffer
- 4.1.66.Final
+ 4.1.68.Final
provided
diff --git a/src/main/java/io/github/yezhihao/protostar/FieldFactory.java b/src/main/java/io/github/yezhihao/protostar/FieldFactory.java
index 794a0e8..c9a8511 100644
--- a/src/main/java/io/github/yezhihao/protostar/FieldFactory.java
+++ b/src/main/java/io/github/yezhihao/protostar/FieldFactory.java
@@ -65,7 +65,7 @@ public abstract class FieldFactory {
fieldSchema = ConvertSchema.getInstance(field.converter());
break;
case LIST:
- fieldSchema = CollectionSchema.getInstance(schema);
+ fieldSchema = CollectionSchema.getInstance(schema, field.lengthSize());
break;
case MAP:
fieldSchema = ConvertSchema.getInstance(field.converter());
@@ -85,7 +85,7 @@ public abstract class FieldFactory {
result = new FixedField.Logger(field, f, fieldSchema);
}
} else {
- if (field.lengthSize() > 0) {
+ if (field.lengthSize() > 0 && !(fieldSchema instanceof CollectionSchema)) {
result = new DynamicLengthField(field, f, fieldSchema);
} else if (field.length() > 0) {
result = new FixedLengthField(field, f, fieldSchema);
diff --git a/src/main/java/io/github/yezhihao/protostar/field/BasicField.java b/src/main/java/io/github/yezhihao/protostar/field/BasicField.java
index 99ae93d..2de58e5 100644
--- a/src/main/java/io/github/yezhihao/protostar/field/BasicField.java
+++ b/src/main/java/io/github/yezhihao/protostar/field/BasicField.java
@@ -3,8 +3,6 @@ package io.github.yezhihao.protostar.field;
import io.github.yezhihao.protostar.annotation.Field;
import io.github.yezhihao.protostar.util.StrUtils;
import io.netty.buffer.ByteBuf;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
/**
* 固定长度的字段
@@ -12,7 +10,6 @@ import org.slf4j.LoggerFactory;
* home https://gitee.com/yezhihao/jt808-server
*/
public abstract class BasicField implements Comparable> {
- protected static Logger log = LoggerFactory.getLogger(BasicField.class.getSimpleName());
protected final int index;
protected final int length;
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 8bc54ae..69cc063 100644
--- a/src/main/java/io/github/yezhihao/protostar/schema/CollectionSchema.java
+++ b/src/main/java/io/github/yezhihao/protostar/schema/CollectionSchema.java
@@ -1,6 +1,7 @@
package io.github.yezhihao.protostar.schema;
import io.github.yezhihao.protostar.Schema;
+import io.github.yezhihao.protostar.util.ByteBufUtils;
import io.github.yezhihao.protostar.util.Cache;
import io.netty.buffer.ByteBuf;
@@ -9,28 +10,42 @@ import java.util.List;
public class CollectionSchema implements Schema> {
- private static final Cache CACHE = new Cache<>();
+ private static final Cache CACHE = new Cache<>();
- public static CollectionSchema getInstance(Schema schema) {
- return CACHE.get(schema, key -> new CollectionSchema(key));
+ public static CollectionSchema getInstance(Schema schema, int lengthSize) {
+ return CACHE.get(schema.hashCode() + "_" + lengthSize, key -> new CollectionSchema(schema, lengthSize));
}
private final Schema schema;
- private CollectionSchema(Schema schema) {
+ private final int lengthSize;
+
+ private CollectionSchema(Schema schema, int lengthSize) {
this.schema = schema;
+ this.lengthSize = lengthSize;
}
@Override
public List readFrom(ByteBuf input) {
if (!input.isReadable())
return null;
- List list = new ArrayList<>();
- do {
- T obj = schema.readFrom(input);
- if (obj == null) break;
- list.add(obj);
- } while (input.isReadable());
+ List list;
+ if (lengthSize > 0) {
+ int length = ByteBufUtils.readInt(input, lengthSize);
+ list = new ArrayList<>(length);
+ for (int i = 0; i < length; i++) {
+ T obj = schema.readFrom(input);
+ if (obj == null) break;
+ list.add(obj);
+ }
+ } else {
+ list = new ArrayList<>(2);
+ do {
+ T obj = schema.readFrom(input);
+ if (obj == null) break;
+ list.add(obj);
+ } while (input.isReadable());
+ }
return list;
}
@@ -48,7 +63,8 @@ public class CollectionSchema implements Schema> {
public void writeTo(ByteBuf output, List list) {
if (list == null || list.isEmpty())
return;
-
+ if (lengthSize > 0)
+ ByteBufUtils.writeInt(output, lengthSize, list.size());
for (T obj : list) {
schema.writeTo(output, obj);
}