< prev index next >

src/java.httpclient/share/classes/java/net/http/AsyncSSLConnection.java

Print this page
rev 15335 : Async Queues


  28 import java.io.IOException;
  29 import java.net.InetSocketAddress;
  30 import java.nio.ByteBuffer;
  31 import java.nio.channels.SocketChannel;
  32 import java.util.concurrent.CompletableFuture;
  33 import java.util.function.Consumer;
  34 
  35 /**
  36  * Asynchronous version of SSLConnection.
  37  */
  38 class AsyncSSLConnection extends HttpConnection implements AsyncConnection {
  39     final AsyncSSLDelegate sslDelegate;
  40     final PlainHttpConnection delegate;
  41 
  42     AsyncSSLConnection(InetSocketAddress addr, HttpClientImpl client, String[] ap) {
  43         super(addr, client);
  44         delegate = new PlainHttpConnection(addr, client);
  45         sslDelegate = new AsyncSSLDelegate(delegate, client, ap);
  46     }
  47 





  48     @Override
  49     public void connect() throws IOException, InterruptedException {
  50         delegate.connect();
  51     }
  52 
  53     @Override
  54     public CompletableFuture<Void> connectAsync() {
  55         return delegate.connectAsync();
  56     }
  57 
  58     @Override
  59     boolean connected() {
  60         return delegate.connected();
  61     }
  62 
  63     @Override
  64     boolean isSecure() {
  65         return true;
  66     }
  67 
  68     @Override
  69     boolean isProxied() {
  70         return false;
  71     }
  72 
  73     @Override
  74     SocketChannel channel() {
  75         return delegate.channel();
  76     }
  77 
  78     @Override
  79     ConnectionPool.CacheKey cacheKey() {
  80         return ConnectionPool.cacheKey(address, null);
  81     }
  82 
  83     @Override
  84     synchronized long write(ByteBuffer[] buffers, int start, int number) throws IOException {
  85         ByteBuffer[] bufs = Utils.reduce(buffers, start, number);
  86         long n = Utils.remaining(bufs);
  87         sslDelegate.write(bufs);

  88         return n;
  89     }
  90 
  91     @Override
  92     long write(ByteBuffer buffer) throws IOException {
  93         long n = buffer.remaining();
  94         sslDelegate.write(buffer);

  95         return n;























  96     }
  97 
  98     @Override
  99     public void close() {
 100         Utils.close(sslDelegate, delegate.channel());
 101     }
 102 
 103     @Override
 104     public void setAsyncCallbacks(Consumer<ByteBuffer> asyncReceiver, Consumer<Throwable> errorReceiver) {
 105         sslDelegate.setAsyncCallbacks(asyncReceiver, errorReceiver);
 106         delegate.setAsyncCallbacks(sslDelegate::lowerRead, errorReceiver);
 107     }
 108 
 109     // Blocking read functions not used here
 110 
 111     @Override
 112     protected ByteBuffer readImpl(int length) throws IOException {
 113         throw new UnsupportedOperationException("Not supported.");
 114     }
 115 


  28 import java.io.IOException;
  29 import java.net.InetSocketAddress;
  30 import java.nio.ByteBuffer;
  31 import java.nio.channels.SocketChannel;
  32 import java.util.concurrent.CompletableFuture;
  33 import java.util.function.Consumer;
  34 
  35 /**
  36  * Asynchronous version of SSLConnection.
  37  */
  38 class AsyncSSLConnection extends HttpConnection implements AsyncConnection {
  39     final AsyncSSLDelegate sslDelegate;
  40     final PlainHttpConnection delegate;
  41 
  42     AsyncSSLConnection(InetSocketAddress addr, HttpClientImpl client, String[] ap) {
  43         super(addr, client);
  44         delegate = new PlainHttpConnection(addr, client);
  45         sslDelegate = new AsyncSSLDelegate(delegate, client, ap);
  46     }
  47 
  48     synchronized void configureMode(Mode mode) throws IOException {
  49         super.configureMode(mode);
  50         delegate.configureMode(mode);
  51     }
  52 
  53     @Override
  54     public void connect() throws IOException, InterruptedException {
  55         delegate.connect();
  56     }
  57 
  58     @Override
  59     public CompletableFuture<Void> connectAsync() {
  60         return delegate.connectAsync();
  61     }
  62 
  63     @Override
  64     boolean connected() {
  65         return delegate.connected();
  66     }
  67 
  68     @Override
  69     boolean isSecure() {
  70         return true;
  71     }
  72 
  73     @Override
  74     boolean isProxied() {
  75         return false;
  76     }
  77 
  78     @Override
  79     SocketChannel channel() {
  80         return delegate.channel();
  81     }
  82 
  83     @Override
  84     ConnectionPool.CacheKey cacheKey() {
  85         return ConnectionPool.cacheKey(address, null);
  86     }
  87 
  88     @Override
  89     long write(ByteBuffer[] buffers, int start, int number) throws IOException {
  90         ByteBuffer[] bufs = Utils.reduce(buffers, start, number);
  91         long n = Utils.remaining(bufs);
  92         sslDelegate.writeAsync(bufs);
  93         sslDelegate.flushAsync();
  94         return n;
  95     }
  96 
  97     @Override
  98     long write(ByteBuffer buffer) throws IOException {
  99         long n = buffer.remaining();
 100         sslDelegate.writeAsync(new ByteBuffer[]{buffer});
 101         sslDelegate.flushAsync();
 102         return n;
 103     }
 104 
 105     @Override
 106     public void writeAsyncUnordered(ByteBuffer[] buffers) throws IOException {
 107         if (mode != Mode.ASYNC) {
 108             write(buffers, 0 , buffers.length);
 109         } else {
 110             sslDelegate.writeAsyncUnordered(buffers);
 111         }
 112     }
 113 
 114     @Override
 115     public void writeAsync(ByteBuffer[] buffers) throws IOException {
 116         if (mode != Mode.ASYNC) {
 117             write(buffers, 0 , buffers.length);
 118         } else {
 119             sslDelegate.writeAsync(buffers);
 120         }
 121     }
 122 
 123     @Override
 124     public void flushAsync() {
 125         sslDelegate.flushAsync();
 126     }
 127 
 128     @Override
 129     public void close() {
 130         Utils.close(sslDelegate, delegate.channel());
 131     }
 132 
 133     @Override
 134     public void setAsyncCallbacks(Consumer<ByteBuffer> asyncReceiver, Consumer<Throwable> errorReceiver) {
 135         sslDelegate.setAsyncCallbacks(asyncReceiver, errorReceiver);
 136         delegate.setAsyncCallbacks(sslDelegate::lowerRead, errorReceiver);
 137     }
 138 
 139     // Blocking read functions not used here
 140 
 141     @Override
 142     protected ByteBuffer readImpl(int length) throws IOException {
 143         throw new UnsupportedOperationException("Not supported.");
 144     }
 145 
< prev index next >