Skip to content

Commit d23be42

Browse files
committed
Improve close behavior some.
1 parent b118303 commit d23be42

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
}
@@ -148,10 +147,10 @@ public void copyDirectoryFromPod(
148147
public static void copyFileFromPod(String namespace, String pod, String srcPath, Path dest)
149148
throws ApiException, IOException {
150149
Copy c = new Copy();
151-
InputStream is = c.copyFileFromPod(namespace, pod, null, srcPath);
152-
FileOutputStream os = new FileOutputStream(dest.toFile());
153-
ByteStreams.copy(is, os);
154-
os.flush();
155-
os.close();
150+
try (InputStream is = c.copyFileFromPod(namespace, pod, null, srcPath);
151+
FileOutputStream os = new FileOutputStream(dest.toFile())) {
152+
ByteStreams.copy(is, os);
153+
os.flush();
154+
}
156155
}
157156
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ protected void handleMessage(int stream, InputStream inStream) throws IOExceptio
266266
ExecProcess.this.notifyAll();
267267
}
268268
}
269+
inStream.close();
269270
} else super.handleMessage(stream, inStream);
270271
}
271272

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
@@ -84,8 +84,12 @@ public void textMessage(Reader in) {
8484
}
8585

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

9195
@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
@@ -129,12 +129,15 @@ public void onOpen(final WebSocket webSocket, Response response) {
129129

130130
@Override
131131
public void onMessage(ResponseBody body) throws IOException {
132-
if (body.contentType() == TEXT) {
133-
listener.textMessage(body.charStream());
134-
} else if (body.contentType() == BINARY) {
135-
listener.bytesMessage(body.byteStream());
132+
try {
133+
if (body.contentType() == TEXT) {
134+
listener.textMessage(body.charStream());
135+
} else if (body.contentType() == BINARY) {
136+
listener.bytesMessage(body.byteStream());
137+
}
138+
} finally {
139+
body.close();
136140
}
137-
body.close();
138141
}
139142

140143
@Override

0 commit comments

Comments
 (0)