mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-14 23:07:58 +02:00
java: fix direct conversion API
This commit is contained in:
@@ -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
|
||||
|
@@ -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());
|
||||
|
@@ -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)
|
||||
{
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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();
|
||||
}
|
||||
|
@@ -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();
|
||||
}
|
||||
|
@@ -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);
|
||||
|
@@ -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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user