mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-06-27 06:45:38 +02:00
Merge branch 'master' of ssh://github.com/msgpack/msgpack
This commit is contained in:
commit
ab0bf37d30
@ -223,6 +223,10 @@ public class Packer {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Packer packBoolean(boolean d) throws IOException {
|
||||||
|
return d ? packTrue() : packFalse();
|
||||||
|
}
|
||||||
|
|
||||||
public Packer packArray(int n) throws IOException {
|
public Packer packArray(int n) throws IOException {
|
||||||
if(n < 16) {
|
if(n < 16) {
|
||||||
final int d = 0x90 | n;
|
final int d = 0x90 | n;
|
||||||
|
@ -20,28 +20,13 @@ package org.msgpack;
|
|||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import org.msgpack.schema.SSchemaParser;
|
import org.msgpack.schema.SSchemaParser;
|
||||||
import org.msgpack.schema.ClassGenerator;
|
//import org.msgpack.schema.ClassGenerator;
|
||||||
|
|
||||||
public abstract class Schema {
|
public abstract class Schema {
|
||||||
private String expression;
|
public Schema() { }
|
||||||
private String name;
|
|
||||||
|
|
||||||
public Schema(String name) {
|
public abstract String getClassName();
|
||||||
this.expression = expression;
|
public abstract String getExpression();
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getFullName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getExpression() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Schema parse(String source) {
|
public static Schema parse(String source) {
|
||||||
return SSchemaParser.parse(source);
|
return SSchemaParser.parse(source);
|
||||||
@ -51,83 +36,43 @@ public abstract class Schema {
|
|||||||
return SSchemaParser.load(source);
|
return SSchemaParser.load(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(Writer output) throws IOException {
|
|
||||||
ClassGenerator.write(this, output);
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract void pack(Packer pk, Object obj) throws IOException;
|
public abstract void pack(Packer pk, Object obj) throws IOException;
|
||||||
|
|
||||||
public abstract Object convert(Object obj) throws MessageTypeException;
|
public abstract Object convert(Object obj) throws MessageTypeException;
|
||||||
|
|
||||||
|
|
||||||
public Object createFromNil() {
|
public Object createFromNil() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object createFromBoolean(boolean v) {
|
public Object createFromBoolean(boolean v) {
|
||||||
throw new RuntimeException("type error");
|
throw new MessageTypeException("type error");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object createFromByte(byte v) {
|
public Object createFromByte(byte v) {
|
||||||
throw new RuntimeException("type error");
|
throw new MessageTypeException("type error");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object createFromShort(short v) {
|
public Object createFromShort(short v) {
|
||||||
throw new RuntimeException("type error");
|
throw new MessageTypeException("type error");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object createFromInt(int v) {
|
public Object createFromInt(int v) {
|
||||||
throw new RuntimeException("type error");
|
throw new MessageTypeException("type error");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object createFromLong(long v) {
|
public Object createFromLong(long v) {
|
||||||
throw new RuntimeException("type error");
|
throw new MessageTypeException("type error");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object createFromFloat(float v) {
|
public Object createFromFloat(float v) {
|
||||||
throw new RuntimeException("type error");
|
throw new MessageTypeException("type error");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object createFromDouble(double v) {
|
public Object createFromDouble(double v) {
|
||||||
throw new RuntimeException("type error");
|
throw new MessageTypeException("type error");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object createFromRaw(byte[] b, int offset, int length) {
|
public Object createFromRaw(byte[] b, int offset, int length) {
|
||||||
throw new RuntimeException("type error");
|
throw new MessageTypeException("type error");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* FIXME
|
|
||||||
public Object createFromBoolean(boolean v) {
|
|
||||||
throw MessageTypeException.schemaMismatch(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object createFromByte(byte v) {
|
|
||||||
throw MessageTypeException.schemaMismatch(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object createFromShort(short v) {
|
|
||||||
throw MessageTypeException.schemaMismatch(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object createFromInt(int v) {
|
|
||||||
throw MessageTypeException.schemaMismatch(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object createFromLong(long v) {
|
|
||||||
throw MessageTypeException.schemaMismatch(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object createFromFloat(float v) {
|
|
||||||
throw MessageTypeException.schemaMismatch(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object createFromDouble(double v) {
|
|
||||||
throw MessageTypeException.schemaMismatch(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object createFromRaw(byte[] b, int offset, int length) {
|
|
||||||
throw MessageTypeException.schemaMismatch(this);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -561,11 +561,11 @@ public class Unpacker implements Iterable<Object> {
|
|||||||
return impl.unpackObject();
|
return impl.unpackObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
final void unpack(MessageUnpackable obj) throws IOException, MessageTypeException {
|
final public void unpack(MessageUnpackable obj) throws IOException, MessageTypeException {
|
||||||
obj.messageUnpack(this);
|
obj.messageUnpack(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
final boolean tryUnpackNull() throws IOException {
|
final public boolean tryUnpackNull() throws IOException {
|
||||||
return impl.tryUnpackNull();
|
return impl.tryUnpackNull();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
64
java/src/main/java/org/msgpack/schema/BooleanSchema.java
Normal file
64
java/src/main/java/org/msgpack/schema/BooleanSchema.java
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
//
|
||||||
|
// 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.schema;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import org.msgpack.*;
|
||||||
|
|
||||||
|
public class BooleanSchema extends Schema {
|
||||||
|
public BooleanSchema() { }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getClassName() {
|
||||||
|
return "Boolean";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getExpression() {
|
||||||
|
return "boolean";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void pack(Packer pk, Object obj) throws IOException {
|
||||||
|
if(obj instanceof Boolean) {
|
||||||
|
pk.packBoolean((Boolean)obj);
|
||||||
|
} else if(obj == null) {
|
||||||
|
pk.packNil();
|
||||||
|
} else {
|
||||||
|
throw MessageTypeException.invalidConvert(obj, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final boolean convertBoolean(Object obj) throws MessageTypeException {
|
||||||
|
if(obj instanceof Boolean) {
|
||||||
|
return (Boolean)obj;
|
||||||
|
}
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object convert(Object obj) throws MessageTypeException {
|
||||||
|
return convertBoolean(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object createFromBoolean(boolean v) {
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -22,59 +22,48 @@ import java.io.IOException;
|
|||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
public class RawSchema extends Schema {
|
public class ByteArraySchema extends Schema {
|
||||||
public RawSchema() {
|
public ByteArraySchema() { }
|
||||||
super("raw");
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getFullName() {
|
@Override
|
||||||
|
public String getClassName() {
|
||||||
return "byte[]";
|
return "byte[]";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void pack(Packer pk, Object obj) throws IOException {
|
public String getExpression() {
|
||||||
// FIXME instanceof GenericObject
|
return "raw";
|
||||||
if(obj instanceof byte[]) {
|
|
||||||
byte[] d = (byte[])obj;
|
|
||||||
pk.packRaw(d.length);
|
|
||||||
pk.packRawBody(d);
|
|
||||||
|
|
||||||
} else if(obj instanceof ByteBuffer) {
|
|
||||||
ByteBuffer d = (ByteBuffer)obj;
|
|
||||||
if(!d.hasArray()) {
|
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
|
||||||
}
|
}
|
||||||
pk.packRaw(d.capacity());
|
|
||||||
pk.packRawBody(d.array(), d.position(), d.capacity());
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void pack(Packer pk, Object obj) throws IOException {
|
||||||
|
if(obj instanceof byte[]) {
|
||||||
|
byte[] b = (byte[])obj;
|
||||||
|
pk.packRaw(b.length);
|
||||||
|
pk.packRawBody(b);
|
||||||
} else if(obj instanceof String) {
|
} else if(obj instanceof String) {
|
||||||
try {
|
try {
|
||||||
byte[] d = ((String)obj).getBytes("UTF-8");
|
byte[] b = ((String)obj).getBytes("UTF-8");
|
||||||
pk.packRaw(d.length);
|
pk.packRaw(b.length);
|
||||||
pk.packRawBody(d);
|
pk.packRawBody(b);
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
throw new MessageTypeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if(obj == null) {
|
} else if(obj == null) {
|
||||||
pk.packNil();
|
pk.packNil();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
throw MessageTypeException.invalidConvert(obj, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public static final byte[] convertByteArray(Object obj) throws MessageTypeException {
|
||||||
public Object convert(Object obj) throws MessageTypeException {
|
|
||||||
// FIXME instanceof GenericObject
|
|
||||||
if(obj instanceof byte[]) {
|
if(obj instanceof byte[]) {
|
||||||
// FIXME copy?
|
// FIXME copy?
|
||||||
//byte[] d = (byte[])obj;
|
//byte[] d = (byte[])obj;
|
||||||
//byte[] v = new byte[d.length];
|
//byte[] v = new byte[d.length];
|
||||||
//System.arraycopy(d, 0, v, 0, d.length);
|
//System.arraycopy(d, 0, v, 0, d.length);
|
||||||
//return v;
|
//return v;
|
||||||
return obj;
|
return (byte[])obj;
|
||||||
|
|
||||||
} else if(obj instanceof ByteBuffer) {
|
} else if(obj instanceof ByteBuffer) {
|
||||||
ByteBuffer d = (ByteBuffer)obj;
|
ByteBuffer d = (ByteBuffer)obj;
|
||||||
byte[] v = new byte[d.capacity()];
|
byte[] v = new byte[d.capacity()];
|
||||||
@ -82,17 +71,20 @@ public class RawSchema extends Schema {
|
|||||||
d.get(v);
|
d.get(v);
|
||||||
d.position(pos);
|
d.position(pos);
|
||||||
return v;
|
return v;
|
||||||
|
|
||||||
} else if(obj instanceof String) {
|
} else if(obj instanceof String) {
|
||||||
try {
|
try {
|
||||||
return ((String)obj).getBytes("UTF-8");
|
return ((String)obj).getBytes("UTF-8");
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
@Override
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
public Object convert(Object obj) throws MessageTypeException {
|
||||||
}
|
return convertByteArray(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
@ -21,8 +21,11 @@ import java.io.IOException;
|
|||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
public class ByteSchema extends Schema {
|
public class ByteSchema extends Schema {
|
||||||
public ByteSchema() {
|
public ByteSchema() { }
|
||||||
super("Byte");
|
|
||||||
|
@Override
|
||||||
|
public String getClassName() {
|
||||||
|
return "Byte";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -33,27 +36,32 @@ public class ByteSchema extends Schema {
|
|||||||
@Override
|
@Override
|
||||||
public void pack(Packer pk, Object obj) throws IOException {
|
public void pack(Packer pk, Object obj) throws IOException {
|
||||||
if(obj instanceof Number) {
|
if(obj instanceof Number) {
|
||||||
pk.packByte( ((Number)obj).byteValue() );
|
short value = ((Number)obj).shortValue();
|
||||||
|
if(value > Byte.MAX_VALUE) {
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
|
pk.packByte((byte)value);
|
||||||
} else if(obj == null) {
|
} else if(obj == null) {
|
||||||
pk.packNil();
|
pk.packNil();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
throw MessageTypeException.invalidConvert(obj, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final byte convertByte(Object obj) throws MessageTypeException {
|
||||||
|
if(obj instanceof Number) {
|
||||||
|
short value = ((Number)obj).shortValue();
|
||||||
|
if(value > Byte.MAX_VALUE) {
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
|
return (byte)value;
|
||||||
|
}
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object convert(Object obj) throws MessageTypeException {
|
public Object convert(Object obj) throws MessageTypeException {
|
||||||
if(obj instanceof Byte) {
|
return convertByte(obj);
|
||||||
return obj;
|
|
||||||
|
|
||||||
} else if(obj instanceof Number) {
|
|
||||||
return ((Number)obj).byteValue();
|
|
||||||
|
|
||||||
} else {
|
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -63,26 +71,25 @@ public class ByteSchema extends Schema {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object createFromShort(short v) {
|
public Object createFromShort(short v) {
|
||||||
|
if(v > Byte.MAX_VALUE) {
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
return (byte)v;
|
return (byte)v;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object createFromInt(int v) {
|
public Object createFromInt(int v) {
|
||||||
|
if(v > Byte.MAX_VALUE) {
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
return (byte)v;
|
return (byte)v;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object createFromLong(long v) {
|
public Object createFromLong(long v) {
|
||||||
return (byte)v;
|
if(v > Byte.MAX_VALUE) {
|
||||||
|
throw new MessageTypeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromFloat(float v) {
|
|
||||||
return (byte)v;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromDouble(double v) {
|
|
||||||
return (byte)v;
|
return (byte)v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,8 +77,11 @@ public class ClassGenerator {
|
|||||||
for(FieldSchema f : cs.getFields()) {
|
for(FieldSchema f : cs.getFields()) {
|
||||||
findSubclassSchema(dst, f.getSchema());
|
findSubclassSchema(dst, f.getSchema());
|
||||||
}
|
}
|
||||||
} else if(s instanceof ArraySchema) {
|
} else if(s instanceof ListSchema) {
|
||||||
ArraySchema as = (ArraySchema)s;
|
ListSchema as = (ListSchema)s;
|
||||||
|
findSubclassSchema(dst, as.getElementSchema(0));
|
||||||
|
} else if(s instanceof SetSchema) {
|
||||||
|
SetSchema as = (SetSchema)s;
|
||||||
findSubclassSchema(dst, as.getElementSchema(0));
|
findSubclassSchema(dst, as.getElementSchema(0));
|
||||||
} else if(s instanceof MapSchema) {
|
} else if(s instanceof MapSchema) {
|
||||||
MapSchema as = (MapSchema)s;
|
MapSchema as = (MapSchema)s;
|
||||||
@ -105,7 +108,7 @@ public class ClassGenerator {
|
|||||||
|
|
||||||
private void writeClass() throws IOException {
|
private void writeClass() throws IOException {
|
||||||
line();
|
line();
|
||||||
line("public final class "+schema.getName()+" implements MessagePackable, MessageConvertable");
|
line("public final class "+schema.getClassName()+" implements MessagePackable, MessageConvertable");
|
||||||
line("{");
|
line("{");
|
||||||
pushIndent();
|
pushIndent();
|
||||||
writeSchema();
|
writeSchema();
|
||||||
@ -117,7 +120,7 @@ public class ClassGenerator {
|
|||||||
|
|
||||||
private void writeSubclass() throws IOException {
|
private void writeSubclass() throws IOException {
|
||||||
line();
|
line();
|
||||||
line("final class "+schema.getName()+" implements MessagePackable, MessageConvertable");
|
line("final class "+schema.getClassName()+" implements MessagePackable, MessageConvertable");
|
||||||
line("{");
|
line("{");
|
||||||
pushIndent();
|
pushIndent();
|
||||||
writeSchema();
|
writeSchema();
|
||||||
@ -135,7 +138,7 @@ public class ClassGenerator {
|
|||||||
private void writeMemberVariables() throws IOException {
|
private void writeMemberVariables() throws IOException {
|
||||||
line();
|
line();
|
||||||
for(FieldSchema f : schema.getFields()) {
|
for(FieldSchema f : schema.getFields()) {
|
||||||
line("public "+f.getSchema().getFullName()+" "+f.getName()+";");
|
line("public "+f.getSchema().getClassName()+" "+f.getName()+";");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,7 +159,7 @@ public class ClassGenerator {
|
|||||||
|
|
||||||
private void writeConstructors() throws IOException {
|
private void writeConstructors() throws IOException {
|
||||||
line();
|
line();
|
||||||
line("public "+schema.getName()+"() { }");
|
line("public "+schema.getClassName()+"() { }");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeAccessors() throws IOException {
|
private void writeAccessors() throws IOException {
|
||||||
@ -195,7 +198,7 @@ public class ClassGenerator {
|
|||||||
line("FieldSchema[] _fields = _SCHEMA.getFields();");
|
line("FieldSchema[] _fields = _SCHEMA.getFields();");
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for(FieldSchema f : schema.getFields()) {
|
for(FieldSchema f : schema.getFields()) {
|
||||||
line("if(_source.length <= "+i+") { return; } this."+f.getName()+" = ("+f.getSchema().getFullName()+")_fields["+i+"].getSchema().convert(_source["+i+"]);");
|
line("if(_source.length <= "+i+") { return; } this."+f.getName()+" = ("+f.getSchema().getClassName()+")_fields["+i+"].getSchema().convert(_source["+i+"]);");
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
popIndent();
|
popIndent();
|
||||||
@ -205,13 +208,13 @@ public class ClassGenerator {
|
|||||||
private void writeFactoryFunction() throws IOException {
|
private void writeFactoryFunction() throws IOException {
|
||||||
line();
|
line();
|
||||||
line("@SuppressWarnings(\"unchecked\")");
|
line("@SuppressWarnings(\"unchecked\")");
|
||||||
line("public static "+schema.getName()+" createFromMessage(Object[] _message)");
|
line("public static "+schema.getClassName()+" createFromMessage(Object[] _message)");
|
||||||
line("{");
|
line("{");
|
||||||
pushIndent();
|
pushIndent();
|
||||||
line(schema.getName()+" _self = new "+schema.getName()+"();");
|
line(schema.getClassName()+" _self = new "+schema.getClassName()+"();");
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for(FieldSchema f : schema.getFields()) {
|
for(FieldSchema f : schema.getFields()) {
|
||||||
line("if(_message.length <= "+i+") { return _self; } _self."+f.getName()+" = ("+f.getSchema().getFullName()+")_message["+i+"];");
|
line("if(_message.length <= "+i+") { return _self; } _self."+f.getName()+" = ("+f.getSchema().getClassName()+")_message["+i+"];");
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
line("return _self;");
|
line("return _self;");
|
||||||
|
@ -22,6 +22,7 @@ import java.util.List;
|
|||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
public abstract class ClassSchema extends Schema implements IArraySchema {
|
public abstract class ClassSchema extends Schema implements IArraySchema {
|
||||||
|
protected String name;
|
||||||
protected FieldSchema[] fields;
|
protected FieldSchema[] fields;
|
||||||
protected List<String> imports;
|
protected List<String> imports;
|
||||||
protected String namespace;
|
protected String namespace;
|
||||||
@ -30,7 +31,7 @@ public abstract class ClassSchema extends Schema implements IArraySchema {
|
|||||||
public ClassSchema(
|
public ClassSchema(
|
||||||
String name, String namespace,
|
String name, String namespace,
|
||||||
List<String> imports, List<FieldSchema> fields) {
|
List<String> imports, List<FieldSchema> fields) {
|
||||||
super(name);
|
this.name = name;
|
||||||
this.namespace = namespace;
|
this.namespace = namespace;
|
||||||
this.imports = imports; // FIXME clone?
|
this.imports = imports; // FIXME clone?
|
||||||
this.fields = new FieldSchema[fields.size()];
|
this.fields = new FieldSchema[fields.size()];
|
||||||
@ -42,6 +43,31 @@ public abstract class ClassSchema extends Schema implements IArraySchema {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getClassName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getExpression() {
|
||||||
|
StringBuffer b = new StringBuffer();
|
||||||
|
b.append("(class ");
|
||||||
|
b.append(name);
|
||||||
|
if(namespace != null) {
|
||||||
|
b.append(" (package "+namespace+")");
|
||||||
|
}
|
||||||
|
for(FieldSchema f : fields) {
|
||||||
|
b.append(" "+f.getExpression());
|
||||||
|
}
|
||||||
|
b.append(")");
|
||||||
|
return b.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean equals(ClassSchema o) {
|
||||||
|
return (namespace != null ? namespace.equals(o.getNamespace()) : o.getNamespace() == null) &&
|
||||||
|
name.equals(o.name);
|
||||||
|
}
|
||||||
|
|
||||||
public final FieldSchema[] getFields() {
|
public final FieldSchema[] getFields() {
|
||||||
return fields;
|
return fields;
|
||||||
}
|
}
|
||||||
@ -61,35 +87,5 @@ public abstract class ClassSchema extends Schema implements IArraySchema {
|
|||||||
void setImports(List<String> imports) {
|
void setImports(List<String> imports) {
|
||||||
this.imports = imports; // FIXME clone?
|
this.imports = imports; // FIXME clone?
|
||||||
}
|
}
|
||||||
|
|
||||||
//@Override
|
|
||||||
//public String getFullName()
|
|
||||||
//{
|
|
||||||
// if(namespace == null) {
|
|
||||||
// return getName();
|
|
||||||
// } else {
|
|
||||||
// return namespace+"."+getName();
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getExpression() {
|
|
||||||
StringBuffer b = new StringBuffer();
|
|
||||||
b.append("(class ");
|
|
||||||
b.append(getName());
|
|
||||||
if(namespace != null) {
|
|
||||||
b.append(" (package "+namespace+")");
|
|
||||||
}
|
|
||||||
for(FieldSchema f : fields) {
|
|
||||||
b.append(" "+f.getExpression());
|
|
||||||
}
|
|
||||||
b.append(")");
|
|
||||||
return b.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean equals(SpecificClassSchema o) {
|
|
||||||
return (namespace != null ? namespace.equals(o.getNamespace()) : o.getNamespace() == null) &&
|
|
||||||
getName().equals(o.getName());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,8 +21,11 @@ import java.io.IOException;
|
|||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
public class DoubleSchema extends Schema {
|
public class DoubleSchema extends Schema {
|
||||||
public DoubleSchema() {
|
public DoubleSchema() { }
|
||||||
super("Double");
|
|
||||||
|
@Override
|
||||||
|
public String getClassName() {
|
||||||
|
return "Double";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -32,43 +35,30 @@ public class DoubleSchema extends Schema {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void pack(Packer pk, Object obj) throws IOException {
|
public void pack(Packer pk, Object obj) throws IOException {
|
||||||
if(obj instanceof Number) {
|
if(obj instanceof Double) {
|
||||||
pk.packDouble( ((Number)obj).doubleValue() );
|
pk.packDouble((Double)obj);
|
||||||
|
} else if(obj instanceof Float) {
|
||||||
|
pk.packFloat((Float)obj);
|
||||||
} else if(obj == null) {
|
} else if(obj == null) {
|
||||||
pk.packNil();
|
pk.packNil();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
throw MessageTypeException.invalidConvert(obj, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final double convertDouble(Object obj) throws MessageTypeException {
|
||||||
|
if(obj instanceof Double) {
|
||||||
|
return (Double)obj;
|
||||||
|
} else if(obj instanceof Float) {
|
||||||
|
return ((Float)obj).doubleValue();
|
||||||
|
} else {
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object convert(Object obj) throws MessageTypeException {
|
public Object convert(Object obj) throws MessageTypeException {
|
||||||
if(obj instanceof Double) {
|
return convertDouble(obj);
|
||||||
return obj;
|
|
||||||
|
|
||||||
} else if(obj instanceof Number) {
|
|
||||||
return ((Number)obj).doubleValue();
|
|
||||||
|
|
||||||
} else {
|
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromByte(byte v) {
|
|
||||||
return (double)v;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromShort(short v) {
|
|
||||||
return (double)v;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromInt(int v) {
|
|
||||||
return (double)v;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -21,8 +21,11 @@ import java.io.IOException;
|
|||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
public class FloatSchema extends Schema {
|
public class FloatSchema extends Schema {
|
||||||
public FloatSchema() {
|
public FloatSchema() { }
|
||||||
super("Float");
|
|
||||||
|
@Override
|
||||||
|
public String getClassName() {
|
||||||
|
return "Float";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -32,43 +35,30 @@ public class FloatSchema extends Schema {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void pack(Packer pk, Object obj) throws IOException {
|
public void pack(Packer pk, Object obj) throws IOException {
|
||||||
if(obj instanceof Number) {
|
if(obj instanceof Double) {
|
||||||
pk.packFloat( ((Number)obj).floatValue() );
|
pk.packDouble((Double)obj);
|
||||||
|
} else if(obj instanceof Float) {
|
||||||
|
pk.packFloat((Float)obj);
|
||||||
} else if(obj == null) {
|
} else if(obj == null) {
|
||||||
pk.packNil();
|
pk.packNil();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
throw MessageTypeException.invalidConvert(obj, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final float convertFloat(Object obj) throws MessageTypeException {
|
||||||
|
if(obj instanceof Double) {
|
||||||
|
return ((Double)obj).floatValue();
|
||||||
|
} else if(obj instanceof Float) {
|
||||||
|
return (Float)obj;
|
||||||
|
} else {
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object convert(Object obj) throws MessageTypeException {
|
public Object convert(Object obj) throws MessageTypeException {
|
||||||
if(obj instanceof Float) {
|
return convertFloat(obj);
|
||||||
return obj;
|
|
||||||
|
|
||||||
} else if(obj instanceof Number) {
|
|
||||||
return ((Number)obj).floatValue();
|
|
||||||
|
|
||||||
} else {
|
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromByte(byte v) {
|
|
||||||
return (float)v;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromShort(short v) {
|
|
||||||
return (float)v;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromInt(int v) {
|
|
||||||
return (float)v;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -41,10 +41,8 @@ public class GenericClassSchema extends ClassSchema {
|
|||||||
FieldSchema f = fields[i];
|
FieldSchema f = fields[i];
|
||||||
f.getSchema().pack(pk, d.get(f.getName()));
|
f.getSchema().pack(pk, d.get(f.getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if(obj == null) {
|
} else if(obj == null) {
|
||||||
pk.packNil();
|
pk.packNil();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
throw MessageTypeException.invalidConvert(obj, this);
|
||||||
}
|
}
|
||||||
@ -55,7 +53,6 @@ public class GenericClassSchema extends ClassSchema {
|
|||||||
if(obj instanceof Collection) {
|
if(obj instanceof Collection) {
|
||||||
// FIXME optimize
|
// FIXME optimize
|
||||||
return createFromArray( ((Collection)obj).toArray() );
|
return createFromArray( ((Collection)obj).toArray() );
|
||||||
|
|
||||||
} else if(obj instanceof Map) {
|
} else if(obj instanceof Map) {
|
||||||
HashMap<String,Object> m = new HashMap<String,Object>(fields.length);
|
HashMap<String,Object> m = new HashMap<String,Object>(fields.length);
|
||||||
Map d = (Map)obj;
|
Map d = (Map)obj;
|
||||||
@ -65,7 +62,6 @@ public class GenericClassSchema extends ClassSchema {
|
|||||||
m.put(fieldName, f.getSchema().convert(d.get(fieldName)));
|
m.put(fieldName, f.getSchema().convert(d.get(fieldName)));
|
||||||
}
|
}
|
||||||
return m;
|
return m;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
throw MessageTypeException.invalidConvert(obj, this);
|
||||||
}
|
}
|
||||||
|
@ -22,11 +22,13 @@ import java.util.List;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
//import org.msgpack.generic.*;
|
|
||||||
|
|
||||||
public class GenericSchema extends Schema implements IArraySchema, IMapSchema {
|
public class GenericSchema extends Schema implements IArraySchema, IMapSchema {
|
||||||
public GenericSchema() {
|
public GenericSchema() { }
|
||||||
super("Object");
|
|
||||||
|
@Override
|
||||||
|
public String getClassName() {
|
||||||
|
return "Object";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -123,70 +125,5 @@ public class GenericSchema extends Schema implements IArraySchema, IMapSchema {
|
|||||||
}
|
}
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
@Override
|
|
||||||
public Object createFromNil() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromBoolean(boolean v) {
|
|
||||||
return new GenericBoolean(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromFromByte(byte v) {
|
|
||||||
return new GenericByte(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromShort(short v) {
|
|
||||||
return new GenericShort(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromInt(int v) {
|
|
||||||
return new GenericInt(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromLong(long v) {
|
|
||||||
return new GenericLong(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromFloat(float v) {
|
|
||||||
return new GenericFloat(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromDouble(double v) {
|
|
||||||
return new GenericDouble(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromRaw(byte[] b, int offset, int length) {
|
|
||||||
return new GenericRaw(b, offset, length);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromArray(Object[] obj) {
|
|
||||||
// FIXME GenericArray
|
|
||||||
return Arrays.asList(obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromMap(Object[] obj) {
|
|
||||||
GenericMap m = new GenericMap(obj.length / 2);
|
|
||||||
int i = 0;
|
|
||||||
while(i < obj.length) {
|
|
||||||
Object k = obj[i++];
|
|
||||||
Object v = obj[i++];
|
|
||||||
m.put(k, v);
|
|
||||||
}
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,8 +21,11 @@ import java.io.IOException;
|
|||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
public class IntSchema extends Schema {
|
public class IntSchema extends Schema {
|
||||||
public IntSchema() {
|
public IntSchema() { }
|
||||||
super("Integer");
|
|
||||||
|
@Override
|
||||||
|
public String getClassName() {
|
||||||
|
return "Integer";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -33,27 +36,38 @@ public class IntSchema extends Schema {
|
|||||||
@Override
|
@Override
|
||||||
public void pack(Packer pk, Object obj) throws IOException {
|
public void pack(Packer pk, Object obj) throws IOException {
|
||||||
if(obj instanceof Number) {
|
if(obj instanceof Number) {
|
||||||
pk.packInt( ((Number)obj).intValue() );
|
int value = ((Number)obj).intValue();
|
||||||
|
if(value >= Short.MAX_VALUE) {
|
||||||
|
long lvalue = ((Number)obj).longValue();
|
||||||
|
if(lvalue > Integer.MAX_VALUE) {
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pk.packInt(value);
|
||||||
} else if(obj == null) {
|
} else if(obj == null) {
|
||||||
pk.packNil();
|
pk.packNil();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
throw MessageTypeException.invalidConvert(obj, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final int convertInt(Object obj) throws MessageTypeException {
|
||||||
|
if(obj instanceof Number) {
|
||||||
|
int value = ((Number)obj).intValue();
|
||||||
|
if(value >= Integer.MAX_VALUE) {
|
||||||
|
long lvalue = ((Number)obj).longValue();
|
||||||
|
if(lvalue > Integer.MAX_VALUE) {
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object convert(Object obj) throws MessageTypeException {
|
public Object convert(Object obj) throws MessageTypeException {
|
||||||
if(obj instanceof Integer) {
|
return convertInt(obj);
|
||||||
return obj;
|
|
||||||
|
|
||||||
} else if(obj instanceof Number) {
|
|
||||||
return ((Number)obj).intValue();
|
|
||||||
|
|
||||||
} else {
|
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -73,16 +87,9 @@ public class IntSchema extends Schema {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object createFromLong(long v) {
|
public Object createFromLong(long v) {
|
||||||
return (int)v;
|
if(v > Integer.MAX_VALUE) {
|
||||||
|
throw new MessageTypeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromFloat(float v) {
|
|
||||||
return (int)v;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromDouble(double v) {
|
|
||||||
return (int)v;
|
return (int)v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,37 +22,32 @@ import java.util.Collection;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.RandomAccess;
|
import java.util.RandomAccess;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
public class ArraySchema extends Schema implements IArraySchema {
|
public class ListSchema extends Schema implements IArraySchema {
|
||||||
private Schema elementSchema;
|
private Schema elementSchema;
|
||||||
|
|
||||||
public ArraySchema(Schema elementSchema)
|
public ListSchema(Schema elementSchema) {
|
||||||
{
|
|
||||||
super("array");
|
|
||||||
this.elementSchema = elementSchema;
|
this.elementSchema = elementSchema;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getFullName()
|
public String getClassName() {
|
||||||
{
|
return "List<"+elementSchema.getClassName()+">";
|
||||||
return "List<"+elementSchema.getFullName()+">";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getExpression()
|
public String getExpression() {
|
||||||
{
|
|
||||||
return "(array "+elementSchema.getExpression()+")";
|
return "(array "+elementSchema.getExpression()+")";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
public void pack(Packer pk, Object obj) throws IOException {
|
||||||
public void pack(Packer pk, Object obj) throws IOException
|
|
||||||
{
|
|
||||||
if(obj instanceof List) {
|
if(obj instanceof List) {
|
||||||
ArrayList<Object> d = (ArrayList<Object>)obj;
|
List<Object> d = (List<Object>)obj;
|
||||||
pk.packArray(d.size());
|
pk.packArray(d.size());
|
||||||
if(obj instanceof RandomAccess) {
|
if(obj instanceof RandomAccess) {
|
||||||
for(int i=0; i < d.size(); ++i) {
|
for(int i=0; i < d.size(); ++i) {
|
||||||
@ -63,62 +58,53 @@ public class ArraySchema extends Schema implements IArraySchema {
|
|||||||
elementSchema.pack(pk, e);
|
elementSchema.pack(pk, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if(obj instanceof Set) {
|
} else if(obj instanceof Set) {
|
||||||
Set<Object> d = (Set<Object>)obj;
|
Set<Object> d = (Set<Object>)obj;
|
||||||
pk.packArray(d.size());
|
pk.packArray(d.size());
|
||||||
for(Object e : d) {
|
for(Object e : d) {
|
||||||
elementSchema.pack(pk, e);
|
elementSchema.pack(pk, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if(obj == null) {
|
} else if(obj == null) {
|
||||||
pk.packNil();
|
pk.packNil();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
throw MessageTypeException.invalidConvert(obj, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Object convert(Object obj) throws MessageTypeException
|
public static final <T> List<T> convertList(Object obj,
|
||||||
{
|
Schema elementSchema, List<T> dest) throws MessageTypeException {
|
||||||
if(obj instanceof List) {
|
if(!(obj instanceof List)) {
|
||||||
List d = (List)obj;
|
throw new MessageTypeException();
|
||||||
ArrayList ar = new ArrayList(d.size());
|
}
|
||||||
|
List<Object> d = (List<Object>)obj;
|
||||||
|
if(dest == null) {
|
||||||
|
dest = new ArrayList<T>(d.size());
|
||||||
|
}
|
||||||
if(obj instanceof RandomAccess) {
|
if(obj instanceof RandomAccess) {
|
||||||
for(int i=0; i < d.size(); ++i) {
|
for(int i=0; i < d.size(); ++i) {
|
||||||
ar.add( elementSchema.convert(d.get(i)) );
|
dest.add( (T)elementSchema.convert(d.get(i)) );
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for(Object e : d) {
|
for(Object e : d) {
|
||||||
ar.add( elementSchema.convert(e) );
|
dest.add( (T)elementSchema.convert(e) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ar;
|
return dest;
|
||||||
|
|
||||||
} else if(obj instanceof Collection) {
|
|
||||||
Collection d = (Collection)obj;
|
|
||||||
ArrayList ar = new ArrayList(d.size());
|
|
||||||
for(Object e : (Collection)obj) {
|
|
||||||
ar.add( elementSchema.convert(e) );
|
|
||||||
}
|
|
||||||
return ar;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Schema getElementSchema(int index)
|
public Object convert(Object obj) throws MessageTypeException {
|
||||||
{
|
return convertList(obj, elementSchema, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Schema getElementSchema(int index) {
|
||||||
return elementSchema;
|
return elementSchema;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object createFromArray(Object[] obj)
|
public Object createFromArray(Object[] obj) {
|
||||||
{
|
|
||||||
return Arrays.asList(obj);
|
return Arrays.asList(obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -21,8 +21,11 @@ import java.io.IOException;
|
|||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
public class LongSchema extends Schema {
|
public class LongSchema extends Schema {
|
||||||
public LongSchema() {
|
public LongSchema() { }
|
||||||
super("Long");
|
|
||||||
|
@Override
|
||||||
|
public String getClassName() {
|
||||||
|
return "Long";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -33,27 +36,25 @@ public class LongSchema extends Schema {
|
|||||||
@Override
|
@Override
|
||||||
public void pack(Packer pk, Object obj) throws IOException {
|
public void pack(Packer pk, Object obj) throws IOException {
|
||||||
if(obj instanceof Number) {
|
if(obj instanceof Number) {
|
||||||
pk.packLong( ((Number)obj).longValue() );
|
long value = ((Number)obj).longValue();
|
||||||
|
pk.packLong(value);
|
||||||
} else if(obj == null) {
|
} else if(obj == null) {
|
||||||
pk.packNil();
|
pk.packNil();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
throw MessageTypeException.invalidConvert(obj, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final long convertLong(Object obj) throws MessageTypeException {
|
||||||
|
if(obj instanceof Number) {
|
||||||
|
return ((Number)obj).longValue();
|
||||||
|
}
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object convert(Object obj) throws MessageTypeException {
|
public Object convert(Object obj) throws MessageTypeException {
|
||||||
if(obj instanceof Long) {
|
return convertLong(obj);
|
||||||
return obj;
|
|
||||||
|
|
||||||
} else if(obj instanceof Number) {
|
|
||||||
return ((Number)obj).longValue();
|
|
||||||
|
|
||||||
} else {
|
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -75,15 +76,5 @@ public class LongSchema extends Schema {
|
|||||||
public Object createFromLong(long v) {
|
public Object createFromLong(long v) {
|
||||||
return (long)v;
|
return (long)v;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromFloat(float v) {
|
|
||||||
return (long)v;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromDouble(double v) {
|
|
||||||
return (long)v;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,14 +27,13 @@ public class MapSchema extends Schema implements IMapSchema {
|
|||||||
private Schema valueSchema;
|
private Schema valueSchema;
|
||||||
|
|
||||||
public MapSchema(Schema keySchema, Schema valueSchema) {
|
public MapSchema(Schema keySchema, Schema valueSchema) {
|
||||||
super("map");
|
|
||||||
this.keySchema = keySchema;
|
this.keySchema = keySchema;
|
||||||
this.valueSchema = valueSchema;
|
this.valueSchema = valueSchema;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getFullName() {
|
public String getClassName() {
|
||||||
return "HashList<"+keySchema.getFullName()+", "+valueSchema.getFullName()+">";
|
return "Map<"+keySchema.getClassName()+", "+valueSchema.getClassName()+">";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -52,29 +51,33 @@ public class MapSchema extends Schema implements IMapSchema {
|
|||||||
keySchema.pack(pk, e.getKey());
|
keySchema.pack(pk, e.getKey());
|
||||||
valueSchema.pack(pk, e.getValue());
|
valueSchema.pack(pk, e.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if(obj == null) {
|
} else if(obj == null) {
|
||||||
pk.packNil();
|
pk.packNil();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
throw MessageTypeException.invalidConvert(obj, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static final <K,V> Map<K,V> convertMap(Object obj,
|
||||||
|
Schema keySchema, Schema valueSchema, Map<K,V> dest) throws MessageTypeException {
|
||||||
|
if(!(obj instanceof Map)) {
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
|
Map<Object,Object> d = (Map<Object,Object>)obj;
|
||||||
|
if(dest == null) {
|
||||||
|
dest = new HashMap<K,V>(d.size());
|
||||||
|
}
|
||||||
|
for(Map.Entry<Object,Object> e : d.entrySet()) {
|
||||||
|
dest.put((K)keySchema.convert(e.getKey()),
|
||||||
|
(V)valueSchema.convert(e.getValue()));
|
||||||
|
}
|
||||||
|
return (Map<K,V>)d;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public Object convert(Object obj) throws MessageTypeException {
|
public Object convert(Object obj) throws MessageTypeException {
|
||||||
if(obj instanceof Map) {
|
return convertMap(obj, keySchema, valueSchema, null);
|
||||||
Map<Object,Object> d = (Map<Object,Object>)obj;
|
|
||||||
Map<Object,Object> m = new HashMap<Object,Object>();
|
|
||||||
for(Map.Entry<Object,Object> e : d.entrySet()) {
|
|
||||||
m.put(keySchema.convert(e.getKey()), valueSchema.convert(e.getValue()));
|
|
||||||
}
|
|
||||||
return m;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -90,14 +93,14 @@ public class MapSchema extends Schema implements IMapSchema {
|
|||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Object createFromMap(Object[] obj) {
|
public Object createFromMap(Object[] obj) {
|
||||||
HashMap m = new HashMap(obj.length / 2);
|
HashMap dest = new HashMap(obj.length / 2);
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(i < obj.length) {
|
while(i < obj.length) {
|
||||||
Object k = obj[i++];
|
Object k = obj[i++];
|
||||||
Object v = obj[i++];
|
Object v = obj[i++];
|
||||||
m.put(k, v);
|
dest.put(k, v);
|
||||||
}
|
}
|
||||||
return m;
|
return dest;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,7 +140,7 @@ public class SSchemaParser {
|
|||||||
if(type.equals("string")) {
|
if(type.equals("string")) {
|
||||||
return new StringSchema();
|
return new StringSchema();
|
||||||
} else if(type.equals("raw")) {
|
} else if(type.equals("raw")) {
|
||||||
return new RawSchema();
|
return new ByteArraySchema();
|
||||||
} else if(type.equals("byte")) {
|
} else if(type.equals("byte")) {
|
||||||
return new ByteSchema();
|
return new ByteSchema();
|
||||||
} else if(type.equals("short")) {
|
} else if(type.equals("short")) {
|
||||||
@ -163,11 +163,13 @@ public class SSchemaParser {
|
|||||||
if(type.equals("class")) {
|
if(type.equals("class")) {
|
||||||
return parseClass(exp);
|
return parseClass(exp);
|
||||||
} else if(type.equals("array")) {
|
} else if(type.equals("array")) {
|
||||||
return parseArray(exp);
|
return parseList(exp);
|
||||||
|
} else if(type.equals("set")) {
|
||||||
|
return parseSet(exp);
|
||||||
} else if(type.equals("map")) {
|
} else if(type.equals("map")) {
|
||||||
return parseMap(exp);
|
return parseMap(exp);
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException("class, array or map is expected but got '"+type+"': "+exp);
|
throw new RuntimeException("class, list, set or map is expected but got '"+type+"': "+exp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -209,12 +211,20 @@ public class SSchemaParser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArraySchema parseArray(SExp exp) {
|
private ListSchema parseList(SExp exp) {
|
||||||
if(exp.size() != 2) {
|
if(exp.size() != 2) {
|
||||||
throw new RuntimeException("array is (array ELEMENT_TYPE): "+exp);
|
throw new RuntimeException("list is (list ELEMENT_TYPE): "+exp);
|
||||||
}
|
}
|
||||||
Schema elementType = readType(exp.getTuple(1));
|
Schema elementType = readType(exp.getTuple(1));
|
||||||
return new ArraySchema(elementType);
|
return new ListSchema(elementType);
|
||||||
|
}
|
||||||
|
|
||||||
|
private SetSchema parseSet(SExp exp) {
|
||||||
|
if(exp.size() != 2) {
|
||||||
|
throw new RuntimeException("list is (list ELEMENT_TYPE): "+exp);
|
||||||
|
}
|
||||||
|
Schema elementType = readType(exp.getTuple(1));
|
||||||
|
return new SetSchema(elementType);
|
||||||
}
|
}
|
||||||
|
|
||||||
private MapSchema parseMap(SExp exp) {
|
private MapSchema parseMap(SExp exp) {
|
||||||
|
115
java/src/main/java/org/msgpack/schema/SetSchema.java
Normal file
115
java/src/main/java/org/msgpack/schema/SetSchema.java
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
//
|
||||||
|
// 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.schema;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.RandomAccess;
|
||||||
|
import java.io.IOException;
|
||||||
|
import org.msgpack.*;
|
||||||
|
|
||||||
|
public class SetSchema extends Schema implements IArraySchema {
|
||||||
|
private Schema elementSchema;
|
||||||
|
|
||||||
|
public SetSchema(Schema elementSchema) {
|
||||||
|
this.elementSchema = elementSchema;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getClassName() {
|
||||||
|
return "Set<"+elementSchema.getClassName()+">";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getExpression() {
|
||||||
|
return "(set "+elementSchema.getExpression()+")";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void pack(Packer pk, Object obj) throws IOException {
|
||||||
|
if(obj instanceof List) {
|
||||||
|
List<Object> d = (List<Object>)obj;
|
||||||
|
pk.packArray(d.size());
|
||||||
|
if(obj instanceof RandomAccess) {
|
||||||
|
for(int i=0; i < d.size(); ++i) {
|
||||||
|
elementSchema.pack(pk, d.get(i));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for(Object e : d) {
|
||||||
|
elementSchema.pack(pk, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if(obj instanceof Set) {
|
||||||
|
Set<Object> d = (Set<Object>)obj;
|
||||||
|
pk.packArray(d.size());
|
||||||
|
for(Object e : d) {
|
||||||
|
elementSchema.pack(pk, e);
|
||||||
|
}
|
||||||
|
} else if(obj == null) {
|
||||||
|
pk.packNil();
|
||||||
|
} else {
|
||||||
|
throw MessageTypeException.invalidConvert(obj, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static final <T> Set<T> convertSet(Object obj,
|
||||||
|
Schema elementSchema, Set<T> dest) throws MessageTypeException {
|
||||||
|
if(!(obj instanceof List)) {
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
|
List<Object> d = (List<Object>)obj;
|
||||||
|
if(dest == null) {
|
||||||
|
dest = new HashSet<T>(d.size());
|
||||||
|
}
|
||||||
|
if(obj instanceof RandomAccess) {
|
||||||
|
for(int i=0; i < d.size(); ++i) {
|
||||||
|
dest.add( (T)elementSchema.convert(d.get(i)) );
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for(Object e : d) {
|
||||||
|
dest.add( (T)elementSchema.convert(e) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object convert(Object obj) throws MessageTypeException {
|
||||||
|
return convertSet(obj, elementSchema, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Schema getElementSchema(int index) {
|
||||||
|
return elementSchema;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object createFromArray(Object[] obj) {
|
||||||
|
Set m = new HashSet(obj.length);
|
||||||
|
for(int i=0; i < obj.length; i++) {
|
||||||
|
m.add(obj[i]);
|
||||||
|
}
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -21,8 +21,11 @@ import java.io.IOException;
|
|||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
public class ShortSchema extends Schema {
|
public class ShortSchema extends Schema {
|
||||||
public ShortSchema() {
|
public ShortSchema() { }
|
||||||
super("Short");
|
|
||||||
|
@Override
|
||||||
|
public String getClassName() {
|
||||||
|
return "Short";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -33,27 +36,32 @@ public class ShortSchema extends Schema {
|
|||||||
@Override
|
@Override
|
||||||
public void pack(Packer pk, Object obj) throws IOException {
|
public void pack(Packer pk, Object obj) throws IOException {
|
||||||
if(obj instanceof Number) {
|
if(obj instanceof Number) {
|
||||||
pk.packShort( ((Number)obj).shortValue() );
|
int value = ((Number)obj).intValue();
|
||||||
|
if(value > Short.MAX_VALUE) {
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
|
pk.packShort((short)value);
|
||||||
} else if(obj == null) {
|
} else if(obj == null) {
|
||||||
pk.packNil();
|
pk.packNil();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
throw MessageTypeException.invalidConvert(obj, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final short convertShort(Object obj) throws MessageTypeException {
|
||||||
|
if(obj instanceof Number) {
|
||||||
|
int value = ((Number)obj).intValue();
|
||||||
|
if(value > Short.MAX_VALUE) {
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
|
return (short)value;
|
||||||
|
}
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object convert(Object obj) throws MessageTypeException {
|
public Object convert(Object obj) throws MessageTypeException {
|
||||||
if(obj instanceof Short) {
|
return convertShort(obj);
|
||||||
return obj;
|
|
||||||
|
|
||||||
} else if(obj instanceof Number) {
|
|
||||||
return ((Number)obj).shortValue();
|
|
||||||
|
|
||||||
} else {
|
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -68,21 +76,17 @@ public class ShortSchema extends Schema {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object createFromInt(int v) {
|
public Object createFromInt(int v) {
|
||||||
|
if(v > Short.MAX_VALUE) {
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
return (short)v;
|
return (short)v;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object createFromLong(long v) {
|
public Object createFromLong(long v) {
|
||||||
return (short)v;
|
if(v > Short.MAX_VALUE) {
|
||||||
|
throw new MessageTypeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromFloat(float v) {
|
|
||||||
return (short)v;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object createFromDouble(double v) {
|
|
||||||
return (short)v;
|
return (short)v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,61 +23,48 @@ import java.io.UnsupportedEncodingException;
|
|||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
public class StringSchema extends Schema {
|
public class StringSchema extends Schema {
|
||||||
public StringSchema() {
|
public StringSchema() { }
|
||||||
super("string");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getFullName() {
|
public String getClassName() {
|
||||||
return "String";
|
return "String";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getExpression() {
|
||||||
|
return "string";
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void pack(Packer pk, Object obj) throws IOException {
|
public void pack(Packer pk, Object obj) throws IOException {
|
||||||
// FIXME instanceof GenericObject
|
if(obj instanceof byte[]) {
|
||||||
if(obj instanceof String) {
|
byte[] b = (byte[])obj;
|
||||||
|
pk.packRaw(b.length);
|
||||||
|
pk.packRawBody(b);
|
||||||
|
} else if(obj instanceof String) {
|
||||||
try {
|
try {
|
||||||
byte[] d = ((String)obj).getBytes("UTF-8");
|
byte[] b = ((String)obj).getBytes("UTF-8");
|
||||||
pk.packRaw(d.length);
|
pk.packRaw(b.length);
|
||||||
pk.packRawBody(d);
|
pk.packRawBody(b);
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
throw new MessageTypeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if(obj instanceof byte[]) {
|
|
||||||
byte[] d = (byte[])obj;
|
|
||||||
pk.packRaw(d.length);
|
|
||||||
pk.packRawBody(d);
|
|
||||||
|
|
||||||
} else if(obj instanceof ByteBuffer) {
|
|
||||||
ByteBuffer d = (ByteBuffer)obj;
|
|
||||||
if(!d.hasArray()) {
|
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
|
||||||
}
|
|
||||||
pk.packRaw(d.capacity());
|
|
||||||
pk.packRawBody(d.array(), d.position(), d.capacity());
|
|
||||||
|
|
||||||
} else if(obj == null) {
|
} else if(obj == null) {
|
||||||
pk.packNil();
|
pk.packNil();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
throw MessageTypeException.invalidConvert(obj, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public static final String convertString(Object obj) throws MessageTypeException {
|
||||||
public Object convert(Object obj) throws MessageTypeException {
|
if(obj instanceof byte[]) {
|
||||||
// FIXME instanceof GenericObject
|
|
||||||
if(obj instanceof String) {
|
|
||||||
return obj;
|
|
||||||
|
|
||||||
} else if(obj instanceof byte[]) {
|
|
||||||
try {
|
try {
|
||||||
return new String((byte[])obj, "UTF-8");
|
return new String((byte[])obj, "UTF-8");
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
throw new MessageTypeException();
|
||||||
}
|
}
|
||||||
|
} else if(obj instanceof String) {
|
||||||
|
return (String)obj;
|
||||||
} else if(obj instanceof ByteBuffer) {
|
} else if(obj instanceof ByteBuffer) {
|
||||||
ByteBuffer d = (ByteBuffer)obj;
|
ByteBuffer d = (ByteBuffer)obj;
|
||||||
try {
|
try {
|
||||||
@ -91,12 +78,16 @@ public class StringSchema extends Schema {
|
|||||||
return new String(v, "UTF-8");
|
return new String(v, "UTF-8");
|
||||||
}
|
}
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new MessageTypeException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
@Override
|
||||||
throw MessageTypeException.invalidConvert(obj, this);
|
public Object convert(Object obj) throws MessageTypeException {
|
||||||
}
|
return convertString(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,3 +1,10 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
from _msgpack import *
|
from _msgpack import *
|
||||||
|
|
||||||
|
# alias for compatibility to simplejson/marshal/pickle.
|
||||||
|
load = unpack
|
||||||
|
loads = unpackb
|
||||||
|
|
||||||
|
dump = pack
|
||||||
|
dumps = packb
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ except ImportError:
|
|||||||
from distutils.command.build_ext import build_ext
|
from distutils.command.build_ext import build_ext
|
||||||
have_cython = False
|
have_cython = False
|
||||||
|
|
||||||
version = '0.1.3'
|
version = '0.1.4'
|
||||||
|
|
||||||
# take care of extension modules.
|
# take care of extension modules.
|
||||||
if have_cython:
|
if have_cython:
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
Gem::Specification.new do |s|
|
Gem::Specification.new do |s|
|
||||||
s.platform = Gem::Platform::RUBY
|
s.platform = Gem::Platform::RUBY
|
||||||
s.name = "msgpack"
|
s.name = "msgpack"
|
||||||
s.version = "0.4.1"
|
s.version = "0.4.2"
|
||||||
s.summary = "MessagePack, a binary-based efficient data interchange format."
|
s.summary = "MessagePack, a binary-based efficient data interchange format."
|
||||||
s.author = "FURUHASHI Sadayuki"
|
s.author = "FURUHASHI Sadayuki"
|
||||||
s.email = "frsyuki@users.sourceforge.jp"
|
s.email = "frsyuki@users.sourceforge.jp"
|
||||||
|
@ -203,6 +203,37 @@ class MessagePackTestPackUnpack < Test::Unit::TestCase
|
|||||||
# #check_map 5, (1<<32)-1 # memory error
|
# #check_map 5, (1<<32)-1 # memory error
|
||||||
# end
|
# end
|
||||||
|
|
||||||
|
it "buffer" do
|
||||||
|
str = "a"*32*1024*4
|
||||||
|
raw = str.to_msgpack
|
||||||
|
pac = MessagePack::Unpacker.new
|
||||||
|
|
||||||
|
len = 0
|
||||||
|
parsed = false
|
||||||
|
|
||||||
|
n = 655
|
||||||
|
time = raw.size / n
|
||||||
|
time += 1 unless raw.size % n == 0
|
||||||
|
off = 0
|
||||||
|
|
||||||
|
time.times do
|
||||||
|
assert(!parsed)
|
||||||
|
|
||||||
|
fe = raw[off, n]
|
||||||
|
assert(fe.length > 0)
|
||||||
|
off += fe.length
|
||||||
|
|
||||||
|
pac.feed fe
|
||||||
|
pac.each {|obj|
|
||||||
|
assert(!parsed)
|
||||||
|
assert_equal(obj, str)
|
||||||
|
parsed = true
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
assert(parsed)
|
||||||
|
end
|
||||||
|
|
||||||
it "gc mark" do
|
it "gc mark" do
|
||||||
obj = [{["a","b"]=>["c","d"]}, ["e","f"], "d"]
|
obj = [{["a","b"]=>["c","d"]}, ["e","f"], "d"]
|
||||||
num = 4
|
num = 4
|
||||||
|
116
ruby/unpack.c
116
ruby/unpack.c
@ -386,90 +386,70 @@ static VALUE MessagePack_Unpacker_stream_set(VALUE self, VALUE val)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#ifdef RUBY_VM
|
static void reserve_buffer(msgpack_unpack_t* mp, size_t require)
|
||||||
# ifndef STR_SHARED
|
|
||||||
# define STR_SHARED FL_USER2
|
|
||||||
# endif
|
|
||||||
# ifndef STR_NOEMBED
|
|
||||||
# define STR_NOEMBED FL_USER1
|
|
||||||
# endif
|
|
||||||
# ifndef STR_ASSOC
|
|
||||||
# define STR_ASSOC FL_USER3
|
|
||||||
# endif
|
|
||||||
# ifndef STR_NOCAPA_P
|
|
||||||
# define STR_NOCAPA_P(s) (FL_TEST(s,STR_NOEMBED) && FL_ANY(s,STR_SHARED|STR_ASSOC))
|
|
||||||
# endif
|
|
||||||
# define NEED_MORE_CAPA(s,size) (!STR_NOCAPA_P(s) && RSTRING(s)->as.heap.aux.capa < size)
|
|
||||||
#else
|
|
||||||
# ifndef STR_NOCAPA
|
|
||||||
# ifndef STR_ASSOC
|
|
||||||
# define STR_ASSOC FL_USER3
|
|
||||||
# endif
|
|
||||||
# ifndef ELTS_SHARED
|
|
||||||
# define ELTS_SHARED FL_USER2
|
|
||||||
# endif
|
|
||||||
# define STR_NOCAPA (ELTS_SHARED|STR_ASSOC)
|
|
||||||
# endif
|
|
||||||
# define NEED_MORE_CAPA(s,size) (!FL_TEST(s,STR_NOCAPA) && RSTRING(s)->aux.capa < size)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static void feed_buffer(msgpack_unpack_t* mp, const char* ptr, size_t len)
|
|
||||||
{
|
{
|
||||||
struct unpack_buffer* buffer = &mp->user.buffer;
|
struct unpack_buffer* buffer = &mp->user.buffer;
|
||||||
|
|
||||||
if(buffer->size == 0) {
|
if(buffer->size == 0) {
|
||||||
char* tmp = ALLOC_N(char, MSGPACK_UNPACKER_BUFFER_INIT_SIZE);
|
size_t nsize = MSGPACK_UNPACKER_BUFFER_INIT_SIZE;
|
||||||
|
while(nsize < require) {
|
||||||
|
nsize *= 2;
|
||||||
|
}
|
||||||
|
char* tmp = ALLOC_N(char, nsize);
|
||||||
buffer->ptr = tmp;
|
buffer->ptr = tmp;
|
||||||
buffer->free = MSGPACK_UNPACKER_BUFFER_INIT_SIZE;
|
buffer->free = nsize;
|
||||||
buffer->size = 0;
|
buffer->size = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
} else if(buffer->size <= mp->user.offset) {
|
if(buffer->size <= mp->user.offset) {
|
||||||
/* clear buffer and rewind offset */
|
/* clear buffer and rewind offset */
|
||||||
buffer->free += buffer->size;
|
buffer->free += buffer->size;
|
||||||
buffer->size = 0;
|
buffer->size = 0;
|
||||||
mp->user.offset = 0;
|
mp->user.offset = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(len <= buffer->free) {
|
if(require <= buffer->free) {
|
||||||
/* enough free space: just copy */
|
/* enough free space */
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t nsize = (buffer->size + buffer->free) * 2;
|
||||||
|
|
||||||
|
if(mp->user.offset <= buffer->size / 2) {
|
||||||
|
/* parsed less than half: realloc only */
|
||||||
|
while(nsize < buffer->size + require) {
|
||||||
|
nsize *= 2;
|
||||||
|
}
|
||||||
|
char* tmp = REALLOC_N(buffer->ptr, char, nsize);
|
||||||
|
buffer->free = nsize - buffer->size;
|
||||||
|
buffer->ptr = tmp;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
/* parsed more than half: realloc and move */
|
||||||
|
size_t not_parsed = buffer->size - mp->user.offset;
|
||||||
|
while(nsize < not_parsed + require) {
|
||||||
|
nsize *= 2;
|
||||||
|
}
|
||||||
|
char* tmp = REALLOC_N(buffer->ptr, char, nsize);
|
||||||
|
memcpy(tmp, tmp + mp->user.offset, not_parsed);
|
||||||
|
buffer->free = nsize - buffer->size;
|
||||||
|
buffer->size = not_parsed;
|
||||||
|
buffer->ptr = tmp;
|
||||||
|
mp->user.offset = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void feed_buffer(msgpack_unpack_t* mp, const char* ptr, size_t len)
|
||||||
|
{
|
||||||
|
struct unpack_buffer* buffer = &mp->user.buffer;
|
||||||
|
|
||||||
|
if(buffer->free < len) {
|
||||||
|
reserve_buffer(mp, len);
|
||||||
|
}
|
||||||
memcpy(buffer->ptr + buffer->size, ptr, len);
|
memcpy(buffer->ptr + buffer->size, ptr, len);
|
||||||
buffer->size += len;
|
buffer->size += len;
|
||||||
buffer->free -= len;
|
buffer->free -= len;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t csize = buffer->size + buffer->free;
|
|
||||||
|
|
||||||
if(mp->user.offset <= buffer->size / 2) {
|
|
||||||
/* parsed less than half: realloc and copy */
|
|
||||||
csize *= 2;
|
|
||||||
while(csize < buffer->size + len) {
|
|
||||||
csize *= 2;
|
|
||||||
}
|
|
||||||
char* tmp = REALLOC_N(buffer->ptr, char, csize);
|
|
||||||
memcpy(tmp + buffer->size, ptr, len);
|
|
||||||
buffer->ptr = tmp;
|
|
||||||
buffer->free = csize - buffer->size;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t not_parsed = buffer->size - mp->user.offset;
|
|
||||||
|
|
||||||
if(csize < not_parsed + len) {
|
|
||||||
/* more buffer size */
|
|
||||||
csize *= 2;
|
|
||||||
while(csize < not_parsed + len) {
|
|
||||||
csize *= 2;
|
|
||||||
}
|
|
||||||
char* tmp = REALLOC_N(buffer->ptr, char, csize);
|
|
||||||
buffer->ptr = tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(buffer->ptr+not_parsed, ptr, not_parsed);
|
|
||||||
buffer->size = not_parsed;
|
|
||||||
buffer->free = csize - buffer->size;
|
|
||||||
buffer->ptr = buffer->ptr;
|
|
||||||
mp->user.offset = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user