Package org.zeromq

Class ZFrame


  • public class ZFrame
    extends java.lang.Object
    ZFrame The ZFrame class provides methods to send and receive single message frames across 0MQ sockets. A 'frame' corresponds to one underlying zmq_msg_t in the libzmq code. When you read a frame from a socket, the more() method indicates if the frame is part of an unfinished multipart message. The send() method normally destroys the frame, but with the ZFRAME_REUSE flag, you can send the same frame many times. Frames are binary, and this class has no special support for text data.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private byte[] data  
      static int DONTWAIT  
      private boolean more  
      static int MORE  
      static int REUSE  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected ZFrame()
      Class Constructor Creates an empty frame.
        ZFrame​(byte[] data)
      Class Constructor Copies message data into ZFrame object
        ZFrame​(java.lang.String data)
      Class Constructor Copies String into frame data
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void destroy()
      Destructor.
      ZFrame duplicate()
      Creates a new frame that duplicates an existing frame
      boolean equals​(java.lang.Object o)  
      byte[] getData()  
      java.lang.String getString​(java.nio.charset.Charset charset)  
      boolean hasData()
      Convenience method to ascertain if this frame contains some message data
      int hashCode()  
      boolean hasMore()  
      boolean hasSameData​(ZFrame other)
      Returns true if both frames have byte - for byte identical data
      void print​(java.lang.String prefix)  
      private byte[] recv​(ZMQ.Socket socket, int flags)
      Internal method to call recv on the socket.
      static ZFrame recvFrame​(ZMQ.Socket socket)
      Receives single frame from socket, returns the received frame object, or null if the recv was interrupted.
      static ZFrame recvFrame​(ZMQ.Socket socket, int flags)
      Receive a new frame off the socket, Returns newly-allocated frame, or null if there was no input waiting, or if the read was interrupted.
      void reset​(byte[] data)
      Sets new contents for frame
      void reset​(java.lang.String data)
      Sets new contents for frame
      boolean send​(ZMQ.Socket socket, int flags)
      Internal method to call org.zeromq.Socket send() method.
      boolean sendAndDestroy​(ZMQ.Socket socket)
      Sends frame to socket if it contains data.
      boolean sendAndDestroy​(ZMQ.Socket socket, int flags)
      Sends frame to socket if it contains data.
      boolean sendAndKeep​(ZMQ.Socket socket)
      Sends frame to socket if it contains any data.
      boolean sendAndKeep​(ZMQ.Socket socket, int flags)
      Sends frame to socket if it contains any data.
      int size()
      Returns byte size of frame, if set, else 0
      boolean streq​(java.lang.String str)
      String equals.
      java.lang.String strhex()  
      java.lang.String toString()
      Returns a human - readable representation of frame's data
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • ZFrame

        protected ZFrame()
        Class Constructor Creates an empty frame. (Useful when reading frames from a 0MQ Socket)
      • ZFrame

        public ZFrame​(byte[] data)
        Class Constructor Copies message data into ZFrame object
        Parameters:
        data - Data to copy into ZFrame object
      • ZFrame

        public ZFrame​(java.lang.String data)
        Class Constructor Copies String into frame data
        Parameters:
        data - String to copy into ZFrame object as bytes, decoded using ZMQ.CHARSET
    • Method Detail

      • destroy

        public void destroy()
        Destructor.
      • getData

        public byte[] getData()
        Returns:
        the data
      • getString

        public java.lang.String getString​(java.nio.charset.Charset charset)
      • hasMore

        public boolean hasMore()
        Returns:
        More flag, true if last read had MORE message parts to come
      • size

        public int size()
        Returns byte size of frame, if set, else 0
        Returns:
        Number of bytes in frame data, else 0
      • hasData

        public boolean hasData()
        Convenience method to ascertain if this frame contains some message data
        Returns:
        True if frame contains data
      • send

        public boolean send​(ZMQ.Socket socket,
                            int flags)
        Internal method to call org.zeromq.Socket send() method.
        Parameters:
        socket - 0MQ socket to send on
        flags - Valid send() method flags, defined in org.zeromq.ZMQ class
        Returns:
        True if success, else False
      • sendAndKeep

        public boolean sendAndKeep​(ZMQ.Socket socket,
                                   int flags)
        Sends frame to socket if it contains any data. Frame contents are kept after the send.
        Parameters:
        socket - 0MQ socket to send frame
        flags - Valid send() method flags, defined in org.zeromq.ZMQ class
        Returns:
        True if success, else False
      • sendAndKeep

        public boolean sendAndKeep​(ZMQ.Socket socket)
        Sends frame to socket if it contains any data. Frame contents are kept after the send. Uses default behaviour of Socket.send() method, with no flags set
        Parameters:
        socket - 0MQ socket to send frame
        Returns:
        True if success, else False
      • sendAndDestroy

        public boolean sendAndDestroy​(ZMQ.Socket socket,
                                      int flags)
        Sends frame to socket if it contains data. Use this method to send a frame and destroy the data after.
        Parameters:
        socket - 0MQ socket to send frame
        flags - Valid send() method flags, defined in org.zeromq.ZMQ class
        Returns:
        True if success, else False
      • sendAndDestroy

        public boolean sendAndDestroy​(ZMQ.Socket socket)
        Sends frame to socket if it contains data. Use this method to send an isolated frame and destroy the data after. Uses default behaviour of Socket.send() method, with no flags set
        Parameters:
        socket - 0MQ socket to send frame
        Returns:
        True if success, else False
      • duplicate

        public ZFrame duplicate()
        Creates a new frame that duplicates an existing frame
        Returns:
        Duplicate of frame; message contents copied into new byte array
      • hasSameData

        public boolean hasSameData​(ZFrame other)
        Returns true if both frames have byte - for byte identical data
        Parameters:
        other - The other ZFrame to compare
        Returns:
        True if both ZFrames have same byte-identical data, else false
      • reset

        public void reset​(java.lang.String data)
        Sets new contents for frame
        Parameters:
        data - New byte array contents for frame
      • reset

        public void reset​(byte[] data)
        Sets new contents for frame
        Parameters:
        data - New byte array contents for frame
      • strhex

        public java.lang.String strhex()
        Returns:
        frame data as a printable hex string
      • streq

        public boolean streq​(java.lang.String str)
        String equals. Uses String compareTo for the comparison (lexigraphical)
        Parameters:
        str - String to compare with frame data
        Returns:
        True if frame body data matches given string
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Returns a human - readable representation of frame's data
        Overrides:
        toString in class java.lang.Object
        Returns:
        A text string or hex-encoded string if data contains any non-printable ASCII characters
      • recv

        private byte[] recv​(ZMQ.Socket socket,
                            int flags)
        Internal method to call recv on the socket. Does not trap any ZMQExceptions but expects caling routine to handle them.
        Parameters:
        socket - 0MQ socket to read from
        Returns:
        byte[] data
      • recvFrame

        public static ZFrame recvFrame​(ZMQ.Socket socket)
        Receives single frame from socket, returns the received frame object, or null if the recv was interrupted. Does a blocking recv, if you want to not block then use recvFrame(socket, ZMQ.DONTWAIT);
        Parameters:
        socket - Socket to read from
        Returns:
        received frame, else null
      • recvFrame

        public static ZFrame recvFrame​(ZMQ.Socket socket,
                                       int flags)
        Receive a new frame off the socket, Returns newly-allocated frame, or null if there was no input waiting, or if the read was interrupted.
        Parameters:
        socket - Socket to read from
        flags - Pass flags to 0MQ socket.recv call
        Returns:
        received frame, else null
      • print

        public void print​(java.lang.String prefix)