java: supports packing/unpacking of BigInteger less than 0xffffffffffffffff

This commit is contained in:
frsyuki 2010-08-18 23:37:47 +09:00
parent d7469e4694
commit 40dc9de6c9
4 changed files with 42 additions and 14 deletions

View file

@ -18,6 +18,6 @@
package org.msgpack;
public interface MessageConvertable {
public void messageConvert(Object obj) throws MessageTypeException;
public void messageConvert(MessagePackObject obj) throws MessageTypeException;
}

View file

@ -196,19 +196,19 @@ public class Packer {
}
public Packer packBigInteger(BigInteger d) throws IOException {
if(d.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) <= 0) {
if(d.bitLength() <= 63) {
return packLong(d.longValue());
} else if(d.bitLength() <= 64) {
} else if(d.bitLength() <= 64 && d.signum() >= 0) {
castBytes[0] = (byte)0xcf;
byte[] barray = d.toByteArray();
castBytes[1] = barray[0];
castBytes[2] = barray[1];
castBytes[3] = barray[2];
castBytes[4] = barray[3];
castBytes[5] = barray[4];
castBytes[6] = barray[5];
castBytes[7] = barray[6];
castBytes[8] = barray[7];
castBytes[1] = barray[barray.length-8];
castBytes[2] = barray[barray.length-7];
castBytes[3] = barray[barray.length-6];
castBytes[4] = barray[barray.length-5];
castBytes[5] = barray[barray.length-4];
castBytes[6] = barray[barray.length-3];
castBytes[7] = barray[barray.length-2];
castBytes[8] = barray[barray.length-1];
out.write(castBytes);
return this;
} else {
@ -436,6 +436,8 @@ public class Packer {
return packFloat((Float)o);
} else if(o instanceof Double) {
return packDouble((Double)o);
} else if(o instanceof BigInteger) {
return packBigInteger((BigInteger)o);
} else {
throw new MessageTypeException("unknown object "+o+" ("+o.getClass()+")");
}

View file

@ -18,6 +18,7 @@
package org.msgpack;
import java.nio.ByteBuffer;
import java.math.BigInteger;
import org.msgpack.object.*;
public class UnpackerImpl {
@ -262,9 +263,7 @@ public class UnpackerImpl {
{
long o = castBuffer.getLong(0);
if(o < 0) {
// FIXME
//obj = GenericBigInteger.valueOf(o & 0x7fffffffL).setBit(31);
throw new UnpackException("uint 64 bigger than 0x7fffffff is not supported");
obj = IntegerType.create(new BigInteger(1, castBuffer.array()));
} else {
obj = IntegerType.create(o);
}