mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-08 18:59:59 +00:00
java: add temporal implementation of ByteBufferTemplate.java
This commit is contained in:
parent
2d3abf8e6e
commit
a078d2360c
8 changed files with 279 additions and 0 deletions
|
|
@ -331,6 +331,23 @@ public class Packer {
|
|||
return packRawBody(b, off, length);
|
||||
}
|
||||
|
||||
public Packer packByteBuffer(ByteBuffer bb) throws IOException {
|
||||
byte[] bytes = byteBufferToByteArray(bb);
|
||||
return packByteArray(bytes);
|
||||
}
|
||||
|
||||
private static byte[] byteBufferToByteArray(ByteBuffer b) {
|
||||
if (b.hasArray() && b.position() == 0 && b.arrayOffset() == 0
|
||||
&& b.remaining() == b.capacity()) {
|
||||
return b.array();
|
||||
} else {
|
||||
int len = b.remaining();
|
||||
byte[] ret = new byte[len];
|
||||
System.arraycopy(b.array(), b.arrayOffset() + b.position(), ret, 0, len);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
public Packer packString(String s) throws IOException {
|
||||
byte[] b = ((String)s).getBytes("UTF-8");
|
||||
packRaw(b.length);
|
||||
|
|
@ -404,6 +421,11 @@ public class Packer {
|
|||
return packBigInteger(o);
|
||||
}
|
||||
|
||||
public Packer pack(ByteBuffer o) throws IOException {
|
||||
if (o == null) { return packNil(); }
|
||||
return packByteBuffer(o);
|
||||
}
|
||||
|
||||
public Packer pack(Float o) throws IOException {
|
||||
if(o == null) { return packNil(); }
|
||||
return packFloat(o);
|
||||
|
|
|
|||
|
|
@ -547,6 +547,11 @@ public class Unpacker implements Iterable<MessagePackObject> {
|
|||
return impl.unpackByteArray();
|
||||
}
|
||||
|
||||
public ByteBuffer unpackByteBuffer() throws IOException {
|
||||
byte[] bytes = impl.unpackByteArray();
|
||||
return ByteBuffer.wrap(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets one {@code String} value from the buffer.
|
||||
* This method calls {@link fill()} method if needed.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,39 @@
|
|||
package org.msgpack.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.msgpack.CustomMessage;
|
||||
import org.msgpack.MessagePackObject;
|
||||
import org.msgpack.MessageTypeException;
|
||||
import org.msgpack.Packer;
|
||||
import org.msgpack.Template;
|
||||
import org.msgpack.Unpacker;
|
||||
|
||||
public class ByteBufferTemplate implements Template {
|
||||
private ByteBufferTemplate() {
|
||||
}
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
pk.packByteBuffer((ByteBuffer)target);
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
return pac.unpackByteBuffer();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
byte[] b = from.asByteArray();
|
||||
return ByteBuffer.wrap(b);
|
||||
}
|
||||
|
||||
static public ByteBufferTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final ByteBufferTemplate instance = new ByteBufferTemplate();
|
||||
|
||||
static {
|
||||
CustomMessage.register(ByteBuffer.class, instance);
|
||||
}
|
||||
}
|
||||
|
|
@ -24,6 +24,7 @@ import java.lang.reflect.Method;
|
|||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
|
@ -52,6 +53,7 @@ import org.msgpack.Unpacker;
|
|||
import org.msgpack.annotation.MessagePackDelegate;
|
||||
import org.msgpack.annotation.MessagePackMessage;
|
||||
import org.msgpack.annotation.MessagePackOrdinalEnum;
|
||||
import org.msgpack.template.ByteBufferTemplate;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
|
@ -317,6 +319,8 @@ public class DynamicCodeGenBase implements Constants {
|
|||
return Templates.tString();
|
||||
} else if (c.equals(BigInteger.class)) {
|
||||
return Templates.tBigInteger();
|
||||
} else if (c.equals(ByteBuffer.class)) {// FIXME
|
||||
return ByteBufferTemplate.getInstance();
|
||||
} else if (CustomConverter.isRegistered(c)) {// FIXME
|
||||
return (Template) CustomConverter.get(c);
|
||||
} else if (CustomMessage.isAnnotated(c, MessagePackMessage.class)) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue