5
5
6
6
import java .io .IOException ;
7
7
import java .io .InputStream ;
8
+ import java .io .OutputStream ;
8
9
import java .net .HttpURLConnection ;
9
10
import java .net .URI ;
10
11
import java .net .URISyntaxException ;
22
23
public class WebClient8Impl extends WebClientCommon {
23
24
24
25
static class Header {
25
- private String name ;
26
- private String value ;
26
+ private final String name ;
27
+ private final String value ;
27
28
28
29
Header (String name , String value ) {
29
30
this .name = name ;
@@ -116,18 +117,19 @@ public Java8WebRequestWithBody(String method, String url, String body) {
116
117
public void completeRequest (HttpURLConnection connection ) throws IOException {
117
118
super .completeRequest (connection );
118
119
connection .setDoOutput (true );
119
- connection .getOutputStream ().write (body .getBytes ());
120
+
121
+ try (final OutputStream outputStream = connection .getOutputStream ()) {
122
+ outputStream .write (body .getBytes ());
123
+ }
120
124
}
121
125
}
122
126
123
127
static class Java8WebResponse implements WebResponse {
124
- private final WebRequest request ;
125
128
private final HttpURLConnection connection ;
126
129
private final int responseCode ;
127
130
private final Map <String , List <String >> headerFields ;
128
131
129
- Java8WebResponse (WebRequest request , HttpURLConnection connection ) throws IOException {
130
- this .request = request ;
132
+ Java8WebResponse (HttpURLConnection connection ) throws IOException {
131
133
this .connection = connection ;
132
134
responseCode = connection .getResponseCode ();
133
135
headerFields = connection .getHeaderFields ();
@@ -153,7 +155,7 @@ public Stream<String> getHeadersAsStream(String headerName) {
153
155
}
154
156
155
157
@ Override
156
- public void close () throws IOException {
158
+ public void close () {
157
159
158
160
}
159
161
}
@@ -164,7 +166,7 @@ public WebResponse send(WebRequest request) throws IOException {
164
166
HttpURLConnection connection = openConnection (request .getURI ().toURL ());
165
167
((Java8WebRequest ) request ).completeRequest (connection );
166
168
167
- return new Java8WebResponse (request , connection );
169
+ return new Java8WebResponse (connection );
168
170
}
169
171
170
172
/**
@@ -186,7 +188,7 @@ private HttpURLConnection openConnection(URL url ) throws IOException {
186
188
}
187
189
188
190
@ Override
189
- public void close () throws IOException {
191
+ public void close () {
190
192
191
193
}
192
194
}
0 commit comments