Java Code Examples for org.msgpack.core.MessagePacker#flush()

The following examples show how to use org.msgpack.core.MessagePacker#flush() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessagePackGenerator.java    From jackson-dataformat-msgpack with Apache License 2.0 6 votes vote down vote up
@Override
public void flush() throws IOException {
    if (rootStackItem != null) {
        if (rootStackItem instanceof StackItemForObject) {
            packObject((StackItemForObject) rootStackItem);
        }
        else if (rootStackItem instanceof StackItemForArray) {
            packArray((StackItemForArray) rootStackItem);
        }
        else {
            throw new IllegalStateException("Unexpected rootStackItem: " + rootStackItem);
        }
        MessagePacker messagePacker = getMessagePacker();
        messagePacker.flush();
    }
}
 
Example 2
Source File: MsgpackIOUtil.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Serializes the {@code message} into an {@link MessageBufferOutput} using the given {@code schema}.
 */
public static <T> void writeTo(MessageBufferOutput out, T message, Schema<T> schema, boolean numeric)
        throws IOException
{

    MessagePacker packer = MessagePack.newDefaultPacker(out);

    try
    {
        writeTo(packer, message, schema, numeric);
    }
    finally
    {
        packer.flush();
    }
}
 
Example 3
Source File: MsgpackIOUtil.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Serializes the {@code messages} into the stream using the given schema.
 */
public static <T> void writeListTo(OutputStream out, List<T> messages,
        Schema<T> schema, boolean numeric) throws IOException
{

    MessagePacker packer = MessagePack.newDefaultPacker(out);

    try
    {
        writeListTo(packer, messages, schema, numeric);
    }
    finally
    {
        packer.flush();
    }
}
 
Example 4
Source File: MsgpackIOUtil.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Serializes the {@code messages} into the stream using the given schema.
 */
public static <T> void writeListTo(MessageBufferOutput out, List<T> messages,
        Schema<T> schema, boolean numeric) throws IOException
{

    MessagePacker packer = MessagePack.newDefaultPacker(out);

    try
    {
        writeListTo(packer, messages, schema, numeric);
    }
    finally
    {
        packer.flush();
    }
}
 
Example 5
Source File: MsgpackIOUtil.java    From protostuff with Apache License 2.0 5 votes vote down vote up
/**
 * Serializes the {@code message} into an {@link OutputStream} using the given {@code schema}.
 */
public static <T> void writeTo(OutputStream out, T message, Schema<T> schema, boolean numeric) throws IOException
{

    MessagePacker packer = MessagePack.newDefaultPacker(out);

    try
    {
        writeTo(packer, message, schema, numeric);
    }
    finally
    {
        packer.flush();
    }
}
 
Example 6
Source File: MessagePackParserTest.java    From jackson-dataformat-msgpack with Apache License 2.0 4 votes vote down vote up
@Test
public void testParserShouldReadArray() throws IOException {
    MessagePacker packer = new MessagePacker(new OutputStreamBufferOutput(out));
    packer.packArrayHeader(10);
    // #1
    packer.packArrayHeader(3);
    {
        packer.packLong(Long.MAX_VALUE);
        packer.packNil();
        packer.packString("FOO BAR");
    }
    // #2
    packer.packString("str");
    // #3
    packer.packInt(Integer.MAX_VALUE);
    // #4
    packer.packLong(Long.MIN_VALUE);
    // #5
    packer.packFloat(Float.MAX_VALUE);
    // #6
    packer.packDouble(Double.MIN_VALUE);
    // #7
    BigInteger bi = new BigInteger(Long.toString(Long.MAX_VALUE));
    bi = bi.add(BigInteger.ONE);
    packer.packBigInteger(bi);
    // #8
    byte[] bytes = new byte[]{(byte) 0xFF, (byte) 0xFE, 0x01, 0x00};
    packer.packBinaryHeader(bytes.length);
    packer.writePayload(bytes);
    // #9
    packer.packMapHeader(2);
    {
        packer.packString("child_map_name");
        packer.packString("komamitsu");
        packer.packString("child_map_age");
        packer.packInt(42);
    }
    // #10
    packer.packBoolean(true);

    packer.flush();

    bytes = out.toByteArray();

    TypeReference<List<Object>> typeReference = new TypeReference<List<Object>>(){};
    List<Object> array = objectMapper.readValue(bytes, typeReference);
    assertEquals(10, array.size());
    int i = 0;
    // #1
    List<Object> childArray = (List<Object>) array.get(i++);
    {
        int j = 0;
        assertEquals(Long.MAX_VALUE, childArray.get(j++));
        assertEquals(null, childArray.get(j++));
        assertEquals("FOO BAR", childArray.get(j++));
    }
    // #2
    assertEquals("str", array.get(i++));
    // #3
    assertEquals(Integer.MAX_VALUE, array.get(i++));
    // #4
    assertEquals(Long.MIN_VALUE, array.get(i++));
    // #5
    assertEquals(Float.MAX_VALUE, (Double)array.get(i++), 0.001f);
    // #6
    assertEquals(Double.MIN_VALUE, (Double)array.get(i++), 0.001f);
    // #7
    assertEquals(bi, array.get(i++));
    // #8
    byte[] bs = (byte[]) array.get(i++);
    assertEquals(4, bs.length);
    assertEquals((byte)0xFF, bs[0]);
    assertEquals((byte)0xFE, bs[1]);
    assertEquals((byte)0x01, bs[2]);
    assertEquals((byte)0x00, bs[3]);
    // #9
    Map<String, Object> childMap = (Map<String, Object>) array.get(i++);
    {
        assertEquals(2, childMap.keySet().size());
        for (Map.Entry<String, Object> entry : childMap.entrySet()) {
            String k = entry.getKey();
            Object v = entry.getValue();
            if (k.equals("child_map_name")) {
                assertEquals("komamitsu", v);
            }
            else if (k.equals("child_map_age")) {
                assertEquals(42, v);
            }
        }
    }
    // #10
    assertEquals(true, array.get(i++));
}