mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-29 21:22:06 +01:00
java: adds OptionalPacker
This commit is contained in:
parent
86043fd87e
commit
71ae75a5bf
@ -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);
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
38
java/src/main/java/org/msgpack/packer/OptionalPacker.java
Normal file
38
java/src/main/java/org/msgpack/packer/OptionalPacker.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user