This commit is contained in:
redkale
2024-09-28 19:39:45 +08:00
parent b50a28132b
commit 7536744d93
4 changed files with 76 additions and 77 deletions

View File

@@ -33,14 +33,6 @@ public class ProtobufByteBufferReader extends ProtobufReader {
return false; return false;
} }
protected int remaining() {
int count = 0;
for (int i = currentIndex; i < buffers.length; i++) {
count += buffers[i].remaining();
}
return count;
}
protected byte nextByte() { protected byte nextByte() {
if (this.currentBuffer.hasRemaining()) { if (this.currentBuffer.hasRemaining()) {
this.position++; this.position++;
@@ -84,6 +76,9 @@ public class ProtobufByteBufferReader extends ProtobufReader {
@Override @Override
public boolean hasNext() { public boolean hasNext() {
if (this.limit > 0 && (this.position + 1) >= this.limit) {
return false;
}
if (currentBuffer.hasRemaining()) { if (currentBuffer.hasRemaining()) {
return true; return true;
} }

View File

@@ -18,14 +18,14 @@ public class ProtobufReader extends Reader {
protected int position = -1; protected int position = -1;
protected byte[] content; protected int limit = -1;
protected int limit;
protected int cacheTag = Integer.MIN_VALUE; protected int cacheTag = Integer.MIN_VALUE;
protected boolean enumtostring; protected boolean enumtostring;
private byte[] content;
public ProtobufReader() { public ProtobufReader() {
// do nothing // do nothing
} }
@@ -284,6 +284,7 @@ public class ProtobufReader extends Reader {
public final int[] readInts() { public final int[] readInts() {
int len = readRawVarint32(); int len = readRawVarint32();
System.out.println("等到长度: " + len);
List<Integer> list = new ArrayList<>(len); List<Integer> list = new ArrayList<>(len);
while (len > 0) { while (len > 0) {
int val = readInt(); int val = readInt();

View File

@@ -33,12 +33,6 @@ class ProtobufStreamReader extends ProtobufByteBufferReader {
return false; return false;
} }
@Override
protected int remaining() {
int count = 0;
return count;
}
@Override @Override
protected byte nextByte() { protected byte nextByte() {
if (backByte != null) { if (backByte != null) {

View File

@@ -27,7 +27,7 @@ import org.redkale.util.Utility;
public class GenericEntityTest { public class GenericEntityTest {
private static final Type ENTITY_TYPE = new TypeToken<GenericEntity<Long, String, SimpleEntity>>() {}.getType(); private static final Type ENTITY_TYPE = new TypeToken<GenericEntity<Long, String, SimpleEntity>>() {}.getType();
private static final String JSON = private static final String JSON =
"{\"entry\":{\"key\":\"aaaa\",\"value\":{\"addr\":\"127.0.0.1:6666\",\"addrs\":[22222,33333,44444,55555,66666,77777,88888,99999],\"desc\":\"\",\"id\":1000000001,\"lists\":[\"aaaa\",\"bbbb\",\"cccc\"],\"map\":{\"AAA\":111,\"CCC\":333,\"BBB\":222},\"name\":\"this is name\\n \\\"test\",\"strings\":[\"zzz\",\"yyy\",\"xxx\"]}},\"list\":[1234567890],\"name\":\"你好\"}"; "{\"oneEntry\":{\"key\":\"aaaa\",\"value\":{\"addr\":\"127.0.0.1:6666\",\"addrs\":[22222,33333,44444,55555,66666,77777,88888,99999],\"desc\":\"\",\"id\":1000000001,\"lists\":[\"aaaa\",\"bbbb\",\"cccc\"],\"map\":{\"AAA\":111,\"CCC\":333,\"BBB\":222},\"name\":\"this is name\\n \\\"test\",\"strings\":[\"zzz\",\"yyy\",\"xxx\"]}},\"oneList\":[1234567890],\"oneName\":\"你好\"}";
public static void main(String[] args) throws Throwable { public static void main(String[] args) throws Throwable {
GenericEntityTest test = new GenericEntityTest(); GenericEntityTest test = new GenericEntityTest();
@@ -44,6 +44,7 @@ public class GenericEntityTest {
@Test @Test
public void runJson1() throws Exception { public void runJson1() throws Exception {
System.out.println("-------------------- runJson1 ---------------------------------");
JsonConvert convert = JsonConvert.root(); JsonConvert convert = JsonConvert.root();
GenericEntity<Long, String, SimpleEntity> bean = createBean(); GenericEntity<Long, String, SimpleEntity> bean = createBean();
String json = convert.convertTo(bean); String json = convert.convertTo(bean);
@@ -54,6 +55,7 @@ public class GenericEntityTest {
@Test @Test
public void runJson2() throws Exception { public void runJson2() throws Exception {
System.out.println("-------------------- runJson2 ---------------------------------");
JsonConvert convert = JsonConvert.root(); JsonConvert convert = JsonConvert.root();
ByteBuffer in = ConvertHelper.createByteBuffer(createBytes()); ByteBuffer in = ConvertHelper.createByteBuffer(createBytes());
GenericEntity<Long, String, SimpleEntity> bean = convert.convertFrom(ENTITY_TYPE, in); GenericEntity<Long, String, SimpleEntity> bean = convert.convertFrom(ENTITY_TYPE, in);
@@ -65,6 +67,7 @@ public class GenericEntityTest {
@Test @Test
public void runJson3() throws Exception { public void runJson3() throws Exception {
System.out.println("-------------------- runJson3 ---------------------------------");
JsonConvert convert = JsonConvert.root(); JsonConvert convert = JsonConvert.root();
InputStream in = ConvertHelper.createInputStream(createBytes()); InputStream in = ConvertHelper.createInputStream(createBytes());
GenericEntity<Long, String, SimpleEntity> bean = convert.convertFrom(ENTITY_TYPE, in); GenericEntity<Long, String, SimpleEntity> bean = convert.convertFrom(ENTITY_TYPE, in);
@@ -76,6 +79,7 @@ public class GenericEntityTest {
@Test @Test
public void runPb1() throws Exception { public void runPb1() throws Exception {
System.out.println("-------------------- runPb1 ---------------------------------");
ProtobufConvert convert = ProtobufConvert.root(); ProtobufConvert convert = ProtobufConvert.root();
GenericEntity<Long, String, SimpleEntity> bean = createBean(); GenericEntity<Long, String, SimpleEntity> bean = createBean();
byte[] bs = convert.convertTo(bean); byte[] bs = convert.convertTo(bean);
@@ -87,32 +91,35 @@ public class GenericEntityTest {
@Test @Test
public void runPb2() throws Exception { public void runPb2() throws Exception {
System.out.println("-------------------- runPb2 ---------------------------------");
ProtobufConvert convert = ProtobufConvert.root(); ProtobufConvert convert = ProtobufConvert.root();
GenericEntity<Long, String, SimpleEntity> bean = createBean(); GenericEntity<Long, String, SimpleEntity> bean = createBean();
byte[] bs = convert.convertTo(bean); byte[] bs = convert.convertTo(bean);
ByteBuffer in = ConvertHelper.createByteBuffer(bs); ByteBuffer in = ConvertHelper.createByteBuffer(bs);
// GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in); GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
// Assertions.assertEquals(JSON, rs.toString()); Assertions.assertEquals(JSON, rs.toString());
// Supplier<ByteBuffer> out = ConvertHelper.createSupplier(); Supplier<ByteBuffer> out = ConvertHelper.createSupplier();
// ByteBuffer[] buffers = convert.convertTo(out, ENTITY_TYPE, rs); ByteBuffer[] buffers = convert.convertTo(out, ENTITY_TYPE, rs);
// Assertions.assertArrayEquals(bs, ConvertHelper.toBytes(buffers)); Assertions.assertArrayEquals(bs, ConvertHelper.toBytes(buffers));
} }
@Test @Test
public void runPb3() throws Exception { public void runPb3() throws Exception {
System.out.println("-------------------- runPb3 ---------------------------------");
ProtobufConvert convert = ProtobufConvert.root(); ProtobufConvert convert = ProtobufConvert.root();
GenericEntity<Long, String, SimpleEntity> bean = createBean(); GenericEntity<Long, String, SimpleEntity> bean = createBean();
byte[] bs = convert.convertTo(bean); byte[] bs = convert.convertTo(bean);
InputStream in = ConvertHelper.createInputStream(bs); InputStream in = ConvertHelper.createInputStream(bs);
// GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in); // GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
// Assertions.assertEquals(JSON, rs.toString()); // Assertions.assertEquals(JSON, rs.toString());
// ByteArrayOutputStream out = new ByteArrayOutputStream(); // ByteArrayOutputStream out = new ByteArrayOutputStream();
// convert.convertTo(out, ENTITY_TYPE, rs); // convert.convertTo(out, ENTITY_TYPE, rs);
// Assertions.assertArrayEquals(bs, out.toByteArray()); // Assertions.assertArrayEquals(bs, out.toByteArray());
} }
@Test @Test
public void runBson1() throws Exception { public void runBson1() throws Exception {
System.out.println("-------------------- runBson1 ---------------------------------");
BsonConvert convert = BsonConvert.root(); BsonConvert convert = BsonConvert.root();
GenericEntity<Long, String, SimpleEntity> bean = createBean(); GenericEntity<Long, String, SimpleEntity> bean = createBean();
byte[] bs = convert.convertTo(bean); byte[] bs = convert.convertTo(bean);
@@ -124,6 +131,7 @@ public class GenericEntityTest {
@Test @Test
public void runBson2() throws Exception { public void runBson2() throws Exception {
System.out.println("-------------------- runBson2 ---------------------------------");
BsonConvert convert = BsonConvert.root(); BsonConvert convert = BsonConvert.root();
GenericEntity<Long, String, SimpleEntity> bean = createBean(); GenericEntity<Long, String, SimpleEntity> bean = createBean();
byte[] bs = convert.convertTo(bean); byte[] bs = convert.convertTo(bean);
@@ -137,6 +145,7 @@ public class GenericEntityTest {
@Test @Test
public void runBson3() throws Exception { public void runBson3() throws Exception {
System.out.println("-------------------- runBson3 ---------------------------------");
BsonConvert convert = BsonConvert.root(); BsonConvert convert = BsonConvert.root();
GenericEntity<Long, String, SimpleEntity> bean = createBean(); GenericEntity<Long, String, SimpleEntity> bean = createBean();
byte[] bs = convert.convertTo(bean); byte[] bs = convert.convertTo(bean);
@@ -154,52 +163,53 @@ public class GenericEntityTest {
private GenericEntity<Long, String, SimpleEntity> createBean() { private GenericEntity<Long, String, SimpleEntity> createBean() {
GenericEntity<Long, String, SimpleEntity> bean = new GenericEntity<>(); GenericEntity<Long, String, SimpleEntity> bean = new GenericEntity<>();
bean.setName("你好"); bean.setOneName("你好");
List<Long> list = new ArrayList<>(); List<Long> list = new ArrayList<>();
list.add(1234567890L); list.add(1234567890L);
bean.setList(list); bean.setOneList(list);
bean.setEntry(new GenericEntity.Entry<>("aaaa", SimpleEntity.create())); bean.setOneEntry(new Entry<>("aaaa", SimpleEntity.create()));
return bean; return bean;
} }
public static class GenericEntity<T, K, V> { public static class GenericEntity<T, K, V> {
@ConvertColumn(index = 3) @ConvertColumn(index = 3)
private K name; private K oneName;
@ConvertColumn(index = 2) @ConvertColumn(index = 2)
private List<? extends T> list; private List<? extends T> oneList;
@ConvertColumn(index = 1) @ConvertColumn(index = 1)
private Entry<K, V> entry; private Entry<K, V> oneEntry;
@Override @Override
public String toString() { public String toString() {
return JsonConvert.root().convertTo(this); return JsonConvert.root().convertTo(this);
} }
public K getName() { public K getOneName() {
return name; return oneName;
} }
public void setName(K name) { public void setOneName(K oneName) {
this.name = name; this.oneName = oneName;
} }
public List<? extends T> getList() { public List<? extends T> getOneList() {
return list; return oneList;
} }
public void setList(List<? extends T> list) { public void setOneList(List<? extends T> oneList) {
this.list = list; this.oneList = oneList;
} }
public Entry<K, V> getEntry() { public Entry<K, V> getOneEntry() {
return entry; return oneEntry;
} }
public void setEntry(Entry<K, V> entry) { public void setOneEntry(Entry<K, V> oneEntry) {
this.entry = entry; this.oneEntry = oneEntry;
}
} }
public static class Entry<K, V> { public static class Entry<K, V> {
@@ -238,5 +248,4 @@ public class GenericEntityTest {
this.value = value; this.value = value;
} }
} }
}
} }