Class Message
- java.lang.Object
- 
- io.sisu.nng.Message
 
- 
- All Implemented Interfaces:
- java.lang.AutoCloseable
 
 public class Message extends java.lang.Object implements java.lang.AutoCloseableWraps the native NNG message structure and provides convenience methods for ferrying data to and from the JVM. Note: Per NNG's design, Message's may change "ownership" when being sent, which is a paradigm not native to Java. The valid flag is designed to indicate if it's safe to continue mutating the Message in the JVM (valid: true) or if it's expected that the Message is owned by the native NNG layer (and expected to have its backing memory freed automatically). 
- 
- 
Field SummaryFields Modifier and Type Field Description static java.util.concurrent.atomic.AtomicIntegercreatedstatic java.util.concurrent.atomic.AtomicIntegerfreedstatic java.util.concurrent.atomic.AtomicIntegerinvalidatedprotected java.util.concurrent.atomic.AtomicBooleanvalid
 - 
Constructor SummaryConstructors Constructor Description Message()Allocate a new Message with a 0-byte BodyMessage(int size)Allocate a new NNG Message of the given size.Message(com.sun.jna.Pointer pointer)Message(MessagePointer pointer)
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description voidappend(byte[] data)Append the given byte array to the body of the Messagevoidappend(com.sun.jna.Pointer p, int size)voidappend(java.lang.String s)Convenience method for appending a Java String to the Message body, assuming UTF-8 encoding.voidappend(java.lang.String s, java.nio.charset.Charset charset)Convenience method for appending a Java String to the Message body using the given Charset.voidappend(java.nio.ByteBuffer data)Append data from the givenByteBufferto the body of the Message, reallocating if necessary.voidappendToHeader(java.nio.ByteBuffer data)Append data from aByteBufferto the header of the Message, increasing the allocation for the header if required.voidappendU16(int i)Append an unsigned, 16-bit number in network byte order to the Message's bodyvoidappendU32(int i)Append an unsigned, 32-bit number in network byte order to the Message's bodyvoidappendU64(long i)Append an unsigned, 64-bit number in network byte order to the Message's bodyvoidclose()protected voidfinalize()Cleanup the Message, attempting to free it if required.voidfree()Attempt to deallocate the underlying nng_msg, releasing unmanaged memory.java.nio.ByteBuffergetBody()Returns a native ByteBuffer backed by the message's body data.java.nio.ByteBuffergetBodyCopy()Convenience method to extract data from native memory onto the JVM by allocating a non-native ByteBuffer and copying the data into it.intgetBodyLen()Get the current length of the Message's body in bytes.java.nio.ByteBuffergetHeader()Get a reference to the Message's header as a directByteBuffer, allowing for read/write access.intgetHeaderLen()Get the current length of the Message's header in bytes.MessagePointergetMessagePointer()voidinsertToHeader(java.nio.ByteBuffer data)Insert data from aByteBufferto the front of the header of the Message.voidinsertU16(int i)Prepends an unsigned, 16-bit number in network byte order to the Message's bodyvoidinsertU32(int i)Prepends an unsigned, 32-bit number in network byte order to the Message's bodyvoidinsertU64(long i)Prepends an unsigned, 64-bit number in network byte order to the Message's bodybooleanisValid()Check if the Message is still valid or not.voidsetInvalid()Set the message invalid, preventing the JVM from attempting to free the underlying native memory during garbage collection.voidsetValid()Set the Message state back to valid and owned by the JVM.voidtrim(int len)Removes bytes from the start of the Message body.inttrim32Bits()Removes 32 bits from the start of the Message body, returning then in network byte order.
 
- 
- 
- 
Constructor Detail- 
Messagepublic Message() throws NngExceptionAllocate a new Message with a 0-byte Body- Throws:
- NngException- if failed to allocate a new Message
 
 - 
Messagepublic Message(int size) throws NngExceptionAllocate a new NNG Message of the given size. Equivalent to a call tonng_msg_alloc - Parameters:
- size- number of bytes to allocate for the message
- Throws:
- NngException- if nng_msg_alloc fails
 
 - 
Messagepublic Message(com.sun.jna.Pointer pointer) throws NngException- Throws:
- NngException
 
 - 
Messagepublic Message(MessagePointer pointer) throws NngException - Throws:
- NngException
 
 
- 
 - 
Method Detail- 
appendToHeaderpublic void appendToHeader(java.nio.ByteBuffer data) throws NngExceptionAppend data from aByteBufferto the header of the Message, increasing the allocation for the header if required.- Parameters:
- data- the ByteBuffer with the data
- Throws:
- NngException- on failure to append
 
 - 
insertToHeaderpublic void insertToHeader(java.nio.ByteBuffer data) throws NngExceptionInsert data from aByteBufferto the front of the header of the Message.Note: the insertion does not occur "at a position" and will prepend the data. If looking to flip specific bits in the existing header, use getHeader()and manipulate it.- Parameters:
- data- the ByteBuffer with the data
- Throws:
- NngException- on failure to append
 
 - 
getMessagePointerpublic MessagePointer getMessagePointer() 
 - 
appendpublic void append(com.sun.jna.Pointer p, int size) throws NngException- Throws:
- NngException
 
 - 
appendpublic void append(java.nio.ByteBuffer data) throws NngExceptionAppend data from the givenByteBufferto the body of the Message, reallocating if necessary.- Parameters:
- data- the ByteBuffer with the data for appending
- Throws:
- NngException- on failure to append
 
 - 
