Class SockIOPool.SockIO

  • All Implemented Interfaces:
    LineInputStream
    Enclosing class:
    SockIOPool

    public static class SockIOPool.SockIO
    extends java.lang.Object
    implements LineInputStream
    MemCached Java client, utility class for Socket IO. This class is a wrapper around a Socket and its streams.
    Version:
    1.5
    Author:
    greg whalin , Richard 'toast' Russo
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String host  
      private java.io.DataInputStream in  
      private static org.apache.log4j.Logger log  
      private java.io.BufferedOutputStream out  
      private SockIOPool pool  
      private java.net.Socket sock  
    • Constructor Summary

      Constructors 
      Constructor Description
      SockIO​(SockIOPool pool, java.lang.String host, int timeout, int connectTimeout, boolean noDelay)
      creates a new SockIO object wrapping a socket connection to host:port, and its input and output streams
      SockIO​(SockIOPool pool, java.lang.String host, int port, int timeout, int connectTimeout, boolean noDelay)
      creates a new SockIO object wrapping a socket connection to host:port, and its input and output streams
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clearEOL()
      reads up to end of line and returns nothing
      (package private) void close()
      sets closed flag and checks in to connection pool but does not close connections
      protected void finalize()
      Hack to reap any leaking children.
      (package private) void flush()
      flushes output stream
      java.nio.channels.SocketChannel getChannel()
      Lets caller get access to underlying channel.
      java.lang.String getHost()
      returns the host this socket is connected to
      protected static java.net.Socket getSocket​(java.lang.String host, int port, int timeout)
      Method which gets a connection from SocketChannel.
      int hashCode()
      use the sockets hashcode for this object so we can key off of SockIOs
      (package private) boolean isAlive()  
      (package private) boolean isConnected()
      checks if the connection is open
      int read​(byte[] b)
      reads length bytes into the passed in byte array from dtream
      java.lang.String readLine()
      reads a line intentionally not using the deprecated readLine method from DataInputStream
      java.lang.String toString()
      returns the string representation of this socket
      void trueClose()
      closes socket and all streams connected to it
      void trueClose​(boolean addToDeadPool)
      closes socket and all streams connected to it
      (package private) void write​(byte[] b)
      writes a byte array to the output stream
      • Methods inherited from class java.lang.Object

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

      • log

        private static org.apache.log4j.Logger log
      • host

        private java.lang.String host
      • sock

        private java.net.Socket sock
      • in

        private java.io.DataInputStream in
      • out

        private java.io.BufferedOutputStream out
    • Constructor Detail

      • SockIO

        public SockIO​(SockIOPool pool,
                      java.lang.String host,
                      int port,
                      int timeout,
                      int connectTimeout,
                      boolean noDelay)
               throws java.io.IOException,
                      java.net.UnknownHostException
        creates a new SockIO object wrapping a socket connection to host:port, and its input and output streams
        Parameters:
        pool - Pool this object is tied to
        host - host to connect to
        port - port to connect to
        timeout - int ms to block on data for read
        connectTimeout - timeout (in ms) for initial connection
        noDelay - TCP NODELAY option?
        Throws:
        java.io.IOException - if an io error occurrs when creating socket
        java.net.UnknownHostException - if hostname is invalid
      • SockIO

        public SockIO​(SockIOPool pool,
                      java.lang.String host,
                      int timeout,
                      int connectTimeout,
                      boolean noDelay)
               throws java.io.IOException,
                      java.net.UnknownHostException
        creates a new SockIO object wrapping a socket connection to host:port, and its input and output streams
        Parameters:
        host - hostname:port
        timeout - read timeout value for connected socket
        connectTimeout - timeout for initial connections
        noDelay - TCP NODELAY option?
        Throws:
        java.io.IOException - if an io error occurrs when creating socket
        java.net.UnknownHostException - if hostname is invalid
    • Method Detail

      • getSocket

        protected static java.net.Socket getSocket​(java.lang.String host,
                                                   int port,
                                                   int timeout)
                                            throws java.io.IOException
        Method which gets a connection from SocketChannel.
        Parameters:
        host - host to establish connection to
        port - port on that host
        timeout - connection timeout in ms
        Returns:
        connected socket
        Throws:
        java.io.IOException - if errors connecting or if connection times out
      • getChannel

        public java.nio.channels.SocketChannel getChannel()
        Lets caller get access to underlying channel.
        Returns:
        the backing SocketChannel
      • getHost

        public java.lang.String getHost()
        returns the host this socket is connected to
        Returns:
        String representation of host (hostname:port)
      • trueClose

        public void trueClose()
                       throws java.io.IOException
        closes socket and all streams connected to it
        Throws:
        java.io.IOException - if fails to close streams or socket
      • trueClose

        public void trueClose​(boolean addToDeadPool)
                       throws java.io.IOException
        closes socket and all streams connected to it
        Throws:
        java.io.IOException - if fails to close streams or socket
      • close

        void close()
        sets closed flag and checks in to connection pool but does not close connections
      • isConnected

        boolean isConnected()
        checks if the connection is open
        Returns:
        true if connected
      • isAlive

        boolean isAlive()
      • readLine

        public java.lang.String readLine()
                                  throws java.io.IOException
        reads a line intentionally not using the deprecated readLine method from DataInputStream
        Specified by:
        readLine in interface LineInputStream
        Returns:
        String that was read in
        Throws:
        java.io.IOException - if io problems during read
      • clearEOL

        public void clearEOL()
                      throws java.io.IOException
        reads up to end of line and returns nothing
        Specified by:
        clearEOL in interface LineInputStream
        Throws:
        java.io.IOException - if io problems during read
      • read

        public int read​(byte[] b)
                 throws java.io.IOException
        reads length bytes into the passed in byte array from dtream
        Specified by:
        read in interface LineInputStream
        Parameters:
        b - byte array
        Returns:
        The number of bytes actually read, or -1 if none could be read.
        Throws:
        java.io.IOException - if io problems during read
      • flush

        void flush()
            throws java.io.IOException
        flushes output stream
        Throws:
        java.io.IOException - if io problems during read
      • write

        void write​(byte[] b)
            throws java.io.IOException
        writes a byte array to the output stream
        Parameters:
        b - byte array to write
        Throws:
        java.io.IOException - if an io error happens
      • hashCode

        public int hashCode()
        use the sockets hashcode for this object so we can key off of SockIOs
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        int hashcode
      • toString

        public java.lang.String toString()
        returns the string representation of this socket
        Overrides:
        toString in class java.lang.Object
        Returns:
        string
      • finalize

        protected void finalize()
                         throws java.lang.Throwable
        Hack to reap any leaking children.
        Overrides:
        finalize in class java.lang.Object
        Throws:
        java.lang.Throwable