/** * 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(); } }
.packMapHeader(numberOfFieldsMappedInHeader(keyValue)) .packInt(KAFKA_MESSAGE_OFFSET) .packLong(keyValue.getOffset()); packer.packInt(KAFKA_MESSAGE_TIMESTAMP) .packLong(timestamp); packer.packInt(KAFKA_HASH_KEY) .packBinaryHeader(kafkaKey.length) .writePayload(kafkaKey); packer.close(); byte[] outBytes = out.toByteArray(); this.mKey.set(outBytes, 0, outBytes.length);
@Override public void writeTo(MessagePacker packer) throws IOException { int size = list.size(); packer.packArrayHeader(size); for (int i = 0; i != size; i++) { list.get(i).writeTo(packer); } }
messagePacker.packNil(); messagePacker.packInt((Integer) v); int len = bb.remaining(); if (bb.hasArray()) { messagePacker.packBinaryHeader(len); messagePacker.writePayload(bb.array(), bb.arrayOffset(), len); messagePacker.packBinaryHeader(len); messagePacker.addPayload(data); messagePacker.packString((String) v); messagePacker.packFloat((Float) v); messagePacker.packLong((Long) v); messagePacker.packDouble((Double) v); messagePacker.packBigInteger((BigInteger) v); messagePacker.packBoolean((Boolean) v); messagePacker.packExtensionTypeHeader(extensionType.getType(), extData.length); messagePacker.writePayload(extData); messagePacker.flush(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
switch (type) { case BOOLEAN: packer.packBoolean(value.getBoolean()); return; case BYTE: packer.packByte(value.getByte()); return; case CHAR: case SHORT: packer.packShort(value.getShort()); return; case DOUBLE: packer.packDouble(value.getDouble()); return; case FLOAT: packer.packFloat(value.getFloat()); return; case INTEGER: packer.packInt(value.getInteger()); return; case LONG: packer.packLong(value.getLong()); return; case NULL: packer.packNil(); return; case RAW: byte[] bytes = value.getRaw(); packer.packBinaryHeader(bytes.length);
packer.packMapHeader(1) .packString("ack").packString(UUID.randomUUID().toString()) .close();
try (MessagePacker packer = MessagePack.newDefaultPacker(output)) { if (timeColExists) { packer.packMapHeader(mapSize); packer.packMapHeader(mapSize + 1); packer.packString("time"); packer.packLong(timestamp); packer.packValue(keyValueArray[i * 2]); packer.packValue(keyValueArray[i * 2 + 1]);
@Override public void ingest(String tag, ByteBuffer dataBuffer) throws IOException { ByteArrayOutputStream header = new ByteArrayOutputStream(); MessagePacker messagePacker = MessagePack.newDefaultPacker(header); int dataLength = dataBuffer.limit(); messagePacker.packArrayHeader(3); messagePacker.packString(tag); messagePacker.packRawStringHeader(dataLength); messagePacker.flush(); ByteBuffer headerBuffer = ByteBuffer.wrap(header.toByteArray()); if (config.isAckResponseMode()) { byte[] uuidBytes = UUID.randomUUID().toString().getBytes(CHARSET); ByteBuffer optionBuffer = ByteBuffer.wrap(objectMapper.writeValueAsBytes(new RequestOption(dataLength, uuidBytes))); List<ByteBuffer> buffers = Arrays.asList(headerBuffer, dataBuffer, optionBuffer); synchronized (sender) { sender.sendWithAck(buffers, uuidBytes); } } else { ByteBuffer optionBuffer = ByteBuffer.wrap(objectMapper.writeValueAsBytes(new RequestOption(dataLength, null))); List<ByteBuffer> buffers = Arrays.asList(headerBuffer, dataBuffer, optionBuffer); synchronized (sender) { sender.send(buffers); } } }
@Override public void serialize(T t, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { var messagePackGenerator = (MessagePackGenerator) jsonGenerator; var byteArrayOutputStream = new ByteArrayOutputStream(); MessagePack.newDefaultPacker(byteArrayOutputStream).packLong(t.getId()).close(); var messagePackExtensionType = new MessagePackExtensionType(typeId, byteArrayOutputStream.toByteArray()); messagePackGenerator.writeExtensionType(messagePackExtensionType); } }
private void packObject(BObject object, MessagePacker packer) throws IOException { var tobePacked = new HashMap<String, BElement>(); for (var entry : object.entrySet()) { if (entry.getValue().isValue() || entry.getValue().isArray() || entry.getValue().isObject()) { tobePacked.put(entry.getKey(), entry.getValue()); } else if (log.isWarnEnabled()) { log.warn("Ignore key {} while packing bObject because of value cannot be packed in msgpack format", entry.getKey()); } } packer.packMapHeader(tobePacked.size()); for (var entry : tobePacked.entrySet()) { packer.packString(entry.getKey()); packAny(entry.getValue(), packer); } }
@Override public void writeTo(MessagePacker pk) throws IOException { pk.packBinaryHeader(data.length); pk.writePayload(data); }
@Override public void writeTo(MessagePacker pk) throws IOException { pk.packMapHeader(kvs.length / 2); for (int i = 0; i < kvs.length; i++) { kvs[i].writeTo(pk); } }
@Override public void writeTo(MessagePacker pk) throws IOException { if (type == Type.BIG_INTEGER) { pk.packBigInteger((BigInteger) objectValue); } else { pk.packLong(longValue); } } }
@Override public void serialize( Options options, OutputStream output, @Optional Object object ) { try( MessagePacker packer = MessagePack.newDefaultPacker( output ) ) { Value value = doSerialize( options, object, true ); packer.packValue( value ); packer.flush(); } catch( IOException ex ) { throw new SerializationException( "Unable to serialize " + object, ex ); } }
/** * Writes an Integer value. * * <p> * This method writes an integer using the smallest format from the int format family. * * @param bi the integer to be written * @return this * @throws IOException when underlying output throws IOException */ public MessagePacker packBigInteger(BigInteger bi) throws IOException { if (bi.bitLength() <= 63) { packLong(bi.longValue()); } else if (bi.bitLength() == 64 && bi.signum() == 1) { writeByteAndLong(UINT64, bi.longValue()); } else { throw new IllegalArgumentException("MessagePack cannot serialize BigInteger larger than 2^64-1"); } return this; }
@Override public void writeTo(MessagePacker pk) throws IOException { pk.packLong(value); }
private void packStringWithGetBytes(String s) throws IOException { // JVM performs various optimizations (memory allocation, reusing encoder etc.) when String.getBytes is used byte[] bytes = s.getBytes(MessagePack.UTF8); // Write the length and payload of small string to the buffer so that it avoids an extra flush of buffer packRawStringHeader(bytes.length); addPayload(bytes); }
private void packBigDecimal(BigDecimal decimal) throws IOException { MessagePacker messagePacker = getMessagePacker(); boolean failedToPackAsBI = false; try { //Check to see if this BigDecimal can be converted to BigInteger BigInteger integer = decimal.toBigIntegerExact(); messagePacker.packBigInteger(integer); } catch (ArithmeticException e) { failedToPackAsBI = true; } catch (IllegalArgumentException e) { failedToPackAsBI = true; } if (failedToPackAsBI) { double doubleValue = decimal.doubleValue(); //Check to make sure this BigDecimal can be represented as a double if (!decimal.stripTrailingZeros().toEngineeringString().equals(BigDecimal.valueOf(doubleValue).toEngineeringString())) { throw new IllegalArgumentException("MessagePack cannot serialize a BigDecimal that can't be represented as double. " + decimal); } messagePacker.packDouble(doubleValue); } }
@Override public void writeTo(MessagePacker pk) throws IOException { pk.packDouble(doubleValue); } }
@Override public void writeTo(MessagePacker packer) throws IOException { packer.packBoolean(value); }