appendpublic void append(byte[] data) throws NngExceptionAppend the given byte array to the body of the Message- Parameters:
- data- an array of bytes to append
- Throws:
- NngException- on error
 
 - 
appendpublic void append(java.lang.String s, java.nio.charset.Charset charset) throws NngExceptionConvenience method for appending a Java String to the Message body using the given Charset.Similar to using String.getBytes(Charset)andappend(byte[])- Parameters:
- s- the String to append
- charset- the charset used for interpreting the String
- Throws:
- NngException- if the call to nng_msg_append fails
 
 - 
appendpublic void append(java.lang.String s) throws NngExceptionConvenience method for appending a Java String to the Message body, assuming UTF-8 encoding.- Parameters:
- s- the String to append
- Throws:
- NngException- on error appending
 
 - 
appendU16public void appendU16(int i) throws NngExceptionAppend an unsigned, 16-bit number in network byte order to the Message's body- Parameters:
- i- the number to append
- Throws:
- NngException- on error
 
 - 
appendU32public void appendU32(int i) throws NngExceptionAppend an unsigned, 32-bit number in network byte order to the Message's body- Parameters:
- i- the number to append
- Throws:
- NngException- on error
 
 - 
appendU64public void appendU64(long i) throws NngExceptionAppend an unsigned, 64-bit number in network byte order to the Message's body- Parameters:
- i- the number to append
- Throws:
- NngException- on error
 
 - 
insertU16public void insertU16(int i) throws NngExceptionPrepends an unsigned, 16-bit number in network byte order to the Message's body- Parameters:
- i- the number to append
- Throws:
- NngException- on error
 
 - 
insertU32public void insertU32(int i) throws NngExceptionPrepends an unsigned, 32-bit number in network byte order to the Message's body- Parameters:
- i- the number to append
- Throws:
- NngException- on error
 
 - 
insertU64public void insertU64(long i) throws NngExceptionPrepends an unsigned, 64-bit number in network byte order to the Message's body- Parameters:
- i- the number to append
- Throws:
- NngException- on error
 
 - 
getBodyLenpublic int getBodyLen() Get the current length of the Message's body in bytes.Gives a measurement from native memory performed by the nng_body_len api call. - Returns:
- size of the body in number of bytes
 
 - 
getHeaderLenpublic int getHeaderLen() Get the current length of the Message's header in bytes.Gives a measurement from native memory performed by the nng_header_len api call. - Returns:
- size of the header in number of bytes
 
 - 
getHeaderpublic java.nio.ByteBuffer getHeader() Get a reference to the Message's header as a directByteBuffer, allowing for read/write access.Caution!: this method exists for now until a safer API can be implemented. You will have direct access to native memory for the header. No safety net is provided. You must prevent this ByteBuffer from being garbage collected before the Message is invalidated or freed. - Returns:
- a direct ByteBuffer accessing the header on success, or a zero-byte ByteBuffer on error
 
 - 
getBodypublic java.nio.ByteBuffer getBody() Returns a native ByteBuffer backed by the message's body data.Caution!: this method exists for now until a safer API can be implemented. You will have direct access to native memory for the body. No safety net is provided. You must prevent this ByteBuffer from being garbage collected before the Message is invalidated or freed. - Returns:
- new native ByteBuffer or an empty ByteBuffer if body length is zero
 
 - 
getBodyCopypublic java.nio.ByteBuffer getBodyCopy() Convenience method to extract data from native memory onto the JVM by allocating a non-native ByteBuffer and copying the data into it.- Returns:
- non-native ByteBuffer containing a copy of the message body data
 
 - 
trimpublic void trim(int len) throws NngExceptionRemoves bytes from the start of the Message body.- Parameters:
- len- number of bytes to remove
- Throws:
- NngException- on error
 
 - 
trim32Bitspublic int trim32Bits() throws NngExceptionRemoves 32 bits from the start of the Message body, returning then in network byte order.Note: it's assumed the value is in network byte order - Returns:
- an int value from the leading 32 bits of the message
- Throws:
- NngException- on error trimming the message
 
 - 
freepublic void free() Attempt to deallocate the underlying nng_msg, releasing unmanaged memory. This action only occurs if the Message is currently valid and owned by the JVM. If freed, the Message is no longer valid. Note: it's recommended that the programmer liberally make use forfree()when they know theirMessageinstance is no longer required and not to rely solely on the garbage collector. Native heap fragmentation can occur if large quantities of messages are freed, as will happen if waiting for a gc event to clean up. The end result typically is excessive memory use and in the worst cases result in out of memory conditions for the JVM process.
 - 
setValidpublic void setValid() Set the Message state back to valid and owned by the JVM. N.b. This should be used typically only when a Send of the Message fails and the nng library didn't take ownership.
 - 
setInvalidpublic void setInvalid() Set the message invalid, preventing the JVM from attempting to free the underlying native memory during garbage collection.
 - 
isValidpublic boolean isValid() Check if the Message is still valid or not.- Returns:
- boolean whether or not the Message is valid
 
 - 
finalizeprotected void finalize() throws java.lang.ThrowableCleanup the Message, attempting to free it if required.TODO: replace with the Java Cleaner api - Overrides:
- finalizein class- java.lang.Object
- Throws:
- java.lang.Throwable- only if the super's finalize throws an error
 
 - 
closepublic void close() - Specified by:
- closein interface- java.lang.AutoCloseable
 
 
- 
 
-