X Tutup
Skip to content

Commit 44ee6ad

Browse files
authored
Merge pull request kubernetes-client#700 from brendandburns/close
Improve close behavior some.
2 parents 10320ef + d23be42 commit 44ee6ad

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

util/src/main/java/io/kubernetes/client/Copy.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ public void copyDirectoryFromPod(
115115
container,
116116
false,
117117
false);
118-
InputStream is = new Base64InputStream(new BufferedInputStream(proc.getInputStream()));
119-
try (ArchiveInputStream archive = new TarArchiveInputStream(is)) {
118+
try (InputStream is = new Base64InputStream(new BufferedInputStream(proc.getInputStream()));
119+
ArchiveInputStream archive = new TarArchiveInputStream(is)) {
120120
// TODO Use new GzipCompressorInputStream(is))) here {
121121
for (ArchiveEntry entry = archive.getNextEntry();
122122
entry != null;
@@ -136,7 +136,6 @@ public void copyDirectoryFromPod(
136136
throw new IOException("create directory failed: " + parent);
137137
}
138138
try (OutputStream fs = new FileOutputStream(f)) {
139-
System.out.println("Writing: " + f.getCanonicalPath());
140139
ByteStreams.copy(archive, fs);
141140
fs.flush();
142141
}
@@ -156,10 +155,10 @@ public void copyDirectoryFromPod(
156155
public static void copyFileFromPod(String namespace, String pod, String srcPath, Path dest)
157156
throws ApiException, IOException {
158157
Copy c = new Copy();
159-
InputStream is = c.copyFileFromPod(namespace, pod, null, srcPath);
160-
FileOutputStream os = new FileOutputStream(dest.toFile());
161-
ByteStreams.copy(is, os);
162-
os.flush();
163-
os.close();
158+
try (InputStream is = c.copyFileFromPod(namespace, pod, null, srcPath);
159+
FileOutputStream os = new FileOutputStream(dest.toFile())) {
160+
ByteStreams.copy(is, os);
161+
os.flush();
162+
}
164163
}
165164
}

util/src/main/java/io/kubernetes/client/Exec.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ protected void handleMessage(int stream, InputStream inStream) throws IOExceptio
275275
ExecProcess.this.notifyAll();
276276
}
277277
}
278+
inStream.close();
278279
} else super.handleMessage(stream, inStream);
279280
}
280281

util/src/main/java/io/kubernetes/client/util/Watch.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.kubernetes.client.ApiException;
2222
import io.kubernetes.client.JSON;
2323
import io.kubernetes.client.models.V1Status;
24+
import java.io.Closeable;
2425
import java.io.IOException;
2526
import java.io.StringReader;
2627
import java.lang.reflect.Type;
@@ -34,7 +35,7 @@
3435
* example CoreV1Api.listNamespace has watch parameter, so you can create a call using
3536
* CoreV1Api.listNamespaceCall and set watch to True and watch the changes to namespaces.
3637
*/
37-
public class Watch<T> implements Watchable<T> {
38+
public class Watch<T> implements Watchable<T>, Closeable {
3839

3940
private static final Logger log = LoggerFactory.getLogger(Watch.class);
4041

@@ -94,7 +95,7 @@ public static <T> Watch<T> createWatch(ApiClient client, Call call, Type watchTy
9495
String respBody = null;
9596
try (ResponseBody body = response.body()) {
9697
if (body != null) {
97-
respBody = response.body().string();
98+
respBody = body.string();
9899
}
99100
} catch (IOException e) {
100101
throw new ApiException(

util/src/main/java/io/kubernetes/client/util/WebSocketStreamHandler.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,12 @@ public void textMessage(Reader in) {
8585
}
8686

8787
protected void handleMessage(int stream, InputStream inStream) throws IOException {
88-
OutputStream out = getSocketInputOutputStream(stream);
89-
ByteStreams.copy(inStream, out);
88+
try {
89+
OutputStream out = getSocketInputOutputStream(stream);
90+
ByteStreams.copy(inStream, out);
91+
} finally {
92+
inStream.close();
93+
}
9094
}
9195

9296
@Override

util/src/main/java/io/kubernetes/client/util/WebSockets.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,15 @@ public void onOpen(final WebSocket webSocket, Response response) {
136136

137137
@Override
138138
public void onMessage(ResponseBody body) throws IOException {
139-
if (body.contentType() == TEXT) {
140-
listener.textMessage(body.charStream());
141-
} else if (body.contentType() == BINARY) {
142-
listener.bytesMessage(body.byteStream());
139+
try {
140+
if (body.contentType() == TEXT) {
141+
listener.textMessage(body.charStream());
142+
} else if (body.contentType() == BINARY) {
143+
listener.bytesMessage(body.byteStream());
144+
}
145+
} finally {
146+
body.close();
143147
}
144-
body.close();
145148
}
146149

147150
@Override

0 commit comments

Comments
 (0)
X Tutup