protobuf
This commit is contained in:
@@ -175,10 +175,13 @@ public class JsonReader extends Reader {
|
||||
* @return 有效字符
|
||||
*/
|
||||
protected char nextGoodChar(boolean allowComment) {
|
||||
char c = 0;
|
||||
char[] text0 = this.text;
|
||||
int end = this.limit;
|
||||
int curr = ++this.position;
|
||||
char c = text0[curr];
|
||||
if (c > ' ' && c != '/') {
|
||||
return c;
|
||||
}
|
||||
for (; curr <= end; curr++) {
|
||||
c = text0[curr];
|
||||
if (c > ' ') {
|
||||
|
||||
@@ -34,10 +34,10 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
|
||||
private final ThreadLocal<ProtobufReader> readerPool = Utility.withInitialThreadLocal(ProtobufReader::new);
|
||||
|
||||
@Nullable
|
||||
private Encodeable lastConvertEncodeable;
|
||||
private Encodeable lastEncodeable;
|
||||
|
||||
@Nullable
|
||||
private Decodeable lastConvertDecodeable;
|
||||
private Decodeable lastDecodeable;
|
||||
|
||||
protected ProtobufConvert(ConvertFactory<ProtobufReader, ProtobufWriter> factory, int features) {
|
||||
super(factory, features);
|
||||
@@ -600,10 +600,10 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
|
||||
return null;
|
||||
}
|
||||
final ProtobufReader reader = new ProtobufReader(bytes, offset, len);
|
||||
Decodeable decoder = this.lastConvertDecodeable;
|
||||
Decodeable decoder = this.lastDecodeable;
|
||||
if (decoder == null || decoder.getType() != type) {
|
||||
decoder = factory.loadDecoder(type);
|
||||
this.lastConvertDecodeable = decoder;
|
||||
this.lastDecodeable = decoder;
|
||||
}
|
||||
if (!(decoder instanceof ObjectDecoder) && !(decoder instanceof SimpledCoder)) {
|
||||
throw new ConvertException(this.getClass().getSimpleName() + " not supported type(" + type + ")");
|
||||
@@ -620,10 +620,10 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
|
||||
if (type == null || in == null) {
|
||||
return null;
|
||||
}
|
||||
Decodeable decoder = this.lastConvertDecodeable;
|
||||
Decodeable decoder = this.lastDecodeable;
|
||||
if (decoder == null || decoder.getType() != type) {
|
||||
decoder = factory.loadDecoder(type);
|
||||
this.lastConvertDecodeable = decoder;
|
||||
this.lastDecodeable = decoder;
|
||||
}
|
||||
if (!(decoder instanceof ObjectDecoder)) {
|
||||
throw new ConvertException(this.getClass().getSimpleName() + " not supported type(" + type + ")");
|
||||
@@ -640,10 +640,10 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
|
||||
if (type == null || Utility.isEmpty(buffers)) {
|
||||
return null;
|
||||
}
|
||||
Decodeable decoder = this.lastConvertDecodeable;
|
||||
Decodeable decoder = this.lastDecodeable;
|
||||
if (decoder == null || decoder.getType() != type) {
|
||||
decoder = factory.loadDecoder(type);
|
||||
this.lastConvertDecodeable = decoder;
|
||||
this.lastDecodeable = decoder;
|
||||
}
|
||||
if (!(decoder instanceof ObjectDecoder)) {
|
||||
throw new ConvertException(this.getClass().getSimpleName() + " not supported type(" + type + ")");
|
||||
@@ -657,10 +657,10 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
|
||||
if (type == null) {
|
||||
return null;
|
||||
}
|
||||
Decodeable decoder = this.lastConvertDecodeable;
|
||||
Decodeable decoder = this.lastDecodeable;
|
||||
if (decoder == null || decoder.getType() != type) {
|
||||
decoder = factory.loadDecoder(type);
|
||||
this.lastConvertDecodeable = decoder;
|
||||
this.lastDecodeable = decoder;
|
||||
}
|
||||
if (!(decoder instanceof ObjectDecoder)) {
|
||||
throw new ConvertException(this.getClass().getSimpleName() + " not supported type(" + type + ")");
|
||||
@@ -681,10 +681,10 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
|
||||
}
|
||||
final Type t = type == null ? value.getClass() : type;
|
||||
final ProtobufWriter writer = pollWriter();
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
Encodeable encoder = this.lastEncodeable;
|
||||
if (encoder == null || encoder.getType() != t) {
|
||||
encoder = factory.loadEncoder(t);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
this.lastEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) {
|
||||
writer.specificObjectType(t);
|
||||
@@ -707,10 +707,10 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
|
||||
return null;
|
||||
}
|
||||
final ProtobufWriter writer = pollWriter();
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
Encodeable encoder = this.lastEncodeable;
|
||||
if (encoder == null || encoder.getType() != type) {
|
||||
encoder = factory.loadEncoder(type);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
this.lastEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) {
|
||||
writer.specificObjectType(type);
|
||||
@@ -739,10 +739,10 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
|
||||
writer.writeNull();
|
||||
} else {
|
||||
final Type t = type == null ? value.getClass() : type;
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
Encodeable encoder = this.lastEncodeable;
|
||||
if (encoder == null || encoder.getType() != t) {
|
||||
encoder = factory.loadEncoder(t);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
this.lastEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) {
|
||||
writer.specificObjectType(t);
|
||||
@@ -765,10 +765,10 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
|
||||
writer.writeNull();
|
||||
} else {
|
||||
final Type t = type == null ? value.getClass() : type;
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
Encodeable encoder = this.lastEncodeable;
|
||||
if (encoder == null || encoder.getType() != t) {
|
||||
encoder = factory.loadEncoder(t);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
this.lastEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) {
|
||||
writer.specificObjectType(t);
|
||||
@@ -790,10 +790,10 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
|
||||
writer.writeNull();
|
||||
} else {
|
||||
final Type t = type == null ? value.getClass() : type;
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
Encodeable encoder = this.lastEncodeable;
|
||||
if (encoder == null || encoder.getType() != t) {
|
||||
encoder = factory.loadEncoder(t);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
this.lastEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) {
|
||||
writer.specificObjectType(t);
|
||||
@@ -815,10 +815,10 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
|
||||
writer.writeNull();
|
||||
} else {
|
||||
final Type t = type == null ? value.getClass() : type;
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
Encodeable encoder = this.lastEncodeable;
|
||||
if (encoder == null || encoder.getType() != t) {
|
||||
encoder = factory.loadEncoder(t);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
this.lastEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) {
|
||||
writer.specificObjectType(t);
|
||||
@@ -839,10 +839,10 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
|
||||
}
|
||||
writer.initOffset = writer.count;
|
||||
final Type t = type == null ? value.getClass() : type;
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
Encodeable encoder = this.lastEncodeable;
|
||||
if (encoder == null || encoder.getType() != t) {
|
||||
encoder = factory.loadEncoder(t);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
this.lastEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) {
|
||||
writer.specificObjectType(t);
|
||||
@@ -856,10 +856,10 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
|
||||
}
|
||||
final ProtobufWriter writer = pollWriter();
|
||||
final Type t = type == null ? value.getClass() : type;
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
Encodeable encoder = this.lastEncodeable;
|
||||
if (encoder == null || encoder.getType() != t) {
|
||||
encoder = factory.loadEncoder(t);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
this.lastEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) {
|
||||
writer.specificObjectType(t);
|
||||
|
||||
@@ -72,7 +72,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
}
|
||||
|
||||
public ProtobufWriter(int size) {
|
||||
this.content = new byte[size > 128 ? size : 128];
|
||||
this.content = new byte[Math.max(size, DEFAULT_SIZE)];
|
||||
}
|
||||
|
||||
public ProtobufWriter(ByteArray array) {
|
||||
@@ -150,7 +150,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
protected int expand(int len) {
|
||||
int newcount = count + len;
|
||||
if (newcount > content.length) {
|
||||
byte[] newdata = new byte[Math.max(content.length * 3 / 2, newcount)];
|
||||
byte[] newdata = new byte[Math.max(content.length * 2, newcount)];
|
||||
System.arraycopy(content, 0, newdata, 0, count);
|
||||
this.content = newdata;
|
||||
}
|
||||
@@ -330,10 +330,6 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
for (Double item : (Collection<Double>) obj) {
|
||||
tmp.writeDouble(item);
|
||||
}
|
||||
} else if (componentType == Long.class) {
|
||||
for (Long item : (Collection<Long>) obj) {
|
||||
tmp.writeLong(item);
|
||||
}
|
||||
} else if (componentType == AtomicInteger.class) {
|
||||
for (AtomicInteger item : (Collection<AtomicInteger>) obj) {
|
||||
tmp.writeInt(item == null ? 0 : item.get());
|
||||
@@ -351,17 +347,17 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
Type componentType = streamEncoder.getComponentType();
|
||||
if (streamEncoder.simple) {
|
||||
if (componentType == Boolean.class) {
|
||||
((Stream<Boolean>) obj).forEach(item -> tmp.writeBoolean(item));
|
||||
((Stream<Boolean>) obj).forEach(tmp::writeBoolean);
|
||||
} else if (componentType == Short.class) {
|
||||
((Stream<Short>) obj).forEach(item -> tmp.writeShort(item));
|
||||
((Stream<Short>) obj).forEach(tmp::writeShort);
|
||||
} else if (componentType == Integer.class) {
|
||||
((Stream<Integer>) obj).forEach(item -> tmp.writeInt(item));
|
||||
((Stream<Integer>) obj).forEach(tmp::writeInt);
|
||||
} else if (componentType == Float.class) {
|
||||
((Stream<Float>) obj).forEach(item -> tmp.writeFloat(item));
|
||||
((Stream<Float>) obj).forEach(tmp::writeFloat);
|
||||
} else if (componentType == Long.class) {
|
||||
((Stream<Long>) obj).forEach(item -> tmp.writeLong(item));
|
||||
((Stream<Long>) obj).forEach(tmp::writeLong);
|
||||
} else if (componentType == Double.class) {
|
||||
((Stream<Double>) obj).forEach(item -> tmp.writeDouble(item));
|
||||
((Stream<Double>) obj).forEach(tmp::writeDouble);
|
||||
} else if (componentType == AtomicInteger.class) {
|
||||
((Stream<AtomicInteger>) obj).forEach(item -> tmp.writeInt(item == null ? 0 : item.get()));
|
||||
} else if (componentType == AtomicLong.class) {
|
||||
@@ -374,7 +370,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
return -1;
|
||||
}
|
||||
int length = tmp.count();
|
||||
writeUInt32(length);
|
||||
writeLength(length);
|
||||
writeTo(tmp.toArray());
|
||||
return length;
|
||||
}
|
||||
@@ -408,8 +404,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
|
||||
@Override
|
||||
public void writeFieldName(EnMember member, String fieldName, Type fieldType, int fieldPos) {
|
||||
int tag = ProtobufFactory.getTag(fieldName, fieldType, fieldPos, enumtostring);
|
||||
writeUInt32(tag);
|
||||
writeTag(member.getTag());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -579,7 +574,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
@Override
|
||||
public void writeString(String value) {
|
||||
byte[] bs = Utility.isLatin1(value) ? Utility.latin1ByteArray(value) : Utility.encodeUTF8(value);
|
||||
writeUInt32(bs.length);
|
||||
writeLength(bs.length);
|
||||
writeTo(bs);
|
||||
}
|
||||
|
||||
@@ -604,6 +599,22 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
}
|
||||
}
|
||||
|
||||
protected void writeTag(int tag) {
|
||||
if (tag < 128) {
|
||||
writeTo((byte) tag);
|
||||
} else {
|
||||
writeUInt32(tag);
|
||||
}
|
||||
}
|
||||
|
||||
protected void writeLength(int value) {
|
||||
if (value < 128) {
|
||||
writeTo((byte) value);
|
||||
} else {
|
||||
writeUInt32(value);
|
||||
}
|
||||
}
|
||||
|
||||
protected void writeUInt32(int value) {
|
||||
while (true) {
|
||||
if ((value & ~0x7F) == 0) {
|
||||
|
||||
97
src/test/java/org/redkale/test/convert/pb/User.java
Normal file
97
src/test/java/org/redkale/test/convert/pb/User.java
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2116 Redkale
|
||||
* All rights reserved.
|
||||
*/
|
||||
package org.redkale.test.convert.pb;
|
||||
|
||||
import java.util.Date;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class User {
|
||||
@ConvertColumn(index = 1)
|
||||
private Long id;
|
||||
|
||||
@ConvertColumn(index = 2)
|
||||
private String name;
|
||||
|
||||
@ConvertColumn(index = 3)
|
||||
private String trueName;
|
||||
|
||||
@ConvertColumn(index = 4)
|
||||
private Integer age;
|
||||
|
||||
@ConvertColumn(index = 5)
|
||||
private String sex;
|
||||
|
||||
@ConvertColumn(index = 6)
|
||||
private Date createTime;
|
||||
|
||||
public static User create() {
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setName("赵侠客"); //
|
||||
user.setAge(29);
|
||||
user.setSex("男");
|
||||
user.setTrueName("公众号");
|
||||
user.setCreateTime(new Date(1451577600000L));
|
||||
return user;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getTrueName() {
|
||||
return trueName;
|
||||
}
|
||||
|
||||
public void setTrueName(String trueName) {
|
||||
this.trueName = trueName;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getSex() {
|
||||
return sex;
|
||||
}
|
||||
|
||||
public void setSex(String sex) {
|
||||
this.sex = sex;
|
||||
}
|
||||
|
||||
public Date getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
public void setCreateTime(Date createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
}
|
||||
27
src/test/java/org/redkale/test/convert/pb/UserTest.java
Normal file
27
src/test/java/org/redkale/test/convert/pb/UserTest.java
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2116 Redkale
|
||||
* All rights reserved.
|
||||
*/
|
||||
package org.redkale.test.convert.pb;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.redkale.convert.pb.ProtobufConvert;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class UserTest {
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
UserTest test = new UserTest();
|
||||
test.run();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void run() throws Exception {
|
||||
User user = User.create();
|
||||
ProtobufConvert convert = ProtobufConvert.root();
|
||||
convert.convertTo(user);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user