Class RESPReader


  • public class RESPReader
    extends Object
    Reader class for reading responses from a Redis Socket connection.
    Author:
    Matthew Tropiano
    • Constructor Detail

      • RESPReader

        public RESPReader​(InputStream in)
        Creates a RedisReader attached to an input stream.
        Parameters:
        in - the InputStream to wrap.
    • Method Detail

      • readOK

        public boolean readOK()
        Reads and expects "OK" from Redis. Will block until something is read from the stream. Should be used if AND ONLY IF "OK" is expected.
        Returns:
        true.
        Throws:
        RedisException - if the server reports an error, or isn't OK.
        RedisParseException - if an error occurs during the read.
      • readQueued

        public boolean readQueued()
        Reads and expects "QUEUED" from Redis. Will block until something is read from the stream. Should be used if AND ONLY IF "QUEUED" is expected.
        Returns:
        true.
        Throws:
        RedisException - if the server reports an error, or isn't QUEUED.
        RedisParseException - if an error occurs during the read.
      • readPong

        public boolean readPong()
        Reads and expects "PONG" from Redis. Will block until something is read from the stream. Should be used if AND ONLY IF "PONG" is expected.
        Returns:
        true.
        Throws:
        RedisException - if the server reports an error, or isn't PONG.
        RedisParseException - if an error occurs during the read.
      • readInteger

        public Long readInteger()
        Reads and expects an Integer Reply from Redis. Will block until something is read from the stream. Should be used if AND ONLY IF an Integer or Null is expected.
        Returns:
        a long or null reply.
        Throws:
        RedisException - if the server reports an error.
        RedisParseException - if an error occurs during the read.
      • readString

        public String readString()
        Reads and expects a String Reply from Redis, bulk or otherwise. Will block until something is read from the stream. Should be used if AND ONLY IF a non-array or Null is expected. Integers are cast to Strings.
        Returns:
        a long or null reply.
        Throws:
        RedisException - if the server reports an error.
        RedisParseException - if an error occurs during the read.
      • readArray

        public String[] readArray()
        Reads and expects an Array Reply from Redis. Will block until something is read from the stream. Should be used if AND ONLY IF an array or Null is expected. Integers are cast to Strings.
        Returns:
        an array or null reply. Arrays may contain null elements!
        Throws:
        RedisException - if the server reports an error.
        RedisParseException - if an error occurs during the read.
      • readObject

        public RedisObject readObject()
        Reads a full Redis data structure as a RedisObject. This is useful for commands that return complex or not-yet-supported responses. Will block until something is read completely from the stream.
        Returns:
        a full string with all breaks and newlines.
        Throws:
        RedisException - if the server reports an error.
        RedisParseException - if an error occurs during the read.
      • readRaw

        public String readRaw()
        Reads until a full Redis data structure is read. Will block until something is read completely from the stream.
        Returns:
        a full string with all breaks and newlines.
        Throws:
        RedisException - if the server reports an error.
        RedisParseException - if an error occurs during the read.
      • readLine

        protected int readLine​(DataList dl)
                        throws IOException
        Reads bytes until CRLF and returns how many bytes in the "string" (before CRLF).
        Parameters:
        dl - the data buffer to read from.
        Returns:
        the amount of bytes read.
        Throws:
        IOException
      • readBulk

        protected int readBulk​(DataList dl,
                               int len)
                        throws IOException
        Reads bytes until CRLF and returns how many bytes in the "string" (before CRLF).
        Parameters:
        dl - the data buffer to read from.
        len - the maximum amount of bytes to read.
        Returns:
        the amount of bytes read.
        Throws:
        IOException