org.msgpack.core.MessagePacker

Java Code Examples for org.msgpack.core.MessagePacker

The following are top voted examples for showing how to use org.msgpack.core.MessagePacker. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to product more good examples. 

+  Save this class to your library
Example 1
Project: Ragefist   File: JUniformPackerMessagePack.java View source code Vote up 6 votes
@Override
public Object fromUniformObject(JUniformObject object) {
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	MessagePacker packer = _msgpack.newPacker(out);
	try {
		this._writeJUniformObjectToPacker(object, packer);
		packer.close();
	} catch (IOException ex) {
		return null;
	}
	return out.toByteArray();
}
 
Example 2
Project: Ragefist   File: JUniformPackerMessagePack.java View source code Vote up 6 votes
private void _writeJUniformObjectToPacker(JUniformObject object, MessagePacker packer) throws IOException {
	if (object == null) {
		packer.packNil();
	}
	else if (object.isMapValue()) {
		packer.packMapHeader(object.getPropertiesCount());
		Iterator> it = object.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry pair = it.next();
			this._writeSimpleObjectToPacker(pair.getKey(), packer);
			this._writeJUniformObjectToPacker(pair.getValue(), packer);
		}
	} else if (object.isArrayValue()) {
		packer.packArrayHeader(object.getElementsCount());
		Iterator it = object.iterator();
		while(it.hasNext()) {
			this._writeJUniformObjectToPacker(it.next(), packer);
		}
	} else {
		this._writeSimpleObjectToPacker(object.getValue(), packer);
	}
}
 
Example 3
Project: Ragefist   File: JUniformPackerMessagePack.java View source code Vote up 6 votes
private void _writeSimpleObjectToPacker(Object object, MessagePacker packer) throws IOException {
	if (object == null) {
		packer.packNil();
	} else if (object instanceof Byte) {
		packer.packByte((Byte)object);
	} else if (object instanceof Boolean) {
		packer.packBoolean((Boolean)object);
	} else if (object instanceof Double) {
		packer.packDouble((Double)object);
	} else if (object instanceof Float) {
		packer.packFloat((Float)object);
	} else if (object instanceof Integer) {
		packer.packInt((Integer)object);
	} else if (object instanceof Long) {
		packer.packLong((Long)object);
	} else if (object instanceof Short) {
		packer.packShort((Short)object);
	} else if (object instanceof String) {
		packer.packString((String)object);
	} else {
		packer.packNil();
	}
}
 
Example 4
Project: jackson-dataformat-msgpack   File: MessagePackGenerator.java View source code Vote up 6 votes
public MessagePackGenerator(int features, ObjectCodec codec, OutputStream out) throws IOException {
    super(features, codec);
    MessagePacker messagePacker = messagePackersHolder.get();
    OutputStreamBufferOutput messageBufferOutput = messageBufferOutputHolder.get();
    if (messageBufferOutput == null) {
        messageBufferOutput = new OutputStreamBufferOutput(out);
    }
    else {
        messageBufferOutput.reset(out);
    }
    messageBufferOutputHolder.set(messageBufferOutput);

    if (messagePacker == null) {
        messagePacker = new MessagePacker(messageBufferOutput);
    }
    else {
        messagePacker.reset(messageBufferOutput);
    }
    messagePackersHolder.set(messagePacker);

    this.stack = new LinkedList();
}
 
Example 5
Project: jackson-dataformat-msgpack   File: MessagePackGenerator.java View source code Vote up 6 votes
private void packValue(Object v) throws IOException {
    MessagePacker messagePacker = getMessagePacker();
    if (v == null) {
        messagePacker.packNil();
    }
    else if (v instanceof Integer) {
        messagePacker.packInt((Integer) v);
    }
    else if (v instanceof ByteBuffer) {
        ByteBuffer bb = (ByteBuffer) v;
        messagePacker.packBinaryHeader(bb.limit());
        messagePacker.writePayload(bb);
    }
    else if (v instanceof String) {
        messagePacker.packString((String) v);
    }
    else if (v instanceof Float) {
        messagePacker.packFloat((Float) v);
    }
    else if (v instanceof Long) {
        messagePacker.packLong((Long) v);
    }
    else if (v instanceof StackItemForObject) {
        packObject((StackItemForObject) v);
    }
    else if (v instanceof StackItemForArray) {
        packArray((StackItemForArray) v);
    }
    else if (v instanceof Double) {
        messagePacker.packDouble((Double) v);
    }
    else if (v instanceof BigInteger) {
        messagePacker.packBigInteger((BigInteger) v);
    }
    else if (v instanceof BigDecimal) {
        // TODO
        throw new NotImplementedException();
    }
    else if (v instanceof Boolean) {
        messagePacker.packBoolean((Boolean) v);
    }
    else {
        throw new IllegalArgumentException(v.toString());
    }
}
 
Example 6
Project: jackson-dataformat-msgpack   File: MessagePackGenerator.java View source code Vote up 6 votes
private void packObject(StackItemForObject stackItem) throws IOException {
    List keys = stackItem.getKeys();
    List values = stackItem.getValues();

    MessagePacker messagePacker = getMessagePacker();
    messagePacker.packMapHeader(keys.size());

    for (int i = 0; i < keys.size(); i++) {
        messagePacker.packString(keys.get(i));
        Object v = values.get(i);
        packValue(v);
    }
}
  
      
     
    
Example 7
Project: jackson-dataformat-msgpack   File: MessagePackGenerator.java View source code Vote up 6 votes
private void packArray(StackItemForArray stackItem) throws IOException {
    List values = stackItem.getValues();

    MessagePacker messagePacker = getMessagePacker();
    messagePacker.packArrayHeader(values.size());

    for (int i = 0; i < values.size(); i++) {
        Object v = values.get(i);
        packValue(v);
    }
}
  
      
     
    
Example 8
Project: jackson-dataformat-msgpack   File: MessagePackGenerator.java View source code Vote up 6 votes
@Override
public void close() throws IOException {
    try {
        flush();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        MessagePacker messagePacker = getMessagePacker();
        messagePacker.close();
    }
}
 
Example 9
Project: jackson-dataformat-msgpack   File: MessagePackGenerator.java View source code Vote up 6 votes
@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 10
Project: jackson-dataformat-msgpack   File: MessagePackGenerator.java View source code Vote up 6 votes
private MessagePacker getMessagePacker() {
    MessagePacker messagePacker = messagePackersHolder.get();
    if (messagePacker == null) {
        throw new IllegalStateException("messagePacker is null");
    }
    return messagePacker;
}
 
Example 11
Project: td-client-java   File: TestTDClient.java View source code Vote up 5 votes
@Test
public void testBulkImport()
        throws Exception
{
    final String bulkImportTable = newTemporaryName("sample_bi");
    client.deleteTableIfExists(SAMPLE_DB, bulkImportTable);
    client.createTableIfNotExists(SAMPLE_DB, bulkImportTable);

    final int numRowsInPart = 10;
    final int numParts = 3;
    String dateStr = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
    final String session = "td-client-java-test-session-" + dateStr;
    try {
        client.createBulkImportSession(session, SAMPLE_DB, bulkImportTable);

        List sessionList = client.listBulkImportSessions();
        TDBulkImportSession foundInList = Iterables.find(sessionList, new Predicate()
        {
            @Override
            public boolean apply(TDBulkImportSession input)
            {
                return input.getName().equals(session);
            }
        });

        TDBulkImportSession bs = client.getBulkImportSession(session);
        logger.info("bulk import session: {}, error message: {}", bs.getJobId(), bs.getErrorMessage());
        assertEquals(session, bs.getName());
        assertEquals(SAMPLE_DB, bs.getDatabaseName());
        assertEquals(bulkImportTable, bs.getTableName());
        assertTrue(bs.isUploading());

        assertEquals(foundInList.getStatus(), bs.getStatus());

        int count = 0;
        final long time = System.currentTimeMillis() / 1000;

        // Upload part 0, 1, 2
        for (int i = 0; i < 3; ++i) {
            String partName = "bip" + i;
            // Prepare msgpack.gz
            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            OutputStream out = new GZIPOutputStream(buf);
            MessagePacker packer = MessagePack.newDefaultPacker(out);
            for (int n = 0; n < numRowsInPart; ++n) {
                ValueFactory.MapBuilder b = ValueFactory.newMapBuilder();
                b.put(ValueFactory.newString("time"), ValueFactory.newInteger(time + count));
                b.put(ValueFactory.newString("event"), ValueFactory.newString("log" + count));
                b.put(ValueFactory.newString("description"), ValueFactory.newString("sample data"));
                packer.packValue(b.build());
                count += 1;
            }
            // Embed an error record
            packer.packValue(ValueFactory.newMap(new Value[] {ValueFactory.newNil(), ValueFactory.newString("invalid data")}));

            packer.close();
            out.close();

            File tmpFile = File.createTempFile(partName, ".msgpack.gz", new File("target"));
            Files.write(tmpFile.toPath(), buf.toByteArray());
            client.uploadBulkImportPart(session, partName, tmpFile);

            // list parts
            List parts = client.listBulkImportParts(session);
            assertTrue(parts.contains(partName));

            // freeze test
            client.freezeBulkImportSession(session);

            // unfreeze test
            client.unfreezeBulkImportSession(session);
        }

        // delete the last
        client.deleteBulkImportPart(session, "bip2");

        List parts = client.listBulkImportParts(session);
        assertTrue(!parts.contains("bip2"));

        // Freeze the session
        client.freezeBulkImportSession(session);

        // Perform the session
        client.performBulkImportSession(session);

        // Wait the perform completion
        ExponentialBackOff backoff = new ExponentialBackOff();
        long deadline = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(10);
        bs = client.getBulkImportSession(session);
        while (bs.getStatus() == TDBulkImportSession.ImportStatus.PERFORMING) {
            assertFalse(bs.isUploading());
            if (System.currentTimeMillis() > deadline) {
                throw new IllegalStateException("timeout error: bulk import perform");
            }
            logger.debug("Waiting bulk import completion");
            Thread.sleep(backoff.nextWaitTimeMillis());
            bs = client.getBulkImportSession(session);
        }

        // Check session contents
        assertTrue(bs.hasErrorOnPerform());
        logger.debug(bs.getErrorMessage());

        // Error record check
        int errorCount = client.getBulkImportErrorRecords(session, new Function()
        {
            int errorRecordCount = 0;

            @Override
            public Integer apply(InputStream input)
            {
                try {
                    MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(new GZIPInputStream(input));
                    while (unpacker.hasNext()) {
                        Value v = unpacker.unpackValue();
                        logger.info("error record: " + v);
                        errorRecordCount += 1;
                    }
                    return errorRecordCount;
                }
                catch (IOException e) {
                    throw Throwables.propagate(e);
                }
            }
        });

        final int numValidParts = numParts - 1;
        assertEquals(numValidParts, errorCount);
        assertEquals(0, bs.getErrorParts());
        assertEquals(numValidParts, bs.getValidParts());
        assertEquals(numValidParts, bs.getErrorRecords());
        assertEquals(numValidParts * numRowsInPart, bs.getValidRecords());

        // Commit the session
        deadline = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(5);
        client.commitBulkImportSession(session);

        // Wait the commit completion
        bs = client.getBulkImportSession(session);
        while (bs.getStatus() != TDBulkImportSession.ImportStatus.COMMITTED) {
            if (System.currentTimeMillis() > deadline) {
                throw new IllegalStateException("timeout error: bulk import commit");
            }
            logger.info("Waiting bulk import perform step completion");
            Thread.sleep(TimeUnit.SECONDS.toMillis(5));
            bs = client.getBulkImportSession(session);
        }

        // Check the data
        TDTable imported = Iterables.find(client.listTables(SAMPLE_DB), new Predicate()
        {
            @Override
            public boolean apply(TDTable input)
            {
                return input.getName().equals(bulkImportTable);
            }
        });

        assertEquals(numRowsInPart * 2, imported.getRowCount());
        List columns = imported.getColumns();
        logger.info(Joiner.on(", ").join(columns));
        assertEquals(2, columns.size()); // event, description, (time)
    }
    finally {
        client.deleteBulkImportSession(session);
    }
}
 
