java: adds Packer.pack(<primitive types>)

This commit is contained in:
frsyuki
2010-08-18 23:47:31 +09:00
parent 40dc9de6c9
commit 48da2b8353
2 changed files with 89 additions and 38 deletions

View File

@@ -315,39 +315,36 @@ public class Packer {
}
public Packer pack(String o) throws IOException {
if(o == null) { return packNil(); }
return packString(o);
}
public Packer pack(MessagePackable o) throws IOException {
if(o == null) { return packNil(); }
o.messagePack(this);
return this;
}
public Packer pack(byte[] o) throws IOException {
if(o == null) { return packNil(); }
packRaw(o.length);
return packRawBody(o);
}
public Packer pack(List o) throws IOException {
if(o == null) { return packNil(); }
packArray(o.size());
for(Object i : o) { pack(i); }
return this;
}
@SuppressWarnings("unchecked")
public Packer pack(Map o) throws IOException {
if(o == null) { return packNil(); }
packMap(o.size());
for(Map.Entry e : ((Map<Object,Object>)o).entrySet()) {
pack(e.getKey());
pack(e.getValue());
public Packer pack(boolean o) throws IOException {
if(o) {
return packTrue();
} else {
return packFalse();
}
return this;
}
public Packer pack(byte o) throws IOException {
return packByte(o);
}
public Packer pack(short o) throws IOException {
return packShort(o);
}
public Packer pack(int o) throws IOException {
return packInt(o);
}
public Packer pack(long o) throws IOException {
return packLong(o);
}
public Packer pack(float o) throws IOException {
return packFloat(o);
}
public Packer pack(double o) throws IOException {
return packDouble(o);
}
public Packer pack(Boolean o) throws IOException {
@@ -379,6 +376,11 @@ public class Packer {
return packLong(o);
}
public Packer pack(BigInteger o) throws IOException {
if(o == null) { return packNil(); }
return packBigInteger(o);
}
public Packer pack(Float o) throws IOException {
if(o == null) { return packNil(); }
return packFloat(o);
@@ -389,8 +391,41 @@ public class Packer {
return packDouble(o);
}
public Packer pack(String o) throws IOException {
if(o == null) { return packNil(); }
return packString(o);
}
public Packer pack(MessagePackable o) throws IOException {
if(o == null) { return packNil(); }
o.messagePack(this);
return this;
}
public Packer pack(byte[] o) throws IOException {
if(o == null) { return packNil(); }
packRaw(o.length);
return packRawBody(o);
}
public Packer pack(List o) throws IOException {
if(o == null) { return packNil(); }
packArray(o.size());
for(Object i : o) { pack(i); }
return this;
}
public Packer pack(Map o) throws IOException {
if(o == null) { return packNil(); }
packMap(o.size());
for(Map.Entry<Object,Object> e : ((Map<Object,Object>)o).entrySet()) {
pack(e.getKey());
pack(e.getValue());
}
return this;
}
@SuppressWarnings("unchecked")
public Packer pack(Object o) throws IOException {
if(o == null) {
return packNil();
@@ -413,7 +448,7 @@ public class Packer {
} else if(o instanceof Map) {
Map<Object,Object> m = (Map<Object,Object>)o;
packMap(m.size());
for(Map.Entry e : m.entrySet()) {
for(Map.Entry<Object,Object> e : m.entrySet()) {
pack(e.getKey());
pack(e.getValue());
}

View File

@@ -37,14 +37,30 @@ public class TestPackUnpack {
testInt(rand.nextInt());
}
public void testLong(long val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
MessagePackObject obj = unpackOne(out);
assertEquals(val, obj.asLong());
}
@Test
public void testLong() throws Exception {
testLong(0);
testLong(-1);
testLong(1);
testLong(Integer.MIN_VALUE);
testLong(Integer.MAX_VALUE);
testLong(Long.MIN_VALUE);
testLong(Long.MAX_VALUE);
Random rand = new Random();
for (int i = 0; i < 1000; i++)
testLong(rand.nextLong());
}
public void testBigInteger(BigInteger val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
MessagePackObject obj = unpackOne(out);
if(!val.equals(obj.asBigInteger())) {
System.out.println("expect: "+val);
System.out.println("but : "+obj.asBigInteger());
}
assertEquals(val, obj.asBigInteger());
}
@Test