java: adds OptionalPacker

This commit is contained in:
frsyuki 2010-10-22 16:18:07 +09:00
parent 86043fd87e
commit 71ae75a5bf
12 changed files with 58 additions and 10 deletions

View File

@ -331,6 +331,16 @@ public class Packer {
}
public Packer packByteArray(byte[] b) throws IOException {
packRaw(b.length);
return packRawBody(b, 0, b.length);
}
public Packer packByteArray(byte[] b, int off, int length) throws IOException {
packRaw(length);
return packRawBody(b, off, length);
}
public Packer packString(String s) throws IOException {
byte[] b = ((String)s).getBytes("UTF-8");
packRaw(b.length);

View File

@ -25,7 +25,7 @@ public class BigIntegerPacker implements MessagePacker {
private BigIntegerPacker() { }
public void pack(Packer pk, Object target) throws IOException {
pk.pack((BigInteger)target);
pk.packBigInteger((BigInteger)target);
}
static public BigIntegerPacker getInstance() {

View File

@ -24,7 +24,7 @@ public class BooleanPacker implements MessagePacker {
private BooleanPacker() { }
public void pack(Packer pk, Object target) throws IOException {
pk.pack((Boolean)target);
pk.packBoolean((Boolean)target);
}
static public BooleanPacker getInstance() {

View File

@ -24,7 +24,7 @@ public class ByteArrayPacker implements MessagePacker {
private ByteArrayPacker() { }
public void pack(Packer pk, Object target) throws IOException {
pk.pack((byte[])target);
pk.packByteArray((byte[])target);
}
static public ByteArrayPacker getInstance() {

View File

@ -24,7 +24,7 @@ public class BytePacker implements MessagePacker {
private BytePacker() { }
public void pack(Packer pk, Object target) throws IOException {
pk.pack(((Byte)target).byteValue());
pk.packByte((Byte)target);
}
static public BytePacker getInstance() {

View File

@ -24,7 +24,7 @@ public class DoublePacker implements MessagePacker {
private DoublePacker() { }
public void pack(Packer pk, Object target) throws IOException {
pk.pack(((Double)target).doubleValue());
pk.packDouble(((Double)target));
}
static public DoublePacker getInstance() {

View File

@ -24,7 +24,7 @@ public class FloatPacker implements MessagePacker {
private FloatPacker() { }
public void pack(Packer pk, Object target) throws IOException {
pk.pack(((Float)target).floatValue());
pk.packFloat((Float)target);
}
static public FloatPacker getInstance() {

View File

@ -24,7 +24,7 @@ public class IntegerPacker implements MessagePacker {
private IntegerPacker() { }
public void pack(Packer pk, Object target) throws IOException {
pk.pack(((Integer)target).intValue());
pk.packInt((Integer)target);
}
static public IntegerPacker getInstance() {

View File

@ -24,7 +24,7 @@ public class LongPacker implements MessagePacker {
private LongPacker() { }
public void pack(Packer pk, Object target) throws IOException {
pk.pack(((Long)target).longValue());
pk.packLong((Long)target);
}
static public LongPacker getInstance() {

View File

@ -0,0 +1,38 @@
//
// 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 OptionalPacker implements MessagePacker {
private MessagePacker elementPacker;
public OptionalPacker(MessagePacker elementPacker) {
this.elementPacker = elementPacker;
}
public void pack(Packer pk, Object target) throws IOException {
if(target == null) {
pk.packNil();
} else {
elementPacker.pack(pk, target);
}
}
}

View File

@ -24,7 +24,7 @@ public class ShortPacker implements MessagePacker {
private ShortPacker() { }
public void pack(Packer pk, Object target) throws IOException {
pk.pack(((Short)target).shortValue());
pk.packShort((Short)target);
}
static public ShortPacker getInstance() {

View File

@ -24,7 +24,7 @@ public class StringPacker implements MessagePacker {
private StringPacker() { }
public void pack(Packer pk, Object target) throws IOException {
pk.pack((String)target);
pk.packString((String)target);
}
static public StringPacker getInstance() {