TagDecodeable

This commit is contained in:
redkale
2024-09-25 18:28:28 +08:00
parent c50cb855d1
commit bc55d24e08
14 changed files with 64 additions and 102 deletions

View File

@@ -23,7 +23,7 @@ import org.redkale.util.Creator;
* @param <T> 反解析的数组元素类型
*/
@SuppressWarnings("unchecked")
public class ArrayDecoder<R extends Reader, T> implements Decodeable<R, T[]> {
public class ArrayDecoder<R extends Reader, T> implements TagDecodeable<R, T[]> {
protected final Type type;

View File

@@ -21,7 +21,7 @@ import org.redkale.util.Creator;
* @param <T> 反解析的集合元素类型
*/
@SuppressWarnings("unchecked")
public class CollectionDecoder<R extends Reader, T> implements Decodeable<R, Collection<T>> {
public class CollectionDecoder<R extends Reader, T> implements TagDecodeable<R, Collection<T>> {
protected final Type type;

View File

@@ -21,7 +21,7 @@ import org.redkale.util.Creator;
* @param <V> Map value的数据类型
*/
@SuppressWarnings("unchecked")
public class MapDecoder<R extends Reader, K, V> implements Decodeable<R, Map<K, V>> {
public class MapDecoder<R extends Reader, K, V> implements TagDecodeable<R, Map<K, V>> {
protected final Type type;

View File

@@ -21,7 +21,7 @@ import java.util.stream.Stream;
* @param <T> 反解析的集合元素类型
*/
@SuppressWarnings("unchecked")
public class StreamDecoder<R extends Reader, T> implements Decodeable<R, Stream<T>> {
public class StreamDecoder<R extends Reader, T> implements TagDecodeable<R, Stream<T>> {
protected final Type type;

View File

@@ -0,0 +1,25 @@
/*
* Copyright (c) 2016-2116 Redkale
* All rights reserved.
*/
package org.redkale.convert;
/**
* 带tag的反序列化操作类
*
* <p>详情见: https://redkale.org
*
* @author zhangjx
* @param <R> Reader输入的子类
* @param <T> 反解析的数据类型
*/
public interface TagDecodeable<R extends Reader, T> extends Decodeable<R, T> {
/**
* 反序列化操作
*
* @param in R
* @param member DeMember
* @return T
*/
public T convertFrom(final R in, DeMember member);
}

View File

@@ -6,7 +6,6 @@
package org.redkale.convert.pb;
import java.lang.reflect.Type;
import java.util.concurrent.atomic.*;
import org.redkale.convert.*;
/**
@@ -26,15 +25,7 @@ public class ProtobufArrayDecoder<T> extends ArrayDecoder<ProtobufReader, T> {
this.enumtostring = factory.enumtostring;
Type comtype = this.getComponentType();
this.string = String.class == comtype;
this.simple = Boolean.class == comtype
|| Short.class == comtype
|| Character.class == comtype
|| Integer.class == comtype
|| Float.class == comtype
|| Long.class == comtype
|| Double.class == comtype
|| AtomicInteger.class == comtype
|| AtomicLong.class == comtype;
this.simple = ProtobufFactory.isNoLenBytesType(comtype);
}
@Override

View File

@@ -6,7 +6,6 @@
package org.redkale.convert.pb;
import java.lang.reflect.Type;
import java.util.concurrent.atomic.*;
import org.redkale.convert.*;
/**
@@ -19,16 +18,7 @@ public class ProtobufArrayEncoder<T> extends ArrayEncoder<ProtobufWriter, T> {
public ProtobufArrayEncoder(ProtobufFactory factory, Type type) {
super(factory, type);
Type comtype = this.getComponentType();
this.simple = Boolean.class == comtype
|| Short.class == comtype
|| Character.class == comtype
|| Integer.class == comtype
|| Float.class == comtype
|| Long.class == comtype
|| Double.class == comtype
|| AtomicInteger.class == comtype
|| AtomicLong.class == comtype;
this.simple = ProtobufFactory.isNoLenBytesType(getComponentType());
}
@Override

View File

@@ -6,7 +6,6 @@
package org.redkale.convert.pb;
import java.lang.reflect.Type;
import java.util.concurrent.atomic.*;
import org.redkale.convert.*;
/**
@@ -26,15 +25,7 @@ public class ProtobufCollectionDecoder<T> extends CollectionDecoder<ProtobufRead
this.enumtostring = factory.enumtostring;
Type comtype = this.getComponentType();
this.string = String.class == comtype;
this.simple = Boolean.class == comtype
|| Short.class == comtype
|| Character.class == comtype
|| Integer.class == comtype
|| Float.class == comtype
|| Long.class == comtype
|| Double.class == comtype
|| AtomicInteger.class == comtype
|| AtomicLong.class == comtype;
this.simple = ProtobufFactory.isNoLenBytesType(comtype);
}
@Override

View File

@@ -6,7 +6,6 @@
package org.redkale.convert.pb;
import java.lang.reflect.Type;
import java.util.concurrent.atomic.*;
import org.redkale.convert.*;
/**
@@ -19,16 +18,7 @@ public class ProtobufCollectionEncoder<T> extends CollectionEncoder<ProtobufWrit
public ProtobufCollectionEncoder(ProtobufFactory factory, Type type) {
super(factory, type);
Type comtype = this.getComponentType();
this.simple = Boolean.class == comtype
|| Short.class == comtype
|| Character.class == comtype
|| Integer.class == comtype
|| Float.class == comtype
|| Long.class == comtype
|| Double.class == comtype
|| AtomicInteger.class == comtype
|| AtomicLong.class == comtype;
this.simple = ProtobufFactory.isNoLenBytesType(getComponentType());
}
@Override

View File

@@ -214,6 +214,20 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
}
}
protected static boolean isNoLenBytesType(Type type) {
return (type instanceof Class && ((Class) type).isPrimitive())
|| type == Boolean.class
|| type == Byte.class
|| type == Short.class
|| type == Character.class
|| type == Integer.class
|| type == Float.class
|| type == Long.class
|| type == Double.class
|| type == AtomicInteger.class
|| type == AtomicLong.class;
}
public static int getTag(String fieldName, Type fieldType, int fieldPos, boolean enumtostring) {
int wiretype = ProtobufFactory.wireType(fieldType, enumtostring);
return (fieldPos << 3 | wiretype);

View File

@@ -33,7 +33,9 @@ public class ProtobufObjectDecoder<T> extends ObjectDecoder<ProtobufReader, T> {
@Override
protected ProtobufReader objectReader(ProtobufReader in) {
if (in.position() > in.initoffset) return new ProtobufReader(in.readByteArray());
if (in.position() > in.initoffset) {
return new ProtobufReader(in.readByteArray());
}
return in;
}
@@ -45,14 +47,8 @@ public class ProtobufObjectDecoder<T> extends ObjectDecoder<ProtobufReader, T> {
@Override
protected Object readDeMemberValue(ProtobufReader in, DeMember member, boolean first) {
Decodeable decoder = member.getDecoder();
if (decoder instanceof ProtobufArrayDecoder) {
return ((ProtobufArrayDecoder) decoder).convertFrom(in, member);
} else if (decoder instanceof ProtobufCollectionDecoder) {
return ((ProtobufCollectionDecoder) decoder).convertFrom(in, member);
} else if (decoder instanceof ProtobufStreamDecoder) {
return ((ProtobufStreamDecoder) decoder).convertFrom(in, member);
} else if (decoder instanceof ProtobufMapDecoder) {
return ((ProtobufMapDecoder) decoder).convertFrom(in, member);
if (decoder instanceof TagDecodeable) {
return ((TagDecodeable) decoder).convertFrom(in, member);
} else {
return member.read(in);
}
@@ -61,14 +57,8 @@ public class ProtobufObjectDecoder<T> extends ObjectDecoder<ProtobufReader, T> {
@Override
protected void readDeMemberValue(ProtobufReader in, DeMember member, T result, boolean first) {
Decodeable decoder = member.getDecoder();
if (decoder instanceof ProtobufArrayDecoder) {
member.getAttribute().set(result, ((ProtobufArrayDecoder) decoder).convertFrom(in, member));
} else if (decoder instanceof ProtobufCollectionDecoder) {
member.getAttribute().set(result, ((ProtobufCollectionDecoder) decoder).convertFrom(in, member));
} else if (decoder instanceof ProtobufStreamDecoder) {
member.getAttribute().set(result, ((ProtobufStreamDecoder) decoder).convertFrom(in, member));
} else if (decoder instanceof ProtobufMapDecoder) {
member.getAttribute().set(result, ((ProtobufMapDecoder) decoder).convertFrom(in, member));
if (decoder instanceof TagDecodeable) {
member.getAttribute().set(result, ((TagDecodeable) decoder).convertFrom(in, member));
} else {
member.read(in, result);
}

View File

@@ -8,7 +8,6 @@ package org.redkale.convert.pb;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.atomic.*;
import org.redkale.convert.*;
/** @author zhangjx */
@@ -20,7 +19,7 @@ public class ProtobufReader extends Reader {
private byte[] content;
protected int cachetag = Integer.MIN_VALUE;
protected int cacheTag = Integer.MIN_VALUE;
protected boolean enumtostring;
@@ -106,6 +105,8 @@ public class ProtobufReader extends Reader {
case 5:
readRawLittleEndian32();
break;
default:
break;
}
}
@@ -146,18 +147,7 @@ public class ProtobufReader extends Reader {
if (!(type instanceof Class)) {
return Reader.SIGN_NOLENBUTBYTES;
}
Class clazz = (Class) type;
if (clazz.isPrimitive()
|| clazz == Boolean.class
|| clazz == Byte.class
|| clazz == Short.class
|| clazz == Character.class
|| clazz == Integer.class
|| clazz == Float.class
|| clazz == Long.class
|| clazz == Double.class
|| clazz == AtomicInteger.class
|| clazz == AtomicLong.class) {
if (ProtobufFactory.isNoLenBytesType(type)) {
return Reader.SIGN_NOLENBUTBYTES;
}
return Reader.SIGN_NOLENGTH;
@@ -276,16 +266,16 @@ public class ProtobufReader extends Reader {
}
protected final int readTag() {
if (cachetag != Integer.MIN_VALUE) {
int tag = cachetag;
cachetag = Integer.MIN_VALUE;
if (cacheTag != Integer.MIN_VALUE) {
int tag = cacheTag;
cacheTag = Integer.MIN_VALUE;
return tag;
}
return readRawVarint32();
}
protected final void backTag(int tag) {
this.cachetag = tag;
this.cacheTag = tag;
}
protected byte currentByte() {

View File

@@ -6,7 +6,6 @@
package org.redkale.convert.pb;
import java.lang.reflect.Type;
import java.util.concurrent.atomic.*;
import org.redkale.convert.*;
/**
@@ -26,15 +25,7 @@ public class ProtobufStreamDecoder<T> extends StreamDecoder<ProtobufReader, T> {
this.enumtostring = ((ProtobufFactory) factory).enumtostring;
Type comtype = this.getComponentType();
this.string = String.class == comtype;
this.simple = Boolean.class == comtype
|| Short.class == comtype
|| Character.class == comtype
|| Integer.class == comtype
|| Float.class == comtype
|| Long.class == comtype
|| Double.class == comtype
|| AtomicInteger.class == comtype
|| AtomicLong.class == comtype;
this.simple = ProtobufFactory.isNoLenBytesType(comtype);
}
@Override

View File

@@ -6,7 +6,6 @@
package org.redkale.convert.pb;
import java.lang.reflect.Type;
import java.util.concurrent.atomic.*;
import org.redkale.convert.*;
/**
@@ -19,16 +18,7 @@ public class ProtobufStreamEncoder<T> extends StreamEncoder<ProtobufWriter, T> {
public ProtobufStreamEncoder(ConvertFactory factory, Type type) {
super(factory, type);
Type comtype = this.getComponentType();
this.simple = Boolean.class == comtype
|| Short.class == comtype
|| Character.class == comtype
|| Integer.class == comtype
|| Float.class == comtype
|| Long.class == comtype
|| Double.class == comtype
|| AtomicInteger.class == comtype
|| AtomicLong.class == comtype;
this.simple = ProtobufFactory.isNoLenBytesType(getComponentType());
}
@Override