mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-09 19:30:21 +00:00
java: move ByteBufferTemplate.java to org.msgpack.util.codegen package
This commit is contained in:
parent
a078d2360c
commit
e3553b87fe
6 changed files with 17 additions and 170 deletions
|
|
@ -331,23 +331,6 @@ 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);
|
||||
|
|
@ -421,11 +404,6 @@ 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,11 +547,6 @@ 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.
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package org.msgpack.template;
|
||||
package org.msgpack.util.codegen;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
|
@ -15,11 +15,13 @@ public class ByteBufferTemplate implements Template {
|
|||
}
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
pk.packByteBuffer((ByteBuffer)target);
|
||||
byte[] bytes = byteBufferToByteArray((ByteBuffer) target);
|
||||
pk.packByteArray(bytes);
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
return pac.unpackByteBuffer();
|
||||
byte[] bytes = pac.unpackByteArray();
|
||||
return ByteBuffer.wrap(bytes);
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
|
|
@ -27,6 +29,18 @@ public class ByteBufferTemplate implements Template {
|
|||
return ByteBuffer.wrap(b);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
static public ByteBufferTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
|
@ -53,7 +53,6 @@ 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;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue