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 | 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 | 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
Example 3
Project: Ragefist File: JUniformPackerMessagePack.java View source code | 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 | 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 | 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 | 6 votes |
private void packObject(StackItemForObject stackItem) throws IOException { Listkeys = stackItem.getKeys(); List
Example 7
Project: jackson-dataformat-msgpack File: MessagePackGenerator.java View source code | 6 votes |
private void packArray(StackItemForArray stackItem) throws IOException { List
Example 8
Project: jackson-dataformat-msgpack File: MessagePackGenerator.java View source code | 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 | 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 | 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 | 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); ListsessionList = 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 | 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
Example 13
Project: jackson-dataformat-msgpack File: MessagePackParserTest.java View source code | 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