org.msgpack.core.MessageUnpacker Java Examples
The following examples show how to use
org.msgpack.core.MessageUnpacker.
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: LogResponse.java From maestro-java with Apache License 2.0 | 6 votes |
public LogResponse(final MessageUnpacker unpacker) throws IOException { super(MaestroCommand.MAESTRO_NOTE_LOG, unpacker); this.locationType = LocationType.byCode(unpacker.unpackInt()); this.locationTypeInfo = SerializationUtils.unpackLocationTypeInfo(unpacker); setFileName(unpacker.unpackString()); setIndex(unpacker.unpackInt()); setTotal(unpacker.unpackInt()); setFileSize(unpacker.unpackLong()); setFileHash(unpacker.unpackString()); int chunkSize = unpacker.unpackBinaryHeader(); data = new byte[chunkSize]; unpacker.readPayload(data); }
Example #2
Source File: MaestroDeserializer.java From maestro-java with Apache License 2.0 | 6 votes |
public static MaestroEvent deserializeEvent(byte[] bytes) throws IOException, MalformedNoteException { try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bytes)){ final short tmpType = unpacker.unpackShort(); final MaestroNoteType type = MaestroNoteType.from(tmpType); switch (Objects.requireNonNull(type)) { case MAESTRO_TYPE_REQUEST: return deserializeRequest(unpacker); case MAESTRO_TYPE_NOTIFICATION: return deserializeNotification(unpacker); case MAESTRO_TYPE_DATA: return deserializeData(unpacker); default: throw new MalformedNoteException("Invalid event type: " + tmpType); } } }
Example #3
Source File: MaestroDeserializer.java From maestro-java with Apache License 2.0 | 6 votes |
public static MaestroNote deserialize(byte[] bytes) throws IOException, MalformedNoteException { try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bytes)){ final short tmpType = unpacker.unpackShort(); final MaestroNoteType type = MaestroNoteType.from(tmpType); switch (Objects.requireNonNull(type)) { case MAESTRO_TYPE_REQUEST: return deserializeRequest(unpacker); case MAESTRO_TYPE_RESPONSE: return deserializeResponse(unpacker); case MAESTRO_TYPE_NOTIFICATION: return deserializeNotification(unpacker); case MAESTRO_TYPE_DATA: return deserializeData(unpacker); default: throw new MalformedNoteException("Invalid note type: " + tmpType); } } }
Example #4
Source File: FluentdIngesterTest.java From fluency with Apache License 2.0 | 6 votes |
@Test void ingestWithoutAck() throws IOException { Ingester ingester = new FluentdIngester(new FluentdIngester.Config(), fluentdSender); ingester.ingest(TAG, ByteBuffer.wrap(DATA)); verify(fluentdSender, times(1)).send(byteBuffersArgumentCaptor.capture()); List<ByteBuffer> byteBuffers = byteBuffersArgumentCaptor.getAllValues().get(0); byte[] ingested = getIngestedData(byteBuffers); MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(ingested); ImmutableArrayValue arrayValue = unpacker.unpackValue().asArrayValue(); assertEquals(3, arrayValue.size()); assertEquals(TAG, arrayValue.get(0).asStringValue().asString()); assertArrayEquals(DATA, arrayValue.get(1).asRawValue().asByteArray()); Map<Value, Value> options = arrayValue.get(2).asMapValue().map(); assertEquals(1, options.size()); assertEquals(DATA.length, options.get(ValueFactory.newString("size")).asIntegerValue().asInt()); }
Example #5
Source File: MsgpackIOUtil.java From protostuff with Apache License 2.0 | 6 votes |
/** * Merges the {@code message} with the byte array using the given {@code schema}. */ public static <T> void mergeFrom(byte[] data, int offset, int length, T message, Schema<T> schema, boolean numeric) throws IOException { ArrayBufferInput bios = new ArrayBufferInput(data, offset, length); MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bios); try { mergeFrom(unpacker, message, schema, numeric); } finally { unpacker.close(); } }
Example #6
Source File: MessagePackTraverser.java From influxdb-java with MIT License | 6 votes |
/** * Traverse over the whole message pack stream. * This method can be used for converting query results in chunk. * * @param is * The MessagePack format input stream * @return an Iterable over the QueryResult objects * */ public Iterable<QueryResult> traverse(final InputStream is) { MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(is); return () -> { return new Iterator<QueryResult>() { @Override public boolean hasNext() { try { return unpacker.hasNext(); } catch (IOException e) { throw new InfluxDBException(e); } } @Override public QueryResult next() { return parse(unpacker); } }; }; }
Example #7
Source File: MsgpackSerializer.java From gridgo with MIT License | 6 votes |
private BElement unpackAny(MessageUnpacker unpacker) throws IOException { var format = unpacker.getNextFormat(); switch (format.getValueType()) { case ARRAY: return unpackArray(unpacker); case MAP: return unpackMap(unpacker); case BINARY: case BOOLEAN: case FLOAT: case INTEGER: case NIL: case STRING: return unpackValue(format, unpacker); case EXTENSION: default: break; } throw new BeanSerializationException("Cannot deserialize as BElement for format: " + format); }
Example #8
Source File: Deserializer.java From tinkergraph-gremlin with Apache License 2.0 | 6 votes |
public A deserialize(byte[] bytes) throws IOException { long start = System.currentTimeMillis(); if (null == bytes) return null; try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bytes)) { final long id = unpacker.unpackLong(); final String label = unpacker.unpackString(); final Map<String, long[]> inEdgeIdsByLabel = unpackEdgeIdsByLabel(unpacker); final Map<String, long[]> outEdgeIdsByLabel = unpackEdgeIdsByLabel(unpacker); final Map<String, Object> properties = unpackProperties(unpacker); A a = createElement(id, label, properties, inEdgeIdsByLabel, outEdgeIdsByLabel); deserializedCount++; deserializationTimeSpentMillis += System.currentTimeMillis() - start; if (deserializedCount % 100000 == 0) { float avgDeserializationTime = deserializationTimeSpentMillis / (float) deserializedCount; logger.debug("stats: deserialized " + deserializedCount + " vertices in total (avg time: " + avgDeserializationTime + "ms)"); } return a; } }
Example #9
Source File: MaestroDeserializer.java From maestro-java with Apache License 2.0 | 6 votes |
private static MaestroData deserializeData(final MessageUnpacker unpacker) throws IOException, MalformedNoteException { final long tmpCommand = unpacker.unpackLong(); final MaestroCommand command = MaestroCommand.from(tmpCommand); switch (Objects.requireNonNull(command)) { case MAESTRO_NOTE_LOG: { LogResponseJoiner instance = LogResponseJoiner.getInstance(); return instance.join(new LogResponse(unpacker)); } default: { logger.error("Type unknown: {}", command.getClass()); throw new MalformedNoteException("Invalid response command: " + tmpCommand); } } }
Example #10
Source File: MsgunpackerAndBuffer.java From gridgo with MIT License | 5 votes |
public MessageUnpacker reset(InputStream in) throws IOException { ByteBuffer buffer; if (ByteBufferInputStream.class.isInstance(in) && (!(buffer = ((ByteBufferInputStream) in).getBuffer()).isDirect() // || IS_BIG_ENDIAN)) { byteBufferInput.reset(buffer); unpacker.reset(byteBufferInput); } else { streamBufferInput.reset(in); unpacker.reset(streamBufferInput); } return unpacker; }
Example #11
Source File: MessagePackRecordFormatterTest.java From fluency with Apache License 2.0 | 5 votes |
private void assertRecord0(byte[] formatted, long expectedTime) throws IOException { try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(formatted)) { Map<Value, Value> map = unpacker.unpackValue().asMapValue().map(); assertEquals(4, map.size()); assertEquals(expectedTime, map.get(KEY_TIME).asNumberValue().toLong()); assertEquals("first", map.get(KEY_NAME).asStringValue().asString()); assertEquals(42, map.get(KEY_AGE).asNumberValue().toInt()); assertEquals("[email protected]", map.get(KEY_EMAIL).asStringValue().asString()); } }
Example #12
Source File: MessagePackRecordFormatterTest.java From fluency with Apache License 2.0 | 5 votes |
private void assertRecord1(byte[] formatted, long expectedTime) throws IOException { try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(formatted)) { Map<Value, Value> map = unpacker.unpackValue().asMapValue().map(); assertEquals(4, map.size()); assertEquals(expectedTime, map.get(KEY_TIME).asNumberValue().toLong()); assertEquals("second", map.get(KEY_NAME).asStringValue().asString()); assertEquals(55, map.get(KEY_AGE).asNumberValue().toInt()); assertEquals("zzzzzz", map.get(KEY_COMMENT).asStringValue().asString()); } }
Example #13
Source File: MessagePackRecordFormatterTest.java From fluency with Apache License 2.0 | 5 votes |
private void assertRecord2(byte[] formatted, long expectedTime) throws IOException { try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(formatted)) { Map<Value, Value> map = unpacker.unpackValue().asMapValue().map(); assertEquals(4, map.size()); assertEquals(expectedTime, map.get(KEY_TIME).asNumberValue().toLong()); assertEquals("third", map.get(KEY_NAME).asStringValue().asString()); assertEquals(99, map.get(KEY_AGE).asNumberValue().toInt()); assertEquals("knight", map.get(KEY_JOB).asStringValue().asString()); } }
Example #14
Source File: FluencyTest.java From fluency with Apache License 2.0 | 5 votes |
@ParameterizedTest @MethodSource("sslFlagsProvider") public void testWithAckResponseWithProperToken(final boolean sslEnabled) throws Throwable { Exception exception = new ConfigurableTestServer(sslEnabled).run( clientSocket -> { MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(clientSocket.getInputStream()); assertEquals(3, unpacker.unpackArrayHeader()); assertEquals("foo.bar", unpacker.unpackString()); ImmutableRawValue rawValue = unpacker.unpackValue().asRawValue(); Map<Value, Value> map = unpacker.unpackValue().asMapValue().map(); assertEquals(2, map.size()); assertEquals(rawValue.asByteArray().length, map.get(KEY_OPTION_SIZE).asIntegerValue().asInt()); String ackResponseToken = map.get(KEY_OPTION_CHUNK).asRawValue().asString(); assertNotNull(ackResponseToken); MessagePacker packer = MessagePack.newDefaultPacker(clientSocket.getOutputStream()); packer.packMapHeader(1) .packString("ack").packString(ackResponseToken) .close(); // Close the input stream after closing the output stream to avoid closing a socket too early unpacker.close(); }, serverPort -> { FluencyBuilderForFluentd builder = new FluencyBuilderForFluentd(); builder.setSslEnabled(sslEnabled); builder.setAckResponseMode(true); try (Fluency fluency = builder.build(serverPort)) { fluency.emit("foo.bar", new HashMap<>()); } }, 5000); assertNull(exception); }
Example #15
Source File: FluentdIngesterTest.java From fluency with Apache License 2.0 | 5 votes |
@Test void ingestWithAck() throws IOException { FluentdIngester.Config config = new FluentdIngester.Config(); config.setAckResponseMode(true); Ingester ingester = new FluentdIngester(config, fluentdSender); ingester.ingest(TAG, ByteBuffer.wrap(DATA)); ArgumentCaptor<byte[]> ackTokenArgumentCaptor = ArgumentCaptor.forClass(byte[].class); verify(fluentdSender, times(1)) .sendWithAck(byteBuffersArgumentCaptor.capture(), ackTokenArgumentCaptor.capture()); List<ByteBuffer> byteBuffers = byteBuffersArgumentCaptor.getAllValues().get(0); byte[] ingested = getIngestedData(byteBuffers); MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(ingested); ImmutableArrayValue arrayValue = unpacker.unpackValue().asArrayValue(); assertEquals(3, arrayValue.size()); assertEquals(TAG, arrayValue.get(0).asStringValue().asString()); assertArrayEquals(DATA, arrayValue.get(1).asRawValue().asByteArray()); Map<Value, Value> options = arrayValue.get(2).asMapValue().map(); assertEquals(2, options.size()); assertEquals(DATA.length, options.get(ValueFactory.newString("size")).asIntegerValue().asInt()); String ackToken = options.get(ValueFactory.newString("chunk")).asRawValue().asString(); UUID uuidFromAckToken = UUID.fromString(ackToken); List<byte[]> ackTokenArgumentCaptorAllValues = ackTokenArgumentCaptor.getAllValues(); assertEquals(1, ackTokenArgumentCaptorAllValues.size()); assertEquals(uuidFromAckToken, UUID.fromString(new String(ackTokenArgumentCaptorAllValues.get(0), CHARSET))); }
Example #16
Source File: FluencyTest.java From fluency with Apache License 2.0 | 5 votes |
@ParameterizedTest @MethodSource("sslFlagsProvider") void testWithAckResponseButNotReceiveToken(final boolean sslEnabled) throws Throwable { Exception exception = new ConfigurableTestServer(sslEnabled).run( clientSocket -> { MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(clientSocket.getInputStream()); assertEquals(3, unpacker.unpackArrayHeader()); assertEquals("foo.bar", unpacker.unpackString()); ImmutableRawValue rawValue = unpacker.unpackValue().asRawValue(); Map<Value, Value> map = unpacker.unpackValue().asMapValue().map(); assertEquals(2, map.size()); assertEquals(rawValue.asByteArray().length, map.get(KEY_OPTION_SIZE).asIntegerValue().asInt()); assertNotNull(map.get(KEY_OPTION_CHUNK).asRawValue().asString()); unpacker.close(); }, serverPort -> { FluencyBuilderForFluentd builder = new FluencyBuilderForFluentd(); builder.setSslEnabled(sslEnabled); builder.setAckResponseMode(true); try (Fluency fluency = builder.build(serverPort)) { fluency.emit("foo.bar", new HashMap<>()); } }, 5000); assertEquals(exception.getClass(), TimeoutException.class); }
Example #17
Source File: MessagePackTraverser.java From influxdb-java with MIT License | 5 votes |
private QueryResult parse(final MessageUnpacker unpacker) { QueryResult queryResult = new QueryResult(); QueryResultModelPath queryResultPath = new QueryResultModelPath(); queryResultPath.add("queryResult", queryResult); try { traverse(unpacker, queryResultPath, 1); } catch (IOException e) { throw new InfluxDBException(e); } return queryResult; }
Example #18
Source File: Deserializer.java From tinkergraph-gremlin with Apache License 2.0 | 5 votes |
private Map<String, Object> unpackProperties(MessageUnpacker unpacker) throws IOException { int propertyCount = unpacker.unpackMapHeader(); Map<String, Object> res = new THashMap<>(propertyCount); for (int i = 0; i < propertyCount; i++) { final String key = unpacker.unpackString(); final Object unpackedProperty = unpackProperty(unpacker.unpackValue().asArrayValue()); res.put(key, unpackedProperty); } return res; }
Example #19
Source File: MessagePackParser.java From jackson-dataformat-msgpack with Apache License 2.0 | 5 votes |
@Override public void close() throws IOException { try { MessageUnpacker messageUnpacker = getMessageUnpacker(); messageUnpacker.close(); } catch (Exception e) { e.printStackTrace(); } finally { isClosed = true; } }
Example #20
Source File: MaestroDeserializer.java From maestro-java with Apache License 2.0 | 5 votes |
private static MaestroNotification deserializeNotification(final MessageUnpacker unpacker) throws IOException, MalformedNoteException { final long tmpCommand = unpacker.unpackLong(); final MaestroCommand command = MaestroCommand.from(tmpCommand); switch (Objects.requireNonNull(command)) { case MAESTRO_NOTE_NOTIFY_FAIL: { return new TestFailedNotification(unpacker); } case MAESTRO_NOTE_NOTIFY_SUCCESS: { return new TestSuccessfulNotification(unpacker); } case MAESTRO_NOTE_ABNORMAL_DISCONNECT: { return new AbnormalDisconnect(unpacker); } case MAESTRO_NOTE_NOTIFY_DRAIN_COMPLETE: { return new DrainCompleteNotification(unpacker); } case MAESTRO_NOTE_NOTIFY_TEST_STARTED: { return new TestStartedNotification(unpacker); } default: { throw new MalformedNoteException("Invalid notification command: " + tmpCommand); } } }
Example #21
Source File: MaestroNotification.java From maestro-java with Apache License 2.0 | 5 votes |
public MaestroNotification(final MaestroCommand maestroCommand, final MessageUnpacker unpacker) throws IOException { super(MaestroNoteType.MAESTRO_TYPE_NOTIFICATION, maestroCommand, unpacker); id = unpacker.unpackString(); final String memberName = unpacker.unpackString(); final String groupName = unpacker.unpackString(); final int role = unpacker.unpackInt(); final String name = unpacker.unpackString(); final String host = unpacker.unpackString(); this.peerInfo = new WorkerPeer(Role.from(role), name, host, new DefaultGroupInfo(memberName, groupName)); }
Example #22
Source File: MessagePackSequenceFileReaderWriterFactory.java From secor with Apache License 2.0 | 5 votes |
@Override public KeyValue next() throws IOException { if (mReader.next(mKey, mValue)) { try(MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(mKey.getBytes())) { int mapSize = unpacker.unpackMapHeader(); long offset = 0; long timestamp = -1; byte[] keyBytes = EMPTY_BYTES; for (int i = 0; i < mapSize; i++) { int key = unpacker.unpackInt(); switch (key) { case KAFKA_MESSAGE_OFFSET: offset = unpacker.unpackLong(); break; case KAFKA_MESSAGE_TIMESTAMP: timestamp = unpacker.unpackLong(); break; case KAFKA_HASH_KEY: int keySize = unpacker.unpackBinaryHeader(); keyBytes = new byte[keySize]; unpacker.readPayload(keyBytes); break; } } return new KeyValue(offset, keyBytes, Arrays.copyOfRange(mValue.getBytes(), 0, mValue.getLength()), timestamp); } } else { return null; } }
Example #23
Source File: StatsResponse.java From maestro-java with Apache License 2.0 | 5 votes |
public StatsResponse(final MessageUnpacker unpacker) throws IOException { super(MaestroCommand.MAESTRO_NOTE_STATS, unpacker); childCount = unpacker.unpackInt(); roleInfo = unpacker.unpackString(); statsType = unpacker.unpackShort(); timestamp = unpacker.unpackString(); count = unpacker.unpackLong(); rate = unpacker.unpackDouble(); latency = unpacker.unpackDouble(); }
Example #24
Source File: GroupJoinRequest.java From maestro-java with Apache License 2.0 | 5 votes |
public GroupJoinRequest(final MessageUnpacker unpacker) throws IOException { super(MaestroCommand.MAESTRO_NOTE_GROUP_JOIN, unpacker); final String memberName = unpacker.unpackString(); final String groupName = unpacker.unpackString(); groupInfo = new DefaultGroupInfo(memberName, groupName); }
Example #25
Source File: AbstractMaestroNotification.java From maestro-java with Apache License 2.0 | 5 votes |
public AbstractMaestroNotification(final MaestroCommand maestroCommand, final MessageUnpacker unpacker) throws IOException { super(MaestroNoteType.MAESTRO_TYPE_NOTIFICATION, maestroCommand, unpacker); id = unpacker.unpackString(); final String memberName = unpacker.unpackString(); final String groupName = unpacker.unpackString(); final int role = unpacker.unpackInt(); final String name = unpacker.unpackString(); final String host = unpacker.unpackString(); this.peerInfo = new WorkerPeer(Role.from(role), name, host, new DefaultGroupInfo(memberName, groupName)); }
Example #26
Source File: SerializationUtils.java From maestro-java with Apache License 2.0 | 5 votes |
public static SutDetails unpackSutDetails(final MessageUnpacker unpacker) throws IOException { int sutId = unpacker.unpackInt(); String sutName = unpacker.unpackString(); String sutVersion = unpacker.unpackString(); String sutJvmVersion = unpacker.unpackString(); String sutOtherInfo = unpacker.unpackString(); String sutTags = unpacker.unpackString(); String labName = unpacker.unpackString(); String testTags = unpacker.unpackString(); return new SutDetails(sutId, sutName, sutVersion, sutJvmVersion, sutOtherInfo, sutTags, labName, testTags); }
Example #27
Source File: SerializationUtils.java From maestro-java with Apache License 2.0 | 5 votes |
public static TestDetails unpackTestDetails(final MessageUnpacker unpacker) throws IOException { TestDetails ret = new TestDetails(); ret.setTestDescription(unpacker.unpackString()); ret.setTestComments(unpacker.unpackString()); return ret; }
Example #28
Source File: InfluxDBException.java From influxdb-java with MIT License | 5 votes |
/** * Create corresponding InfluxDBException from the message pack error body. * @param messagePackErrorBody * the error body if any * @return the Exception */ public static InfluxDBException buildExceptionForErrorState(final InputStream messagePackErrorBody) { try { MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(messagePackErrorBody); ImmutableMapValue mapVal = (ImmutableMapValue) unpacker.unpackValue(); return InfluxDBException.buildExceptionFromErrorMessage( mapVal.map().get(new ImmutableStringValueImpl("error")).toString()); } catch (Exception e) { return new InfluxDBException(e); } }
Example #29
Source File: MessagePackParser.java From jackson-dataformat-msgpack with Apache License 2.0 | 5 votes |
private MessageUnpacker getMessageUnpacker() { MessageUnpacker messageUnpacker = messageUnpackerHolder.get(); if (messageUnpacker == null) { throw new IllegalStateException("messageUnpacker is null"); } return messageUnpacker; }
Example #30
Source File: MsgpackSerializer.java From gridgo with MIT License | 5 votes |
private BArray unpackArray(MessageUnpacker unpacker) throws IOException { var result = this.getFactory().newArray(); int size = unpacker.unpackArrayHeader(); for (int i = 0; i < size; i++) { result.addAny(this.unpackAny(unpacker)); } return result; }