title: Properly closing SSLSocket
body: <p>I want to implement an SSL proxy in Java. I basically open two sockets <code>browser-proxy</code>,<code>proxy-server</code>, and run two threads which would write to <code>proxy-server</code> what they read from <code>browser-proxy</code>, and vice versa. Each thread looks like this:</p>
  
  <pre><code>while (true) {
     nr = in.read(buffer);
     if (nr == -1) System.out.println(sockin.toString()+" EOF  "+nr);
     if (nr == -1) break;
     out.write(buffer, 0, nr);
  }
  sockin.shutdownInput();
  sockout.shutdownOutput(); // now the second thread will receive -1 on read
  </code></pre>
  
  <p>Each thread will only close the input socket, so that eventually both sockets are closed.</p>
  
  <p>But what do I do if I want to use an <code>SSLSocket</code>? It seems that the <code>shutdownOutput/Input</code> methods are not supported there. Here's the exception I get.</p>
  
  <pre><code>Exception in thread "Thread-35" java.lang.UnsupportedOperationException: \
  The method shutdownInput() is not supported in SSLSocket
      at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.shutdownInput(Unknown Source)
  </code></pre>
  
  <p>What I came up with is:</p>
  
  <pre><code>try {
      while (true) {
         nr = in.read(buffer);
         if (nr == -1) System.out.println(sockin.toString()+" EOF  "+nr);
         if (nr == -1) break;
         out.write(buffer, 0, nr);
      }
      // possible race condition if by some mysterious way both threads would get EOF
      if (!sockin.isClosed()) sockin.close();
      if (!sockout.isClosed()) sockout.close();
  } catch (SocketException e) {/*ignore expected "socket closed" exception, */}
  </code></pre>
  
  <p>I must catch and ignore end of socket exception every time my socket ends.</p>
  
  <p>My questions are:</p>
  
  <ol>
  <li>If <code>shutdownInput()</code> is unsupported, how come I'm getting a <code>-1</code> answer from an <code>SSLSocket</code>.</li>
  <li><p>Is there a better solution for my agony? I don't think there's anything sane, since one of the thread might be already in the blocking <code>read</code> method when the other thread marks him "I'm done". And the only way I see to kick him out of the blocking thread is by closing the socket and raising a "closed socket" exception.</p>
  
  <p>(You could I guess use some unholy combination of multithread message passing and asynchronous data reading in order to signal the other thread your work is done, but this is so horrible that I'm afraid IntelliJ's style cop will come after me just for thinking about it...)</p></li>
  </ol>
  
  <p><strong>Clarification:</strong> I know that <code>shutdownInput</code> and <code>shutdownOutput</code> doesn't make sense, since you cannot have a half-duplex TLS connection, per spec. But given that, how can I end a TLS connection in Java without getting an exception?</p>
  
  <p>Please, don't tell me <code>shutdownIput</code> doesn't make sense for TLS. I know that, <strong>this is not what I'm asking</strong>. I'm asking what else can I use, in order to close <code>SSLSocket</code> properly (hence the title, "Properly closing SSLSocket".</p>