java.lang.Object
java.net.Socket
net.i2p.util.InternalSocket
All Implemented Interfaces:
Closeable, AutoCloseable

public class InternalSocketextends Socket
A simple in-JVM Socket using Piped Streams. We use port numbers just like regular sockets. Can only connect to InternalServerSocket.
  • Constructor Details

    • InternalSocket

      InternalSocket(InputStream is, OutputStream os)
      Server side.
      Parameters:
      is - the piped stream carrying data from the client to this end
      os - the piped stream carrying data from this end to the client
    • InternalSocket

      public InternalSocket(int port) throws IOException
      Client side.
      Parameters:
      port - > 0
      Throws:
      IOException - if the port is not positive, or no internal server is listening on it
  • Method Details

    • getSocket

      public static Socket getSocket(String host, int port) throws IOException
      Convenience method to return either a Socket or an InternalSocket.
      Parameters:
      host - the host requested, which is only honoured for the loopback names inside a running router
      port - > 0
      Returns:
      an InternalSocket when running under a router and the host is a loopback name, otherwise a TCP Socket to the host and port
      Throws:
      IOException - if neither an internal socket nor a TCP socket to the given host and port can be opened
    • getInputStream

      public InputStream getInputStream()
      Input stream connected to the internal server.
      Overrides:
      getInputStream in class Socket
      Returns:
      the input stream
    • getOutputStream

      public OutputStream getOutputStream()
      Output stream connected to the internal server.
      Overrides:
      getOutputStream in class Socket
      Returns:
      the output stream
    • setInputStream

      void setInputStream(InputStream is)
      Input stream.
      Parameters:
      is - the replacement stream, installed by the internal server after a client has connected
    • setOutputStream

      void setOutputStream(OutputStream os)
      Output stream.
      Parameters:
      os - the replacement stream, installed by the internal server after a client has connected
    • close

      public void close()
      Close both streams.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class Socket
    • isClosed

      public boolean isClosed()
      True if either stream is null.
      Overrides:
      isClosed in class Socket
      Returns:
      true once either stream has been shut down
    • toString

      public String toString()
      Descriptive label.
      Overrides:
      toString in class Socket
    • setSoTimeout

      public void setSoTimeout(int timeout)
      Supported as of 0.9.34, if constructed with TimeoutPipedInputStream and TimeoutPipedOutputStream. Otherwise, does nothing.
      Overrides:
      setSoTimeout in class Socket
      See Also:
    • getSoTimeout

      public int getSoTimeout()
      Always returns 0, even if setSoTimeout() was called.
      Overrides:
      getSoTimeout in class Socket
      Returns:
      the so timeout
    • getLocalPort

      public int getLocalPort()
      Supported as of 0.9.33, prior to that threw UnsupportedOperationException.
      Overrides:
      getLocalPort in class Socket
      Returns:
      1 if connected, -1 if not
    • getPort

      public int getPort()
      Supported as of 0.9.33, prior to that threw UnsupportedOperationException.
      Overrides:
      getPort in class Socket
      Returns:
      if connected: actual port for clients, 1 for servers; -1 if not
    • getSoLinger

      public int getSoLinger()
      Supported as of 0.9.33, prior to that threw UnsupportedOperationException.
      Overrides:
      getSoLinger in class Socket
      Returns:
      -1 always
    • isConnected

      public boolean isConnected()
      Supported as of 0.9.33, prior to that threw UnsupportedOperationException.
      Overrides:
      isConnected in class Socket
      Returns:
      true unless both streams are shut down; a plain socket is never "connected"
    • isInputShutdown

      public boolean isInputShutdown()
      Supported as of 0.9.33, prior to that threw UnsupportedOperationException.
      Overrides:
      isInputShutdown in class Socket
      Returns:
      whether input shutdown
    • isOutputShutdown

      public boolean isOutputShutdown()
      Supported as of 0.9.33, prior to that threw UnsupportedOperationException.
      Overrides:
      isOutputShutdown in class Socket
      Returns:
      whether output shutdown
    • setSoLinger

      public void setSoLinger(boolean on, int linger)
      Does nothing as of 0.9.33, prior to that threw UnsupportedOperationException.
      Overrides:
      setSoLinger in class Socket
    • shutdownInput

      public void shutdownInput() throws IOException
      Supported as of 0.9.33, prior to that threw UnsupportedOperationException.
      Overrides:
      shutdownInput in class Socket
      Throws:
      IOException
    • shutdownOutput

      public void shutdownOutput() throws IOException
      Flushes (as the Socket javadocs advise) and closes. Supported as of 0.9.33, prior to that threw UnsupportedOperationException.
      Overrides:
      shutdownOutput in class Socket
      Throws:
      IOException