/** * 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(); } }
/** * 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(); } }
/** * 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(); } }
/** * 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(); } }
private void flushMessagePacker() throws IOException { MessagePacker messagePacker = getMessagePacker(); messagePacker.flush(); }
/** * Closes underlying output. * <p> * This method flushes internal buffer before closing. */ @Override public void close() throws IOException { try { flush(); } finally { out.close(); } }
/** * Replaces underlying output. * <p> * This method flushes current internal buffer to the output, swaps it with the new given output, then returns * the old output. * * <p> * This method doesn't close the old output. * * @param out new output * @return the old output * @throws IOException when underlying output throws IOException * @throws NullPointerException the given output is null */ public MessageBufferOutput reset(MessageBufferOutput out) throws IOException { // Validate the argument MessageBufferOutput newOut = checkNotNull(out, "MessageBufferOutput is null"); // Flush before reset flush(); MessageBufferOutput old = this.out; this.out = newOut; // Reset totalFlushBytes this.totalFlushBytes = 0; return old; }
@Override public void serialize(@NonNull BElement element, @NonNull OutputStream out) { try (var packer = MessagePack.newDefaultPacker(out)) { packAny(element, packer); packer.flush(); } catch (IOException e) { throw new BeanSerializationException("Error while serialize element", e); } }
@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); } } }
/** * Writes a byte array to the output. * <p> * This method is used with {@link #packRawStringHeader(int)} or {@link #packBinaryHeader(int)} methods. * * @param src the data to add * @param off the start offset in the data * @param len the number of bytes to add * @return this * @throws IOException when underlying output throws IOException */ public MessagePacker writePayload(byte[] src, int off, int len) throws IOException { if (buffer == null || buffer.size() - position < len || len > bufferFlushThreshold) { flush(); // call flush before write // Directly write payload to the output without using the buffer out.write(src, off, len); totalFlushBytes += len; } else { buffer.putBytes(position, src, off, len); position += len; } return this; }
/** * Writes a byte array to the output. * <p> * Unlike {@link #writePayload(byte[], int, int)} method, this method doesn't copy the byte array even when * given byte array is shorter than {@link MessagePack.PackerConfig#withBufferFlushThreshold(int)}. * This is faster than {@link #writePayload(byte[], int, int)} method but caller must not modify the byte array * after calling this method. * * @param src the data to add * @param off the start offset in the data * @param len the number of bytes to add * @return this * @throws IOException when underlying output throws IOException */ public MessagePacker addPayload(byte[] src, int off, int len) throws IOException { if (buffer == null || buffer.size() - position < len || len > bufferFlushThreshold) { flush(); // call flush before add // Directly add the payload without using the buffer out.add(src, off, len); totalFlushBytes += len; } else { buffer.putBytes(position, src, off, len); position += len; } return this; } }
@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 ); } }
messagePacker.flush(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); MessagePackGenerator messagePackGenerator = new MessagePackGenerator(getFeatureMask(), getCodec(), outputStream, packerConfig, false);