1 package software.amazon.awssdk.crt.eventstream; 2 3 import software.amazon.awssdk.crt.CrtResource; 4 5 import java.nio.ByteBuffer; 6 import java.util.List; 7 8 /** 9 * Wrapper around an instance of aws-event-stream-message. It's auto closable, so be sure 10 * to call close when finished with the object. 11 */ 12 public class Message extends CrtResource { 13 /** 14 * Creates a message using headers and payload. 15 * @param headers list of headers to include in the message's header block. Can be null. 16 * @param payload payload body to include in the message's payload block. Can be null. 17 */ Message(List<Header> headers, byte[] payload)18 public Message(List<Header> headers, byte[] payload) { 19 acquireNativeHandle(messageNew(Header.marshallHeadersForJNI(headers), payload)); 20 } 21 22 /** 23 * Get the binary format of this message (i.e. for sending across the wire manually) 24 * @return ByteBuffer wrapping the underlying message data. This buffer is only valid 25 * as long as the message itself is valid. 26 */ getMessageBuffer()27 public ByteBuffer getMessageBuffer() { 28 return messageBuffer(getNativeHandle()); 29 } 30 31 @Override releaseNativeHandle()32 protected void releaseNativeHandle() { 33 if (!isNull()) { 34 messageDelete(getNativeHandle()); 35 } 36 } 37 38 @Override canReleaseReferencesImmediately()39 protected boolean canReleaseReferencesImmediately() { 40 return true; 41 } 42 messageNew(byte[] serializedHeaders, byte[] payload)43 private static native long messageNew(byte[] serializedHeaders, byte[] payload); messageDelete(long messageHandle)44 private static native void messageDelete(long messageHandle); messageBuffer(long messageHandle)45 private static native ByteBuffer messageBuffer(long messageHandle); 46 } 47