1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.newsclub.net.unix.ssl;
19
20 import java.io.FilterInputStream;
21 import java.io.FilterOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.net.Socket;
26 import java.net.SocketException;
27
28 import javax.net.ssl.SSLSocket;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 final class BuilderSSLSocket extends FilterSSLSocket {
45
46
47
48 private final Socket underlyingSocket;
49
50
51
52
53
54 private final boolean doCloseUnderlyingSocket;
55
56 BuilderSSLSocket(SSLSocket socket, Socket underlyingSocket, boolean doCloseUnderlyingSocket) {
57 super(socket);
58 this.underlyingSocket = underlyingSocket;
59 this.doCloseUnderlyingSocket = doCloseUnderlyingSocket;
60 }
61
62 @Override
63 public InputStream getInputStream() throws IOException {
64 return new FilterInputStream(super.getInputStream()) {
65
66 @Override
67 public void close() throws IOException {
68 try {
69 super.close();
70 } catch (SocketException e) {
71
72
73 }
74 }
75 };
76 }
77
78 @Override
79 public OutputStream getOutputStream() throws IOException {
80 return new FilterOutputStream(super.getOutputStream()) {
81
82 @Override
83 public void close() throws IOException {
84 try {
85 super.close();
86 } catch (SocketException e) {
87
88
89 }
90 }
91 };
92 }
93
94 @Override
95 public synchronized void close() throws IOException {
96 try {
97 super.close();
98 } catch (SocketException e) {
99
100
101 if (doCloseUnderlyingSocket) {
102 Socket s = underlyingSocket;
103 if (s != null) {
104 try {
105 s.close();
106 } catch (IOException e1) {
107 e1.addSuppressed(e);
108 throw e1;
109 }
110 }
111 }
112 }
113 }
114 }