From b7a938ff4af15240e17ef7d1a36c6e305bfa6219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=89=91=E5=99=A8=E8=BF=91?= Date: Sat, 18 Sep 2021 18:31:29 +0800 Subject: [PATCH] =?UTF-8?q?deployed=202.0.4.RELEASE=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E9=9B=86=E5=90=88=E7=B1=BB=E5=9E=8B=E9=95=BF=E5=BA=A6?= =?UTF-8?q?=E5=9F=9F=EF=BC=8C=E5=8D=87=E7=BA=A7netty=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 +- .../yezhihao/protostar/FieldFactory.java | 4 +- .../yezhihao/protostar/field/BasicField.java | 3 -- .../protostar/schema/CollectionSchema.java | 38 +++++++++++++------ 4 files changed, 31 insertions(+), 18 deletions(-) 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); }