summaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
authorfrsyuki <frsyuki@users.sourceforge.jp>2010-08-19 01:19:34 +0900
committerfrsyuki <frsyuki@users.sourceforge.jp>2010-08-19 01:19:34 +0900
commitb4c98584db002cbb4d3960b6ed025212248dcce8 (patch)
treeeb1447469b02b154482e0513db3222e4e205bc3f /java
parent1d17836b7d2eb4e5e0f25d2466df70e137642061 (diff)
downloadmsgpack-python-b4c98584db002cbb4d3960b6ed025212248dcce8.tar.gz
java: adds Unpacker.unpackBigInteger()
Diffstat (limited to 'java')
-rw-r--r--java/src/main/java/org/msgpack/BufferedUnpackerImpl.java25
-rw-r--r--java/src/main/java/org/msgpack/Unpacker.java10
-rw-r--r--java/src/test/java/org/msgpack/TestDirectConversion.java24
3 files changed, 55 insertions, 4 deletions
diff --git a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java
index 9496238..5b449c7 100644
--- a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java
+++ b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java
@@ -19,7 +19,7 @@ package org.msgpack;
import java.io.IOException;
import java.nio.ByteBuffer;
-//import java.math.BigInteger;
+import java.math.BigInteger;
abstract class BufferedUnpackerImpl extends UnpackerImpl {
int offset = 0;
@@ -198,8 +198,7 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
{
long o = castBuffer.getLong(0);
if(o < 0) {
- // FIXME unpackBigInteger
- throw new MessageTypeException("uint 64 bigger than 0x7fffffff is not supported");
+ throw new MessageTypeException();
}
advance(9);
return o;
@@ -231,7 +230,25 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
}
}
- // FIXME unpackBigInteger
+ final BigInteger unpackBigInteger() throws IOException, MessageTypeException {
+ more(1);
+ int b = buffer[offset];
+ if((b & 0xff) != 0xcf) {
+ return BigInteger.valueOf(unpackLong());
+ }
+
+ // unsigned int 64
+ more(9);
+ castBuffer.rewind();
+ castBuffer.put(buffer, offset+1, 8);
+ advance(9);
+ long o = castBuffer.getLong(0);
+ if(o < 0) {
+ return new BigInteger(1, castBuffer.array());
+ } else {
+ return BigInteger.valueOf(o);
+ }
+ }
final float unpackFloat() throws IOException, MessageTypeException {
more(1);
diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java
index 3a95243..3cae502 100644
--- a/java/src/main/java/org/msgpack/Unpacker.java
+++ b/java/src/main/java/org/msgpack/Unpacker.java
@@ -22,6 +22,7 @@ import java.io.InputStream;
import java.io.IOException;
import java.util.Iterator;
import java.nio.ByteBuffer;
+import java.math.BigInteger;
/**
* Unpacker enables you to deserialize objects from stream.
@@ -453,6 +454,15 @@ public class Unpacker implements Iterable<MessagePackObject> {
}
/**
+ * Gets one {@code BigInteger} value from the buffer.
+ * This method calls {@link fill()} method if needed.
+ * @throws MessageTypeException the first value of the buffer is not a {@code BigInteger}.
+ */
+ public BigInteger unpackBigInteger() throws IOException, MessageTypeException {
+ return impl.unpackBigInteger();
+ }
+
+ /**
* Gets one {@code float} value from the buffer.
* This method calls {@link fill()} method if needed.
* @throws MessageTypeException the first value of the buffer is not a {@code float}.
diff --git a/java/src/test/java/org/msgpack/TestDirectConversion.java b/java/src/test/java/org/msgpack/TestDirectConversion.java
index dbacf01..1822ecb 100644
--- a/java/src/test/java/org/msgpack/TestDirectConversion.java
+++ b/java/src/test/java/org/msgpack/TestDirectConversion.java
@@ -3,6 +3,7 @@ package org.msgpack;
import org.msgpack.*;
import java.io.*;
import java.util.*;
+import java.math.BigInteger;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -49,6 +50,29 @@ public class TestDirectConversion {
}
@Test
+ public void testBigInteger() throws Exception {
+ testBigInteger(BigInteger.valueOf(0));
+ testBigInteger(BigInteger.valueOf(-1));
+ testBigInteger(BigInteger.valueOf(1));
+ testBigInteger(BigInteger.valueOf(Integer.MIN_VALUE));
+ testBigInteger(BigInteger.valueOf(Integer.MAX_VALUE));
+ testBigInteger(BigInteger.valueOf(Long.MIN_VALUE));
+ testBigInteger(BigInteger.valueOf(Long.MAX_VALUE));
+ BigInteger max = BigInteger.valueOf(Long.MAX_VALUE).setBit(63);
+ testBigInteger(max);
+ Random rand = new Random();
+ for (int i = 0; i < 1000; i++)
+ testBigInteger( max.subtract(BigInteger.valueOf( Math.abs(rand.nextLong()) )) );
+ }
+ public void testBigInteger(BigInteger val) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(val);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker pac = new Unpacker(in);
+ assertEquals(val, pac.unpackBigInteger());
+ }
+
+ @Test
public void testFloat() throws Exception {
testFloat((float)0.0);
testFloat((float)-0.0);