summaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
authorfrsyuki <frsyuki@users.sourceforge.jp>2010-05-20 05:44:44 +0900
committerfrsyuki <frsyuki@users.sourceforge.jp>2010-05-20 05:44:44 +0900
commit135a9f558600ddbd4cd0d07a57ae1f7fb5b8634a (patch)
tree591f4e5d233c9b140ac47272b1167f811d6e6c9a /java
parent979ff809827ab25005364dad41d2fd043b8eaa4d (diff)
downloadmsgpack-python-135a9f558600ddbd4cd0d07a57ae1f7fb5b8634a.tar.gz
java: fix direct conversion API
Diffstat (limited to 'java')
-rw-r--r--java/src/main/java/org/msgpack/BufferedUnpackerImpl.java205
-rw-r--r--java/src/main/java/org/msgpack/MessageUnpackable.java2
-rw-r--r--java/src/main/java/org/msgpack/UnpackCursor.java96
-rw-r--r--java/src/main/java/org/msgpack/UnpackIterator.java1
-rw-r--r--java/src/main/java/org/msgpack/Unpacker.java102
-rw-r--r--java/src/main/java/org/msgpack/schema/ClassGenerator.java10
-rw-r--r--java/src/main/java/org/msgpack/schema/SpecificClassSchema.java4
-rw-r--r--java/src/test/java/org/msgpack/TestDirectConversion.java42
-rw-r--r--java/test/README2
-rw-r--r--java/test/thrift-protobuf-compare/tpc/src/serializers/BenchmarkRunner.java2
-rw-r--r--java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MediaContent.java78
-rw-r--r--java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDirectSerializer.java68
-rw-r--r--java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDynamicSerializer.java2
-rw-r--r--java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackGenericSerializer.java2
-rw-r--r--java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackIndirectSerializer.java2
-rw-r--r--java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackSpecificSerializer.java2
16 files changed, 356 insertions, 264 deletions
diff --git a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java
index 0ef9c12..5fde4e1 100644
--- a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java
+++ b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java
@@ -22,16 +22,17 @@ import java.nio.ByteBuffer;
//import java.math.BigInteger;
abstract class BufferedUnpackerImpl extends UnpackerImpl {
+ int offset = 0;
int filled = 0;
byte[] buffer = null;
private ByteBuffer castBuffer = ByteBuffer.allocate(8);
abstract boolean fill() throws IOException;
- final int next(int offset, UnpackResult result) throws IOException, UnpackException {
+ final boolean next(UnpackResult result) throws IOException, UnpackException {
if(filled == 0) {
if(!fill()) {
- return offset;
+ return false;
}
}
@@ -39,8 +40,9 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
int noffset = super.execute(buffer, offset, filled);
if(noffset <= offset) {
if(!fill()) {
- return offset;
+ return false;
}
+ continue;
}
offset = noffset;
} while(!super.isFinished());
@@ -49,10 +51,10 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
super.reset();
result.done(obj);
- return offset;
+ return true;
}
- private final void more(int offset, int require) throws IOException, UnpackException {
+ private final void more(int require) throws IOException, UnpackException {
while(filled - offset < require) {
if(!fill()) {
// FIXME
@@ -61,41 +63,46 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
}
}
- final byte unpackByte(UnpackCursor c, int offset) throws IOException, MessageTypeException {
- int o = unpackInt(c, offset);
+ private final void advance(int length) {
+ offset += length;
+ }
+
+ final byte unpackByte() throws IOException, MessageTypeException {
+ int o = unpackInt();
if(0x7f < o || o < -0x80) {
throw new MessageTypeException();
}
return (byte)o;
}
- final short unpackShort(UnpackCursor c, int offset) throws IOException, MessageTypeException {
- int o = unpackInt(c, offset);
+ final short unpackShort() throws IOException, MessageTypeException {
+ int o = unpackInt();
if(0x7fff < o || o < -0x8000) {
throw new MessageTypeException();
}
return (short)o;
}
- final int unpackInt(UnpackCursor c, int offset) throws IOException, MessageTypeException {
- more(offset, 1);
+ final int unpackInt() throws IOException, MessageTypeException {
+ more(1);
int b = buffer[offset];
if((b & 0x80) == 0 || (b & 0xe0) == 0xe0) { // Fixnum
+ advance(1);
return (int)b;
}
switch(b & 0xff) {
case 0xcc: // unsigned int 8
- more(offset, 2);
- c.advance(2);
+ more(2);
+ advance(2);
return (int)((short)buffer[offset+1] & 0xff);
case 0xcd: // unsigned int 16
- more(offset, 3);
+ more(3);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 2);
- c.advance(3);
+ advance(3);
return (int)((int)castBuffer.getShort(0) & 0xffff);
case 0xce: // unsigned int 32
- more(offset, 5);
+ more(5);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 4);
{
@@ -103,11 +110,11 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
if(o < 0) {
throw new MessageTypeException();
}
- c.advance(5);
+ advance(5);
return o;
}
case 0xcf: // unsigned int 64
- more(offset, 9);
+ more(9);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 8);
{
@@ -115,27 +122,27 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
if(o < 0 || o > 0x7fffffffL) {
throw new MessageTypeException();
}
- c.advance(9);
+ advance(9);
return (int)o;
}
case 0xd0: // signed int 8
- more(offset, 2);
- c.advance(2);
+ more(2);
+ advance(2);
return (int)buffer[offset+1];
case 0xd1: // signed int 16
- more(offset, 3);
+ more(3);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 2);
- c.advance(3);
+ advance(3);
return (int)castBuffer.getShort(0);
case 0xd2: // signed int 32
- more(offset, 4);
+ more(4);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 4);
- c.advance(4);
+ advance(4);
return (int)castBuffer.getInt(0);
case 0xd3: // signed int 64
- more(offset, 9);
+ more(9);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 8);
{
@@ -143,7 +150,7 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
if(0x7fffffffL < o || o < -0x80000000L) {
throw new MessageTypeException();
}
- c.advance(9);
+ advance(9);
return (int)o;
}
default:
@@ -151,31 +158,32 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
}
}
- final long unpackLong(UnpackCursor c, int offset) throws IOException, MessageTypeException {
- more(offset, 1);
+ final long unpackLong() throws IOException, MessageTypeException {
+ more(1);
int b = buffer[offset];
if((b & 0x80) == 0 || (b & 0xe0) == 0xe0) { // Fixnum
+ advance(1);
return (long)b;
}
switch(b & 0xff) {
case 0xcc: // unsigned int 8
- more(offset, 2);
- c.advance(2);
+ more(2);
+ advance(2);
return (long)((short)buffer[offset+1] & 0xff);
case 0xcd: // unsigned int 16
- more(offset, 3);
+ more(3);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 2);
- c.advance(3);
+ advance(3);
return (long)((int)castBuffer.getShort(0) & 0xffff);
case 0xce: // unsigned int 32
- more(offset, 5);
+ more(5);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 4);
- c.advance(5);
+ advance(5);
return ((long)castBuffer.getInt(0) & 0xffffffffL);
case 0xcf: // unsigned int 64
- more(offset, 9);
+ more(9);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 8);
{
@@ -184,51 +192,51 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
// FIXME
throw new MessageTypeException("uint 64 bigger than 0x7fffffff is not supported");
}
- c.advance(9);
+ advance(9);
return o;
}
case 0xd0: // signed int 8
- more(offset, 2);
- c.advance(2);
+ more(2);
+ advance(2);
return (long)buffer[offset+1];
case 0xd1: // signed int 16
- more(offset, 3);
+ more(3);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 2);
- c.advance(3);
+ advance(3);
return (long)castBuffer.getShort(0);
case 0xd2: // signed int 32
- more(offset, 4);
+ more(4);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 4);
- c.advance(4);
+ advance(4);
return (long)castBuffer.getInt(0);
case 0xd3: // signed int 64
- more(offset, 9);
+ more(9);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 8);
- c.advance(9);
+ advance(9);
return (long)castBuffer.getLong(0);
default:
throw new MessageTypeException();
}
}
- final float unpackFloat(UnpackCursor c, int offset) throws IOException, MessageTypeException {
- more(offset, 1);
+ final float unpackFloat() throws IOException, MessageTypeException {
+ more(1);
int b = buffer[offset];
switch(b & 0xff) {
case 0xca: // float
- more(offset, 5);
+ more(5);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 4);
- c.advance(5);
+ advance(5);
return castBuffer.getFloat(0);
case 0xcb: // double
- more(offset, 9);
+ more(9);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 8);
- c.advance(9);
+ advance(9);
// FIXME overflow check
return (float)castBuffer.getDouble(0);
default:
@@ -236,70 +244,70 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
}
}
- final double unpackDouble(UnpackCursor c, int offset) throws IOException, MessageTypeException {
- more(offset, 1);
+ final double unpackDouble() throws IOException, MessageTypeException {
+ more(1);
int b = buffer[offset];
switch(b & 0xff) {
case 0xca: // float
- more(offset, 5);
+ more(5);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 4);
- c.advance(5);
+ advance(5);
return (double)castBuffer.getFloat(0);
case 0xcb: // double
- more(offset, 9);
+ more(9);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 8);
- c.advance(9);
+ advance(9);
return castBuffer.getDouble(0);
default:
throw new MessageTypeException();
}
}
- final Object unpackNull(UnpackCursor c, int offset) throws IOException, MessageTypeException {
- more(offset, 1);
+ final Object unpackNull() throws IOException, MessageTypeException {
+ more(1);
int b = buffer[offset] & 0xff;
if(b != 0xc0) { // nil
throw new MessageTypeException();
}
- c.advance(1);
+ advance(1);
return null;
}
- final boolean unpackBoolean(UnpackCursor c, int offset) throws IOException, MessageTypeException {
- more(offset, 1);
+ final boolean unpackBoolean() throws IOException, MessageTypeException {
+ more(1);
int b = buffer[offset] & 0xff;
if(b == 0xc2) { // false
- c.advance(1);
+ advance(1);
return false;
} else if(b == 0xc3) { // true
- c.advance(1);
+ advance(1);
return true;
} else {
throw new MessageTypeException();
}
}
- final int unpackArray(UnpackCursor c, int offset) throws IOException, MessageTypeException {
- more(offset, 1);
+ final int unpackArray() throws IOException, MessageTypeException {
+ more(1);
int b = buffer[offset];
if((b & 0xf0) == 0x90) { // FixArray
- c.advance(1);
+ advance(1);
return (int)(b & 0x0f);
}
switch(b & 0xff) {
case 0xdc: // array 16
- more(offset, 3);
+ more(3);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 2);
- c.advance(3);
+ advance(3);
return (int)castBuffer.getShort(0) & 0xffff;
case 0xdd: // array 32
- more(offset, 5);
+ more(5);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 4);
- c.advance(5);
+ advance(5);
// FIXME overflow check
return castBuffer.getInt(0) & 0x7fffffff;
default:
@@ -307,25 +315,25 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
}
}
- final int unpackMap(UnpackCursor c, int offset) throws IOException, MessageTypeException {
- more(offset, 1);
+ final int unpackMap() throws IOException, MessageTypeException {
+ more(1);
int b = buffer[offset];
if((b & 0xf0) == 0x80) { // FixMap
- c.advance(1);
+ advance(1);
return (int)(b & 0x0f);
}
switch(b & 0xff) {
case 0xde: // map 16
- more(offset, 3);
+ more(3);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 2);
- c.advance(3);
+ advance(3);
return (int)castBuffer.getShort(0) & 0xffff;
case 0xdf: // map 32
- more(offset, 5);
+ more(5);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 4);
- c.advance(5);
+ advance(5);
// FIXME overflow check
return castBuffer.getInt(0) & 0x7fffffff;
default:
@@ -333,25 +341,25 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
}
}
- final int unpackRaw(UnpackCursor c, int offset) throws IOException, MessageTypeException {
- more(offset, 1);
+ final int unpackRaw() throws IOException, MessageTypeException {
+ more(1);
int b = buffer[offset];
if((b & 0xe0) == 0xa0) { // FixRaw
- c.advance(1);
- return (int)(b & 0x0f);
+ advance(1);
+ return (int)(b & 0x1f);
}
switch(b & 0xff) {
case 0xda: // raw 16
- more(offset, 3);
+ more(3);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 2);
- c.advance(3);
+ advance(3);
return (int)castBuffer.getShort(0) & 0xffff;
case 0xdb: // raw 32
- more(offset, 5);
+ more(5);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 4);
- c.advance(5);
+ advance(5);
// FIXME overflow check
return castBuffer.getInt(0) & 0x7fffffff;
default:
@@ -359,26 +367,35 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
}
}
- final byte[] unpackRawBody(UnpackCursor c, int offset, int length) throws IOException, MessageTypeException {
- more(offset, length);
+ final byte[] unpackRawBody(int length) throws IOException, MessageTypeException {
+ more(length);
byte[] bytes = new byte[length];
System.arraycopy(buffer, offset, bytes, 0, length);
- c.advance(length);
+ advance(length);
return bytes;
}
- final String unpackString(UnpackCursor c, int offset) throws IOException, MessageTypeException {
- int length = unpackRaw(c, offset);
- offset = c.getOffset();
- more(offset, length);
+ final String unpackString() throws IOException, MessageTypeException {
+ int length = unpackRaw();
+ more(length);
String s;
try {
s = new String(buffer, offset, length, "UTF-8");
} catch (Exception e) {
throw new MessageTypeException();
}
- c.advance(length);
+ advance(length);
return s;
}
+
+ final Object unpackObject() throws IOException, MessageTypeException {
+ // FIXME save state, restore state
+ UnpackResult result = new UnpackResult();
+ if(!next(result)) {
+ super.reset();
+ throw new MessageTypeException();
+ }
+ return result.getData();
+ }
}
diff --git a/java/src/main/java/org/msgpack/MessageUnpackable.java b/java/src/main/java/org/msgpack/MessageUnpackable.java
index 20e4d56..cc206e7 100644
--- a/java/src/main/java/org/msgpack/MessageUnpackable.java
+++ b/java/src/main/java/org/msgpack/MessageUnpackable.java
@@ -20,6 +20,6 @@ package org.msgpack;
import java.io.IOException;
public interface MessageUnpackable {
- public void messageUnpack(Unpacker pk) throws IOException, MessageTypeException;
+ public void messageUnpack(Unpacker pac) throws IOException, MessageTypeException;
}
diff --git a/java/src/main/java/org/msgpack/UnpackCursor.java b/java/src/main/java/org/msgpack/UnpackCursor.java
deleted file mode 100644
index 33f4258..0000000
--- a/java/src/main/java/org/msgpack/UnpackCursor.java
+++ /dev/null
@@ -1,96 +0,0 @@
-//
-// MessagePack for Java
-//
-// Copyright (C) 2009-2010 FURUHASHI Sadayuki
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-package org.msgpack;
-
-import java.io.IOException;
-
-public class UnpackCursor {
- private Unpacker pac;
- private int offset;
-
- UnpackCursor(Unpacker pac, int offset)
- {
- this.pac = pac;
- this.offset = offset;
- }
-
- final void advance(int length) {
- offset += length;
- }
-
- final int getOffset() {
- return offset;
- }
-
- public byte unpackByte() throws IOException, MessageTypeException {
- return pac.impl.unpackByte(this, offset);
- }
-
- public short unpackShort() throws IOException, MessageTypeException {
- return pac.impl.unpackShort(this, offset);
- }
-
- public int unpackInt() throws IOException, MessageTypeException {
- return pac.impl.unpackInt(this, offset);
- }
-
- public long unpackLong() throws IOException, MessageTypeException {
- return pac.impl.unpackLong(this, offset);
- }
-
- public float unpackFloat() throws IOException, MessageTypeException {
- return pac.impl.unpackFloat(this, offset);
- }
-
- public double unpackDouble() throws IOException, MessageTypeException {
- return pac.impl.unpackDouble(this, offset);
- }
-
- public Object unpackNull() throws IOException, MessageTypeException {
- return pac.impl.unpackNull(this, offset);
- }
-
- public boolean unpackBoolean() throws IOException, MessageTypeException {
- return pac.impl.unpackBoolean(this, offset);
- }
-
- public int unpackArray() throws IOException, MessageTypeException {
- return pac.impl.unpackArray(this, offset);
- }
-
- public int unpackMap() throws IOException, MessageTypeException {
- return pac.impl.unpackMap(this, offset);
- }
-
- public int unpackRaw() throws IOException, MessageTypeException {
- return pac.impl.unpackRaw(this, offset);
- }
-
- public byte[] unpackRawBody(int length) throws IOException, MessageTypeException {
- return pac.impl.unpackRawBody(this, offset, length);
- }
-
- public String unpackString() throws IOException, MessageTypeException {
- return pac.impl.unpackString(this, offset);
- }
-
- public void commit() {
- pac.setOffset(offset);
- }
-}
-
diff --git a/java/src/main/java/org/msgpack/UnpackIterator.java b/java/src/main/java/org/msgpack/UnpackIterator.java
index 5cc994d..f17e229 100644
--- a/java/src/main/java/org/msgpack/UnpackIterator.java
+++ b/java/src/main/java/org/msgpack/UnpackIterator.java
@@ -30,6 +30,7 @@ public class UnpackIterator extends UnpackResult implements Iterator<Object> {
}
public boolean hasNext() {
+ if(finished) { return true; }
try {
return pac.next(this);
} catch (IOException e) {
diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java
index 20a8c4a..4d8da7b 100644
--- a/java/src/main/java/org/msgpack/Unpacker.java
+++ b/java/src/main/java/org/msgpack/Unpacker.java
@@ -36,7 +36,6 @@ public class Unpacker implements Iterable<Object> {
private static final int DEFAULT_BUFFER_SIZE = 32*1024;
- protected int offset;
protected int parsed;
protected int bufferReserveSize;
protected InputStream stream;
@@ -73,7 +72,6 @@ public class Unpacker implements Iterable<Object> {
}
public Unpacker(InputStream stream, int bufferReserveSize) {
- this.offset = 0;
this.parsed = 0;
this.bufferReserveSize = bufferReserveSize/2;
this.stream = stream;
@@ -97,7 +95,7 @@ public class Unpacker implements Iterable<Object> {
int length = buffer.remaining();
if (length == 0) return;
reserveBuffer(length);
- buffer.get(impl.buffer, this.offset, length);
+ buffer.get(impl.buffer, impl.offset, length);
bufferConsumed(length);
}
@@ -107,7 +105,7 @@ public class Unpacker implements Iterable<Object> {
public void feed(byte[] buffer, int offset, int length) {
reserveBuffer(length);
- System.arraycopy(buffer, offset, impl.buffer, this.offset, length);
+ System.arraycopy(buffer, offset, impl.buffer, impl.offset, length);
bufferConsumed(length);
}
@@ -121,13 +119,12 @@ public class Unpacker implements Iterable<Object> {
public UnpackResult next() throws IOException, UnpackException {
UnpackResult result = new UnpackResult();
- this.offset = impl.next(this.offset, result);
+ impl.next(result);
return result;
}
public boolean next(UnpackResult result) throws IOException, UnpackException {
- this.offset = impl.next(this.offset, result);
- return result.isFinished();
+ return impl.next(result);
}
@@ -143,17 +140,17 @@ public class Unpacker implements Iterable<Object> {
}
int nextSize = impl.buffer.length * 2;
- int notParsed = impl.filled - this.offset;
+ int notParsed = impl.filled - impl.offset;
while(nextSize < require + notParsed) {
nextSize *= 2;
}
byte[] tmp = new byte[nextSize];
- System.arraycopy(impl.buffer, this.offset, tmp, 0, impl.filled - this.offset);
+ System.arraycopy(impl.buffer, impl.offset, tmp, 0, impl.filled - impl.offset);
impl.buffer = tmp;
impl.filled = notParsed;
- this.offset = 0;
+ impl.offset = 0;
}
public byte[] getBuffer() {
@@ -173,12 +170,12 @@ public class Unpacker implements Iterable<Object> {
}
public boolean execute() throws UnpackException {
- int noffset = impl.execute(impl.buffer, offset, impl.filled);
- if(noffset <= offset) {
+ int noffset = impl.execute(impl.buffer, impl.offset, impl.filled);
+ if(noffset <= impl.offset) {
return false;
}
- parsed += noffset - offset;
- offset = noffset;
+ parsed += noffset - impl.offset;
+ impl.offset = noffset;
return impl.isFinished();
}
@@ -188,8 +185,8 @@ public class Unpacker implements Iterable<Object> {
}
public int execute(byte[] buffer, int offset, int length) throws UnpackException {
- int noffset = impl.execute(buffer, offset + this.offset, length);
- this.offset = noffset - offset;
+ int noffset = impl.execute(buffer, offset + impl.offset, length);
+ impl.offset = noffset - offset;
if(impl.isFinished()) {
impl.resetState();
}
@@ -208,15 +205,8 @@ public class Unpacker implements Iterable<Object> {
impl.reset();
}
-
- public UnpackCursor begin()
- {
- return new UnpackCursor(this, offset);
- }
-
-
public int getMessageSize() {
- return parsed - offset + impl.filled;
+ return parsed - impl.offset + impl.filled;
}
public int getParsedSize() {
@@ -224,22 +214,72 @@ public class Unpacker implements Iterable<Object> {
}
public int getNonParsedSize() {
- return impl.filled - offset;
+ return impl.filled - impl.offset;
}
public void skipNonparsedBuffer(int size) {
- offset += size;
+ impl.offset += size;
}
public void removeNonparsedBuffer() {
- impl.filled = offset;
+ impl.filled = impl.offset;
+ }
+
+
+ final public byte unpackByte() throws IOException, MessageTypeException {
+ return impl.unpackByte();
+ }
+
+ final public short unpackShort() throws IOException, MessageTypeException {
+ return impl.unpackShort();
+ }
+
+ final public int unpackInt() throws IOException, MessageTypeException {
+ return impl.unpackInt();
+ }
+
+ final public long unpackLong() throws IOException, MessageTypeException {
+ return impl.unpackLong();
+ }
+
+ final public float unpackFloat() throws IOException, MessageTypeException {
+ return impl.unpackFloat();
}
+ final public double unpackDouble() throws IOException, MessageTypeException {
+ return impl.unpackDouble();
+ }
+
+ final public Object unpackNull() throws IOException, MessageTypeException {
+ return impl.unpackNull();
+ }
+
+ final public boolean unpackBoolean() throws IOException, MessageTypeException {
+ return impl.unpackBoolean();
+ }
+
+ final public int unpackArray() throws IOException, MessageTypeException {
+ return impl.unpackArray();
+ }
+
+ final public int unpackMap() throws IOException, MessageTypeException {
+ return impl.unpackMap();
+ }
+
+ final public int unpackRaw() throws IOException, MessageTypeException {
+ return impl.unpackRaw();
+ }
+
+ final public byte[] unpackRawBody(int length) throws IOException, MessageTypeException {
+ return impl.unpackRawBody(length);
+ }
+
+ final public String unpackString() throws IOException, MessageTypeException {
+ return impl.unpackString();
+ }
- void setOffset(int offset)
- {
- parsed += offset - this.offset;
- this.offset = offset;
+ final public Object unpackObject() throws IOException, MessageTypeException {
+ return impl.unpackObject();
}
}
diff --git a/java/src/main/java/org/msgpack/schema/ClassGenerator.java b/java/src/main/java/org/msgpack/schema/ClassGenerator.java
index 061dcbb..f8a13fa 100644
--- a/java/src/main/java/org/msgpack/schema/ClassGenerator.java
+++ b/java/src/main/java/org/msgpack/schema/ClassGenerator.java
@@ -105,7 +105,7 @@ public class ClassGenerator {
private void writeClass() throws IOException {
line();
- line("public final class "+schema.getName()+" implements MessagePackable, MessageMergeable");
+ line("public final class "+schema.getName()+" implements MessagePackable, MessageConvertable");
line("{");
pushIndent();
writeSchema();
@@ -117,7 +117,7 @@ public class ClassGenerator {
private void writeSubclass() throws IOException {
line();
- line("final class "+schema.getName()+" implements MessagePackable, MessageMergeable");
+ line("final class "+schema.getName()+" implements MessagePackable, MessageConvertable");
line("{");
pushIndent();
writeSchema();
@@ -150,7 +150,7 @@ public class ClassGenerator {
writeConstructors();
writeAccessors();
writePackFunction();
- writeMergeFunction();
+ writeConvertFunction();
writeFactoryFunction();
}
@@ -184,11 +184,11 @@ public class ClassGenerator {
line("}");
}
- private void writeMergeFunction() throws IOException {
+ private void writeConvertFunction() throws IOException {
line();
line("@Override");
line("@SuppressWarnings(\"unchecked\")");
- line("public void messageMerge(Object obj) throws MessageTypeException");
+ line("public void messageConvert(Object obj) throws MessageTypeException");
line("{");
pushIndent();
line("Object[] _source = ((List)obj).toArray();");
diff --git a/java/src/main/java/org/msgpack/schema/SpecificClassSchema.java b/java/src/main/java/org/msgpack/schema/SpecificClassSchema.java
index 30bd9e1..850f621 100644
--- a/java/src/main/java/org/msgpack/schema/SpecificClassSchema.java
+++ b/java/src/main/java/org/msgpack/schema/SpecificClassSchema.java
@@ -59,8 +59,8 @@ public class SpecificClassSchema extends ClassSchema {
cacheConstructor();
}
try {
- MessageMergeable o = (MessageMergeable)constructorCache.newInstance((Object[])null);
- o.messageMerge(obj);
+ MessageConvertable o = (MessageConvertable)constructorCache.newInstance((Object[])null);
+ o.messageConvert(obj);
return o;
} catch (InvocationTargetException e) {
throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage());
diff --git a/java/src/test/java/org/msgpack/TestDirectConversion.java b/java/src/test/java/org/msgpack/TestDirectConversion.java
index d77fe13..77bbc58 100644
--- a/java/src/test/java/org/msgpack/TestDirectConversion.java
+++ b/java/src/test/java/org/msgpack/TestDirectConversion.java
@@ -8,12 +8,6 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class TestDirectConversion {
- private UnpackCursor prepareCursor(ByteArrayOutputStream out) {
- ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
- Unpacker upk = new Unpacker(in);
- return upk.begin();
- }
-
@Test
public void testInt() throws Exception {
testInt(0);
@@ -28,9 +22,9 @@ public class TestDirectConversion {
public void testInt(int val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
- UnpackCursor c = prepareCursor(out);
- assertEquals(val, c.unpackInt());
- c.commit();
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker upk = new Unpacker(in);
+ assertEquals(val, upk.unpackInt());
}
@Test
@@ -51,14 +45,14 @@ public class TestDirectConversion {
public void testFloat(float val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
- UnpackCursor c = prepareCursor(out);
- float f = c.unpackFloat();
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker upk = new Unpacker(in);
+ float f = upk.unpackFloat();
if(Float.isNaN(val)) {
assertTrue(Float.isNaN(f));
} else {
assertEquals(val, f, 10e-10);
}
- c.commit();
}
@Test
@@ -79,23 +73,23 @@ public class TestDirectConversion {
public void testDouble(double val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
- UnpackCursor c = prepareCursor(out);
- double f = c.unpackDouble();
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker upk = new Unpacker(in);
+ double f = upk.unpackDouble();
if(Double.isNaN(val)) {
assertTrue(Double.isNaN(f));
} else {
assertEquals(val, f, 10e-10);
}
- c.commit();
}
@Test
public void testNil() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).packNil();
- UnpackCursor c = prepareCursor(out);
- assertEquals(null, c.unpackNull());
- c.commit();
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker upk = new Unpacker(in);
+ assertEquals(null, upk.unpackNull());
}
@Test
@@ -106,9 +100,9 @@ public class TestDirectConversion {
public void testBoolean(boolean val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
- UnpackCursor c = prepareCursor(out);
- assertEquals(val, c.unpackBoolean());
- c.commit();
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker upk = new Unpacker(in);
+ assertEquals(val, upk.unpackBoolean());
}
@Test
@@ -145,9 +139,9 @@ public class TestDirectConversion {
public void testString(String val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
- UnpackCursor c = prepareCursor(out);
- assertEquals(val, c.unpackString());
- c.commit();
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker upk = new Unpacker(in);
+ assertEquals(val, upk.unpackString());
}
// FIXME container types
diff --git a/java/test/README b/java/test/README
index 4e16454..9b98d91 100644
--- a/java/test/README
+++ b/java/test/README
@@ -1,7 +1,7 @@
#!/bin/sh
svn checkout -r114 http://thrift-protobuf-compare.googlecode.com/svn/trunk/ thrift-protobuf-compare-base
cp -rf thrift-protobuf-compare/tpc thrift-protobuf-compare-base
-cp ../dist/msgpack.jar thrift-protobuf-compare-base/tpc/lib/
+cp ../target/msgpack*.jar thrift-protobuf-compare-base/tpc/lib/msgpack.jar
cd thrift-protobuf-compare-base/tpc/
ant compile
./run-benchmark.sh
diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/BenchmarkRunner.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/BenchmarkRunner.java
index b17dfb2..fa88b6b 100644
--- a/java/test/thrift-protobuf-compare/tpc/src/serializers/BenchmarkRunner.java
+++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/BenchmarkRunner.java
@@ -11,6 +11,7 @@ import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
+import serializers.msgpack.MessagePackDirectSerializer;
import serializers.msgpack.MessagePackSpecificSerializer;
import serializers.msgpack.MessagePackIndirectSerializer;
import serializers.msgpack.MessagePackDynamicSerializer;
@@ -39,6 +40,7 @@ public class BenchmarkRunner
BenchmarkRunner runner = new BenchmarkRunner();
// binary codecs first
+ runner.addObjectSerializer(new MessagePackDirectSerializer());
runner.addObjectSerializer(new MessagePackSpecificSerializer());
runner.addObjectSerializer(new MessagePackIndirectSerializer());
runner.addObjectSerializer(new MessagePackDynamicSerializer());
diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MediaContent.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MediaContent.java
index 5dfbc8d..e750b5a 100644
--- a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MediaContent.java
+++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MediaContent.java
@@ -6,7 +6,7 @@ import org.msgpack.*;
import org.msgpack.schema.ClassSchema;
import org.msgpack.schema.FieldSchema;
-public final class MediaContent implements MessagePackable, MessageMergeable
+public final class MediaContent implements MessagePackable, MessageConvertable, MessageUnpackable
{
private static final ClassSchema _SCHEMA = (ClassSchema)Schema.load("(class MediaContent (package serializers.msgpack) (field image (array (class Image (package serializers.msgpack) (field uri string) (field title string) (field width int) (field height int) (field size int)))) (field media (class Media (package serializers.msgpack) (field uri string) (field title string) (field width int) (field height int) (field format string) (field duration long) (field size long) (field bitrate int) (field person (array string)) (field player int) (field copyright string))))");
public static ClassSchema getSchema() { return _SCHEMA; }
@@ -27,7 +27,7 @@ public final class MediaContent implements MessagePackable, MessageMergeable
@Override
@SuppressWarnings("unchecked")
- public void messageMerge(Object obj) throws MessageTypeException
+ public void messageConvert(Object obj) throws MessageTypeException
{
Object[] _source = ((List)obj).toArray();
FieldSchema[] _fields = _SCHEMA.getFields();
@@ -35,6 +35,23 @@ public final class MediaContent implements MessagePackable, MessageMergeable
if(_source.length <= 1) { return; } this.media = (Media)_fields[1].getSchema().convert(_source[1]);
}
+ @Override
+ public void messageUnpack(Unpacker _pac) throws IOException, MessageTypeException {
+ int _length = _pac.unpackArray();
+ if(_length <= 0) { return; }
+ int _image_length = _pac.unpackArray();
+ this.image = new ArrayList(_image_length);
+ for(int _i=0; _i < _image_length; ++_i) {
+ Image _image_i = new Image();
+ _image_i.messageUnpack(_pac);
+ this.image.add(_image_i);
+ }
+ if(_length <= 1) { return; }
+ this.media = new Media();
+ this.media.messageUnpack(_pac);
+ for(int _i=2; _i < _length; ++_i) { _pac.unpackObject(); }
+ }
+
@SuppressWarnings("unchecked")
public static MediaContent createFromMessage(Object[] _message)
{
@@ -45,7 +62,7 @@ public final class MediaContent implements MessagePackable, MessageMergeable
}
}
-final class Image implements MessagePackable, MessageMergeable
+final class Image implements MessagePackable, MessageConvertable, MessageUnpackable
{
private static final ClassSchema _SCHEMA = (ClassSchema)Schema.load("(class Image (package serializers.msgpack) (field uri string) (field title string) (field width int) (field height int) (field size int))");
public static ClassSchema getSchema() { return _SCHEMA; }
@@ -72,7 +89,7 @@ final class Image implements MessagePackable, MessageMergeable
@Override
@SuppressWarnings("unchecked")
- public void messageMerge(Object obj) throws MessageTypeException
+ public void messageConvert(Object obj) throws MessageTypeException
{
Object[] _source = ((List)obj).toArray();
FieldSchema[] _fields = _SCHEMA.getFields();
@@ -83,6 +100,22 @@ final class Image implements MessagePackable, MessageMergeable
if(_source.length <= 4) { return; } this.size = (Integer)_fields[4].getSchema().convert(_source[4]);
}
+ @Override
+ public void messageUnpack(Unpacker _pac) throws IOException, MessageTypeException {
+ int _length = _pac.unpackArray();
+ if(_length <= 0) { return; }
+ this.uri = _pac.unpackString();
+ if(_length <= 1) { return; }
+ this.title = _pac.unpackString();
+ if(_length <= 2) { return; }
+ this.width = _pac.unpackInt();
+ if(_length <= 3) { return; }
+ this.height = _pac.unpackInt();
+ if(_length <= 4) { return; }
+ this.size = _pac.unpackInt();
+ for(int _i=5; _i < _length; ++_i) { _pac.unpackObject(); }
+ }
+
@SuppressWarnings("unchecked")
public static Image createFromMessage(Object[] _message)
{
@@ -96,7 +129,7 @@ final class Image implements MessagePackable, MessageMergeable
}
}
-final class Media implements MessagePackable, MessageMergeable
+final class Media implements MessagePackable, MessageConvertable, MessageUnpackable
{
private static final ClassSchema _SCHEMA = (ClassSchema)Schema.load("(class Media (package serializers.msgpack) (field uri string) (field title string) (field width int) (field height int) (field format string) (field duration long) (field size long) (field bitrate int) (field person (array string)) (field player int) (field copyright string))");
public static ClassSchema getSchema() { return _SCHEMA; }
@@ -135,7 +168,7 @@ final class Media implements MessagePackable, MessageMergeable
@Override
@SuppressWarnings("unchecked")
- public void messageMerge(Object obj) throws MessageTypeException
+ public void messageConvert(Object obj) throws MessageTypeException
{
Object[] _source = ((List)obj).toArray();
FieldSchema[] _fields = _SCHEMA.getFields();
@@ -152,6 +185,39 @@ final class Media implements MessagePackable, MessageMergeable
if(_source.length <= 10) { return; } this.copyright = (String)_fields[10].getSchema().convert(_source[10]);
}
+ @Override
+ public void messageUnpack(Unpacker _pac) throws IOException, MessageTypeException {
+ int _length = _pac.unpackArray();
+ if(_length <= 0) { return; }
+ this.uri = _pac.unpackString();
+ if(_length <= 1) { return; }
+ this.title = _pac.unpackString();
+ if(_length <= 2) { return; }
+ this.width = _pac.unpackInt();
+ if(_length <= 3) { return; }
+ this.height = _pac.unpackInt();
+ if(_length <= 4) { return; }
+ this.format = _pac.unpackString();
+ if(_length <= 5) { return; }
+ this.duration = _pac.unpackLong();
+ if(_length <= 6) { return; }
+ this.size = _pac.unpackLong();
+ if(_length <= 7) { return; }
+ this.bitrate = _pac.unpackInt();
+ if(_length <= 8) { return; }
+ int _person_length = _pac.unpackArray();
+ this.person = new ArrayList(_person_length);
+ for(int _i=0; _i < _person_length; ++_i) {
+ String _person_i = _pac.unpackString();
+ this.person.add(_person_i);
+ }
+ if(_length <= 9) { return; }
+ this.player = _pac.unpackInt();
+ if(_length <= 10) { return; }
+ this.copyright = _pac.unpackString();
+ for(int _i=11; _i < _length; ++_i) { _pac.unpackObject(); }
+ }
+
@SuppressWarnings("unchecked")
public static Media createFromMessage(Object[] _message)
{
diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDirectSerializer.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDirectSerializer.java
new file mode 100644
index 0000000..721e95b
--- /dev/null
+++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDirectSerializer.java
@@ -0,0 +1,68 @@
+package serializers.msgpack;
+
+import java.io.*;
+import java.util.*;
+import org.msgpack.*;
+import serializers.ObjectSerializer;
+
+public class MessagePackDirectSerializer implements ObjectSerializer<MediaContent>
+{
+ public String getName() {
+ return "msgpack-direct";
+ }
+
+ public MediaContent create() throws Exception {
+ Media media = new Media();
+ media.uri = "http://javaone.com/keynote.mpg";
+ media.format = "video/mpg4";
+ media.title = "Javaone Keynote";
+ media.duration = 1234567L;
+ media.bitrate = 0;
+ media.person = new ArrayList<String>(2);
+ media.person.add("Bill Gates");
+ media.person.add("Steve Jobs");
+ media.player = 0;
+ media.height = 0;
+ media.width = 0;
+ media.size = 123L;
+ media.copyright = "";
+
+ Image image1 = new Image();
+ image1.uri = "http://javaone.com/keynote_large.jpg";
+ image1.width = 0;
+ image1.height = 0;
+ image1.size = 2;
+ image1.title = "Javaone Keynote";
+
+ Image image2 = new Image();
+ image2.uri = "http://javaone.com/keynote_thumbnail.jpg";
+ image2.width = 0;
+ image2.height = 0;
+ image2.size = 1;
+ image2.title = "Javaone Keynote";
+
+ MediaContent content = new MediaContent();
+ content.media = media;
+ content.image = new ArrayList<Image>(2);
+ content.image.add(image1);
+ content.image.add(image2);
+
+ return content;
+ }
+
+ public byte[] serialize(MediaContent content) throws Exception {
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ Packer pk = new Packer(os);
+ pk.pack(content);
+ return os.toByteArray();
+ }
+
+ public MediaContent deserialize(byte[] array) throws Exception {
+ Unpacker pac = new Unpacker();
+ pac.feed(array);
+ MediaContent obj = new MediaContent();
+ obj.messageUnpack(pac);
+ return obj;
+ }
+}
+
diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDynamicSerializer.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDynamicSerializer.java
index c8a88ac..9c8ccbe 100644
--- a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDynamicSerializer.java
+++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDynamicSerializer.java
@@ -60,7 +60,7 @@ public class MessagePackDynamicSerializer implements ObjectSerializer<Object>
}
public Object deserialize(byte[] array) throws Exception {
- UnbufferedUnpacker pac = new UnbufferedUnpacker();
+ Unpacker pac = new Unpacker();
pac.execute(array);
return (Object)pac.getData();
}
diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackGenericSerializer.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackGenericSerializer.java
index 4935899..316389a 100644
--- a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackGenericSerializer.java
+++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackGenericSerializer.java
@@ -62,7 +62,7 @@ public class MessagePackGenericSerializer implements ObjectSerializer<Object>
}
public Object deserialize(byte[] array) throws Exception {
- UnbufferedUnpacker pac = new UnbufferedUnpacker().useSchema(MEDIA_CONTENT_SCHEMA);
+ Unpacker pac = new Unpacker().useSchema(MEDIA_CONTENT_SCHEMA);
pac.execute(array);
return (Object)pac.getData();
}
diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackIndirectSerializer.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackIndirectSerializer.java
index 2767474..e24472b 100644
--- a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackIndirectSerializer.java
+++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackIndirectSerializer.java
@@ -58,7 +58,7 @@ public class MessagePackIndirectSerializer implements ObjectSerializer<MediaCont
}
public MediaContent deserialize(byte[] array) throws Exception {
- UnbufferedUnpacker pac = new UnbufferedUnpacker();
+ Unpacker pac = new Unpacker();
pac.execute(array);
Object obj = pac.getData();
return (MediaContent)MediaContent.getSchema().convert(obj);
diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackSpecificSerializer.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackSpecificSerializer.java
index 91ded5c..9b6a8ce 100644
--- a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackSpecificSerializer.java
+++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackSpecificSerializer.java
@@ -58,7 +58,7 @@ public class MessagePackSpecificSerializer implements ObjectSerializer<MediaCont
}
public MediaContent deserialize(byte[] array) throws Exception {
- UnbufferedUnpacker pac = new UnbufferedUnpacker().useSchema(MediaContent.getSchema());
+ Unpacker pac = new Unpacker().useSchema(MediaContent.getSchema());
pac.execute(array);
return (MediaContent)pac.getData();
}