Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,35 @@
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.IOException;
import java.nio.ByteBuffer;

@JsonSerialize(using = MessagePackExtensionType.Serializer.class)
public class MessagePackExtensionType
{
private final int extType;
private final ByteBuffer byteBuffer;
private final byte type;
private final byte[] data;

public MessagePackExtensionType(int extType, ByteBuffer byteBuffer) {
this.extType = extType;
this.byteBuffer = byteBuffer.isReadOnly() ?
byteBuffer : byteBuffer.asReadOnlyBuffer();
public MessagePackExtensionType(byte type, byte[] data)
{
this.type = type;
this.data = data;
}

public int extType() {
return extType;
public byte getType()
{
return type;
}

public ByteBuffer byteBuffer() {
return byteBuffer;
public byte[] getData()
{
return data;
}

public static class Serializer extends JsonSerializer<MessagePackExtensionType> {
public static class Serializer extends JsonSerializer<MessagePackExtensionType>
{
@Override
public void serialize(MessagePackExtensionType value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
throws IOException, JsonProcessingException
{
if (gen instanceof MessagePackGenerator) {
MessagePackGenerator msgpackGenerator = (MessagePackGenerator)gen;
msgpackGenerator.writeExtendedType(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ else if (v instanceof Boolean) {
}
else if (v instanceof MessagePackExtensionType) {
MessagePackExtensionType extendedType = (MessagePackExtensionType) v;
ByteBuffer buf = extendedType.byteBuffer();
messagePacker.packExtensionTypeHeader((byte)extendedType.extType(), buf.remaining());
messagePacker.writePayload(buf);
byte[] extData = extendedType.getData();
messagePacker.packExtensionTypeHeader((byte)extendedType.getType(), extData.length);
messagePacker.writePayload(extData);
}
else {
throw new IllegalArgumentException(v.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@
import org.msgpack.core.buffer.ArrayBufferInput;
import org.msgpack.core.buffer.InputStreamBufferInput;
import org.msgpack.core.buffer.MessageBufferInput;
import org.msgpack.value.IntegerValue;
import org.msgpack.value.Value;
import org.msgpack.value.ValueFactory;
import org.msgpack.value.ValueType;
import org.msgpack.value.Variable;
import org.msgpack.value.*;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -398,7 +394,8 @@ public Object getEmbeddedObject()
return value.asBinaryValue().getByteArray();
}
else if (value.isExtensionValue()) {
return value.asExtensionValue();
ExtensionValue extensionValue = value.asExtensionValue();
return new MessagePackExtensionType(extensionValue.getType(), extensionValue.getData());
}
else {
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ public void testGeneratorShouldWriteObject()
hashMap.put("childArray", childArray);
// #10
byte[] hello = "hello".getBytes("UTF-8");
ByteBuffer buffer = ByteBuffer.wrap(hello);
hashMap.put("ext", new MessagePackExtensionType(17, buffer));
hashMap.put("ext", new MessagePackExtensionType((byte) 17, hello));

long bitmap = 0;
byte[] bytes = objectMapper.writeValueAsBytes(hashMap);
Expand Down Expand Up @@ -362,7 +361,7 @@ public void testWritePrimitiveObjectViaObjectMapper()
OutputStream out = new FileOutputStream(tempFile);

ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
objectMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
objectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
objectMapper.writeValue(out, 1);
objectMapper.writeValue(out, "two");
objectMapper.writeValue(out, 3.14);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ else if (k.equals("bool")) {
else if (k.equals("ext")) {
// #9
bitmap |= 1 << 10;
ExtensionValue extensionValue = (ExtensionValue) v;
assertEquals(0, extensionValue.getType());
assertArrayEquals(extPayload, extensionValue.getData());
MessagePackExtensionType extensionType = (MessagePackExtensionType) v;
assertEquals(0, extensionType.getType());
assertArrayEquals(extPayload, extensionType.getData());
}
}
assertEquals(0x7FF, bitmap);
Expand Down Expand Up @@ -276,9 +276,9 @@ else if (k.equals("child_map_age")) {
// #10
assertEquals(true, array.get(i++));
// #11
ExtensionValue extensionValue = (ExtensionValue) array.get(i++);
assertEquals(-1, extensionValue.getType());
assertArrayEquals(extPayload, extensionValue.getData());
MessagePackExtensionType extensionType = (MessagePackExtensionType) array.get(i++);
assertEquals(-1, extensionType.getType());
assertArrayEquals(extPayload, extensionType.getData());
}

@Test
Expand Down Expand Up @@ -479,7 +479,7 @@ public void testReadPrimitiveObjectViaObjectMapper()

FileInputStream in = new FileInputStream(tempFile);
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
objectMapper.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
assertEquals("foo", objectMapper.readValue(in, new TypeReference<String>() {}));
assertEquals(Long.MAX_VALUE, objectMapper.readValue(in, new TypeReference<Long>() {}));
assertEquals(3.14, objectMapper.readValue(in, new TypeReference<Double>() {}));
Expand Down