diff --git a/src/main/java/org/redkale/convert/pb/ProtobufByteBufferReader.java b/src/main/java/org/redkale/convert/pb/ProtobufByteBufferReader.java index 868039347..1ecd49673 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufByteBufferReader.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufByteBufferReader.java @@ -33,14 +33,6 @@ public class ProtobufByteBufferReader extends ProtobufReader { 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() { if (this.currentBuffer.hasRemaining()) { this.position++; @@ -84,6 +76,9 @@ public class ProtobufByteBufferReader extends ProtobufReader { @Override public boolean hasNext() { + if (this.limit > 0 && (this.position + 1) >= this.limit) { + return false; + } if (currentBuffer.hasRemaining()) { return true; } diff --git a/src/main/java/org/redkale/convert/pb/ProtobufReader.java b/src/main/java/org/redkale/convert/pb/ProtobufReader.java index c81d5c278..6105fcb27 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufReader.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufReader.java @@ -18,14 +18,14 @@ public class ProtobufReader extends Reader { protected int position = -1; - protected byte[] content; - - protected int limit; + protected int limit = -1; protected int cacheTag = Integer.MIN_VALUE; protected boolean enumtostring; + private byte[] content; + public ProtobufReader() { // do nothing } @@ -284,6 +284,7 @@ public class ProtobufReader extends Reader { public final int[] readInts() { int len = readRawVarint32(); + System.out.println("等到长度: " + len); List list = new ArrayList<>(len); while (len > 0) { int val = readInt(); diff --git a/src/main/java/org/redkale/convert/pb/ProtobufStreamReader.java b/src/main/java/org/redkale/convert/pb/ProtobufStreamReader.java index a9ac45b9a..cd4eb44b4 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufStreamReader.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufStreamReader.java @@ -33,12 +33,6 @@ class ProtobufStreamReader extends ProtobufByteBufferReader { return false; } - @Override - protected int remaining() { - int count = 0; - return count; - } - @Override protected byte nextByte() { if (backByte != null) { diff --git a/src/test/java/org/redkale/test/convert/GenericEntityTest.java b/src/test/java/org/redkale/test/convert/GenericEntityTest.java index 4aad875b3..b0faf9695 100644 --- a/src/test/java/org/redkale/test/convert/GenericEntityTest.java +++ b/src/test/java/org/redkale/test/convert/GenericEntityTest.java @@ -27,7 +27,7 @@ import org.redkale.util.Utility; public class GenericEntityTest { private static final Type ENTITY_TYPE = new TypeToken>() {}.getType(); 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 { GenericEntityTest test = new GenericEntityTest(); @@ -44,6 +44,7 @@ public class GenericEntityTest { @Test public void runJson1() throws Exception { + System.out.println("-------------------- runJson1 ---------------------------------"); JsonConvert convert = JsonConvert.root(); GenericEntity bean = createBean(); String json = convert.convertTo(bean); @@ -54,6 +55,7 @@ public class GenericEntityTest { @Test public void runJson2() throws Exception { + System.out.println("-------------------- runJson2 ---------------------------------"); JsonConvert convert = JsonConvert.root(); ByteBuffer in = ConvertHelper.createByteBuffer(createBytes()); GenericEntity bean = convert.convertFrom(ENTITY_TYPE, in); @@ -65,6 +67,7 @@ public class GenericEntityTest { @Test public void runJson3() throws Exception { + System.out.println("-------------------- runJson3 ---------------------------------"); JsonConvert convert = JsonConvert.root(); InputStream in = ConvertHelper.createInputStream(createBytes()); GenericEntity bean = convert.convertFrom(ENTITY_TYPE, in); @@ -76,6 +79,7 @@ public class GenericEntityTest { @Test public void runPb1() throws Exception { + System.out.println("-------------------- runPb1 ---------------------------------"); ProtobufConvert convert = ProtobufConvert.root(); GenericEntity bean = createBean(); byte[] bs = convert.convertTo(bean); @@ -87,32 +91,35 @@ public class GenericEntityTest { @Test public void runPb2() throws Exception { + System.out.println("-------------------- runPb2 ---------------------------------"); ProtobufConvert convert = ProtobufConvert.root(); GenericEntity bean = createBean(); byte[] bs = convert.convertTo(bean); ByteBuffer in = ConvertHelper.createByteBuffer(bs); -// GenericEntity rs = convert.convertFrom(ENTITY_TYPE, in); -// Assertions.assertEquals(JSON, rs.toString()); -// Supplier out = ConvertHelper.createSupplier(); -// ByteBuffer[] buffers = convert.convertTo(out, ENTITY_TYPE, rs); -// Assertions.assertArrayEquals(bs, ConvertHelper.toBytes(buffers)); + GenericEntity rs = convert.convertFrom(ENTITY_TYPE, in); + Assertions.assertEquals(JSON, rs.toString()); + Supplier out = ConvertHelper.createSupplier(); + ByteBuffer[] buffers = convert.convertTo(out, ENTITY_TYPE, rs); + Assertions.assertArrayEquals(bs, ConvertHelper.toBytes(buffers)); } @Test public void runPb3() throws Exception { + System.out.println("-------------------- runPb3 ---------------------------------"); ProtobufConvert convert = ProtobufConvert.root(); GenericEntity bean = createBean(); byte[] bs = convert.convertTo(bean); InputStream in = ConvertHelper.createInputStream(bs); -// GenericEntity rs = convert.convertFrom(ENTITY_TYPE, in); -// Assertions.assertEquals(JSON, rs.toString()); -// ByteArrayOutputStream out = new ByteArrayOutputStream(); -// convert.convertTo(out, ENTITY_TYPE, rs); -// Assertions.assertArrayEquals(bs, out.toByteArray()); + // GenericEntity rs = convert.convertFrom(ENTITY_TYPE, in); + // Assertions.assertEquals(JSON, rs.toString()); + // ByteArrayOutputStream out = new ByteArrayOutputStream(); + // convert.convertTo(out, ENTITY_TYPE, rs); + // Assertions.assertArrayEquals(bs, out.toByteArray()); } @Test public void runBson1() throws Exception { + System.out.println("-------------------- runBson1 ---------------------------------"); BsonConvert convert = BsonConvert.root(); GenericEntity bean = createBean(); byte[] bs = convert.convertTo(bean); @@ -124,6 +131,7 @@ public class GenericEntityTest { @Test public void runBson2() throws Exception { + System.out.println("-------------------- runBson2 ---------------------------------"); BsonConvert convert = BsonConvert.root(); GenericEntity bean = createBean(); byte[] bs = convert.convertTo(bean); @@ -137,6 +145,7 @@ public class GenericEntityTest { @Test public void runBson3() throws Exception { + System.out.println("-------------------- runBson3 ---------------------------------"); BsonConvert convert = BsonConvert.root(); GenericEntity bean = createBean(); byte[] bs = convert.convertTo(bean); @@ -154,89 +163,89 @@ public class GenericEntityTest { private GenericEntity createBean() { GenericEntity bean = new GenericEntity<>(); - bean.setName("你好"); + bean.setOneName("你好"); List list = new ArrayList<>(); list.add(1234567890L); - bean.setList(list); - bean.setEntry(new GenericEntity.Entry<>("aaaa", SimpleEntity.create())); + bean.setOneList(list); + bean.setOneEntry(new Entry<>("aaaa", SimpleEntity.create())); return bean; } public static class GenericEntity { @ConvertColumn(index = 3) - private K name; + private K oneName; @ConvertColumn(index = 2) - private List list; + private List oneList; @ConvertColumn(index = 1) - private Entry entry; + private Entry oneEntry; @Override public String toString() { return JsonConvert.root().convertTo(this); } - public K getName() { - return name; + public K getOneName() { + return oneName; } - public void setName(K name) { - this.name = name; + public void setOneName(K oneName) { + this.oneName = oneName; } - public List getList() { - return list; + public List getOneList() { + return oneList; } - public void setList(List list) { - this.list = list; + public void setOneList(List oneList) { + this.oneList = oneList; } - public Entry getEntry() { - return entry; + public Entry getOneEntry() { + return oneEntry; } - public void setEntry(Entry entry) { - this.entry = entry; + public void setOneEntry(Entry oneEntry) { + this.oneEntry = oneEntry; + } + } + + public static class Entry { + + @ConvertColumn(index = 1) + private K key; + + @ConvertColumn(index = 2) + private V value; + + public Entry() {} + + public Entry(K key, V value) { + this.key = key; + this.value = value; } - public static class Entry { + @Override + public String toString() { + return JsonConvert.root().convertTo(this); + } - @ConvertColumn(index = 1) - private K key; + public K getKey() { + return key; + } - @ConvertColumn(index = 2) - private V value; + public void setKey(K key) { + this.key = key; + } - public Entry() {} + public V getValue() { + return value; + } - public Entry(K key, V value) { - this.key = key; - this.value = value; - } - - @Override - public String toString() { - return JsonConvert.root().convertTo(this); - } - - public K getKey() { - return key; - } - - public void setKey(K key) { - this.key = key; - } - - public V getValue() { - return value; - } - - public void setValue(V value) { - this.value = value; - } + public void setValue(V value) { + this.value = value; } } }