protobuf
This commit is contained in:
@@ -317,6 +317,37 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
return ptype.getActualTypeArguments()[0];
|
||||
}
|
||||
|
||||
protected boolean isStreamType(final Type type) {
|
||||
if (type == null) {
|
||||
return false;
|
||||
}
|
||||
if (type instanceof Class) {
|
||||
return Stream.class.isAssignableFrom((Class) type);
|
||||
}
|
||||
ParameterizedType ptype = (ParameterizedType) type;
|
||||
if (!(ptype.getRawType() instanceof Class)) {
|
||||
return false;
|
||||
}
|
||||
Type[] ptargs = ptype.getActualTypeArguments();
|
||||
if (ptargs == null || ptargs.length != 1) {
|
||||
return false;
|
||||
}
|
||||
Class ownerType = (Class) ptype.getRawType();
|
||||
return Stream.class.isAssignableFrom(ownerType);
|
||||
}
|
||||
|
||||
// 返回null表示type不是Stream类型
|
||||
protected Type getStreamionComponentType(final Type type) {
|
||||
if (!isStreamType(type)) {
|
||||
return null;
|
||||
}
|
||||
if (type instanceof Class) {
|
||||
return Object.class;
|
||||
}
|
||||
ParameterizedType ptype = ((ParameterizedType) type);
|
||||
return ptype.getActualTypeArguments()[0];
|
||||
}
|
||||
|
||||
protected <E> Encodeable<W, E> createDyncEncoder(Type type) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
package org.redkale.convert.ext;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Objects;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.convert.json.*;
|
||||
|
||||
@@ -22,6 +23,16 @@ public class BigIntegerSimpledCoder<R extends Reader, W extends Writer> extends
|
||||
|
||||
public static final BigIntegerSimpledCoder instance = new BigIntegerSimpledCoder();
|
||||
|
||||
protected final SimpledCoder<R, W, byte[]> bsSimpledCoder;
|
||||
|
||||
protected BigIntegerSimpledCoder() {
|
||||
this.bsSimpledCoder = ByteArraySimpledCoder.instance;
|
||||
}
|
||||
|
||||
public BigIntegerSimpledCoder(SimpledCoder<R, W, byte[]> bSimpledCoder) {
|
||||
this.bsSimpledCoder = Objects.requireNonNull(bSimpledCoder);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void convertTo(W out, BigInteger value) {
|
||||
@@ -29,13 +40,13 @@ public class BigIntegerSimpledCoder<R extends Reader, W extends Writer> extends
|
||||
out.writeNull();
|
||||
return;
|
||||
}
|
||||
ByteArraySimpledCoder.instance.convertTo(out, value.toByteArray());
|
||||
bsSimpledCoder.convertTo(out, value.toByteArray());
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public BigInteger convertFrom(R in) {
|
||||
byte[] bytes = ByteArraySimpledCoder.instance.convertFrom(in);
|
||||
byte[] bytes = bsSimpledCoder.convertFrom(in);
|
||||
return bytes == null ? null : new BigInteger(bytes);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
package org.redkale.convert.ext;
|
||||
|
||||
import java.net.*;
|
||||
import java.util.Objects;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.convert.json.*;
|
||||
import org.redkale.util.StringWrapper;
|
||||
@@ -24,18 +25,29 @@ public class InetAddressSimpledCoder<R extends Reader, W extends Writer> extends
|
||||
|
||||
public static final InetAddressSimpledCoder instance = new InetAddressSimpledCoder();
|
||||
|
||||
protected final SimpledCoder<R, W, byte[]> bsSimpledCoder;
|
||||
|
||||
protected InetAddressSimpledCoder() {
|
||||
this.bsSimpledCoder = ByteArraySimpledCoder.instance;
|
||||
}
|
||||
|
||||
public InetAddressSimpledCoder(SimpledCoder<R, W, byte[]> bSimpledCoder) {
|
||||
this.bsSimpledCoder = Objects.requireNonNull(bSimpledCoder);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, InetAddress value) {
|
||||
if (value == null) {
|
||||
out.writeNull();
|
||||
return;
|
||||
}
|
||||
ByteArraySimpledCoder.instance.convertTo(out, value.getAddress());
|
||||
bsSimpledCoder.convertTo(out, value.getAddress());
|
||||
}
|
||||
|
||||
@Override
|
||||
public InetAddress convertFrom(R in) {
|
||||
byte[] bytes = ByteArraySimpledCoder.instance.convertFrom(in);
|
||||
byte[] bytes = bsSimpledCoder.convertFrom(in);
|
||||
if (bytes == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
package org.redkale.convert.ext;
|
||||
|
||||
import java.time.*;
|
||||
import java.util.Objects;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.convert.json.*;
|
||||
|
||||
@@ -18,13 +19,20 @@ import org.redkale.convert.json.*;
|
||||
* @param <R> Reader输入的子类型
|
||||
* @param <W> Writer输出的子类型
|
||||
*/
|
||||
public class LocalDateTimeSimpledCoder<R extends Reader, W extends Writer>
|
||||
extends SimpledCoder<R, W, LocalDateTime> {
|
||||
|
||||
private static final ByteArraySimpledCoder bsSimpledCoder = ByteArraySimpledCoder.instance;
|
||||
public class LocalDateTimeSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, LocalDateTime> {
|
||||
|
||||
public static final LocalDateTimeSimpledCoder instance = new LocalDateTimeSimpledCoder();
|
||||
|
||||
protected final SimpledCoder<R, W, byte[]> bsSimpledCoder;
|
||||
|
||||
protected LocalDateTimeSimpledCoder() {
|
||||
this.bsSimpledCoder = ByteArraySimpledCoder.instance;
|
||||
}
|
||||
|
||||
public LocalDateTimeSimpledCoder(SimpledCoder<R, W, byte[]> bSimpledCoder) {
|
||||
this.bsSimpledCoder = Objects.requireNonNull(bSimpledCoder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, LocalDateTime value) {
|
||||
if (value == null) {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
package org.redkale.convert.ext;
|
||||
|
||||
import java.util.Objects;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.convert.json.*;
|
||||
import org.redkale.util.*;
|
||||
@@ -20,10 +21,18 @@ import org.redkale.util.*;
|
||||
*/
|
||||
public class Uint128SimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Uint128> {
|
||||
|
||||
private static final ByteArraySimpledCoder bsSimpledCoder = ByteArraySimpledCoder.instance;
|
||||
|
||||
public static final Uint128SimpledCoder instance = new Uint128SimpledCoder();
|
||||
|
||||
protected final SimpledCoder<R, W, byte[]> bsSimpledCoder;
|
||||
|
||||
protected Uint128SimpledCoder() {
|
||||
this.bsSimpledCoder = ByteArraySimpledCoder.instance;
|
||||
}
|
||||
|
||||
public Uint128SimpledCoder(SimpledCoder<R, W, byte[]> bSimpledCoder) {
|
||||
this.bsSimpledCoder = Objects.requireNonNull(bSimpledCoder);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void convertTo(final W out, final Uint128 value) {
|
||||
|
||||
@@ -34,13 +34,21 @@ public class ProtobufArrayDecoder<T> extends ArrayDecoder<ProtobufReader, T>
|
||||
final List<T> result = new ArrayList();
|
||||
final int limit = in.limit();
|
||||
while (in.hasNext()) {
|
||||
ProtobufReader subin = in;
|
||||
boolean nodata = false;
|
||||
if (!simpled) {
|
||||
int contentLen = in.readRawVarint32();
|
||||
in.limit(in.position() + contentLen + 1);
|
||||
if (contentLen == 0) {
|
||||
nodata = true;
|
||||
} else {
|
||||
in.limit(in.position() + contentLen + 1);
|
||||
}
|
||||
}
|
||||
if (nodata) {
|
||||
result.add(null);
|
||||
} else {
|
||||
result.add(itemDecoder.convertFrom(in));
|
||||
in.limit(limit);
|
||||
}
|
||||
result.add(itemDecoder.convertFrom(subin));
|
||||
in.limit(limit);
|
||||
if (!in.readNextTag(member)) { // 元素结束
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -42,11 +42,10 @@ public class ProtobufArrayEncoder<T> extends ArrayEncoder<ProtobufWriter, T>
|
||||
for (T item : array) {
|
||||
out.writeField(member);
|
||||
if (item == null) {
|
||||
out.writeUInt32(0);
|
||||
out.writeLength(0);
|
||||
} else if (componentSimpled) {
|
||||
itemEncoder.convertTo(out, item);
|
||||
} else {
|
||||
// itemEncoder.convertTo(out, item);
|
||||
ProtobufWriter tmp = out.pollChild();
|
||||
itemEncoder.convertTo(tmp, item);
|
||||
out.writeTuple(tmp);
|
||||
@@ -74,6 +73,11 @@ public class ProtobufArrayEncoder<T> extends ArrayEncoder<ProtobufWriter, T>
|
||||
|
||||
@Override
|
||||
public boolean requireSize() {
|
||||
return componentSizeRequired;
|
||||
return !componentSimpled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final ProtobufTypeEnum typeEnum() {
|
||||
return ProtobufTypeEnum.BYTES;
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -34,12 +34,21 @@ public class ProtobufCollectionDecoder<T> extends CollectionDecoder<ProtobufRead
|
||||
final Collection<T> result = this.creator.create();
|
||||
final int limit = in.limit();
|
||||
while (in.hasNext()) {
|
||||
boolean nodata = false;
|
||||
if (!simpled) {
|
||||
int contentLen = in.readRawVarint32();
|
||||
in.limit(in.position() + contentLen + 1);
|
||||
if (contentLen == 0) {
|
||||
nodata = true;
|
||||
} else {
|
||||
in.limit(in.position() + contentLen + 1);
|
||||
}
|
||||
}
|
||||
if (nodata) {
|
||||
result.add(null);
|
||||
} else {
|
||||
result.add(itemDecoder.convertFrom(in));
|
||||
in.limit(limit);
|
||||
}
|
||||
result.add(itemDecoder.convertFrom(in));
|
||||
in.limit(limit);
|
||||
if (!in.readNextTag(member)) { // 元素结束
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ public class ProtobufCollectionEncoder<T> extends CollectionEncoder<ProtobufWrit
|
||||
if (value == null || value.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
ProtobufEncodeable itemEncoder = (ProtobufEncodeable)this.componentEncoder;
|
||||
ProtobufEncodeable itemEncoder = (ProtobufEncodeable) this.componentEncoder;
|
||||
// if (componentSizeRequired) {
|
||||
// int tagSize = ProtobufFactory.computeSInt32SizeNoTag(member.getTag());
|
||||
// out.writeLength(computeSize(tagSize, value));
|
||||
@@ -40,7 +40,7 @@ public class ProtobufCollectionEncoder<T> extends CollectionEncoder<ProtobufWrit
|
||||
for (T item : value) {
|
||||
out.writeField(member);
|
||||
if (item == null) {
|
||||
out.writeUInt32(0);
|
||||
out.writeLength(0);
|
||||
} else if (componentSimpled) {
|
||||
itemEncoder.convertTo(out, item);
|
||||
} else {
|
||||
@@ -71,6 +71,11 @@ public class ProtobufCollectionEncoder<T> extends CollectionEncoder<ProtobufWrit
|
||||
|
||||
@Override
|
||||
public boolean requireSize() {
|
||||
return componentSizeRequired;
|
||||
return !componentSimpled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final ProtobufTypeEnum typeEnum() {
|
||||
return ProtobufTypeEnum.BYTES;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ package org.redkale.convert.pb;
|
||||
import java.lang.reflect.*;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.*;
|
||||
import org.redkale.asm.*;
|
||||
import static org.redkale.asm.ClassWriter.COMPUTE_FRAMES;
|
||||
import static org.redkale.asm.Opcodes.*;
|
||||
@@ -50,9 +49,9 @@ public abstract class ProtobufDynEncoder<T> extends ProtobufObjectEncoder<T> {
|
||||
protected static ProtobufDynEncoder generateDyncEncoder(final ProtobufFactory factory, final Class clazz) {
|
||||
final ObjectEncoder selfObjEncoder = factory.createObjectEncoder(clazz);
|
||||
selfObjEncoder.init(factory); // 必须执行,初始化EnMember内部信息
|
||||
// if (((ProtobufObjectEncoder) selfObjEncoder).requiredMemberSize()) { // 嵌套对象
|
||||
// return null;
|
||||
// }
|
||||
if (((ProtobufObjectEncoder) selfObjEncoder).requiredMemberSize()) { // 嵌套对象
|
||||
return null;
|
||||
}
|
||||
final Map<String, SimpledCoder> simpledCoders = new HashMap<>();
|
||||
final Map<String, EnMember> otherMembers = new HashMap<>();
|
||||
StringBuilder elementb = new StringBuilder();
|
||||
@@ -254,12 +253,6 @@ public abstract class ProtobufDynEncoder<T> extends ProtobufObjectEncoder<T> {
|
||||
wmethodName = "writeFieldLongsValue";
|
||||
} else if (componentType == Double.class) {
|
||||
wmethodName = "writeFieldDoublesValue";
|
||||
} else if (componentType == AtomicBoolean.class) {
|
||||
wmethodName = "writeFieldAtomicBooleansValue";
|
||||
} else if (componentType == AtomicInteger.class) {
|
||||
wmethodName = "writeFieldAtomicIntegersValue";
|
||||
} else if (componentType == AtomicLong.class) {
|
||||
wmethodName = "writeFieldAtomicLongsValue";
|
||||
} else if (componentType == String.class) {
|
||||
wmethodName = "writeFieldStringsValue";
|
||||
}
|
||||
|
||||
@@ -31,4 +31,8 @@ public interface ProtobufEncodeable<W extends Writer, T> extends Encodeable<W, T
|
||||
default void convertTo(W out, EnMember member, T value) {
|
||||
convertTo(out, value);
|
||||
}
|
||||
|
||||
// 获取数据类型枚举
|
||||
public ProtobufTypeEnum typeEnum();
|
||||
|
||||
}
|
||||
|
||||
@@ -78,4 +78,9 @@ public class ProtobufEnumSimpledCoder<R extends ProtobufReader, W extends Protob
|
||||
public boolean requireSize() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final ProtobufTypeEnum typeEnum() {
|
||||
return enumtostring ? ProtobufTypeEnum.BYTES : ProtobufTypeEnum.INT;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,6 +93,7 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
this.register(float[].class, ProtobufCoders.ProtobufFloatArraySimpledCoder.instance);
|
||||
this.register(long[].class, ProtobufCoders.ProtobufLongArraySimpledCoder.instance);
|
||||
this.register(double[].class, ProtobufCoders.ProtobufDoubleArraySimpledCoder.instance);
|
||||
|
||||
this.register(Boolean[].class, ProtobufCoders.ProtobufBoolArraySimpledCoder2.instance);
|
||||
this.register(Byte[].class, ProtobufCoders.ProtobufByteArraySimpledCoder2.instance);
|
||||
this.register(Character[].class, ProtobufCoders.ProtobufCharArraySimpledCoder2.instance);
|
||||
@@ -101,12 +102,9 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
this.register(Float[].class, ProtobufCoders.ProtobufFloatArraySimpledCoder2.instance);
|
||||
this.register(Long[].class, ProtobufCoders.ProtobufLongArraySimpledCoder2.instance);
|
||||
this.register(Double[].class, ProtobufCoders.ProtobufDoubleArraySimpledCoder2.instance);
|
||||
this.register(AtomicInteger[].class, ProtobufCoders.ProtobufAtomicIntegerArraySimpledCoder.instance);
|
||||
this.register(AtomicLong[].class, ProtobufCoders.ProtobufAtomicLongArraySimpledCoder.instance);
|
||||
|
||||
this.register(String[].class, this.createArrayDecoder(String[].class));
|
||||
this.register(String[].class, this.createArrayEncoder(String[].class));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,42 +188,38 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
|
||||
@Override
|
||||
protected <E> Decodeable<ProtobufReader, E> createCollectionDecoder(Type type) {
|
||||
if (type instanceof ParameterizedType) {
|
||||
ParameterizedType pt = (ParameterizedType) type;
|
||||
Type componentType = pt.getActualTypeArguments()[0];
|
||||
if (componentType == Boolean.class) {
|
||||
Creator<? extends Collection> creator = loadCreator((Class) pt.getRawType());
|
||||
return (Decodeable) new ProtobufCoders.ProtobufBoolCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Byte.class) {
|
||||
Creator<? extends Collection> creator = loadCreator((Class) pt.getRawType());
|
||||
return (Decodeable) new ProtobufCoders.ProtobufByteCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Character.class) {
|
||||
Creator<? extends Collection> creator = loadCreator((Class) pt.getRawType());
|
||||
return (Decodeable) new ProtobufCoders.ProtobufCharCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Short.class) {
|
||||
Creator<? extends Collection> creator = loadCreator((Class) pt.getRawType());
|
||||
return (Decodeable) new ProtobufCoders.ProtobufShortCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Integer.class) {
|
||||
Creator<? extends Collection> creator = loadCreator((Class) pt.getRawType());
|
||||
return (Decodeable) new ProtobufCoders.ProtobufIntCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Float.class) {
|
||||
Creator<? extends Collection> creator = loadCreator((Class) pt.getRawType());
|
||||
return (Decodeable) new ProtobufCoders.ProtobufFloatCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Long.class) {
|
||||
Creator<? extends Collection> creator = loadCreator((Class) pt.getRawType());
|
||||
return (Decodeable) new ProtobufCoders.ProtobufLongCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Double.class) {
|
||||
Creator<? extends Collection> creator = loadCreator((Class) pt.getRawType());
|
||||
return (Decodeable) new ProtobufCoders.ProtobufDoubleCollectionSimpledCoder(creator);
|
||||
} else if (componentType == AtomicBoolean.class) {
|
||||
Creator<? extends Collection> creator = loadCreator((Class) pt.getRawType());
|
||||
return (Decodeable) new ProtobufCoders.ProtobufAtomicBooleanCollectionSimpledCoder(creator);
|
||||
} else if (componentType == AtomicInteger.class) {
|
||||
Creator<? extends Collection> creator = loadCreator((Class) pt.getRawType());
|
||||
return (Decodeable) new ProtobufCoders.ProtobufAtomicIntegerCollectionSimpledCoder(creator);
|
||||
} else if (componentType == AtomicLong.class) {
|
||||
Creator<? extends Collection> creator = loadCreator((Class) pt.getRawType());
|
||||
return (Decodeable) new ProtobufCoders.ProtobufAtomicLongCollectionSimpledCoder(creator);
|
||||
Class createClazz = TypeToken.typeToClass(type);
|
||||
Type componentType = getCollectionComponentType(type);
|
||||
if (componentType == Boolean.class) {
|
||||
Creator<? extends Collection> creator = loadCreator(createClazz);
|
||||
return (Decodeable) new ProtobufCoders.ProtobufBoolCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Byte.class) {
|
||||
Creator<? extends Collection> creator = loadCreator(createClazz);
|
||||
return (Decodeable) new ProtobufCoders.ProtobufByteCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Character.class) {
|
||||
Creator<? extends Collection> creator = loadCreator(createClazz);
|
||||
return (Decodeable) new ProtobufCoders.ProtobufCharCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Short.class) {
|
||||
Creator<? extends Collection> creator = loadCreator(createClazz);
|
||||
return (Decodeable) new ProtobufCoders.ProtobufShortCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Integer.class) {
|
||||
Creator<? extends Collection> creator = loadCreator(createClazz);
|
||||
return (Decodeable) new ProtobufCoders.ProtobufIntCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Float.class) {
|
||||
Creator<? extends Collection> creator = loadCreator(createClazz);
|
||||
return (Decodeable) new ProtobufCoders.ProtobufFloatCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Long.class) {
|
||||
Creator<? extends Collection> creator = loadCreator(createClazz);
|
||||
return (Decodeable) new ProtobufCoders.ProtobufLongCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Double.class) {
|
||||
Creator<? extends Collection> creator = loadCreator(createClazz);
|
||||
return (Decodeable) new ProtobufCoders.ProtobufDoubleCollectionSimpledCoder(creator);
|
||||
} else {
|
||||
Decodeable componentCoder = findDecoder(componentType);
|
||||
if (componentCoder instanceof ProtobufPrimitivable) {
|
||||
Creator<? extends Collection> creator = loadCreator(createClazz);
|
||||
ProtobufPrimitivable primCoder = (ProtobufPrimitivable) componentCoder;
|
||||
return (Decodeable) new ProtobufCoders.ProtobufPrimitiveCollectionSimpledCoder(creator, primCoder);
|
||||
}
|
||||
}
|
||||
return new ProtobufCollectionDecoder(this, type);
|
||||
@@ -233,32 +227,29 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
|
||||
@Override
|
||||
protected <E> Encodeable<ProtobufWriter, E> createCollectionEncoder(Type type) {
|
||||
if (type instanceof ParameterizedType) {
|
||||
ParameterizedType pt = (ParameterizedType) type;
|
||||
Type componentType = pt.getActualTypeArguments()[0];
|
||||
Creator<? extends Collection> creator = ProtobufCoders.LIST_CREATOR;
|
||||
if (componentType == Boolean.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufBoolCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Byte.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufByteCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Character.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufCharCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Short.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufShortCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Integer.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufIntCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Float.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufFloatCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Long.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufLongCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Double.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufDoubleCollectionSimpledCoder(creator);
|
||||
} else if (componentType == AtomicBoolean.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufAtomicBooleanCollectionSimpledCoder(creator);
|
||||
} else if (componentType == AtomicInteger.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufAtomicIntegerCollectionSimpledCoder(creator);
|
||||
} else if (componentType == AtomicLong.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufAtomicLongCollectionSimpledCoder(creator);
|
||||
Creator<? extends Collection> creator = ProtobufCoders.LIST_CREATOR;
|
||||
Type componentType = getCollectionComponentType(type);
|
||||
if (componentType == Boolean.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufBoolCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Byte.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufByteCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Character.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufCharCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Short.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufShortCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Integer.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufIntCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Float.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufFloatCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Long.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufLongCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Double.class) {
|
||||
return (Encodeable) new ProtobufCoders.ProtobufDoubleCollectionSimpledCoder(creator);
|
||||
} else {
|
||||
Encodeable componentCoder = findEncoder(componentType);
|
||||
if (componentCoder instanceof ProtobufPrimitivable) {
|
||||
ProtobufPrimitivable primCoder = (ProtobufPrimitivable) componentCoder;
|
||||
return (Encodeable) new ProtobufCoders.ProtobufPrimitiveCollectionSimpledCoder(creator, primCoder);
|
||||
}
|
||||
}
|
||||
return new ProtobufCollectionEncoder(this, type);
|
||||
@@ -266,31 +257,28 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
|
||||
@Override
|
||||
protected <E> Decodeable<ProtobufReader, E> createStreamDecoder(Type type) {
|
||||
if (type instanceof ParameterizedType) {
|
||||
ParameterizedType pt = (ParameterizedType) type;
|
||||
Type componentType = pt.getActualTypeArguments()[0];
|
||||
if (componentType == Boolean.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufBoolStreamSimpledCoder.instance;
|
||||
} else if (componentType == Byte.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufByteStreamSimpledCoder.instance;
|
||||
} else if (componentType == Character.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufCharStreamSimpledCoder.instance;
|
||||
} else if (componentType == Short.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufShortStreamSimpledCoder.instance;
|
||||
} else if (componentType == Integer.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufIntStreamSimpledCoder.instance;
|
||||
} else if (componentType == Float.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufFloatStreamSimpledCoder.instance;
|
||||
} else if (componentType == Long.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufLongStreamSimpledCoder.instance;
|
||||
} else if (componentType == Double.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufDoubleStreamSimpledCoder.instance;
|
||||
} else if (componentType == AtomicBoolean.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufAtomicBooleanStreamSimpledCoder.instance;
|
||||
} else if (componentType == AtomicInteger.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufAtomicIntegerStreamSimpledCoder.instance;
|
||||
} else if (componentType == AtomicLong.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufAtomicLongStreamSimpledCoder.instance;
|
||||
Type componentType = getStreamionComponentType(type);
|
||||
if (componentType == Boolean.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufBoolStreamSimpledCoder.instance;
|
||||
} else if (componentType == Byte.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufByteStreamSimpledCoder.instance;
|
||||
} else if (componentType == Character.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufCharStreamSimpledCoder.instance;
|
||||
} else if (componentType == Short.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufShortStreamSimpledCoder.instance;
|
||||
} else if (componentType == Integer.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufIntStreamSimpledCoder.instance;
|
||||
} else if (componentType == Float.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufFloatStreamSimpledCoder.instance;
|
||||
} else if (componentType == Long.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufLongStreamSimpledCoder.instance;
|
||||
} else if (componentType == Double.class) {
|
||||
return (Decodeable) ProtobufCoders.ProtobufDoubleStreamSimpledCoder.instance;
|
||||
} else {
|
||||
Decodeable componentCoder = findDecoder(componentType);
|
||||
if (componentCoder instanceof ProtobufPrimitivable) {
|
||||
ProtobufPrimitivable primCoder = (ProtobufPrimitivable) componentCoder;
|
||||
return (Decodeable) new ProtobufCoders.ProtobufPrimitiveStreamSimpledCoder(primCoder);
|
||||
}
|
||||
}
|
||||
return new ProtobufStreamDecoder(this, type);
|
||||
@@ -298,31 +286,28 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
|
||||
@Override
|
||||
protected <E> Encodeable<ProtobufWriter, E> createStreamEncoder(Type type) {
|
||||
if (type instanceof ParameterizedType) {
|
||||
ParameterizedType pt = (ParameterizedType) type;
|
||||
Type componentType = pt.getActualTypeArguments()[0];
|
||||
if (componentType == Boolean.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufBoolStreamSimpledCoder.instance;
|
||||
} else if (componentType == Byte.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufByteStreamSimpledCoder.instance;
|
||||
} else if (componentType == Character.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufCharStreamSimpledCoder.instance;
|
||||
} else if (componentType == Short.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufShortStreamSimpledCoder.instance;
|
||||
} else if (componentType == Integer.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufIntStreamSimpledCoder.instance;
|
||||
} else if (componentType == Float.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufFloatStreamSimpledCoder.instance;
|
||||
} else if (componentType == Long.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufLongStreamSimpledCoder.instance;
|
||||
} else if (componentType == Double.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufDoubleStreamSimpledCoder.instance;
|
||||
} else if (componentType == AtomicBoolean.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufAtomicBooleanStreamSimpledCoder.instance;
|
||||
} else if (componentType == AtomicInteger.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufAtomicIntegerStreamSimpledCoder.instance;
|
||||
} else if (componentType == AtomicLong.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufAtomicLongStreamSimpledCoder.instance;
|
||||
Type componentType = getStreamionComponentType(type);
|
||||
if (componentType == Boolean.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufBoolStreamSimpledCoder.instance;
|
||||
} else if (componentType == Byte.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufByteStreamSimpledCoder.instance;
|
||||
} else if (componentType == Character.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufCharStreamSimpledCoder.instance;
|
||||
} else if (componentType == Short.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufShortStreamSimpledCoder.instance;
|
||||
} else if (componentType == Integer.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufIntStreamSimpledCoder.instance;
|
||||
} else if (componentType == Float.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufFloatStreamSimpledCoder.instance;
|
||||
} else if (componentType == Long.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufLongStreamSimpledCoder.instance;
|
||||
} else if (componentType == Double.class) {
|
||||
return (Encodeable) ProtobufCoders.ProtobufDoubleStreamSimpledCoder.instance;
|
||||
} else {
|
||||
Encodeable componentCoder = findEncoder(componentType);
|
||||
if (componentCoder instanceof ProtobufPrimitivable) {
|
||||
ProtobufPrimitivable primCoder = (ProtobufPrimitivable) componentCoder;
|
||||
return (Encodeable) new ProtobufCoders.ProtobufPrimitiveStreamSimpledCoder(primCoder);
|
||||
}
|
||||
}
|
||||
return new ProtobufStreamEncoder(this, type);
|
||||
@@ -366,6 +351,7 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
return true;
|
||||
}
|
||||
|
||||
// 对应ProtobufWriter.writeFieldValue方法
|
||||
protected static boolean isSimpleType(Class fieldClass) {
|
||||
return fieldClass.isPrimitive()
|
||||
|| fieldClass == Boolean.class
|
||||
@@ -376,9 +362,6 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
|| fieldClass == Float.class
|
||||
|| fieldClass == Long.class
|
||||
|| fieldClass == Double.class
|
||||
|| fieldClass == AtomicBoolean.class
|
||||
|| fieldClass == AtomicInteger.class
|
||||
|| fieldClass == AtomicLong.class
|
||||
|| fieldClass == String.class
|
||||
|| fieldClass == boolean[].class
|
||||
|| fieldClass == byte[].class
|
||||
@@ -396,9 +379,6 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
|| fieldClass == Float[].class
|
||||
|| fieldClass == Long[].class
|
||||
|| fieldClass == Double[].class
|
||||
|| fieldClass == AtomicBoolean[].class
|
||||
|| fieldClass == AtomicInteger[].class
|
||||
|| fieldClass == AtomicLong[].class
|
||||
|| fieldClass == String[].class;
|
||||
}
|
||||
|
||||
@@ -412,9 +392,6 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
|| componentType == Float.class
|
||||
|| componentType == Long.class
|
||||
|| componentType == Double.class
|
||||
|| componentType == AtomicBoolean.class
|
||||
|| componentType == AtomicInteger.class
|
||||
|| componentType == AtomicLong.class
|
||||
|| componentType == String.class;
|
||||
}
|
||||
|
||||
@@ -485,34 +462,18 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
}
|
||||
|
||||
public static int getTag(String fieldName, Type fieldType, int fieldPos, boolean enumtostring) {
|
||||
int wiretype = ProtobufFactory.wireTypeBit(fieldType, enumtostring);
|
||||
return (fieldPos << 3 | wiretype);
|
||||
ProtobufTypeEnum typeEnum = ProtobufTypeEnum.valueOf(fieldType, enumtostring);
|
||||
return (fieldPos << 3 | typeEnum.getValue());
|
||||
}
|
||||
|
||||
public static int getTag(int index, ProtobufTypeEnum typeEnum) {
|
||||
return index << 3 | typeEnum.getValue();
|
||||
}
|
||||
|
||||
public static int getTag(DeMember member, boolean enumtostring) {
|
||||
int wiretype = ProtobufFactory.wireTypeBit(member.getAttribute().type(), enumtostring);
|
||||
return (member.getPosition() << 3 | wiretype);
|
||||
}
|
||||
|
||||
public static int wireTypeBit(Type javaType, boolean enumtostring) {
|
||||
if (javaType == double.class || javaType == Double.class) {
|
||||
return 1;
|
||||
} else if (javaType == float.class || javaType == Float.class) {
|
||||
return 5;
|
||||
} else if ((javaType == boolean.class || javaType == Boolean.class)
|
||||
|| (javaType == byte.class || javaType == Byte.class)
|
||||
|| (javaType == char.class || javaType == Character.class)
|
||||
|| (javaType == short.class || javaType == Short.class)
|
||||
|| (javaType == int.class || javaType == Integer.class)
|
||||
|| (javaType == long.class || javaType == Long.class)
|
||||
|| (javaType == AtomicBoolean.class || javaType == AtomicInteger.class)
|
||||
|| javaType == AtomicLong.class) {
|
||||
return 0;
|
||||
} else if (!enumtostring && (javaType instanceof Class) && ((Class) javaType).isEnum()) {
|
||||
return 0;
|
||||
} else { // byte[]
|
||||
return 2;
|
||||
}
|
||||
ProtobufTypeEnum typeEnum =
|
||||
ProtobufTypeEnum.valueOf(member.getAttribute().type(), enumtostring);
|
||||
return (member.getPosition() << 3 | typeEnum.getValue());
|
||||
}
|
||||
|
||||
public static String wireTypeString(Type javaType, boolean enumtostring) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.lang.reflect.Type;
|
||||
import java.util.*;
|
||||
import java.util.function.BiFunction;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.util.Utility;
|
||||
|
||||
/**
|
||||
* @author zhangjx
|
||||
@@ -18,22 +19,20 @@ import org.redkale.convert.*;
|
||||
public class ProtobufMapEncoder<K, V> extends MapEncoder<ProtobufWriter, K, V>
|
||||
implements ProtobufEncodeable<ProtobufWriter, Map<K, V>> {
|
||||
|
||||
private final boolean enumtostring;
|
||||
private final int keyTag;
|
||||
private final int valTag;
|
||||
|
||||
public ProtobufMapEncoder(ConvertFactory factory, Type type) {
|
||||
super(factory, type);
|
||||
this.enumtostring = ((ProtobufFactory) factory).enumtostring;
|
||||
this.keyTag = 1 << 3 | ProtobufFactory.wireTypeBit(keyEncoder.getType(), enumtostring);
|
||||
this.valTag = 2 << 3 | ProtobufFactory.wireTypeBit(valueEncoder.getType(), enumtostring);
|
||||
this.keyTag = ProtobufFactory.getTag(1, ((ProtobufEncodeable) keyEncoder).typeEnum());
|
||||
this.valTag = ProtobufFactory.getTag(2, ((ProtobufEncodeable) valueEncoder).typeEnum());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void convertTo(ProtobufWriter out, EnMember member, Map<K, V> value) {
|
||||
this.checkInited();
|
||||
final Map<K, V> values = value;
|
||||
if (values == null || values.isEmpty()) {
|
||||
if (Utility.isEmpty(value)) {
|
||||
out.writeNull();
|
||||
return;
|
||||
}
|
||||
@@ -64,14 +63,19 @@ public class ProtobufMapEncoder<K, V> extends MapEncoder<ProtobufWriter, K, V>
|
||||
|
||||
@Override
|
||||
public int computeSize(ProtobufWriter out, int tagLen, Map<K, V> value) {
|
||||
if (value == null || value.isEmpty()) {
|
||||
if (Utility.isEmpty(value)) {
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean requireSize() {
|
||||
public final boolean requireSize() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final ProtobufTypeEnum typeEnum() {
|
||||
return ProtobufTypeEnum.BYTES;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,11 +107,8 @@ public class ProtobufObjectEncoder<T> extends ObjectEncoder<ProtobufWriter, T>
|
||||
int itemDataSize = encodeable.computeSize(out, itemTagSize, member.getFieldValue(value));
|
||||
if (itemDataSize > 0) {
|
||||
size += itemTagSize + itemDataSize;
|
||||
System.out.println(
|
||||
"member: " + member.getFieldName() + ", tag: " + itemTagSize + ", data: " + itemDataSize);
|
||||
}
|
||||
}
|
||||
System.out.println("对象: " + value + ", 大小: " + size);
|
||||
return size;
|
||||
}
|
||||
|
||||
@@ -123,4 +120,9 @@ public class ProtobufObjectEncoder<T> extends ObjectEncoder<ProtobufWriter, T>
|
||||
public final boolean requireSize() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final ProtobufTypeEnum typeEnum() {
|
||||
return ProtobufTypeEnum.BYTES;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,13 @@ package org.redkale.convert.pb;
|
||||
/**
|
||||
* 只能用于基本类型, 不能用于如String的其他类型
|
||||
* @author zhangjx
|
||||
* @param <T> 基本类型泛型
|
||||
*/
|
||||
public interface ProtobufPrimitivable {
|
||||
//
|
||||
public interface ProtobufPrimitivable<T> {
|
||||
|
||||
// 获取java类型分类
|
||||
public Class primitiveType();
|
||||
|
||||
// 计算内容长度
|
||||
public int computeSize(T value);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ package org.redkale.convert.pb;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.*;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.util.Creator;
|
||||
|
||||
@@ -190,24 +189,6 @@ public class ProtobufReader extends Reader {
|
||||
return data;
|
||||
}
|
||||
|
||||
public final AtomicBoolean readAtomicBoolean() {
|
||||
return new AtomicBoolean(readBoolean());
|
||||
}
|
||||
|
||||
public final AtomicBoolean[] readAtomicBooleans() {
|
||||
Collection<AtomicBoolean> data = readAtomicBooleans(LIS_CREATOR);
|
||||
return data.toArray(new AtomicBoolean[data.size()]);
|
||||
}
|
||||
|
||||
public final Collection<AtomicBoolean> readAtomicBooleans(Creator<? extends Collection> creator) {
|
||||
int size = readRawVarint32();
|
||||
Collection<AtomicBoolean> data = creator.create();
|
||||
for (int i = 0; i < size; i++) {
|
||||
data.add(new AtomicBoolean(readBoolean()));
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final byte readByte() {
|
||||
return (byte) readInt();
|
||||
@@ -319,22 +300,6 @@ public class ProtobufReader extends Reader {
|
||||
return data;
|
||||
}
|
||||
|
||||
public final AtomicInteger[] readAtomicIntegers() {
|
||||
Collection<AtomicInteger> data = readAtomicIntegers(LIS_CREATOR);
|
||||
return data.toArray(new AtomicInteger[data.size()]);
|
||||
}
|
||||
|
||||
public final Collection<AtomicInteger> readAtomicIntegers(Creator<? extends Collection> creator) {
|
||||
Collection<AtomicInteger> data = creator.create();
|
||||
int len = readRawVarint32();
|
||||
while (len > 0) {
|
||||
int val = readInt();
|
||||
data.add(new AtomicInteger(val));
|
||||
len -= ProtobufFactory.computeSInt32SizeNoTag(val);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final float readFloat() {
|
||||
return Float.intBitsToFloat(readRawLittleEndian32());
|
||||
@@ -390,22 +355,6 @@ public class ProtobufReader extends Reader {
|
||||
return data;
|
||||
}
|
||||
|
||||
public final AtomicLong[] readAtomicLongs() {
|
||||
Collection<AtomicLong> data = readAtomicLongs(LIS_CREATOR);
|
||||
return data.toArray(new AtomicLong[data.size()]);
|
||||
}
|
||||
|
||||
public final Collection<AtomicLong> readAtomicLongs(Creator<? extends Collection> creator) {
|
||||
Collection<AtomicLong> data = creator.create();
|
||||
int len = readRawVarint32();
|
||||
while (len > 0) {
|
||||
long val = readLong();
|
||||
data.add(new AtomicLong(val));
|
||||
len -= ProtobufFactory.computeSInt64SizeNoTag(val);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public final String[] readStrings(int tag) {
|
||||
Collection<String> data = readStrings(tag, LIS_CREATOR);
|
||||
return data.toArray(new String[data.size()]);
|
||||
@@ -428,8 +377,8 @@ public class ProtobufReader extends Reader {
|
||||
}
|
||||
|
||||
public final double[] readDoubles() {
|
||||
int len = readRawVarint32();
|
||||
double[] rs = new double[len / 8];
|
||||
int size = readRawVarint32();
|
||||
double[] rs = new double[size / 8];
|
||||
for (int i = 0; i < rs.length; i++) {
|
||||
rs[i] = readDouble();
|
||||
}
|
||||
@@ -438,8 +387,8 @@ public class ProtobufReader extends Reader {
|
||||
|
||||
public final Collection<Double> readDoubles(Creator<? extends Collection> creator) {
|
||||
Collection<Double> data = creator.create();
|
||||
int len = readRawVarint32() / 8;
|
||||
for (int i = 0; i < len; i++) {
|
||||
int size = readRawVarint32() / 8;
|
||||
for (int i = 0; i < size; i++) {
|
||||
data.add(readDouble());
|
||||
}
|
||||
return data;
|
||||
@@ -626,14 +575,14 @@ public class ProtobufReader extends Reader {
|
||||
throw new ConvertException("readRawVarint64SlowPath error");
|
||||
}
|
||||
|
||||
protected int readRawLittleEndian32() { //float
|
||||
protected int readRawLittleEndian32() { // float
|
||||
return ((content[++this.position] & 0xff)
|
||||
| ((content[++this.position] & 0xff) << 8)
|
||||
| ((content[++this.position] & 0xff) << 16)
|
||||
| ((content[++this.position] & 0xff) << 24));
|
||||
}
|
||||
|
||||
protected long readRawLittleEndian64() { //double
|
||||
protected long readRawLittleEndian64() { // double
|
||||
return ((content[++this.position] & 0xffL)
|
||||
| ((content[++this.position] & 0xffL) << 8)
|
||||
| ((content[++this.position] & 0xffL) << 16)
|
||||
|
||||
@@ -33,12 +33,21 @@ public class ProtobufStreamDecoder<T> extends StreamDecoder<ProtobufReader, T>
|
||||
final List<T> result = new ArrayList();
|
||||
final int limit = in.limit();
|
||||
while (in.hasNext()) {
|
||||
boolean nodata = false;
|
||||
if (!simpled) {
|
||||
int contentLen = in.readRawVarint32();
|
||||
in.limit(in.position() + contentLen + 1);
|
||||
if (contentLen == 0) {
|
||||
nodata = true;
|
||||
} else {
|
||||
in.limit(in.position() + contentLen + 1);
|
||||
}
|
||||
}
|
||||
if (nodata) {
|
||||
result.add(null);
|
||||
} else {
|
||||
result.add(itemDecoder.convertFrom(in));
|
||||
in.limit(limit);
|
||||
}
|
||||
result.add(itemDecoder.convertFrom(in));
|
||||
in.limit(limit);
|
||||
if (!in.readNextTag(member)) { // 元素结束
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -32,12 +32,12 @@ public class ProtobufStreamEncoder<T> extends StreamEncoder<ProtobufWriter, T>
|
||||
if (array == null || array.length < 1) {
|
||||
return;
|
||||
}
|
||||
ProtobufEncodeable itemEncoder = (ProtobufEncodeable)this.componentEncoder;
|
||||
ProtobufEncodeable itemEncoder = (ProtobufEncodeable) this.componentEncoder;
|
||||
out.writeArrayB(array.length, itemEncoder, array);
|
||||
for (Object item : array) {
|
||||
out.writeField(member);
|
||||
if (item == null) {
|
||||
out.writeUInt32(0);
|
||||
out.writeLength(0);
|
||||
} else if (componentSimpled) {
|
||||
itemEncoder.convertTo(out, item);
|
||||
} else {
|
||||
@@ -58,6 +58,11 @@ public class ProtobufStreamEncoder<T> extends StreamEncoder<ProtobufWriter, T>
|
||||
|
||||
@Override
|
||||
public boolean requireSize() {
|
||||
return componentSizeRequired;
|
||||
return !componentSimpled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final ProtobufTypeEnum typeEnum() {
|
||||
return ProtobufTypeEnum.BYTES;
|
||||
}
|
||||
}
|
||||
|
||||
56
src/main/java/org/redkale/convert/pb/ProtobufTypeEnum.java
Normal file
56
src/main/java/org/redkale/convert/pb/ProtobufTypeEnum.java
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2116 Redkale
|
||||
* All rights reserved.
|
||||
*/
|
||||
package org.redkale.convert.pb;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public enum ProtobufTypeEnum {
|
||||
// boolean/byte/char/short/int/long
|
||||
INT(0),
|
||||
// double
|
||||
DOUBLE(1),
|
||||
// float
|
||||
FLOAT(5),
|
||||
// byte[]
|
||||
BYTES(2);
|
||||
|
||||
private int value;
|
||||
|
||||
private ProtobufTypeEnum(int v) {
|
||||
this.value = v;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public static ProtobufTypeEnum valueOf(Type type, boolean enumtostring) {
|
||||
if (type == double.class || type == Double.class) {
|
||||
return DOUBLE;
|
||||
} else if (type == float.class || type == Float.class) {
|
||||
return FLOAT;
|
||||
} else if ((type == boolean.class || type == Boolean.class)
|
||||
|| (type == byte.class || type == Byte.class)
|
||||
|| (type == char.class || type == Character.class)
|
||||
|| (type == short.class || type == Short.class)
|
||||
|| (type == int.class || type == Integer.class)
|
||||
|| (type == long.class || type == Long.class)
|
||||
|| (type == AtomicBoolean.class || type == AtomicInteger.class)
|
||||
|| type == AtomicLong.class) {
|
||||
return INT;
|
||||
} else if (!enumtostring && (type instanceof Class) && ((Class) type).isEnum()) {
|
||||
return INT;
|
||||
} else { // byte[]
|
||||
return BYTES;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,6 @@ import java.lang.reflect.Type;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.*;
|
||||
import java.util.function.*;
|
||||
import java.util.stream.Stream;
|
||||
import org.redkale.annotation.ClassDepends;
|
||||
@@ -73,7 +72,9 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
|
||||
private byte[] content;
|
||||
|
||||
private ArrayDeque<ProtobufWriter> childWriters;
|
||||
private List<ProtobufWriter> children;
|
||||
|
||||
private ArrayDeque<ProtobufWriter> pool;
|
||||
|
||||
protected ProtobufWriter(ProtobufWriter parent) {
|
||||
this();
|
||||
@@ -156,14 +157,20 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
if (this.content.length > DEFAULT_SIZE) {
|
||||
this.content = new byte[DEFAULT_SIZE];
|
||||
}
|
||||
if (this.children != null) {
|
||||
for (ProtobufWriter child : children) {
|
||||
offerChild2(child);
|
||||
}
|
||||
this.children = null;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public final ProtobufWriter pollChild() {
|
||||
Queue<ProtobufWriter> queue = this.childWriters;
|
||||
Queue<ProtobufWriter> queue = this.pool;
|
||||
if (queue == null) {
|
||||
this.childWriters = new ArrayDeque<>(CHILD_SIZE);
|
||||
queue = this.childWriters;
|
||||
this.pool = new ArrayDeque<>(CHILD_SIZE);
|
||||
queue = this.pool;
|
||||
}
|
||||
ProtobufWriter result = queue.poll();
|
||||
if (result == null) {
|
||||
@@ -172,8 +179,33 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
return result.configFieldFunc(this);
|
||||
}
|
||||
|
||||
public final void offerChild(final ProtobufWriter child) {
|
||||
Queue<ProtobufWriter> queue = this.childWriters;
|
||||
public void offerChild(final ProtobufWriter child) {
|
||||
Queue<ProtobufWriter> queue = this.pool;
|
||||
if (child != null && queue != null && queue.size() < CHILD_SIZE) {
|
||||
child.recycle();
|
||||
queue.offer(child);
|
||||
}
|
||||
}
|
||||
|
||||
public final ProtobufWriter createChild2() {
|
||||
Queue<ProtobufWriter> queue = this.pool;
|
||||
if (queue == null) {
|
||||
this.pool = new ArrayDeque<>(CHILD_SIZE);
|
||||
queue = this.pool;
|
||||
}
|
||||
ProtobufWriter result = queue.poll();
|
||||
if (result == null) {
|
||||
result = new ProtobufWriter();
|
||||
}
|
||||
if (this.children == null) {
|
||||
this.children = new ArrayList<>();
|
||||
}
|
||||
this.children.add(result);
|
||||
return result.configFieldFunc(this);
|
||||
}
|
||||
|
||||
private void offerChild2(final ProtobufWriter child) {
|
||||
Queue<ProtobufWriter> queue = this.pool;
|
||||
if (child != null && queue != null && queue.size() < CHILD_SIZE) {
|
||||
child.recycle();
|
||||
queue.offer(child);
|
||||
@@ -182,11 +214,32 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
|
||||
@Override
|
||||
public final int length() {
|
||||
return count;
|
||||
int total = count;
|
||||
if (children != null) {
|
||||
for (ProtobufWriter child : children) {
|
||||
int len = child.length();
|
||||
total += ProtobufFactory.computeSInt32SizeNoTag(len) + len;
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] content() {
|
||||
if (children != null) {
|
||||
byte[] data = new byte[length()];
|
||||
System.arraycopy(content, 0, data, 0, count);
|
||||
int pos = count;
|
||||
Utility.println("自身对象: ", content, 0, count);
|
||||
for (ProtobufWriter child : children) {
|
||||
int len = child.length();
|
||||
data[pos++] = (byte) len;
|
||||
Utility.println("子对象: ", child.content(), 0, child.length());
|
||||
System.arraycopy(child.content(), 0, data, pos, len);
|
||||
pos += len;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
@@ -214,6 +267,9 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
|
||||
@Override
|
||||
public byte[] toArray() {
|
||||
if (children != null) {
|
||||
return content();
|
||||
}
|
||||
byte[] copy = new byte[count];
|
||||
System.arraycopy(content, 0, copy, 0, count);
|
||||
return copy;
|
||||
@@ -257,7 +313,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.getClass().getSimpleName() + "[count=" + this.count + "]";
|
||||
return this.getClass().getSimpleName() + "@" + Objects.hashCode(this) + "[count=" + this.count + "]";
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
@@ -764,100 +820,6 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
}
|
||||
}
|
||||
|
||||
public final void writeAtomicBooleans(AtomicBoolean[] value) {
|
||||
AtomicBoolean[] array = value;
|
||||
if (array != null && array.length > 0) {
|
||||
writeLength(array.length);
|
||||
for (AtomicBoolean item : array) {
|
||||
writeTo(item != null && item.get() ? (byte) 1 : (byte) 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public final void writeAtomicBooleans(Collection<AtomicBoolean> value) {
|
||||
Collection<AtomicBoolean> array = value;
|
||||
if (array != null && !array.isEmpty()) {
|
||||
writeLength(array.size());
|
||||
for (AtomicBoolean item : array) {
|
||||
writeTo(item != null && item.get() ? (byte) 1 : (byte) 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public final void writeAtomicBooleans(Stream<AtomicBoolean> value) {
|
||||
if (value != null) {
|
||||
writeAtomicBooleans(value.toArray(s -> new AtomicBoolean[s]));
|
||||
}
|
||||
}
|
||||
|
||||
public final void writeAtomicIntegers(AtomicInteger[] value) {
|
||||
AtomicInteger[] array = value;
|
||||
if (array != null && array.length > 0) {
|
||||
int len = 0;
|
||||
for (AtomicInteger item : array) {
|
||||
len += ProtobufFactory.computeSInt32SizeNoTag(item == null ? 0 : item.get());
|
||||
}
|
||||
writeLength(len);
|
||||
for (AtomicInteger item : array) {
|
||||
writeInt(item == null ? 0 : item.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public final void writeAtomicIntegers(Collection<AtomicInteger> value) {
|
||||
Collection<AtomicInteger> array = value;
|
||||
if (array != null && !array.isEmpty()) {
|
||||
int len = 0;
|
||||
for (AtomicInteger item : array) {
|
||||
len += ProtobufFactory.computeSInt32SizeNoTag(item == null ? 0 : item.get());
|
||||
}
|
||||
writeLength(len);
|
||||
for (AtomicInteger item : array) {
|
||||
writeInt(item == null ? 0 : item.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public final void writeAtomicIntegers(Stream<AtomicInteger> value) {
|
||||
if (value != null) {
|
||||
writeAtomicIntegers(value.toArray(s -> new AtomicInteger[s]));
|
||||
}
|
||||
}
|
||||
|
||||
public final void writeAtomicLongs(AtomicLong[] value) {
|
||||
AtomicLong[] array = value;
|
||||
if (array != null && array.length > 0) {
|
||||
int len = 0;
|
||||
for (AtomicLong item : array) {
|
||||
len += ProtobufFactory.computeSInt64SizeNoTag(item == null ? 0 : item.get());
|
||||
}
|
||||
writeLength(len);
|
||||
for (AtomicLong item : array) {
|
||||
writeLong(item == null ? 0 : item.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public final void writeAtomicLongs(Collection<AtomicLong> value) {
|
||||
Collection<AtomicLong> array = value;
|
||||
if (array != null && !array.isEmpty()) {
|
||||
int len = 0;
|
||||
for (AtomicLong item : array) {
|
||||
len += ProtobufFactory.computeSInt64SizeNoTag(item == null ? 0 : item.get());
|
||||
}
|
||||
writeLength(len);
|
||||
for (AtomicLong item : array) {
|
||||
writeLong(item == null ? 0 : item.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public final void writeAtomicLongs(Stream<AtomicLong> value) {
|
||||
if (value != null) {
|
||||
writeAtomicLongs(value.toArray(s -> new AtomicLong[s]));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void writeWrapper(StringWrapper value) {
|
||||
if (value != null) {
|
||||
@@ -993,30 +955,6 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
}
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final void writeFieldValue(int tag, AtomicBoolean value) {
|
||||
if (value != null && value.get()) {
|
||||
writeTag(tag);
|
||||
writeBoolean(value.get());
|
||||
}
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final void writeFieldValue(int tag, AtomicInteger value) {
|
||||
if (value != null && value.get() != 0) {
|
||||
writeTag(tag);
|
||||
writeInt(value.get());
|
||||
}
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final void writeFieldValue(int tag, AtomicLong value) {
|
||||
if (value != null && value.get() != 0) {
|
||||
writeTag(tag);
|
||||
writeLong(value.get());
|
||||
}
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final void writeFieldValue(int tag, boolean[] value) {
|
||||
if (value != null && value.length > 0) {
|
||||
@@ -1145,30 +1083,6 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
}
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final void writeFieldValue(int tag, AtomicBoolean[] value) {
|
||||
if (value != null && value.length > 0) {
|
||||
writeTag(tag);
|
||||
writeAtomicBooleans(value);
|
||||
}
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final void writeFieldValue(int tag, AtomicInteger[] value) {
|
||||
if (value != null && value.length > 0) {
|
||||
writeTag(tag);
|
||||
writeAtomicIntegers(value);
|
||||
}
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final void writeFieldValue(int tag, AtomicLong[] value) {
|
||||
if (value != null && value.length > 0) {
|
||||
writeTag(tag);
|
||||
writeAtomicLongs(value);
|
||||
}
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final void writeFieldValue(int tag, String[] value) {
|
||||
if (value != null && value.length > 0) {
|
||||
@@ -1240,30 +1154,6 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
}
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final void writeFieldAtomicBooleansValue(int tag, Collection<AtomicBoolean> value) {
|
||||
if (value != null && !value.isEmpty()) {
|
||||
writeTag(tag);
|
||||
writeAtomicBooleans(value);
|
||||
}
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final void writeFieldAtomicIntegersValue(int tag, Collection<AtomicInteger> value) {
|
||||
if (value != null && !value.isEmpty()) {
|
||||
writeTag(tag);
|
||||
writeAtomicIntegers(value);
|
||||
}
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final void writeFieldAtomicLongsValue(int tag, Collection<AtomicLong> value) {
|
||||
if (value != null && !value.isEmpty()) {
|
||||
writeTag(tag);
|
||||
writeAtomicLongs(value);
|
||||
}
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final void writeFieldStringsValue(int tag, Collection<String> value) {
|
||||
if (value != null && !value.isEmpty()) {
|
||||
|
||||
@@ -33,10 +33,15 @@ public class RequiredBeanTest {
|
||||
+ "0x1a,0x03,0x63,0x63,0x63,0x1a,0x03,0x64,0x64,0x64,0x22,0x02,0x02,0x04]");
|
||||
Utility.println("序列化0: ", bytes);
|
||||
RequiredArray array = new RequiredArray();
|
||||
array.beans = new RequiredBean[] {bean};
|
||||
array.id = 32;
|
||||
array.beans = new RequiredBean[] {bean, null, bean};
|
||||
byte[] bytes2 = convert.convertTo(array);
|
||||
System.out.println("序列化s: 29.[0x1f,0x0a,0x1a,0x08,0x0c,0x12,0x03,0x61,0x61,0x61,0x12,0x03,0x62,0x62,0x62,"
|
||||
+ "0x1a,0x03,0x63,0x63,0x63,0x1a,0x03,0x64,0x64,0x64,0x22,0x02,0x02,0x04]");
|
||||
System.out.println("序列化s: 60.[0x0a,0x1a,0x08,0x0c,0x12,0x03,0x61,0x61,0x61,0x12,0x03,0x62,0x62,0x62,"
|
||||
+ "0x1a,0x03,0x63,0x63,0x63,0x1a,0x03,0x64,0x64,0x64,0x22,0x02,0x02,0x04,"
|
||||
+ "0x0a,0x00,"
|
||||
+ "0x0a,0x1a,0x08,0x0c,0x12,0x03,0x61,0x61,0x61,0x12,0x03,0x62,0x62,0x62,"
|
||||
+ "0x1a,0x03,0x63,0x63,0x63,0x1a,0x03,0x64,0x64,0x64,0x22,0x02,0x02,0x04,"
|
||||
+ "0x10,0x40]");
|
||||
Utility.println("序列化s: ", bytes2);
|
||||
System.out.println("-----------------------------------------------");
|
||||
String jsons1 = JsonConvert.root().convertTo(array);
|
||||
@@ -51,6 +56,9 @@ public class RequiredBeanTest {
|
||||
@ConvertColumn(index = 1)
|
||||
private RequiredBean[] beans;
|
||||
|
||||
@ConvertColumn(index = 2)
|
||||
private int id;
|
||||
|
||||
public RequiredBean[] getBeans() {
|
||||
return beans;
|
||||
}
|
||||
@@ -58,6 +66,14 @@ public class RequiredBeanTest {
|
||||
public void setBeans(RequiredBean[] beans) {
|
||||
this.beans = beans;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
public static class RequiredBean {
|
||||
|
||||
@@ -7,7 +7,6 @@ package org.redkale.test.convert.pb;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
@@ -142,28 +141,17 @@ public class UserBean {
|
||||
private List<Byte> bit6;
|
||||
|
||||
@ConvertColumn(index = 41)
|
||||
private Map<String, String> map;
|
||||
|
||||
@ConvertColumn(index = 42)
|
||||
public UserKind kind;
|
||||
|
||||
@ConvertColumn(index = 43)
|
||||
@ConvertColumn(index = 42)
|
||||
public AtomicInteger count;
|
||||
|
||||
@ConvertColumn(index = 44)
|
||||
@ConvertColumn(index = 43)
|
||||
public AtomicLong[] count2;
|
||||
|
||||
@ConvertColumn(index = 45)
|
||||
@ConvertColumn(index = 44)
|
||||
private List<String> strs;
|
||||
|
||||
public Map<String, String> getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public void setMap(Map<String, String> map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public List<Integer> getId6() {
|
||||
return id6;
|
||||
}
|
||||
|
||||
@@ -78,10 +78,6 @@ public class UserBeanProtoDynEncoder extends ProtobufDynEncoder<UserBean> {
|
||||
out.writeFieldBytesValue(23, value.getBit6());
|
||||
out.writeFieldStringsValue(23, value.getStrs());
|
||||
|
||||
out.writeFieldValue(100, value.kind);
|
||||
out.writeFieldValue(101, value.count);
|
||||
out.writeFieldValue(102, value.count2);
|
||||
|
||||
out.writeObjectField(mapEnMember, value);
|
||||
out.writeObjectE(value);
|
||||
offerWriter(out0, out);
|
||||
|
||||
Reference in New Issue
Block a user