public class Message extends Object implements Streamable, Constructable<Message>
The byte buffer can point to a reference, and we can subset it using index and length. However, when the message is serialized, we only write the bytes between index and length.
Modifier and Type | Class and Description |
---|---|
static class |
Message.Flag |
static class |
Message.TransientFlag |
Modifier and Type | Field and Description |
---|---|
protected byte[] |
buf
The payload
|
protected Address |
dest_addr |
protected short |
flags |
protected Header[] |
headers
All headers are placed here
|
protected int |
length
The number of bytes in the buffer (usually buf.length is buf not equal to null).
|
protected int |
offset
The index into the payload (usually 0)
|
protected Address |
src_addr |
protected byte |
transient_flags |
Constructor and Description |
---|
Message() |
Message(Address dest)
Constructs a message given a destination address
|
Message(Address dest,
Buffer buf) |
Message(Address dest,
byte[] buf)
Constructs a message given a destination and source address and the payload byte buffer
|
Message(Address dest,
byte[] buf,
int offset,
int length)
Constructs a message.
|
Message(Address dest,
Object obj)
Constructs a message given a destination and source address and the payload object
|
Message(boolean create_headers) |
Modifier and Type | Method and Description |
---|---|
byte[] |
buffer() |
Message |
buffer(Buffer b) |
Message |
buffer(byte[] b) |
Buffer |
buffer2() |
Message |
clearFlag(Message.Flag... flags)
Clears a number of flags in a message
|
Message |
clearTransientFlag(Message.TransientFlag... flags) |
Message |
copy() |
Message |
copy(boolean copy_buffer)
Create a copy of the message.
|
Message |
copy(boolean copy_buffer,
boolean copy_headers)
Create a copy of the message.
|
Message |
copy(boolean copy_buffer,
short starting_id)
Doesn't copy any headers except for those with ID >= copy_headers_above
|
Message |
copy(boolean copy_buffer,
short starting_id,
short... copy_only_ids)
Copies a message.
|
Supplier<? extends Message> |
create()
Creates an instance of the class implementing this interface
|
protected static Header[] |
createHeaders(int size) |
Address |
dest() |
Message |
dest(Address new_dest) |
static String |
flagsToString(short flags) |
byte[] |
getBuffer()
Returns a copy of the buffer if offset and length are used, otherwise a reference.
|
Buffer |
getBuffer2() |
Address |
getDest() |
short |
getFlags()
Returns the internal representation of flags.
|
<T extends Header> |
getHeader(short... ids)
Returns a header for a range of IDs, or null if not found
|
<T extends Header> |
getHeader(short id) |
Map<Short,Header> |
getHeaders()
Returns a reference to the headers hashmap, which is immutable.
|
int |
getLength() |
int |
getNumHeaders() |
<T> T |
getObject() |
<T> T |
getObject(ClassLoader loader)
Uses custom serialization to create an object from the buffer of the message.
|
int |
getOffset() |
byte[] |
getRawBuffer()
Returns a reference to the payload (byte buffer).
|
Address |
getSrc() |
short |
getTransientFlags() |
boolean |
isFlagSet(Message.Flag flag)
Checks if a given flag is set
|
static boolean |
isFlagSet(short flags,
Message.Flag flag) |
boolean |
isTransientFlagSet(Message.TransientFlag flag) |
static boolean |
isTransientFlagSet(short flags,
Message.TransientFlag flag) |
int |
length() |
Message |
makeReply() |
int |
numHeaders() |
int |
offset() |
String |
printHeaders() |
String |
printObjectHeaders() |
Message |
putHeader(short id,
Header hdr)
Puts a header given an ID into the hashmap.
|
byte[] |
rawBuffer() |
void |
readFrom(DataInput in)
Read the state of the current object (including superclasses) from instream
Note that the input stream must not be closed
|
int |
readFromSkipPayload(ByteArrayDataInputStream in)
Reads the message's contents from an input stream, but skips the buffer and instead returns the
position (offset) at which the buffer starts
|
protected static Header |
readHeader(DataInput in) |
Message |
setBuffer(Buffer buf)
Sets the buffer
Note that the byte[] buffer passed as argument must not be modified.
|
Message |
setBuffer(byte[] b)
Sets the buffer.
Note that the byte[] buffer passed as argument must not be modified.
|
Message |
setBuffer(byte[] b,
int offset,
int length)
Sets the internal buffer to point to a subset of a given buffer.
Note that the byte[] buffer passed as argument must not be modified.
|
Message |
setDest(Address new_dest) |
Message |
setFlag(Message.Flag... flags)
Sets a number of flags in a message
|
Message |
setFlag(short flag)
Sets the flags from a short.
|
Message |
setObject(Object obj)
Takes an object and uses Java serialization to generate the byte[] buffer which is set in the
message.
|
Message |
setSrc(Address new_src) |
Message |
setTransientFlag(Message.TransientFlag... flags)
Same as
setFlag(Flag...) except that transient flags are not marshalled |
Message |
setTransientFlag(short flag) |
boolean |
setTransientFlagIfAbsent(Message.TransientFlag flag)
Atomically checks if a given flag is set and - if not - sets it.
|
long |
size()
Returns the exact size of the marshalled message.
|
Address |
src() |
Message |
src(Address new_src) |
String |
toString() |
static String |
transientFlagsToString(short flags) |
protected static void |
writeHeader(Header hdr,
DataOutput out) |
void |
writeTo(DataOutput out)
Streams all members (dest and src addresses, buffer and headers) to the output stream.
|
void |
writeToNoAddrs(Address src,
DataOutput out,
short... excluded_headers)
Writes the message to the output stream, but excludes the dest and src addresses unless the
src address given as argument is different from the message's src address
|
protected Address dest_addr
protected Address src_addr
protected byte[] buf
protected int offset
protected int length
protected volatile Header[] headers
protected volatile short flags
protected volatile byte transient_flags
public Message(Address dest)
dest
- The Address of the receiver. If it is null, then the message is sent to the group. Otherwise, it is
sent to a single member.public Message(Address dest, byte[] buf)
dest
- The Address of the receiver. If it is null, then the message is sent to the group. Otherwise, it is
sent to a single member.buf
- The payload. Note that this buffer must not be modified (e.g. buf[0]='x' is not
allowed) since we don't copy the contents.public Message(Address dest, byte[] buf, int offset, int length)
dest
- The Address of the receiver. If it is null, then the message is sent to the group. Otherwise, it is
sent to a single member.buf
- A reference to a byte bufferoffset
- The index into the byte bufferlength
- The number of bytes to be used from buf. Both index and length are checked
for array index violations and an ArrayIndexOutOfBoundsException will be thrown if invalidpublic Message(Address dest, Object obj)
dest
- The Address of the receiver. If it is null, then the message is sent to the group. Otherwise, it is
sent to a single member.obj
- The object that will be marshalled into the byte buffer. Has to be serializable (e.g. implementing
Serializable, Externalizable or Streamable, or be a basic type (e.g. Integer, Short etc)).public Message()
public Message(boolean create_headers)
public Supplier<? extends Message> create()
Constructable
create
in interface Constructable<Message>
public Address getDest()
public Address dest()
public Address getSrc()
public Address src()
public int getOffset()
public int offset()
public int getLength()
public int length()
public byte[] getRawBuffer()
public byte[] rawBuffer()
public byte[] buffer()
public Buffer buffer2()
public Message buffer(byte[] b)
public int getNumHeaders()
public int numHeaders()
public byte[] getBuffer()
public Buffer getBuffer2()
public Message setBuffer(byte[] b)
public Message setBuffer(byte[] b, int offset, int length)
b
- The reference to a given buffer. If null, we'll reset the buffer to nulloffset
- The initial positionlength
- The number of bytespublic Message setBuffer(Buffer buf)
public Map<Short,Header> getHeaders()
public String printHeaders()
public Message setObject(Object obj)
public <T> T getObject()
public <T> T getObject(ClassLoader loader)
public Message setFlag(Message.Flag... flags)
flags
- The flag or flagspublic Message setTransientFlag(Message.TransientFlag... flags)
setFlag(Flag...)
except that transient flags are not marshalledflags
- The flagpublic Message setFlag(short flag)
setFlag(org.jgroups.Message.Flag...)
instead),
as the internal representation of flags might change anytime.flag
- public Message setTransientFlag(short flag)
public short getFlags()
public short getTransientFlags()
public Message clearFlag(Message.Flag... flags)
flags
- The flagspublic Message clearTransientFlag(Message.TransientFlag... flags)
public static boolean isFlagSet(short flags, Message.Flag flag)
public boolean isFlagSet(Message.Flag flag)
flag
- The flagpublic static boolean isTransientFlagSet(short flags, Message.TransientFlag flag)
public boolean isTransientFlagSet(Message.TransientFlag flag)
public boolean setTransientFlagIfAbsent(Message.TransientFlag flag)
flag
- public Message putHeader(short id, Header hdr)
public <T extends Header> T getHeader(short id)
public <T extends Header> T getHeader(short... ids)
public Message copy()
public Message copy(boolean copy_buffer)
copy_buffer
- public Message copy(boolean copy_buffer, boolean copy_headers)
putHeader(short,Header)
again.copy_buffer
- copy_headers
- Copy the headerspublic Message copy(boolean copy_buffer, short starting_id)
copy_buffer
- starting_id
- public Message copy(boolean copy_buffer, short starting_id, short... copy_only_ids)
copy_buffer
- starting_id
- copy_only_ids
- public Message makeReply()
public String printObjectHeaders()
public void writeTo(DataOutput out) throws Exception
writeTo
in interface Streamable
out
- Exception
public void writeToNoAddrs(Address src, DataOutput out, short... excluded_headers) throws Exception
src
- out
- excluded_headers
- Don't marshal headers that are part of excluded_headersException
public void readFrom(DataInput in) throws Exception
Streamable
readFrom
in interface Streamable
Exception
public int readFromSkipPayload(ByteArrayDataInputStream in) throws Exception
Exception
public long size()
getLength()
) plus metadata (e.g. flags,
headers, source and dest addresses etc). Since the largest payload can be Integer.MAX_VALUE, adding the metadata
might lead to an int overflow, that's why we use a long.public static String flagsToString(short flags)
public static String transientFlagsToString(short flags)
protected static void writeHeader(Header hdr, DataOutput out) throws Exception
Exception
protected static Header[] createHeaders(int size)
Copyright © 2018 JBoss, a division of Red Hat. All rights reserved.