Class Message

  • Direct Known Subclasses:
    DBusSignal, Error, MethodBase

    public class Message
    extends java.lang.Object
    Superclass of all messages which are sent over the Bus. This class deals with all the marshalling to/from the wire format.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  Message.ArgumentType
      Defines constants for each argument type.
      static interface  Message.Endian
      Defines constants representing the endianness of the message.
      (package private) static interface  Message.ExtractMethod
      Interface defining a method to extract a specific data type.
      static interface  Message.Flags
      Defines constants representing the flags which can be set on a message.
      static interface  Message.HeaderField
      Defines constants for each valid header field type.
      static interface  Message.MessageType
      Defines constants for each message type.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Message()
      Create a blank message.
      protected Message​(byte _endian, byte _type, byte _flags)
      Create a message; only to be called by sub-classes.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      int align​(int _current, byte _type)
      Align a counter to the given type.
      void append​(java.lang.String _sig, java.lang.Object... _data)
      Append a series of values to the message.
      protected void appendByte​(byte _b)
      Appends a byte to the buffer list.
      protected void appendBytes​(byte[] _buf)
      Appends a buffer to the buffer list.
      void appendint​(long _l, int _width)
      Marshalls an integer of a given width and appends it to the message.
      private int appendOne​(byte[] _sigb, int _sigofs, java.lang.Object _data)
      Appends a value to the message.
      protected java.lang.Object[] createHeaderArgs​(byte _header, java.lang.String _argType, java.lang.Object _value)
      Creates a message header.
      static long demarshallint​(byte[] _buf, int _ofs, byte _endian, int _width)
      Demarshalls an integer of a given width from a buffer.
      long demarshallint​(byte[] _buf, int _ofs, int _width)
      Demarshalls an integer of a given width from a buffer.
      static long demarshallintBig​(byte[] _buf, int _ofs, int _width)
      Demarshalls an integer of a given width from a buffer using big-endian format.
      static long demarshallintLittle​(byte[] _buf, int _ofs, int _width)
      Demarshalls an integer of a given width from a buffer using little-endian format.
      (package private) java.lang.String dumpWireData()
      Dumps the current content of wiredata to String.
      private void ensureBuffers​(int _num)
      Ensures there are enough free buffers.
      java.lang.Object[] extract​(java.lang.String _signature, byte[] _dataBuf, int _offsets)
      Demarshall values from a buffer.
      java.lang.Object[] extract​(java.lang.String _signature, byte[] _dataBuf, int[] _offsets)
      Demarshall values from a buffer.
      (package private) java.lang.Object[] extract​(java.lang.String _signature, byte[] _dataBuf, int[] _offsets, Message.ExtractMethod _method)  
      private java.lang.Object extractArray​(byte[] _signatureBuf, byte[] _dataBuf, int[] _offsets, boolean _contained, Message.ExtractMethod _extractMethod)
      Extracts an array from the data received on bus.
      private java.lang.Object extractByte​(byte[] _dataBuf, int[] _offsets)
      Extracts a byte from the data received on bus.
      (package private) java.lang.Object[] extractHeader​(byte[] _headers)
      Extracts the header information from the given byte array.
      private java.lang.Object extractOne​(byte[] _signatureBuf, byte[] _dataBuf, int[] _offsets, boolean _contained)
      Demarshall one value from a buffer.
      private java.lang.Object extractStruct​(byte[] _signatureBuf, byte[] _dataBuf, int[] _offsets, Message.ExtractMethod _extractMethod)
      Extracts a struct from the data received on bus.
      private java.lang.Object extractVariant​(byte[] _dataBuf, int[] _offsets, java.util.function.BiFunction<java.lang.String,​java.lang.Object,​java.lang.Object> _variantFactory)
      Extracts a Variant from the data received on bus.
      static int getAlignment​(byte _type)
      Return the alignment for a given type.
      (package private) long getBodylen()  
      protected long getByteCounter()  
      java.lang.String getDestination()
      Returns the destination of the message.
      byte getEndianess()  
      java.util.List<FileDescriptor> getFiledescriptors()  
      int getFlags()
      Returns the message flags.
      protected java.lang.Object[] getHeader()  
      java.lang.Object getHeader​(byte _type)
      Returns the value of the header field of a given field.
      static java.lang.String getHeaderFieldName​(byte _field)
      Returns the name of the given header field.
      protected java.util.Map<java.lang.Byte,​java.lang.Object> getHeaders()
      Deprecated, for removal: This API element is subject to removal in a future version.
      use getHeader().
      java.lang.String getInterface()
      Returns the interface of the message.
      java.lang.String getName()
      Returns the member name or error name this message represents.
      java.lang.Object[] getParameters()
      Parses and returns the parameters to this message as an Object array.
      java.lang.String getPath()
      Returns the object path of the message.
      (package private) byte getProtover()  
      long getReplySerial()
      If this is a reply to a message, this returns its serial.
      long getSerial()
      Returns the message serial ID (unique for this connection)
      java.lang.String getSig()
      Returns the dbus signature of the parameters.
      java.lang.String getSource()
      Returns the Bus ID that sent the message.
      byte getType()
      Type of this message.
      protected byte[][] getWiredata()  
      byte[][] getWireData()  
      void marshallint​(long _l, byte[] _buf, int _ofs, int _width)
      Marshalls an integer of a given width into a buffer.
      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.
      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.
      private java.lang.Object optimizePrimitives​(byte[] _signatureBuf, byte[] _dataBuf, int[] _offsets, long _size, byte _algn, int _length, Message.ExtractMethod _extractMethod)
      Will create primitive arrays when an array is read.
      void pad​(byte _type)
      Pad the message to the proper alignment for the given type.
      protected void padAndMarshall​(java.util.List<java.lang.Object> _hargs, long _serial, java.lang.String _sig, java.lang.Object... _args)
      Adds message padding and marshalling.
      (package private) void populate​(byte[] _msg, byte[] _headers, byte[] _body, java.util.List<FileDescriptor> _descriptors)
      Create a message from wire-format data.
      private void preallocate​(int _num)
      Create a buffer of num bytes.
      private int prepareCollection​(byte[] _signatureBuf, int[] _offsets, long _size)  
      private java.lang.Object readHeaderVariants​(byte[] _signatureBuf, byte[] _dataBuf, int[] _offsets, boolean _contained)
      Special lightweight version to read the variant objects in DBus message header.
      void setArgs​(java.lang.Object[] _args)  
      protected void setByteCounter​(long _bytecounter)  
      protected void setHeader​(java.lang.Object[] _header)
      Set header content.
      protected void setSerial​(long _serial)  
      void setSource​(java.lang.String _source)
      Warning, do not use this method unless you really know what you are doing.
      protected void setWiredata​(byte[][] _wiredata)  
      java.lang.String toString()
      Formats the message in a human-readable format.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • PROTOCOL

        public static final byte PROTOCOL
        The current protocol major version.
        See Also:
        Constant Field Values
      • OFFSET_DATA

        private static final int OFFSET_DATA
        Position of data offset in int array.
        See Also:
        Constant Field Values
      • OFFSET_SIG

        private static final int OFFSET_SIG
        Position of signature offset in int array.
        See Also:
        Constant Field Values
      • padding

        private static byte[][] padding
        Keep a static reference to each size of padding array to prevent allocation.
      • BUFFERINCREMENT

        private static final int BUFFERINCREMENT
        Steps to increment the buffer array.
        See Also:
        Constant Field Values
      • GLOBAL_SERIAL

        private static final java.util.concurrent.atomic.AtomicLong GLOBAL_SERIAL
      • logger

        protected final org.slf4j.Logger logger
      • filedescriptors

        private final java.util.List<FileDescriptor> filedescriptors
      • headers

        private final java.lang.Object[] 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

        private java.lang.Object[] 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
    • Constructor Detail

      • Message

        protected Message​(byte _endian,
                          byte _type,
                          byte _flags)
                   throws DBusException
        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 Detail

      • populate

        void populate​(byte[] _msg,
                      byte[] _headers,
                      byte[] _body,
                      java.util.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
      • getHeaders

        @Deprecated(forRemoval=true,
                    since="4.2.2 - 2023-01-19")
        protected java.util.Map<java.lang.Byte,​java.lang.Object> getHeaders()
        Deprecated, for removal: This API element is subject to removal in a future version.
        use getHeader(). This method did return a map containing message header. It allows changing the map, but changes did not result in changing the actual message header. Therefore using a map was removed and an object array is used instead. Changes to that array (content) will be result in a changed header in the message.
        Returns:
        map of header
      • getHeader

        protected java.lang.Object[] getHeader()
      • setHeader

        protected void setHeader​(java.lang.Object[] _header)
        Set header content. null value is ignored.
        Parameters:
        _header - header to set
      • getByteCounter

        protected long getByteCounter()
      • setByteCounter

        protected void setByteCounter​(long _bytecounter)
      • setSerial

        protected void setSerial​(long _serial)
      • getWiredata

        protected byte[][] getWiredata()
      • 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

        public 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
      • 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
      • appendint

        public 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

        public 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.
      • 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.
      • getWireData

        public byte[][] getWireData()
      • getFiledescriptors

        public java.util.List<FileDescriptor> getFiledescriptors()
      • toString

        public java.lang.String toString()
        Formats the message in a human-readable format.
        Overrides:
        toString in class java.lang.Object
      • getHeader

        public java.lang.Object getHeader​(byte _type)
        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.
      • appendOne

        private int appendOne​(byte[] _sigb,
                              int _sigofs,
                              java.lang.Object _data)
                       throws DBusException
        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
      • pad

        public void pad​(byte _type)
        Pad the message to the proper alignment for the given type.
        Parameters:
        _type - type
      • getAlignment

        public static int getAlignment​(byte _type)
        Return the alignment for a given type.
        Parameters:
        _type - type
        Returns:
        int
      • append

        public void append​(java.lang.String _sig,
                           java.lang.Object... _data)
                    throws DBusException
        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
      • align

        public 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

        java.lang.Object[] extractHeader​(byte[] _headers)
                                  throws DBusException
        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 java.lang.Object readHeaderVariants​(byte[] _signatureBuf,
                                                    byte[] _dataBuf,
                                                    int[] _offsets,
                                                    boolean _contained)
                                             throws DBusException
        Special lightweight version to read the variant objects in DBus message header. This method will not create Variant objects it directly extracts the Variant data content.
        Parameters:
        _signatureBuf - DBus signature string as byte array
        _dataBuf - buffer with header data
        _offsets - current offsets
        _contained - boolean to indicate if nested lists should be resolved (false usually)
        Returns:
        Object
        Throws:
        DBusException - when parsing fails
      • extractOne

        private java.lang.Object extractOne​(byte[] _signatureBuf,
                                            byte[] _dataBuf,
                                            int[] _offsets,
                                            boolean _contained)
                                     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.
        _contained - converts nested arrays to Lists
        Returns:
        The demarshalled value.
        Throws:
        DBusException
      • extractByte

        private java.lang.Object extractByte​(byte[] _dataBuf,
                                             int[] _offsets)
        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 java.lang.Object extractStruct​(byte[] _signatureBuf,
                                               byte[] _dataBuf,
                                               int[] _offsets,
                                               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)
        _extractMethod - method to be called for every entry contained of the struct
        Returns:
        Object
        Throws:
        DBusException - when parsing fails
      • extractArray

        private java.lang.Object extractArray​(byte[] _signatureBuf,
                                              byte[] _dataBuf,
                                              int[] _offsets,
                                              boolean _contained,
                                              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)
        _contained - resolve nested lists
        _extractMethod - method to be called for every entry contained in the array
        Returns:
        Object
        Throws:
        MarshallingException - when Array is too large
        DBusException - when parsing fails
      • extractVariant

        private java.lang.Object extractVariant​(byte[] _dataBuf,
                                                int[] _offsets,
                                                java.util.function.BiFunction<java.lang.String,​java.lang.Object,​java.lang.Object> _variantFactory)
                                         throws DBusException
        Extracts a Variant from the data received on bus.
        Parameters:
        _dataBuf - buffer containing the variant
        _offsets - current offsets in the buffer (will be updated)
        _variantFactory - method to create new Variant objects (or other object types)
        Returns:
        Object / Variant
        Throws:
        DBusException - when parsing fails
      • optimizePrimitives

        private java.lang.Object optimizePrimitives​(byte[] _signatureBuf,
                                                    byte[] _dataBuf,
                                                    int[] _offsets,
                                                    long _size,
                                                    byte _algn,
                                                    int _length,
                                                    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
        _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

        public java.lang.Object[] extract​(java.lang.String _signature,
                                          byte[] _dataBuf,
                                          int _offsets)
                                   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.
        Returns:
        The demarshalled value(s).
        Throws:
        DBusException - on error
      • extract

        public java.lang.Object[] extract​(java.lang.String _signature,
                                          byte[] _dataBuf,
                                          int[] _offsets)
                                   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.
        Returns:
        The demarshalled value(s).
        Throws:
        DBusException - on error
      • getSource

        public java.lang.String getSource()
        Returns the Bus ID that sent the message.
        Returns:
        string
      • getDestination

        public java.lang.String getDestination()
        Returns the destination of the message.
        Returns:
        string
      • getInterface

        public java.lang.String getInterface()
        Returns the interface of the message.
        Returns:
        string
      • getPath

        public java.lang.String getPath()
        Returns the object path of the message.
        Returns:
        string
      • getName

        public java.lang.String getName()
        Returns the member name or error name this message represents.
        Returns:
        string
      • getSig

        public java.lang.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

        public java.lang.Object[] getParameters()
                                         throws DBusException
        Parses and returns the parameters to this message as an Object array.
        Returns:
        object array
        Throws:
        DBusException - on failure
      • setArgs

        public void setArgs​(java.lang.Object[] _args)
      • setSource

        public void setSource​(java.lang.String _source)
                       throws DBusException
        Warning, do not use this method unless you really know what you are doing.
        Parameters:
        _source - string
        Throws:
        DBusException - on error
      • dumpWireData

        java.lang.String dumpWireData()
        Dumps the current content of wiredata to 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

        protected java.lang.Object[] createHeaderArgs​(byte _header,
                                                      java.lang.String _argType,
                                                      java.lang.Object _value)
        Creates a message header. Will automatically add the values to the current instances header map.
        Parameters:
        _header - header type (one of Message.HeaderField)
        _argType - argument type (one of Message.ArgumentType)
        _value - value
        Returns:
        Object array
      • padAndMarshall

        protected void padAndMarshall​(java.util.List<java.lang.Object> _hargs,
                                      long _serial,
                                      java.lang.String _sig,
                                      java.lang.Object... _args)
                               throws DBusException
        Adds message padding and marshalling.
        Parameters:
        _hargs -
        _serial -
        _sig -
        _args -
        Throws:
        DBusException
      • getHeaderFieldName

        public static java.lang.String getHeaderFieldName​(byte _field)
        Returns the name of the given header field.
        Parameters:
        _field - field
        Returns:
        string