mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-04-01 01:16:52 +02: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 {
|
public Packer packString(String s) throws IOException {
|
||||||
byte[] b = ((String)s).getBytes("UTF-8");
|
byte[] b = ((String)s).getBytes("UTF-8");
|
||||||
packRaw(b.length);
|
packRaw(b.length);
|
||||||
|
@ -25,7 +25,7 @@ public class BigIntegerPacker implements MessagePacker {
|
|||||||
private BigIntegerPacker() { }
|
private BigIntegerPacker() { }
|
||||||
|
|
||||||
public void pack(Packer pk, Object target) throws IOException {
|
public void pack(Packer pk, Object target) throws IOException {
|
||||||
pk.pack((BigInteger)target);
|
pk.packBigInteger((BigInteger)target);
|
||||||
}
|
}
|
||||||
|
|
||||||
static public BigIntegerPacker getInstance() {
|
static public BigIntegerPacker getInstance() {
|
||||||
|
@ -24,7 +24,7 @@ public class BooleanPacker implements MessagePacker {
|
|||||||
private BooleanPacker() { }
|
private BooleanPacker() { }
|
||||||
|
|
||||||
public void pack(Packer pk, Object target) throws IOException {
|
public void pack(Packer pk, Object target) throws IOException {
|
||||||
pk.pack((Boolean)target);
|
pk.packBoolean((Boolean)target);
|
||||||
}
|
}
|
||||||
|
|
||||||
static public BooleanPacker getInstance() {
|
static public BooleanPacker getInstance() {
|
||||||
|
@ -24,7 +24,7 @@ public class ByteArrayPacker implements MessagePacker {
|
|||||||
private ByteArrayPacker() { }
|
private ByteArrayPacker() { }
|
||||||
|
|
||||||
public void pack(Packer pk, Object target) throws IOException {
|
public void pack(Packer pk, Object target) throws IOException {
|
||||||
pk.pack((byte[])target);
|
pk.packByteArray((byte[])target);
|
||||||
}
|
}
|
||||||
|
|
||||||
static public ByteArrayPacker getInstance() {
|
static public ByteArrayPacker getInstance() {
|
||||||
|
@ -24,7 +24,7 @@ public class BytePacker implements MessagePacker {
|
|||||||
private BytePacker() { }
|
private BytePacker() { }
|
||||||
|
|
||||||
public void pack(Packer pk, Object target) throws IOException {
|
public void pack(Packer pk, Object target) throws IOException {
|
||||||
pk.pack(((Byte)target).byteValue());
|
pk.packByte((Byte)target);
|
||||||
}
|
}
|
||||||
|
|
||||||
static public BytePacker getInstance() {
|
static public BytePacker getInstance() {
|
||||||
|
@ -24,7 +24,7 @@ public class DoublePacker implements MessagePacker {
|
|||||||
private DoublePacker() { }
|
private DoublePacker() { }
|
||||||
|
|
||||||
public void pack(Packer pk, Object target) throws IOException {
|
public void pack(Packer pk, Object target) throws IOException {
|
||||||
pk.pack(((Double)target).doubleValue());
|
pk.packDouble(((Double)target));
|
||||||
}
|
}
|
||||||
|
|
||||||
static public DoublePacker getInstance() {
|
static public DoublePacker getInstance() {
|
||||||
|
@ -24,7 +24,7 @@ public class FloatPacker implements MessagePacker {
|
|||||||
private FloatPacker() { }
|
private FloatPacker() { }
|
||||||
|
|
||||||
public void pack(Packer pk, Object target) throws IOException {
|
public void pack(Packer pk, Object target) throws IOException {
|
||||||
pk.pack(((Float)target).floatValue());
|
pk.packFloat((Float)target);
|
||||||
}
|
}
|
||||||
|
|
||||||
static public FloatPacker getInstance() {
|
static public FloatPacker getInstance() {
|
||||||
|
@ -24,7 +24,7 @@ public class IntegerPacker implements MessagePacker {
|
|||||||
private IntegerPacker() { }
|
private IntegerPacker() { }
|
||||||
|
|
||||||
public void pack(Packer pk, Object target) throws IOException {
|
public void pack(Packer pk, Object target) throws IOException {
|
||||||
pk.pack(((Integer)target).intValue());
|
pk.packInt((Integer)target);
|
||||||
}
|
}
|
||||||
|
|
||||||
static public IntegerPacker getInstance() {
|
static public IntegerPacker getInstance() {
|
||||||
|
@ -24,7 +24,7 @@ public class LongPacker implements MessagePacker {
|
|||||||
private LongPacker() { }
|
private LongPacker() { }
|
||||||
|
|
||||||
public void pack(Packer pk, Object target) throws IOException {
|
public void pack(Packer pk, Object target) throws IOException {
|
||||||
pk.pack(((Long)target).longValue());
|
pk.packLong((Long)target);
|
||||||
}
|
}
|
||||||
|
|
||||||
static public LongPacker getInstance() {
|
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() { }
|
private ShortPacker() { }
|
||||||
|
|
||||||
public void pack(Packer pk, Object target) throws IOException {
|
public void pack(Packer pk, Object target) throws IOException {
|
||||||
pk.pack(((Short)target).shortValue());
|
pk.packShort((Short)target);
|
||||||
}
|
}
|
||||||
|
|
||||||
static public ShortPacker getInstance() {
|
static public ShortPacker getInstance() {
|
||||||
|
@ -24,7 +24,7 @@ public class StringPacker implements MessagePacker {
|
|||||||
private StringPacker() { }
|
private StringPacker() { }
|
||||||
|
|
||||||
public void pack(Packer pk, Object target) throws IOException {
|
public void pack(Packer pk, Object target) throws IOException {
|
||||||
pk.pack((String)target);
|
pk.packString((String)target);
|
||||||
}
|
}
|
||||||
|
|
||||||
static public StringPacker getInstance() {
|
static public StringPacker getInstance() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user