Class SockIOPool.SockIO

java.lang.Object
com.danga.MemCached.SockIOPool.SockIO
All Implemented Interfaces:
LineInputStream
Enclosing class:
SockIOPool

public static class SockIOPool.SockIO extends 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 <greg@meetup.com>, Richard 'toast' Russo <russor@msoe.edu>
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private String
     
     
    private static org.apache.log4j.Logger
     
     
    private SockIOPool
     
    private Socket
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    SockIO(SockIOPool pool, 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, 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

    Modifier and Type
    Method
    Description
    void
    reads up to end of line and returns nothing
    (package private) void
    sets closed flag and checks in to connection pool but does not close connections
    protected void
    Hack to reap any leaking children.
    (package private) void
    flushes output stream
    Lets caller get access to underlying channel.
    returns the host this socket is connected to
    protected static Socket
    getSocket(String host, int port, int timeout)
    Method which gets a connection from SocketChannel.
    int
    use the sockets hashcode for this object so we can key off of SockIOs
    (package private) boolean
     
    (package private) boolean
    checks if the connection is open
    int
    read(byte[] b)
    reads length bytes into the passed in byte array from dtream
    reads a line intentionally not using the deprecated readLine method from DataInputStream
    returns the string representation of this socket
    void
    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 Details

  • Constructor Details

    • SockIO

      public SockIO(SockIOPool pool, String host, int port, int timeout, int connectTimeout, boolean noDelay) throws IOException, 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:
      IOException - if an io error occurrs when creating socket
      UnknownHostException - if hostname is invalid
    • SockIO

      public SockIO(SockIOPool pool, String host, int timeout, int connectTimeout, boolean noDelay) throws IOException, 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:
      IOException - if an io error occurrs when creating socket
      UnknownHostException - if hostname is invalid
  • Method Details

    • getSocket

      protected static Socket getSocket(String host, int port, int timeout) throws 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:
      IOException - if errors connecting or if connection times out
    • getChannel

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

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

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

      public void trueClose(boolean addToDeadPool) throws IOException
      closes socket and all streams connected to it
      Throws:
      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 String readLine() throws 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:
      IOException - if io problems during read
    • clearEOL

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

      public int read(byte[] b) throws 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:
      IOException - if io problems during read
    • flush

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

      void write(byte[] b) throws IOException
      writes a byte array to the output stream
      Parameters:
      b - byte array to write
      Throws:
      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 Object
      Returns:
      int hashcode
    • toString

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

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