Class SSLSocketChannel2

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.ByteChannel, java.nio.channels.Channel, java.nio.channels.ReadableByteChannel, java.nio.channels.WritableByteChannel, ISSLChannel, WrappedByteChannel

    public class SSLSocketChannel2
    extends java.lang.Object
    implements java.nio.channels.ByteChannel, WrappedByteChannel, ISSLChannel
    Implements the relevant portions of the SocketChannel interface with the SSLEngine wrapper.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int bufferallocations
      Should be used to count the buffer allocations.
      protected static java.nio.ByteBuffer emptybuffer
      This object is used to feed the SSLEngine's wrap and unwrap methods during the handshake phase.
      protected java.util.concurrent.ExecutorService exec  
      protected java.nio.ByteBuffer inCrypt
      encrypted data incoming
      protected java.nio.ByteBuffer inData
      raw payload incomming
      private org.slf4j.Logger log
      Logger instance
      protected java.nio.ByteBuffer outCrypt
      encrypted data outgoing
      protected javax.net.ssl.SSLEngineResult readEngineResult  
      private byte[] saveCryptData  
      protected java.nio.channels.SelectionKey selectionKey
      used to set interestOP SelectionKey.OP_WRITE for the underlying channel
      protected java.nio.channels.SocketChannel socketChannel
      the underlying channel
      protected javax.net.ssl.SSLEngine sslEngine  
      protected java.util.List<java.util.concurrent.Future<?>> tasks  
      protected javax.net.ssl.SSLEngineResult writeEngineResult  
    • Constructor Summary

      Constructors 
      Constructor Description
      SSLSocketChannel2​(java.nio.channels.SocketChannel channel, javax.net.ssl.SSLEngine sslEngine, java.util.concurrent.ExecutorService exec, java.nio.channels.SelectionKey key)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      java.nio.channels.SelectableChannel configureBlocking​(boolean b)  
      boolean connect​(java.net.SocketAddress remote)  
      protected void consumeDelegatedTasks()  
      private void consumeFutureUninterruptible​(java.util.concurrent.Future<?> f)  
      protected void createBuffers​(javax.net.ssl.SSLSession session)  
      boolean finishConnect()  
      javax.net.ssl.SSLEngine getSSLEngine()
      Get the ssl engine used for the de- and encryption of the communication.
      boolean isBlocking()
      This function returns the blocking state of the channel
      boolean isConnected()  
      private boolean isHandShakeComplete()  
      boolean isInboundDone()  
      boolean isNeedRead()
      returns whether readMore should be called to fetch data which has been decoded but not yet been returned.
      boolean isNeedWrite()
      returns whether writeMore should be called write additional data.
      boolean isOpen()  
      private void processHandshake()
      This method will do whatever necessary to process the sslengine handshake.
      int read​(java.nio.ByteBuffer dst)
      Blocks when in blocking mode until at least one byte has been decoded.
      When not in blocking mode 0 may be returned.
      int readMore​(java.nio.ByteBuffer dst)
      This function does not read data from the underlying channel at all.
      private int readRemaining​(java.nio.ByteBuffer dst)
      read(ByteBuffer) may not be to leave all buffers(inData, inCrypt)
      private void saveCryptedData()  
      java.net.Socket socket()  
      private int transfereTo​(java.nio.ByteBuffer from, java.nio.ByteBuffer to)  
      private void tryRestoreCryptedData()  
      private java.nio.ByteBuffer unwrap()
      performs the unwrap operation by unwrapping from inCrypt to inData
      private java.nio.ByteBuffer wrap​(java.nio.ByteBuffer b)  
      int write​(java.nio.ByteBuffer src)  
      void writeMore()
      Gets called when WrappedByteChannel.isNeedWrite() ()} requires a additional rite
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • emptybuffer

        protected static java.nio.ByteBuffer emptybuffer
        This object is used to feed the SSLEngine's wrap and unwrap methods during the handshake phase.
      • log

        private final org.slf4j.Logger log
        Logger instance
        Since:
        1.4.0
      • exec

        protected java.util.concurrent.ExecutorService exec
      • tasks

        protected java.util.List<java.util.concurrent.Future<?>> tasks
      • inData

        protected java.nio.ByteBuffer inData
        raw payload incomming
      • outCrypt

        protected java.nio.ByteBuffer outCrypt
        encrypted data outgoing
      • inCrypt

        protected java.nio.ByteBuffer inCrypt
        encrypted data incoming
      • socketChannel

        protected java.nio.channels.SocketChannel socketChannel
        the underlying channel
      • selectionKey

        protected java.nio.channels.SelectionKey selectionKey
        used to set interestOP SelectionKey.OP_WRITE for the underlying channel
      • sslEngine

        protected javax.net.ssl.SSLEngine sslEngine
      • readEngineResult

        protected javax.net.ssl.SSLEngineResult readEngineResult
      • writeEngineResult

        protected javax.net.ssl.SSLEngineResult writeEngineResult
      • bufferallocations

        protected int bufferallocations
        Should be used to count the buffer allocations. But because of #190 where HandshakeStatus.FINISHED is not properly returned by nio wrap/unwrap this variable is used to check whether createBuffers(SSLSession) needs to be called.
      • saveCryptData

        private byte[] saveCryptData
    • Constructor Detail

      • SSLSocketChannel2

        public SSLSocketChannel2​(java.nio.channels.SocketChannel channel,
                                 javax.net.ssl.SSLEngine sslEngine,
                                 java.util.concurrent.ExecutorService exec,
                                 java.nio.channels.SelectionKey key)
                          throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • consumeFutureUninterruptible

        private void consumeFutureUninterruptible​(java.util.concurrent.Future<?> f)
      • processHandshake

        private void processHandshake()
                               throws java.io.IOException
        This method will do whatever necessary to process the sslengine handshake. Thats why it's called both from the read(ByteBuffer) and write(ByteBuffer)
        Throws:
        java.io.IOException
      • wrap

        private java.nio.ByteBuffer wrap​(java.nio.ByteBuffer b)
                                  throws javax.net.ssl.SSLException
        Throws:
        javax.net.ssl.SSLException
      • unwrap

        private java.nio.ByteBuffer unwrap()
                                    throws javax.net.ssl.SSLException
        performs the unwrap operation by unwrapping from inCrypt to inData
        Throws:
        javax.net.ssl.SSLException
      • consumeDelegatedTasks

        protected void consumeDelegatedTasks()
      • createBuffers

        protected void createBuffers​(javax.net.ssl.SSLSession session)
      • write

        public int write​(java.nio.ByteBuffer src)
                  throws java.io.IOException
        Specified by:
        write in interface java.nio.channels.WritableByteChannel
        Throws:
        java.io.IOException
      • read

        public int read​(java.nio.ByteBuffer dst)
                 throws java.io.IOException
        Blocks when in blocking mode until at least one byte has been decoded.
        When not in blocking mode 0 may be returned.
        Specified by:
        read in interface java.nio.channels.ReadableByteChannel
        Returns:
        the number of bytes read.
        Throws:
        java.io.IOException
      • readRemaining

        private int readRemaining​(java.nio.ByteBuffer dst)
                           throws javax.net.ssl.SSLException
        read(ByteBuffer) may not be to leave all buffers(inData, inCrypt)
        Throws:
        javax.net.ssl.SSLException
      • isConnected

        public boolean isConnected()
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.nio.channels.Channel
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • isHandShakeComplete

        private boolean isHandShakeComplete()
      • configureBlocking

        public java.nio.channels.SelectableChannel configureBlocking​(boolean b)
                                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • connect

        public boolean connect​(java.net.SocketAddress remote)
                        throws java.io.IOException
        Throws:
        java.io.IOException
      • finishConnect

        public boolean finishConnect()
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • socket

        public java.net.Socket socket()
      • isInboundDone

        public boolean isInboundDone()
      • isOpen

        public boolean isOpen()
        Specified by:
        isOpen in interface java.nio.channels.Channel
      • isNeedWrite

        public boolean isNeedWrite()
        Description copied from interface: WrappedByteChannel
        returns whether writeMore should be called write additional data.
        Specified by:
        isNeedWrite in interface WrappedByteChannel
        Returns:
        is a additional write needed
      • readMore

        public int readMore​(java.nio.ByteBuffer dst)
                     throws javax.net.ssl.SSLException
        Description copied from interface: WrappedByteChannel
        This function does not read data from the underlying channel at all. It is just a way to fetch data which has already be received or decoded but was but was not yet returned to the user. This could be the case when the decoded data did not fit into the buffer the user passed to ReadableByteChannel.read(ByteBuffer).
        Specified by:
        readMore in interface WrappedByteChannel
        Parameters:
        dst - the destiny of the read
        Returns:
        the amount of remaining data
        Throws:
        javax.net.ssl.SSLException
      • transfereTo

        private int transfereTo​(java.nio.ByteBuffer from,
                                java.nio.ByteBuffer to)
      • isBlocking

        public boolean isBlocking()
        Description copied from interface: WrappedByteChannel
        This function returns the blocking state of the channel
        Specified by:
        isBlocking in interface WrappedByteChannel
        Returns:
        is the channel blocking
      • getSSLEngine

        public javax.net.ssl.SSLEngine getSSLEngine()
        Description copied from interface: ISSLChannel
        Get the ssl engine used for the de- and encryption of the communication.
        Specified by:
        getSSLEngine in interface ISSLChannel
        Returns:
        the ssl engine of this channel
      • saveCryptedData

        private void saveCryptedData()
      • tryRestoreCryptedData

        private void tryRestoreCryptedData()