summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfrsyuki <frsyuki@users.sourceforge.jp>2010-08-18 23:47:31 +0900
committerfrsyuki <frsyuki@users.sourceforge.jp>2010-08-18 23:47:31 +0900
commit48da2b8353a640de6bdc4e59c7fe91b640321b27 (patch)
treeb4b75011bb9bd49abb6b8916914a85920d3a80c9
parent40dc9de6c9bc90a95e55d5d2999f7e45d34cabc8 (diff)
downloadmsgpack-python-48da2b8353a640de6bdc4e59c7fe91b640321b27.tar.gz
java: adds Packer.pack(<primitive types>)
-rw-r--r--java/src/main/java/org/msgpack/Packer.java89
-rw-r--r--java/src/test/java/org/msgpack/TestPackUnpack.java24
2 files changed, 82 insertions, 31 deletions
diff --git a/java/src/main/java/org/msgpack/Packer.java b/java/src/main/java/org/msgpack/Packer.java
index 60e04bf..0ac8d89 100644
--- a/java/src/main/java/org/msgpack/Packer.java
+++ b/java/src/main/java/org/msgpack/Packer.java
@@ -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(boolean o) throws IOException {
+ if(o) {
+ return packTrue();
+ } else {
+ return packFalse();
+ }
}
- public Packer pack(MessagePackable o) throws IOException {
- if(o == null) { return packNil(); }
- o.messagePack(this);
- return this;
+ public Packer pack(byte o) throws IOException {
+ return packByte(o);
}
- public Packer pack(byte[] o) throws IOException {
- if(o == null) { return packNil(); }
- packRaw(o.length);
- return packRawBody(o);
+ public Packer pack(short o) throws IOException {
+ return packShort(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(int o) throws IOException {
+ return packInt(o);
}
- @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());
- }
- return this;
+ 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());
}
diff --git a/java/src/test/java/org/msgpack/TestPackUnpack.java b/java/src/test/java/org/msgpack/TestPackUnpack.java
index 75c5fe7..8163678 100644
--- a/java/src/test/java/org/msgpack/TestPackUnpack.java
+++ b/java/src/test/java/org/msgpack/TestPackUnpack.java
@@ -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