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 @@ -350,11 +350,16 @@ else if (rootStackItem instanceof StackItemForArray) {
throw new IllegalStateException("Unexpected rootStackItem: " + rootStackItem);
}
rootStackItem = null;
MessagePacker messagePacker = getMessagePacker();
messagePacker.flush();
flushMessagePacker();
}
}

private void flushMessagePacker() throws IOException
{
MessagePacker messagePacker = getMessagePacker();
messagePacker.flush();
}

@Override
protected void _releaseBuffers() {

Expand Down Expand Up @@ -395,11 +400,19 @@ private void addKeyToStackTop(String key) {
getStackTop().addKey(key);
}

private void addValueToStackTop(Object value) {
getStackTop().addValue(value);
private void addValueToStackTop(Object value) throws IOException
{
if (stack.isEmpty()) {
packValue(value);
flushMessagePacker();
}
else {
getStackTop().addValue(value);
}
}

private void popStackAndStoreTheItemAsValue() {
private void popStackAndStoreTheItemAsValue() throws IOException
{
StackItem child = stack.pop();
if (stack.size() > 0) {
addValueToStackTop(child);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -175,4 +178,28 @@ public static class ChangingPropertyNamesPojo {
public void setTheName(String n) { _name = n; }
}

protected interface FileSetup {
void setup(File f) throws Exception;
}

protected File createTempFile() throws Exception
{
return createTempFile(null);
}

protected File createTempFile(FileSetup fileSetup) throws Exception
{
File tempFile = File.createTempFile("test", "msgpack");
tempFile.deleteOnExit();
if (fileSetup != null) {
fileSetup.setup(tempFile);
}
return tempFile;
}

protected OutputStream createTempFileOutputStream() throws IOException {
File tempFile = File.createTempFile("test", "msgpack");
tempFile.deleteOnExit();
return new FileOutputStream(tempFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.*;

Expand Down Expand Up @@ -197,10 +198,9 @@ else if (key.equals("num")) {
}

@Test
public void testMessagePackGeneratorDirectly() throws IOException {
public void testMessagePackGeneratorDirectly() throws Exception {
MessagePackFactory messagePackFactory = new MessagePackFactory();
File tempFile = File.createTempFile("msgpackTest", "msgpack");
tempFile.deleteOnExit();
File tempFile = createTempFile();

JsonGenerator generator = messagePackFactory.createGenerator(tempFile, JsonEncoding.UTF8);
assertTrue(generator instanceof MessagePackGenerator);
Expand All @@ -222,6 +222,27 @@ public void testMessagePackGeneratorDirectly() throws IOException {
assertFalse(unpacker.hasNext());
}

@Test
public void testWritePrimitives() throws Exception {
MessagePackFactory messagePackFactory = new MessagePackFactory();
File tempFile = createTempFile();

JsonGenerator generator = messagePackFactory.createGenerator(tempFile, JsonEncoding.UTF8);
assertTrue(generator instanceof MessagePackGenerator);
generator.writeNumber(0);
generator.writeString("one");
generator.writeNumber(2.0f);
generator.flush();
generator.close();

FileInputStream fileInputStream = new FileInputStream(tempFile);
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(fileInputStream);
assertEquals(0, unpacker.unpackInt());
assertEquals("one", unpacker.unpackString());
assertEquals(2.0f, unpacker.unpackFloat(), 0.001f);
assertFalse(unpacker.hasNext());
}

@Test
public void testBigDecimal() throws IOException {
ObjectMapper mapper = new ObjectMapper(new MessagePackFactory());
Expand Down Expand Up @@ -267,9 +288,7 @@ public void testBigDecimal() throws IOException {

@Test(expected = IOException.class)
public void testEnableFeatureAutoCloseTarget() throws IOException {
File tempFile = File.createTempFile("test", "msgpack");
tempFile.deleteOnExit();
FileOutputStream out = new FileOutputStream(tempFile);
OutputStream out = createTempFileOutputStream();
MessagePackFactory messagePackFactory = new MessagePackFactory();
ObjectMapper objectMapper = new ObjectMapper(messagePackFactory);
List<Integer> integers = Arrays.asList(1);
Expand All @@ -278,10 +297,9 @@ public void testEnableFeatureAutoCloseTarget() throws IOException {
}

@Test
public void testDisableFeatureAutoCloseTarget() throws IOException {
File tempFile = File.createTempFile("test", "msgpack");
tempFile.deleteOnExit();
FileOutputStream out = new FileOutputStream(tempFile);
public void testDisableFeatureAutoCloseTarget() throws Exception {
File tempFile = createTempFile();
OutputStream out = new FileOutputStream(tempFile);
MessagePackFactory messagePackFactory = new MessagePackFactory();
ObjectMapper objectMapper = new ObjectMapper(messagePackFactory);
objectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
Expand All @@ -296,4 +314,26 @@ public void testDisableFeatureAutoCloseTarget() throws IOException {
assertEquals(1, unpacker.unpackArrayHeader());
assertEquals(1, unpacker.unpackInt());
}

@Test
public void testWritePrimitiveObjectViaObjectMapper() throws Exception {
File tempFile = createTempFile();
OutputStream out = new FileOutputStream(tempFile);

ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
objectMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
objectMapper.writeValue(out, 1);
objectMapper.writeValue(out, "two");
objectMapper.writeValue(out, 3.14);
objectMapper.writeValue(out, Arrays.asList(4));
objectMapper.writeValue(out, 5L);

MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(new FileInputStream(tempFile));
assertEquals(1, unpacker.unpackInt());
assertEquals("two", unpacker.unpackString());
assertEquals(3.14, unpacker.unpackFloat(), 0.0001);
assertEquals(1, unpacker.unpackArrayHeader());
assertEquals(4, unpacker.unpackInt());
assertEquals(5, unpacker.unpackLong());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,36 @@ public void testMessagePackParserDirectly() throws IOException {
parser.close(); // Intentional
}

@Test
public void testReadPrimitives() throws Exception
{
MessagePackFactory messagePackFactory = new MessagePackFactory();
File tempFile = createTempFile();

FileOutputStream out = new FileOutputStream(tempFile);
MessagePacker packer = MessagePack.newDefaultPacker(out);
packer.packString("foo");
packer.packDouble(3.14);
packer.packLong(Long.MAX_VALUE);
byte[] bytes = {0x00, 0x11, 0x22};
packer.packBinaryHeader(bytes.length);
packer.writePayload(bytes);
packer.close();

JsonParser parser = messagePackFactory.createParser(new FileInputStream(tempFile));
assertEquals(JsonToken.VALUE_STRING, parser.nextToken());
assertEquals("foo", parser.getText());
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, parser.nextToken());
assertEquals(3.14, parser.getDoubleValue(), 0.0001);
assertEquals(JsonToken.VALUE_NUMBER_INT, parser.nextToken());
assertEquals(Long.MAX_VALUE, parser.getLongValue());
assertEquals(JsonToken.VALUE_EMBEDDED_OBJECT, parser.nextToken());
assertEquals(bytes.length, parser.getBinaryValue().length);
assertEquals(bytes[0], parser.getBinaryValue()[0]);
assertEquals(bytes[1], parser.getBinaryValue()[1]);
assertEquals(bytes[2], parser.getBinaryValue()[2]);
}

@Test
public void testBigDecimal() throws IOException {
double d0 = 1.23456789;
Expand All @@ -343,19 +373,21 @@ public void testBigDecimal() throws IOException {
assertEquals(BigDecimal.valueOf(Double.MIN_NORMAL), objects.get(idx++));
}

private File createTestFile() throws IOException {
File tempFile = File.createTempFile("test", "msgpack");
tempFile.deleteOnExit();
FileOutputStream out = new FileOutputStream(tempFile);
MessagePack.newDefaultPacker(out)
.packArrayHeader(1).packInt(1)
.packArrayHeader(1).packInt(1)
.close();
private File createTestFile() throws Exception {
File tempFile = createTempFile(new FileSetup() {
@Override
public void setup(File f) throws IOException {
MessagePack.newDefaultPacker(new FileOutputStream(f))
.packArrayHeader(1).packInt(1)
.packArrayHeader(1).packInt(1)
.close();
}
});
return tempFile;
}

@Test(expected = IOException.class)
public void testEnableFeatureAutoCloseSource() throws IOException {
public void testEnableFeatureAutoCloseSource() throws Exception {
File tempFile = createTestFile();
MessagePackFactory messagePackFactory = new MessagePackFactory();
FileInputStream in = new FileInputStream(tempFile);
Expand All @@ -365,11 +397,10 @@ public void testEnableFeatureAutoCloseSource() throws IOException {
}

@Test
public void testDisableFeatureAutoCloseSource() throws IOException {
public void testDisableFeatureAutoCloseSource() throws Exception {
File tempFile = createTestFile();
MessagePackFactory messagePackFactory = new MessagePackFactory();
FileInputStream in = new FileInputStream(tempFile);
ObjectMapper objectMapper = new ObjectMapper(messagePackFactory);
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
objectMapper.readValue(in, new TypeReference<List<Integer>>() {});
objectMapper.readValue(in, new TypeReference<List<Integer>>() {});
Expand All @@ -386,4 +417,31 @@ public void testParseBigDecimal() throws IOException {
bytes, new TypeReference<ArrayList<BigDecimal>>() {});
assertEquals(list, result);
}

@Test
public void testReadPrimitiveObjectViaObjectMapper() throws Exception {
File tempFile = createTempFile();
FileOutputStream out = new FileOutputStream(tempFile);

MessagePacker packer = MessagePack.newDefaultPacker(out);
packer.packString("foo");
packer.packLong(Long.MAX_VALUE);
packer.packDouble(3.14);
byte[] bytes = {0x00, 0x11, 0x22};
packer.packBinaryHeader(bytes.length);
packer.writePayload(bytes);
packer.close();

FileInputStream in = new FileInputStream(tempFile);
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
objectMapper.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
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>() {}));
byte[] bs = objectMapper.readValue(in, new TypeReference<byte []>() {});
assertEquals(bytes.length, bs.length);
assertEquals(bytes[0], bs[0]);
assertEquals(bytes[1], bs[1]);
assertEquals(bytes[2], bs[2]);
}
}