mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-21 15:10:01 +01:00
java: adds standard packer classes
This commit is contained in:
parent
a3b1ef9527
commit
7c76f07384
@ -28,6 +28,7 @@ import java.math.BigInteger;
|
||||
import org.msgpack.annotation.MessagePackDelegate;
|
||||
import org.msgpack.annotation.MessagePackMessage;
|
||||
import org.msgpack.annotation.MessagePackOrdinalEnum;
|
||||
import org.msgpack.packer.*;
|
||||
|
||||
/**
|
||||
* Packer enables you to serialize objects into OutputStream.
|
||||
@ -46,6 +47,22 @@ import org.msgpack.annotation.MessagePackOrdinalEnum;
|
||||
* You can serialize objects that implements {@link MessagePackable} interface.
|
||||
*/
|
||||
public class Packer {
|
||||
static {
|
||||
// final classes
|
||||
BooleanPacker.getInstance();
|
||||
ByteArrayPacker.getInstance();
|
||||
BytePacker.getInstance();
|
||||
DoublePacker.getInstance();
|
||||
FloatPacker.getInstance();
|
||||
IntegerPacker.getInstance();
|
||||
LongPacker.getInstance();
|
||||
ShortPacker.getInstance();
|
||||
StringPacker.getInstance();
|
||||
//BigIntegerPacker.getInstance(); // BigInteger is not final
|
||||
}
|
||||
|
||||
public static void load() { }
|
||||
|
||||
protected byte[] castBytes = new byte[9];
|
||||
protected ByteBuffer castBuffer = ByteBuffer.wrap(castBytes);
|
||||
protected OutputStream out;
|
||||
@ -434,17 +451,17 @@ public class Packer {
|
||||
public Packer pack(Object o) throws IOException {
|
||||
if(o == null) {
|
||||
return packNil();
|
||||
} else if(o instanceof String) {
|
||||
byte[] b = ((String)o).getBytes("UTF-8");
|
||||
packRaw(b.length);
|
||||
return packRawBody(b);
|
||||
//} else if(o instanceof String) {
|
||||
// byte[] b = ((String)o).getBytes("UTF-8");
|
||||
// packRaw(b.length);
|
||||
// return packRawBody(b);
|
||||
} else if(o instanceof MessagePackable) {
|
||||
((MessagePackable)o).messagePack(this);
|
||||
return this;
|
||||
} else if(o instanceof byte[]) {
|
||||
byte[] b = (byte[])o;
|
||||
packRaw(b.length);
|
||||
return packRawBody(b);
|
||||
//} else if(o instanceof byte[]) {
|
||||
// byte[] b = (byte[])o;
|
||||
// packRaw(b.length);
|
||||
// return packRawBody(b);
|
||||
} else if(o instanceof List) {
|
||||
List<Object> l = (List<Object>)o;
|
||||
packArray(l.size());
|
||||
@ -463,27 +480,27 @@ public class Packer {
|
||||
pack(e.getValue());
|
||||
}
|
||||
return this;
|
||||
} else if(o instanceof Boolean) {
|
||||
if((Boolean)o) {
|
||||
return packTrue();
|
||||
} else {
|
||||
return packFalse();
|
||||
}
|
||||
} else if(o instanceof Integer) {
|
||||
return packInt((Integer)o);
|
||||
} else if(o instanceof Long) {
|
||||
return packLong((Long)o);
|
||||
} else if(o instanceof Short) {
|
||||
return packShort((Short)o);
|
||||
} else if(o instanceof Byte) {
|
||||
return packByte((Byte)o);
|
||||
} else if(o instanceof Float) {
|
||||
return packFloat((Float)o);
|
||||
} else if(o instanceof Double) {
|
||||
return packDouble((Double)o);
|
||||
//} else if(o instanceof Boolean) {
|
||||
// if((Boolean)o) {
|
||||
// return packTrue();
|
||||
// } else {
|
||||
// return packFalse();
|
||||
// }
|
||||
//} else if(o instanceof Integer) {
|
||||
// return packInt((Integer)o);
|
||||
//} else if(o instanceof Long) {
|
||||
// return packLong((Long)o);
|
||||
//} else if(o instanceof Short) {
|
||||
// return packShort((Short)o);
|
||||
//} else if(o instanceof Byte) {
|
||||
// return packByte((Byte)o);
|
||||
//} else if(o instanceof Float) {
|
||||
// return packFloat((Float)o);
|
||||
//} else if(o instanceof Double) {
|
||||
// return packDouble((Double)o);
|
||||
} else if(o instanceof BigInteger) {
|
||||
return packBigInteger((BigInteger)o);
|
||||
}
|
||||
}
|
||||
|
||||
Class<?> klass = o.getClass();
|
||||
MessagePacker packer = CustomPacker.get(klass);
|
||||
@ -506,5 +523,5 @@ public class Packer {
|
||||
}
|
||||
throw new MessageTypeException("unknown object "+o+" ("+o.getClass()+")");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -588,10 +588,11 @@ public class Unpacker implements Iterable<MessagePackObject> {
|
||||
}
|
||||
|
||||
MessageUnpacker unpacker = CustomUnpacker.get(klass);
|
||||
|
||||
if(unpacker != null) {
|
||||
return unpacker.unpack(this);
|
||||
}
|
||||
|
||||
|
||||
Template tmpl = null;
|
||||
if (CustomMessage.isAnnotated(klass, MessagePackMessage.class)) {
|
||||
tmpl = ReflectionTemplate.create(klass);
|
||||
|
41
java/src/main/java/org/msgpack/packer/BigIntegerPacker.java
Normal file
41
java/src/main/java/org/msgpack/packer/BigIntegerPacker.java
Normal file
@ -0,0 +1,41 @@
|
||||
//
|
||||
// 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.packer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class BigIntegerPacker implements MessagePacker {
|
||||
private BigIntegerPacker() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
pk.pack((BigInteger)target);
|
||||
}
|
||||
|
||||
static public BigIntegerPacker getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final BigIntegerPacker instance = new BigIntegerPacker();
|
||||
|
||||
static {
|
||||
CustomMessage.registerPacker(BigInteger.class, instance);
|
||||
}
|
||||
}
|
||||
|
40
java/src/main/java/org/msgpack/packer/BooleanPacker.java
Normal file
40
java/src/main/java/org/msgpack/packer/BooleanPacker.java
Normal file
@ -0,0 +1,40 @@
|
||||
//
|
||||
// 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.packer;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class BooleanPacker implements MessagePacker {
|
||||
private BooleanPacker() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
pk.pack((Boolean)target);
|
||||
}
|
||||
|
||||
static public BooleanPacker getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final BooleanPacker instance = new BooleanPacker();
|
||||
|
||||
static {
|
||||
CustomMessage.registerPacker(Boolean.class, instance);
|
||||
}
|
||||
}
|
||||
|
40
java/src/main/java/org/msgpack/packer/ByteArrayPacker.java
Normal file
40
java/src/main/java/org/msgpack/packer/ByteArrayPacker.java
Normal file
@ -0,0 +1,40 @@
|
||||
//
|
||||
// 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.packer;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class ByteArrayPacker implements MessagePacker {
|
||||
private ByteArrayPacker() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
pk.pack((byte[])target);
|
||||
}
|
||||
|
||||
static public ByteArrayPacker getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final ByteArrayPacker instance = new ByteArrayPacker();
|
||||
|
||||
static {
|
||||
CustomMessage.registerPacker(byte[].class, instance);
|
||||
}
|
||||
}
|
||||
|
40
java/src/main/java/org/msgpack/packer/BytePacker.java
Normal file
40
java/src/main/java/org/msgpack/packer/BytePacker.java
Normal file
@ -0,0 +1,40 @@
|
||||
//
|
||||
// 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.packer;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class BytePacker implements MessagePacker {
|
||||
private BytePacker() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
pk.pack(((Byte)target).byteValue());
|
||||
}
|
||||
|
||||
static public BytePacker getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final BytePacker instance = new BytePacker();
|
||||
|
||||
static {
|
||||
CustomMessage.registerPacker(Byte.class, instance);
|
||||
}
|
||||
}
|
||||
|
40
java/src/main/java/org/msgpack/packer/DoublePacker.java
Normal file
40
java/src/main/java/org/msgpack/packer/DoublePacker.java
Normal file
@ -0,0 +1,40 @@
|
||||
//
|
||||
// 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.packer;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class DoublePacker implements MessagePacker {
|
||||
private DoublePacker() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
pk.pack(((Double)target).doubleValue());
|
||||
}
|
||||
|
||||
static public DoublePacker getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final DoublePacker instance = new DoublePacker();
|
||||
|
||||
static {
|
||||
CustomMessage.registerPacker(Double.class, instance);
|
||||
}
|
||||
}
|
||||
|
40
java/src/main/java/org/msgpack/packer/FloatPacker.java
Normal file
40
java/src/main/java/org/msgpack/packer/FloatPacker.java
Normal file
@ -0,0 +1,40 @@
|
||||
//
|
||||
// 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.packer;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class FloatPacker implements MessagePacker {
|
||||
private FloatPacker() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
pk.pack(((Float)target).floatValue());
|
||||
}
|
||||
|
||||
static public FloatPacker getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final FloatPacker instance = new FloatPacker();
|
||||
|
||||
static {
|
||||
CustomMessage.registerPacker(Float.class, instance);
|
||||
}
|
||||
}
|
||||
|
40
java/src/main/java/org/msgpack/packer/IntegerPacker.java
Normal file
40
java/src/main/java/org/msgpack/packer/IntegerPacker.java
Normal file
@ -0,0 +1,40 @@
|
||||
//
|
||||
// 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.packer;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class IntegerPacker implements MessagePacker {
|
||||
private IntegerPacker() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
pk.pack(((Integer)target).intValue());
|
||||
}
|
||||
|
||||
static public IntegerPacker getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final IntegerPacker instance = new IntegerPacker();
|
||||
|
||||
static {
|
||||
CustomMessage.registerPacker(Integer.class, instance);
|
||||
}
|
||||
}
|
||||
|
40
java/src/main/java/org/msgpack/packer/LongPacker.java
Normal file
40
java/src/main/java/org/msgpack/packer/LongPacker.java
Normal file
@ -0,0 +1,40 @@
|
||||
//
|
||||
// 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.packer;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class LongPacker implements MessagePacker {
|
||||
private LongPacker() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
pk.pack(((Long)target).longValue());
|
||||
}
|
||||
|
||||
static public LongPacker getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final LongPacker instance = new LongPacker();
|
||||
|
||||
static {
|
||||
CustomMessage.registerPacker(Long.class, instance);
|
||||
}
|
||||
}
|
||||
|
40
java/src/main/java/org/msgpack/packer/ShortPacker.java
Normal file
40
java/src/main/java/org/msgpack/packer/ShortPacker.java
Normal file
@ -0,0 +1,40 @@
|
||||
//
|
||||
// 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.packer;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class ShortPacker implements MessagePacker {
|
||||
private ShortPacker() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
pk.pack(((Short)target).shortValue());
|
||||
}
|
||||
|
||||
static public ShortPacker getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final ShortPacker instance = new ShortPacker();
|
||||
|
||||
static {
|
||||
CustomMessage.registerPacker(Short.class, instance);
|
||||
}
|
||||
}
|
||||
|
40
java/src/main/java/org/msgpack/packer/StringPacker.java
Normal file
40
java/src/main/java/org/msgpack/packer/StringPacker.java
Normal file
@ -0,0 +1,40 @@
|
||||
//
|
||||
// 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.packer;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class StringPacker implements MessagePacker {
|
||||
private StringPacker() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
pk.pack((String)target);
|
||||
}
|
||||
|
||||
static public StringPacker getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final StringPacker instance = new StringPacker();
|
||||
|
||||
static {
|
||||
CustomMessage.registerPacker(String.class, instance);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user