deployed 2.0.4.RELEASE,增加集合类型长度域,升级netty版本

master
剑器近 2021-09-18 18:31:29 +08:00
parent abcf217332
commit b7a938ff4a
4 changed files with 31 additions and 18 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.yezhihao</groupId>
<artifactId>protostar</artifactId>
<version>2.0.3.RELEASE</version>
<version>2.0.4.RELEASE</version>
<packaging>jar</packaging>
<name>Protostar</name>
@ -62,7 +62,7 @@
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
<version>4.1.66.Final</version>
<version>4.1.68.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>

View File

@ -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);

View File

@ -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<T> implements Comparable<BasicField<T>> {
protected static Logger log = LoggerFactory.getLogger(BasicField.class.getSimpleName());
protected final int index;
protected final int length;

View File

@ -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<T> implements Schema<List<T>> {
private static final Cache<Schema, CollectionSchema> CACHE = new Cache<>();
private static final Cache<String, CollectionSchema> 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<T> schema;
private CollectionSchema(Schema<T> schema) {
private final int lengthSize;
private CollectionSchema(Schema<T> schema, int lengthSize) {
this.schema = schema;
this.lengthSize = lengthSize;
}
@Override
public List<T> readFrom(ByteBuf input) {
if (!input.isReadable())
return null;
List<T> list = new ArrayList<>();
do {
T obj = schema.readFrom(input);
if (obj == null) break;
list.add(obj);
} while (input.isReadable());
List<T> 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<T> implements Schema<List<T>> {
public void writeTo(ByteBuf output, List<T> list) {
if (list == null || list.isEmpty())
return;
if (lengthSize > 0)
ByteBufUtils.writeInt(output, lengthSize, list.size());
for (T obj : list) {
schema.writeTo(output, obj);
}