java.lang.Object
org.freedesktop.dbus.messages.Message
- Direct Known Subclasses:
DBusSignal,Error,MethodBase
Superclass of all messages which are sent over the Bus.
This class deals with all the marshalling to/from the wire format.
This class deals with all the marshalling to/from the wire format.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static enum(package private) static interfaceInterface defining a method to extract a specific data type.(package private) static final recordAdditional options to optimize value extraction. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Object[]private booleanprivate byte[]private longprivate static final intSteps to increment the buffer array.private intprivate longprivate static final Message.ExtractOptionsDefault extraction options.private booleanprivate final List<FileDescriptor> private byteprivate static final AtomicLongprivate final Object[]protected final org.slf4j.Loggerstatic final intstatic final intstatic final intprivate static final intPosition of data offset in int array.private static final intPosition of signature offset in int array.private byte[]private static byte[][]Keep a static reference to each size of padding array to prevent allocation.private intprivate intstatic final byteThe current protocol major version.private byteprivate longprivate byteprivate byte[][] -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected intalign(int _current, byte _type) Align a counter to the given type.protected voidAppend a series of values to the message.protected voidappendByte(byte _b) Appends a byte to the buffer list.protected voidappendBytes(byte[] _buf) Appends a buffer to the buffer list.protected voidappendint(long _l, int _width) Marshalls an integer of a given width and appends it to the message.private intAppends a value to the message.(package private) MessageCreates a clone of this message and setting serial to next available.protected Object[]createHeaderArgs(byte _header, String _argType, Object _value) Creates a message header.static longdemarshallint(byte[] _buf, int _ofs, byte _endian, int _width) Demarshalls an integer of a given width from a buffer.protected longdemarshallint(byte[] _buf, int _ofs, int _width) Demarshalls an integer of a given width from a buffer.static longdemarshallintBig(byte[] _buf, int _ofs, int _width) Demarshalls an integer of a given width from a buffer using big-endian format.static longdemarshallintLittle(byte[] _buf, int _ofs, int _width) Demarshalls an integer of a given width from a buffer using little-endian format.(package private) StringDumps the current content ofwiredatato String.private voidensureBuffers(int _num) Ensures there are enough free buffers.protected Object[]extract(String _signature, byte[] _dataBuf, int[] _offsets, Message.ExtractOptions _options) Demarshall values from a buffer.(package private) Object[]extract(String _signature, byte[] _dataBuf, int[] _offsets, Message.ExtractOptions _options, Message.ExtractMethod _method) protected Object[]extract(String _signature, byte[] _dataBuf, int _offsets, Message.ExtractOptions _options) Demarshall values from a buffer.private Object[]extractArgs(List<Type[]> _constructorArgs) Creates a object array containing all objects which should be used to call a constructor.private ObjectextractArray(byte[] _signatureBuf, byte[] _dataBuf, int[] _offsets, Message.ExtractOptions _options, Message.ExtractMethod _extractMethod) Extracts an array from the data received on bus.private ObjectextractByte(byte[] _dataBuf, int[] _offsets) Extracts a byte from the data received on bus.(package private) Object[]extractHeader(byte[] _headers) Extracts the header information from the given byte array.private ObjectextractOne(byte[] _signatureBuf, byte[] _dataBuf, int[] _offsets, Message.ExtractOptions _options) Demarshall one value from a buffer.private ObjectextractStruct(byte[] _signatureBuf, byte[] _dataBuf, int[] _offsets, Message.ExtractOptions _options, Message.ExtractMethod _extractMethod) Extracts a struct from the data received on bus.private ObjectextractVariant(byte[] _dataBuf, int[] _offsets, Message.ExtractOptions _options, BiFunction<String, Object, Object> _variantFactory) Extracts aVariantfrom the data received on bus.static intgetAlignment(byte _type) Return the alignment for a given type.(package private) longprotected longReturns the destination of the message.byteintgetFlags()Returns the message flags.protected Object[]protected ObjectgetHeader(byte _type) Returns the value of the header field of a given field.static StringgetHeaderFieldName(byte _field) Returns the name of the given header field.Returns the interface of the message.getName()Returns the member name or error name this message represents.Object[]Parses and returns the parameters to this message as an Object array.Object[]getParameters(List<Type[]> _constructorArgs) Parses and returns the parameters to this message as an Object array.getPath()Returns the object path of the message.(package private) bytelongIf this is a reply to a message, this returns its serial.longReturns the message serial ID (unique for this connection)getSig()Returns the dbus signature of the parameters.Returns the Bus ID that sent the message.bytegetType()Type of this message.byte[][]protected voidmarshallint(long _l, byte[] _buf, int _ofs, int _width) Marshalls an integer of a given width into a buffer.static voidmarshallintBig(long _l, byte[] _buf, int _ofs, int _width) Marshalls an integer of a given width into a buffer using big-endian format.static voidmarshallintLittle(long _l, byte[] _buf, int _ofs, int _width) Marshalls an integer of a given width into a buffer using little-endian format.private ObjectoptimizePrimitives(byte[] _signatureBuf, byte[] _dataBuf, int[] _offsets, long _size, byte _algn, int _length, Message.ExtractOptions _options, Message.ExtractMethod _extractMethod) Will create primitive arrays when an array is read.protected voidpad(byte _type) Pad the message to the proper alignment for the given type.protected voidpadAndMarshall(List<Object> _hargs, long _serial, String _sig, Object... _args) Adds message padding and marshalling.(package private) voidpopulate(byte[] _msg, byte[] _headers, byte[] _body, List<FileDescriptor> _descriptors) Create a message from wire-format data.private voidpreallocate(int _num) Create a buffer of num bytes.private intprepareCollection(byte[] _signatureBuf, int[] _offsets, long _size) private ObjectreadHeaderVariants(byte[] _signatureBuf, byte[] _dataBuf, int[] _offsets, Message.ExtractOptions _options) Special lightweight version to read the variant objects in DBus message header.voidprotected voidsetByteCounter(long _bytecounter) protected voidSet header content.protected voidsetSerial(long _serial) voidWarning, do not use this method unless you really know what you are doing.protected voidsetWireData(byte[][] _wiredata) toString()Formats the message in a human-readable format.voidupdateEndianess(byte _endianess) (package private) static List<Message.ConstructorArgType> usesPrimitives(List<Type[]> _constructorArgs, List<Type> _dataType) Compares a list of Type[] with a list of desired types.
-
Field Details
-
MAXIMUM_ARRAY_LENGTH
public static final int MAXIMUM_ARRAY_LENGTH- See Also:
-
MAXIMUM_MESSAGE_LENGTH
public static final int MAXIMUM_MESSAGE_LENGTH- See Also:
-
MAXIMUM_NUM_UNIX_FDS
public static final int MAXIMUM_NUM_UNIX_FDS- See Also:
-
PROTOCOL
public static final byte PROTOCOLThe current protocol major version.- See Also:
-
DEFAULT_OPTIONS
Default extraction options. -
OFFSET_DATA
private static final int OFFSET_DATAPosition of data offset in int array.- See Also:
-
OFFSET_SIG
private static final int OFFSET_SIGPosition of signature offset in int array.- See Also:
-
padding
private static byte[][] paddingKeep a static reference to each size of padding array to prevent allocation. -
BUFFERINCREMENT
private static final int BUFFERINCREMENTSteps to increment the buffer array.- See Also:
-
GLOBAL_SERIAL
-
logger
protected final org.slf4j.Logger logger -
filedescriptors
-
headers
-
wiredata
private byte[][] wiredata -
bytecounter
private long bytecounter -
serial
private long serial -
type
private byte type -
flags
private byte flags -
protover
private byte protover -
big
private boolean big -
args
-
body
private byte[] body -
bodylen
private long bodylen -
preallocated
private int preallocated -
paofs
private int paofs -
pabuf
private byte[] pabuf -
bufferuse
private int bufferuse -
endianWasSet
private boolean endianWasSet
-
-
Constructor Details
-
Message
Create a message; only to be called by sub-classes.- Parameters:
_endian- The endianness to create the message._type- The message type._flags- Any message flags.- Throws:
DBusException- on error
-
Message
protected Message()Create a blank message. Only to be used when calling populate.
-
-
Method Details
-
updateEndianess
public void updateEndianess(byte _endianess) -
populate
void populate(byte[] _msg, byte[] _headers, byte[] _body, List<FileDescriptor> _descriptors) throws DBusException Create a message from wire-format data.- Parameters:
_msg- D-Bus serialized data of type yyyuu_headers- D-Bus serialized data of type a(yv)_body- D-Bus serialized data of the signature defined in headers.- Throws:
DBusException
-
getHeader
-
setHeader
Set header content.nullvalue is ignored.- Parameters:
_header- header to set
-
getByteCounter
protected long getByteCounter() -
setByteCounter
protected void setByteCounter(long _bytecounter) -
setSerial
protected void setSerial(long _serial) -
setWireData
protected void setWireData(byte[][] _wiredata) -
getProtover
byte getProtover() -
getBodylen
long getBodylen() -
preallocate
private void preallocate(int _num) Create a buffer of num bytes. Data is copied to this rather than added to the buffer list. -
ensureBuffers
private void ensureBuffers(int _num) Ensures there are enough free buffers.- Parameters:
_num- number of free buffers to create.
-
appendBytes
protected void appendBytes(byte[] _buf) Appends a buffer to the buffer list.- Parameters:
_buf- buffer byte array
-
appendByte
protected void appendByte(byte _b) Appends a byte to the buffer list.- Parameters:
_b- byte
-
demarshallint
protected long demarshallint(byte[] _buf, int _ofs, int _width) Demarshalls an integer of a given width from a buffer. Endianness is determined from the format of the message.- Parameters:
_buf- The buffer to demarshall from._ofs- The offset to demarshall from._width- The byte-width of the int.- Returns:
- long
-
appendint
protected void appendint(long _l, int _width) Marshalls an integer of a given width and appends it to the message. Endianness is determined from the message.- Parameters:
_l- The integer to marshall._width- The byte-width of the int.
-
marshallint
protected void marshallint(long _l, byte[] _buf, int _ofs, int _width) Marshalls an integer of a given width into a buffer. Endianness is determined from the message.- Parameters:
_l- The integer to marshall._buf- The buffer to marshall to._ofs- The offset to marshall to._width- The byte-width of the int.
-
getWireData
public byte[][] getWireData() -
getFiledescriptors
-
toString
Formats the message in a human-readable format. -
getHeader
Returns the value of the header field of a given field.- Parameters:
_type- The field to return.- Returns:
- The value of the field or null if unset.
-
pad
protected void pad(byte _type) Pad the message to the proper alignment for the given type.- Parameters:
_type- type
-
append
Append a series of values to the message.- Parameters:
_sig- The signature(s) of the value(s)._data- The value(s).- Throws:
DBusException- on error
-
appendOne
Appends a value to the message. The type of the value is read from a D-Bus signature and used to marshall the value.- Parameters:
_sigb- A buffer of the D-Bus signature._sigofs- The offset into the signature corresponding to this value._data- The value to marshall.- Returns:
- The offset into the signature of the end of this value's type.
- Throws:
DBusException
-
align
protected int align(int _current, byte _type) Align a counter to the given type.- Parameters:
_current- The current counter._type- The type to align to.- Returns:
- The new, aligned, counter.
-
extractHeader
Extracts the header information from the given byte array.- Parameters:
_headers- D-Bus serialized data of type a(yv)- Returns:
- Object array containing header data
- Throws:
DBusException- when parsing fails
-
readHeaderVariants
private Object readHeaderVariants(byte[] _signatureBuf, byte[] _dataBuf, int[] _offsets, Message.ExtractOptions _options) throws DBusException Special lightweight version to read the variant objects in DBus message header. This method will not createVariantobjects it directly extracts the Variant data content.- Parameters:
_signatureBuf- DBus signature string as byte array_dataBuf- buffer with header data_offsets- current offsets_options- additional options- Returns:
- Object
- Throws:
DBusException- when parsing fails
-
extractOne
private Object extractOne(byte[] _signatureBuf, byte[] _dataBuf, int[] _offsets, Message.ExtractOptions _options) throws DBusException Demarshall one value from a buffer.- Parameters:
_signatureBuf- A buffer of the D-Bus signature._dataBuf- The buffer to demarshall from._offsets- An array of two ints, which holds the position of the current signature offset and the current offset of the data buffer._options- extract options- Returns:
- The demarshalled value.
- Throws:
DBusException
-
extractByte
Extracts a byte from the data received on bus.- Parameters:
_dataBuf- buffer holding the byte_offsets- offset position in buffer (will be updated)- Returns:
- Object
-
extractStruct
private Object extractStruct(byte[] _signatureBuf, byte[] _dataBuf, int[] _offsets, Message.ExtractOptions _options, Message.ExtractMethod _extractMethod) throws DBusException Extracts a struct from the data received on bus.- Parameters:
_signatureBuf- signature (as byte array) defining the struct content_dataBuf- buffer containing the struct_offsets- offset position in buffer (will be updated)_options- extract options_extractMethod- method to be called for every entry contained of the struct- Returns:
- Object
- Throws:
DBusException- when parsing fails
-
extractArray
private Object extractArray(byte[] _signatureBuf, byte[] _dataBuf, int[] _offsets, Message.ExtractOptions _options, Message.ExtractMethod _extractMethod) throws MarshallingException, DBusException Extracts an array from the data received on bus.- Parameters:
_signatureBuf- signature string (as byte array) of the content of the array_dataBuf- buffer containing the array to read_offsets- current offsets in the buffer (will be updated)_options- additional options_extractMethod- method to be called for every entry contained in the array- Returns:
- Object
- Throws:
MarshallingException- when Array is too largeDBusException- when parsing fails
-
extractVariant
private Object extractVariant(byte[] _dataBuf, int[] _offsets, Message.ExtractOptions _options, BiFunction<String, Object, throws DBusExceptionObject> _variantFactory) Extracts aVariantfrom the data received on bus.- Parameters:
_dataBuf- buffer containing the variant_offsets- current offsets in the buffer (will be updated)_options- extract options_variantFactory- method to create newVariantobjects (or other object types)- Returns:
- Object / Variant
- Throws:
DBusException- when parsing fails
-
optimizePrimitives
private Object optimizePrimitives(byte[] _signatureBuf, byte[] _dataBuf, int[] _offsets, long _size, byte _algn, int _length, Message.ExtractOptions _options, Message.ExtractMethod _extractMethod) throws DBusException Will create primitive arrays when an array is read.
In case the array is not compatible with primitives (e.g. object types are used or array contains Struct/Maps etc) an array of the appropriate type will be created.- Parameters:
_signatureBuf- signature string (as byte array) containing the type of array_dataBuf- buffer containing the array_offsets- current offset in buffer (will be updated)_size- size of a byte_algn- data offset padding width when reading primitives (except byte)_length- length of the array_options- extract options_extractMethod- method to be called for every entry contained in the array if not primitive array- Returns:
- Object array
- Throws:
DBusException- when parsing fails
-
prepareCollection
private int prepareCollection(byte[] _signatureBuf, int[] _offsets, long _size) throws DBusException - Throws:
DBusException
-
extract
protected Object[] extract(String _signature, byte[] _dataBuf, int _offsets, Message.ExtractOptions _options) throws DBusException Demarshall values from a buffer.- Parameters:
_signature- The D-Bus signature(s) of the value(s)._dataBuf- The buffer to demarshall from._offsets- The offset into the data buffer to start._options- additional options- Returns:
- The demarshalled value(s).
- Throws:
DBusException- on error
-
extract
protected Object[] extract(String _signature, byte[] _dataBuf, int[] _offsets, Message.ExtractOptions _options) throws DBusException Demarshall values from a buffer.- Parameters:
_signature- The D-Bus signature(s) of the value(s)._dataBuf- The buffer to demarshall from._offsets- An array of two ints, which holds the position of the current signature offset and the current offset of the data buffer. These values will be updated to the start of the next value after demarshalling._options- additional options- Returns:
- The demarshalled value(s).
- Throws:
DBusException- on error
-
extract
Object[] extract(String _signature, byte[] _dataBuf, int[] _offsets, Message.ExtractOptions _options, Message.ExtractMethod _method) throws DBusException - Throws:
DBusException
-
getSource
Returns the Bus ID that sent the message.- Returns:
- string
-
getDestination
Returns the destination of the message.- Returns:
- string
-
getInterface
Returns the interface of the message.- Returns:
- string
-
getPath
Returns the object path of the message.- Returns:
- string
-
getName
Returns the member name or error name this message represents.- Returns:
- string
-
getSig
Returns the dbus signature of the parameters.- Returns:
- string
-
getFlags
public int getFlags()Returns the message flags.- Returns:
- int
-
getSerial
public long getSerial()Returns the message serial ID (unique for this connection)- Returns:
- the message serial.
-
getReplySerial
public long getReplySerial()If this is a reply to a message, this returns its serial.- Returns:
- The reply serial, or 0 if it is not a reply.
-
getParameters
Parses and returns the parameters to this message as an Object array.- Returns:
- object array
- Throws:
DBusException- on failure
-
getParameters
Parses and returns the parameters to this message as an Object array. This method takes a list of Type[] where each entry of the list represents a constructor call. The constructor arguments are used to determine if a collection of array of primitive type should be used when calling the constructor.- Parameters:
_constructorArgs- list of desired constructor arguments- Returns:
- object array
- Throws:
DBusException- on failure
-
extractArgs
Creates a object array containing all objects which should be used to call a constructor.- Parameters:
_constructorArgs- list of desired constructor arguments- Returns:
- object array
- Throws:
DBusException- on failure
-
usesPrimitives
static List<Message.ConstructorArgType> usesPrimitives(List<Type[]> _constructorArgs, List<Type> _dataType) Compares a list of Type[] with a list of desired types. This is used to decide if an array of primitive types, an array of object types or a Collection/List of a object type is used in the constructor.- Parameters:
_constructorArgs- list of constructor types to check_dataType- list of desired types- Returns:
- List of ConstructorArgType
-
setArgs
-
setSource
Warning, do not use this method unless you really know what you are doing.- Parameters:
_source- string- Throws:
DBusException- on error
-
dumpWireData
String dumpWireData()Dumps the current content ofwiredatato String.- Returns:
- String, maybe empty
- Since:
- v4.2.2 - 2023-01-20
-
getType
public byte getType()Type of this message.- Returns:
- byte
-
getEndianess
public byte getEndianess() -
createHeaderArgs
Creates a message header. Will automatically add the values to the current instances header map.- Parameters:
_header- header type (one ofHeaderField)_argType- argument type (one ofArgumentType)_value- value- Returns:
- Object array
-
padAndMarshall
protected void padAndMarshall(List<Object> _hargs, long _serial, String _sig, Object... _args) throws DBusException Adds message padding and marshalling.- Parameters:
_hargs-_serial-_sig-_args-- Throws:
DBusException
-
demarshallint
public static long demarshallint(byte[] _buf, int _ofs, byte _endian, int _width) Demarshalls an integer of a given width from a buffer.- Parameters:
_buf- The buffer to demarshall from._ofs- The offset to demarshall from._endian- The endianness to use in demarshalling._width- The byte-width of the int.- Returns:
- long
-
demarshallintBig
public static long demarshallintBig(byte[] _buf, int _ofs, int _width) Demarshalls an integer of a given width from a buffer using big-endian format.- Parameters:
_buf- The buffer to demarshall from._ofs- The offset to demarshall from._width- The byte-width of the int.- Returns:
- long
-
demarshallintLittle
public static long demarshallintLittle(byte[] _buf, int _ofs, int _width) Demarshalls an integer of a given width from a buffer using little-endian format.- Parameters:
_buf- The buffer to demarshall from._ofs- The offset to demarshall from._width- The byte-width of the int.- Returns:
- long
-
marshallintBig
public static void marshallintBig(long _l, byte[] _buf, int _ofs, int _width) Marshalls an integer of a given width into a buffer using big-endian format.- Parameters:
_l- The integer to marshall._buf- The buffer to marshall to._ofs- The offset to marshall to._width- The byte-width of the int.
-
marshallintLittle
public static void marshallintLittle(long _l, byte[] _buf, int _ofs, int _width) Marshalls an integer of a given width into a buffer using little-endian format.- Parameters:
_l- The integer to marshall._buf- The buffer to demarshall to._ofs- The offset to demarshall to._width- The byte-width of the int.
-
getAlignment
public static int getAlignment(byte _type) Return the alignment for a given type.- Parameters:
_type- type- Returns:
- int
-
getHeaderFieldName
Returns the name of the given header field.- Parameters:
_field- field- Returns:
- string
-
cloneWithNewSerial
Message cloneWithNewSerial()Creates a clone of this message and setting serial to next available.- Returns:
- Message
-