Skip to content

Commit a2e8e49

Browse files
committed
Add Java 8 IOUtils and update Process input handling
1 parent 5de503e commit a2e8e49

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ test {
3131
}
3232

3333
java {
34-
sourceCompatibility = JavaVersion.VERSION_11
35-
targetCompatibility = JavaVersion.VERSION_11
34+
sourceCompatibility = JavaVersion.VERSION_1_8
35+
targetCompatibility = JavaVersion.VERSION_1_8
3636
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.github.intisy.docker;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
7+
/**
8+
* I/O utility methods for Java 8 compatibility.
9+
*
10+
* @author Finn Birich
11+
*/
12+
final class IOUtils {
13+
14+
private IOUtils() {
15+
// Utility class
16+
}
17+
18+
/**
19+
* Reads all bytes from an input stream (Java 8 compatible replacement for InputStream.readAllBytes()).
20+
*
21+
* @param inputStream the input stream to read from
22+
* @return byte array containing all bytes read
23+
* @throws IOException if an I/O error occurs
24+
*/
25+
static byte[] readAllBytes(InputStream inputStream) throws IOException {
26+
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
27+
byte[] data = new byte[8192];
28+
int bytesRead;
29+
while ((bytesRead = inputStream.read(data, 0, data.length)) != -1) {
30+
buffer.write(data, 0, bytesRead);
31+
}
32+
return buffer.toByteArray();
33+
}
34+
}

src/main/java/io/github/intisy/docker/WindowsDockerProvider.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.util.zip.ZipEntry;
1616
import java.util.zip.ZipInputStream;
1717

18+
import static io.github.intisy.docker.IOUtils.readAllBytes;
19+
1820
/**
1921
* Windows-specific Docker provider.
2022
* Supports both native Windows containers (requires admin) and WSL2-based Docker (no admin required).
@@ -109,7 +111,7 @@ private void ensureWsl2DockerInstalled() throws IOException {
109111
ProcessBuilder checkPb = new ProcessBuilder("wsl", "-d", wslDistro, "-e", "bash", "-c", "command -v dockerd");
110112
checkPb.redirectErrorStream(true);
111113
Process checkProcess = checkPb.start();
112-
byte[] output = checkProcess.getInputStream().readAllBytes();
114+
byte[] output = readAllBytes(checkProcess.getInputStream());
113115
int exitCode = checkProcess.waitFor();
114116

115117
log.debug("dockerd check exit code: {}, output: {}", exitCode, new String(output).trim());
@@ -340,7 +342,7 @@ private void startWsl2Docker() throws IOException, InterruptedException {
340342

341343
// Check if process is still running
342344
if (!dockerProcess.isAlive()) {
343-
byte[] output = dockerProcess.getInputStream().readAllBytes();
345+
byte[] output = readAllBytes(dockerProcess.getInputStream());
344346
String outputStr = new String(output).trim();
345347
log.error("WSL process died. Output: {}", outputStr.isEmpty() ? "(empty)" : outputStr);
346348

@@ -423,7 +425,7 @@ private boolean checkPasswordlessSudo() {
423425
"sudo -n dockerd --version >/dev/null 2>&1 && echo yes || echo no");
424426
pb.redirectErrorStream(true);
425427
Process process = pb.start();
426-
byte[] output = process.getInputStream().readAllBytes();
428+
byte[] output = readAllBytes(process.getInputStream());
427429
boolean completed = process.waitFor(5, TimeUnit.SECONDS);
428430

429431
if (!completed) {
@@ -450,7 +452,7 @@ private String runWslCommand(String command, boolean useSudo, int timeoutSeconds
450452
ProcessBuilder pb = new ProcessBuilder("wsl", "-d", wslDistro, "-e", "bash", "-c", fullCommand);
451453
pb.redirectErrorStream(true);
452454
Process process = pb.start();
453-
byte[] output = process.getInputStream().readAllBytes();
455+
byte[] output = readAllBytes(process.getInputStream());
454456
process.waitFor(timeoutSeconds, TimeUnit.SECONDS);
455457
return new String(output).trim();
456458
} catch (IOException | InterruptedException e) {
@@ -490,7 +492,7 @@ private boolean isAdministrator() {
490492
ProcessBuilder pb = new ProcessBuilder("net", "session");
491493
pb.redirectErrorStream(true);
492494
Process process = pb.start();
493-
byte[] output = process.getInputStream().readAllBytes();
495+
byte[] output = readAllBytes(process.getInputStream());
494496
int exitCode = process.waitFor();
495497
log.debug("Admin check (net session) exit code: {}", exitCode);
496498
return exitCode == 0;
@@ -510,7 +512,7 @@ private boolean isWsl2Available() {
510512
Process process = pb.start();
511513

512514
// Read as bytes and convert, handling potential UTF-16 encoding
513-
byte[] outputBytes = process.getInputStream().readAllBytes();
515+
byte[] outputBytes = readAllBytes(process.getInputStream());
514516
String output = new String(outputBytes, java.nio.charset.StandardCharsets.UTF_16LE);
515517

516518
int exitCode = process.waitFor();
@@ -564,7 +566,7 @@ private boolean isWsl2Available() {
564566
ProcessBuilder testPb = new ProcessBuilder("wsl", "-d", wslDistro, "-e", "bash", "-c", "echo test");
565567
testPb.redirectErrorStream(true);
566568
Process testProcess = testPb.start();
567-
testProcess.getInputStream().readAllBytes();
569+
readAllBytes(testProcess.getInputStream());
568570
int testExitCode = testProcess.waitFor();
569571

570572
if (testExitCode == 0) {

src/test/java/io/github/intisy/docker/WslDiagnosticTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.io.*;
88
import java.nio.charset.StandardCharsets;
99

10+
import static io.github.intisy.docker.IOUtils.readAllBytes;
11+
1012
/**
1113
* Diagnostic test for WSL2 detection on Windows.
1214
* Run this test to see what's happening with WSL detection.
@@ -28,7 +30,7 @@ void diagnoseWsl2() throws Exception {
2830
ProcessBuilder pb = new ProcessBuilder("wsl", "--version");
2931
pb.redirectErrorStream(true);
3032
Process process = pb.start();
31-
byte[] output = process.getInputStream().readAllBytes();
33+
byte[] output = readAllBytes(process.getInputStream());
3234
int exitCode = process.waitFor();
3335

3436
// Try different encodings
@@ -48,7 +50,7 @@ void diagnoseWsl2() throws Exception {
4850
ProcessBuilder pb = new ProcessBuilder("wsl", "-l", "-v");
4951
pb.redirectErrorStream(true);
5052
Process process = pb.start();
51-
byte[] output = process.getInputStream().readAllBytes();
53+
byte[] output = readAllBytes(process.getInputStream());
5254
int exitCode = process.waitFor();
5355

5456
// WSL output is usually UTF-16LE on Windows
@@ -72,7 +74,7 @@ void diagnoseWsl2() throws Exception {
7274
ProcessBuilder pb = new ProcessBuilder("wsl", "-e", "echo", "hello from wsl");
7375
pb.redirectErrorStream(true);
7476
Process process = pb.start();
75-
byte[] output = process.getInputStream().readAllBytes();
77+
byte[] output = readAllBytes(process.getInputStream());
7678
int exitCode = process.waitFor();
7779

7880
log.info("wsl -e echo exit code: {}", exitCode);
@@ -87,7 +89,7 @@ void diagnoseWsl2() throws Exception {
8789
ProcessBuilder pb = new ProcessBuilder("wsl", "-d", "Ubuntu", "-e", "bash", "-c", "command -v dockerd || echo 'not found'");
8890
pb.redirectErrorStream(true);
8991
Process process = pb.start();
90-
byte[] output = process.getInputStream().readAllBytes();
92+
byte[] output = readAllBytes(process.getInputStream());
9193
int exitCode = process.waitFor();
9294

9395
log.info("dockerd check exit code: {}", exitCode);
@@ -102,7 +104,7 @@ void diagnoseWsl2() throws Exception {
102104
ProcessBuilder pb = new ProcessBuilder("wsl", "-d", "Ubuntu", "-e", "bash", "-c", "groups && id");
103105
pb.redirectErrorStream(true);
104106
Process process = pb.start();
105-
byte[] output = process.getInputStream().readAllBytes();
107+
byte[] output = readAllBytes(process.getInputStream());
106108
int exitCode = process.waitFor();
107109

108110
log.info("groups check exit code: {}", exitCode);
@@ -118,7 +120,7 @@ void diagnoseWsl2() throws Exception {
118120
"dockerd -H unix:///tmp/test-docker.sock 2>&1 & sleep 3; cat /tmp/test-docker.log 2>/dev/null; pkill -f 'dockerd.*test-docker'");
119121
pb.redirectErrorStream(true);
120122
Process process = pb.start();
121-
byte[] output = process.getInputStream().readAllBytes();
123+
byte[] output = readAllBytes(process.getInputStream());
122124
int exitCode = process.waitFor();
123125

124126
log.info("dockerd test exit code: {}", exitCode);
@@ -133,7 +135,7 @@ void diagnoseWsl2() throws Exception {
133135
ProcessBuilder pb = new ProcessBuilder("wsl", "-e", "cat", "/etc/os-release");
134136
pb.redirectErrorStream(true);
135137
Process process = pb.start();
136-
byte[] output = process.getInputStream().readAllBytes();
138+
byte[] output = readAllBytes(process.getInputStream());
137139
int exitCode = process.waitFor();
138140

139141
log.info("os-release exit code: {}", exitCode);

0 commit comments

Comments
 (0)