This commit is contained in:
wentch
2015-12-30 11:06:43 +08:00
parent d12efbb2de
commit 85f5c3451f
3 changed files with 40 additions and 2 deletions

View File

@@ -5,12 +5,12 @@
*/
package org.redkale.test.convert;
import java.io.*;
import org.redkale.convert.bson.BsonByteBufferWriter;
import org.redkale.convert.bson.BsonFactory;
import org.redkale.util.Utility;
import org.redkale.convert.bson.BsonConvert;
import org.redkale.convert.json.JsonFactory;
import java.io.Serializable;
import java.nio.*;
import java.util.Arrays;
@@ -27,9 +27,34 @@ public class BsonTestMain {
Serializable[] a = convert.convertFrom(Serializable[].class, bytes);
System.out.println(Arrays.toString(a));
main2(args);
main3(args);
}
public static void main2(String[] args) throws Exception {
System.out.println(new String(new byte[]{109, 121, 110, 97, 109, 101}));
final BsonConvert convert = BsonFactory.root().getConvert();
SimpleChildEntity entry = SimpleChildEntity.create();
byte[] bytes = convert.convertTo(SimpleEntity.class, entry);
System.out.println("长度: " + bytes.length);
BsonByteBufferWriter writer = convert.pollBsonWriter(() -> ByteBuffer.allocate(1));
convert.convertTo(writer,SimpleEntity.class, entry);
ByteBuffer[] buffers = writer.toBuffers();
int len = 0;
ByteArrayOutputStream out = new ByteArrayOutputStream();
for(ByteBuffer b : buffers){
len += b.remaining();
byte[] ts = new byte[b.remaining()];
b.get(ts);
out.write(ts);
b.flip();
}
System.out.println("长度: " + len);
SimpleChildEntity entry2 = convert.convertFrom(SimpleChildEntity.class, buffers);
System.out.println(entry);
System.out.println(entry2);
}
public static void main3(String[] args) throws Exception {
final BsonConvert convert = BsonFactory.root().getConvert();
SimpleChildEntity entry = SimpleChildEntity.create();
byte[] bytes = convert.convertTo(SimpleEntity.class, entry);

View File

@@ -15,6 +15,8 @@ import java.util.*;
@ConvertEntity("myname")
public class SimpleChildEntity extends SimpleEntity {
private short st = -1234;
private String extend;
public static SimpleChildEntity create() {
@@ -22,7 +24,7 @@ public class SimpleChildEntity extends SimpleEntity {
v.setName("this is name\n \"test");
v.setId(1000000001);
v.setAddrs(new int[]{22222, 33333, 44444, 55555, 66666, 77777, 88888, 99999});
v.setStrings(new String[]{"zzz","yyy","xxx"});
v.setStrings(new String[]{"zzz", "yyy", "xxx"});
List<String> list = new ArrayList<>();
list.add("aaaa");
list.add("bbbb");
@@ -37,6 +39,14 @@ public class SimpleChildEntity extends SimpleEntity {
return v;
}
public short getSt() {
return st;
}
public void setSt(short st) {
this.st = st;
}
public String getExtend() {
return extend;
}

View File

@@ -12,6 +12,9 @@ import java.io.*;
import static java.lang.Thread.sleep;
import java.text.*;
import java.util.concurrent.atomic.*;
import static java.lang.Thread.sleep;
import static java.lang.Thread.sleep;
import static java.lang.Thread.sleep;
/**
*