Example 12
Project: jackson-dataformat-msgpack   File: MessagePackParserTest.java View source code Vote up 5 votes
@Test
public void testParserShouldReadObject() throws IOException {
    MessagePacker packer = new MessagePacker(new OutputStreamBufferOutput(out));
    packer.packMapHeader(8);
    // #1
    packer.packString("str");
    packer.packString("foobar");
    // #2
    packer.packString("int");
    packer.packInt(Integer.MIN_VALUE);
    // #3
    packer.packString("map");
    {
        packer.packMapHeader(2);
        packer.packString("child_str");
        packer.packString("bla bla bla");
        packer.packString("child_int");
        packer.packInt(Integer.MAX_VALUE);
    }
    // #4
    packer.packString("double");
    packer.packDouble(Double.MAX_VALUE);
    // #5
    packer.packString("long");
    packer.packLong(Long.MIN_VALUE);
    // #6
    packer.packString("bi");
    BigInteger bigInteger = new BigInteger(Long.toString(Long.MAX_VALUE));
    packer.packBigInteger(bigInteger.add(BigInteger.ONE));
    // #7
    packer.packString("array");
    {
        packer.packArrayHeader(3);
        packer.packFloat(Float.MIN_VALUE);
        packer.packNil();
        packer.packString("array_child_str");
    }
    // #8
    packer.packString("bool");
    packer.packBoolean(false);

    packer.flush();

    byte[] bytes = out.toByteArray();

    TypeReference> typeReference = new TypeReference>(){};
    Map object = objectMapper.readValue(bytes, typeReference);
    assertEquals(8, object.keySet().size());

    int bitmap = 0;
    for (Map.Entry entry : object.entrySet()) {
        String k = entry.getKey();
        Object v = entry.getValue();
        if (k.equals("str")) {
            // #1
            bitmap |= 1 << 0;
            assertEquals("foobar", v);
        }
        else if (k.equals("int")) {
            // #2
            bitmap |= 1 << 1;
            assertEquals(Integer.MIN_VALUE, v);
        }
        else if (k.equals("map")) {
            // #3
            bitmap |= 1 << 2;
            Map child = (Map) v;
            assertEquals(2, child.keySet().size());
            for (Map.Entry childEntry : child.entrySet()) {
                String ck = childEntry.getKey();
                Object cv = childEntry.getValue();
                if (ck.equals("child_str")) {
                    bitmap |= 1 << 3;
                    assertEquals("bla bla bla", cv);
                }
                else if (ck.equals("child_int")) {
                    bitmap |= 1 << 4;
                    assertEquals(Integer.MAX_VALUE, cv);
                }
            }
        }
        else if (k.equals("double")) {
            // #4
            bitmap |= 1 << 5;
            assertEquals(Double.MAX_VALUE, (Double) v, 0.0001f);
        }
        else if (k.equals("long")) {
            // #5
            bitmap |= 1 << 6;
            assertEquals(Long.MIN_VALUE, v);
        }
        else if (k.equals("bi")) {
            // #6
            bitmap |= 1 << 7;
            BigInteger bi = new BigInteger(Long.toString(Long.MAX_VALUE));
            assertEquals(bi.add(BigInteger.ONE), v);
        }
        else if (k.equals("array")) {
            // #7
            bitmap |= 1 << 8;
            List expected = Arrays.asList((double)Float.MIN_VALUE, null, "array_child_str");
            assertEquals(expected, v);
        }
        else if (k.equals("bool")) {
            // #8
            bitmap |= 1 << 9;
            assertEquals(false, v);
        }
    }
    assertEquals(0x3FF, bitmap);
}
 
Example 13
Project: jackson-dataformat-msgpack   File: MessagePackParserTest.java View source code Vote up 5 votes
@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> typeReference = new TypeReference>(){};
    List array = objectMapper.readValue(bytes, typeReference);
    assertEquals(10, array.size());
    int i = 0;
    // #1
    List childArray = (List) 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 childMap = (Map) array.get(i++);
    {
        assertEquals(2, childMap.keySet().size());
        for (Map.Entry 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++));
}
  
      
     
    
   

你可能感兴趣的:(android,java,MSGPack)