1
0
mirror of https://github.com/msgpack/msgpack-c.git synced 2025-03-25 19:32:11 +01:00

java: move ByteBufferTemplate.java to org.msgpack.util.codegen package

This commit is contained in:
Muga Nishizawa 2010-11-06 11:31:40 +09:00
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;

@ -2,7 +2,6 @@ package org.msgpack.template;
import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -280,73 +279,6 @@ public class TestPackConvert extends TestCase {
assertEquals(src, dst);
}
@Test
public void testByteBuffer() throws Exception {
_testByteBuffer(ByteBuffer.wrap(("".getBytes())));
_testByteBuffer(ByteBuffer.wrap(("a".getBytes())));
_testByteBuffer(ByteBuffer.wrap(("ab".getBytes())));
_testByteBuffer(ByteBuffer.wrap(("abc".getBytes())));
// small size string
for (int i = 0; i < 100; i++) {
StringBuilder sb = new StringBuilder();
int len = (int) Math.random() % 31 + 1;
for (int j = 0; j < len; j++) {
sb.append('a' + ((int) Math.random()) & 26);
}
_testByteBuffer(ByteBuffer.wrap(sb.toString().getBytes()));
}
// medium size string
for (int i = 0; i < 100; i++) {
StringBuilder sb = new StringBuilder();
int len = (int) Math.random() % 100 + (1 << 15);
for (int j = 0; j < len; j++) {
sb.append('a' + ((int) Math.random()) & 26);
}
_testByteBuffer(ByteBuffer.wrap(sb.toString().getBytes()));
}
// large size string
for (int i = 0; i < 10; i++) {
StringBuilder sb = new StringBuilder();
int len = (int) Math.random() % 100 + (1 << 31);
for (int j = 0; j < len; j++) {
sb.append('a' + ((int) Math.random()) & 26);
}
_testByteBuffer(ByteBuffer.wrap(sb.toString().getBytes()));
}
}
static void _testByteBuffer(ByteBuffer src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = ByteBufferTemplate.getInstance();
ByteBuffer dst = (ByteBuffer) tmpl.convert(obj);
assertEquals(src, dst);
}
@Test
public void testNullByteBuffer() throws Exception {
ByteBuffer src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = ByteBufferTemplate.getInstance();
ByteBuffer dst = null;
try {
dst = (ByteBuffer) tmpl.convert(obj);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(ByteBufferTemplate.getInstance());
dst = (ByteBuffer) tmpl.convert(obj);
assertEquals(src, dst);
}
@Test
public void testString() throws Exception {
_testString("");

@ -3,7 +3,6 @@ package org.msgpack.template;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -274,76 +273,6 @@ public class TestPackUnpack extends TestCase {
assertEquals(src, dst);
}
@Test
public void testByteBuffer() throws Exception {
_testByteBuffer(ByteBuffer.wrap("".getBytes()));
_testByteBuffer(ByteBuffer.wrap("a".getBytes()));
_testByteBuffer(ByteBuffer.wrap("ab".getBytes()));
_testByteBuffer(ByteBuffer.wrap("abc".getBytes()));
// small size string
for (int i = 0; i < 100; i++) {
StringBuilder sb = new StringBuilder();
int len = (int) Math.random() % 31 + 1;
for (int j = 0; j < len; j++) {
sb.append('a' + ((int) Math.random()) & 26);
}
_testByteBuffer(ByteBuffer.wrap(sb.toString().getBytes()));
}
// medium size string
for (int i = 0; i < 100; i++) {
StringBuilder sb = new StringBuilder();
int len = (int) Math.random() % 100 + (1 << 15);
for (int j = 0; j < len; j++) {
sb.append('a' + ((int) Math.random()) & 26);
}
_testByteBuffer(ByteBuffer.wrap(sb.toString().getBytes()));
}
// large size string
for (int i = 0; i < 10; i++) {
StringBuilder sb = new StringBuilder();
int len = (int) Math.random() % 100 + (1 << 31);
for (int j = 0; j < len; j++) {
sb.append('a' + ((int) Math.random()) & 26);
}
_testByteBuffer(ByteBuffer.wrap(sb.toString().getBytes()));
}
}
static void _testByteBuffer(ByteBuffer src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Template tmpl = ByteBufferTemplate.getInstance();
ByteBuffer dst = (ByteBuffer) tmpl.unpack(new Unpacker(in));
assertEquals(src, dst);
}
@Test
public void testNullByteBuffer() throws Exception {
ByteBuffer src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
byte[] bytes = out.toByteArray();
Template tmpl = null;
Unpacker unpacker = new Unpacker();
ByteBuffer dst = null;
try {
tmpl = ByteBufferTemplate.getInstance();
unpacker.wrap(bytes);
dst = (ByteBuffer) tmpl.unpack(unpacker);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new OptionalTemplate(ByteBufferTemplate.getInstance());
dst = (ByteBuffer) tmpl.unpack(unpacker);
assertEquals(src, dst);
}
@Test
public void testString() throws Exception {
_testString("");