1313import java .io .File ;
1414import java .io .IOException ;
1515import java .io .InputStream ;
16- import java .net .URI ;
17- import java .net .http .HttpClient ;
18- import java .net .http .HttpRequest ;
19- import java .net .http .HttpResponse ;
16+ import java .net .HttpURLConnection ;
17+ import java .net .URL ;
2018import java .nio .file .Files ;
2119import java .nio .file .Path ;
20+ import java .nio .file .Paths ;
2221import java .nio .file .StandardCopyOption ;
2322import java .util .concurrent .TimeUnit ;
2423
@@ -31,7 +30,7 @@ public class LinuxDockerProvider implements DockerProvider {
3130 private static final String ROOTLESSKIT_DOWNLOAD_URL = "https://github.com/rootless-containers/rootlesskit/releases/download/%s/rootlesskit-%s.tar.gz" ;
3231 private static final String DOCKER_ROOTLESS_SCRIPT_URL = "https://raw.githubusercontent.com/moby/moby/master/contrib/dockerd-rootless.sh" ;
3332 private static final String DOCKER_DOWNLOAD_URL = "https://download.docker.com/linux/static/stable/%s/docker-%s.tgz" ;
34- private static final Path DOCKER_DIR = Path . of (System .getProperty ("user.home" ), ".docker-java" );
33+ private static final Path DOCKER_DIR = Paths . get (System .getProperty ("user.home" ), ".docker-java" );
3534 private static final Path DOCKER_PATH = DOCKER_DIR .resolve ("docker/dockerd" );
3635 private static final Path ROOTLESSKIT_PATH = DOCKER_DIR .resolve ("rootlesskit" );
3736 private static final Path DOCKER_SOCKET_PATH = DOCKER_DIR .resolve ("run/docker.sock" );
@@ -46,22 +45,22 @@ public class LinuxDockerProvider implements DockerProvider {
4645 private Process dockerProcess ;
4746
4847 @ Override
49- public void ensureInstalled () throws IOException , InterruptedException {
48+ public void ensureInstalled () throws IOException {
5049 ensureDockerInstalled ();
5150 ensureRootlessScriptInstalled ();
5251 ensureRootlessKitInstalled ();
5352 ensureSlirp4netnsInstalled ();
5453 }
5554
5655 @ SuppressWarnings ("ResultOfMethodCallIgnored" )
57- private void ensureDockerInstalled () throws IOException , InterruptedException {
56+ private void ensureDockerInstalled () throws IOException {
5857 boolean autoUpdate = Boolean .parseBoolean (System .getProperty ("docker.auto.update" , "true" ));
5958 String latestVersion = DockerVersionFetcher .getLatestVersion ();
6059 boolean needsUpdate = true ;
6160
6261 if (Files .exists (DOCKER_PATH )) {
6362 if (autoUpdate && Files .exists (DOCKER_VERSION_FILE )) {
64- String installedVersion = Files .readString (DOCKER_VERSION_FILE ).trim ();
63+ String installedVersion = new String ( Files .readAllBytes (DOCKER_VERSION_FILE ) ).trim ();
6564 if (!installedVersion .equals (latestVersion )) {
6665 System .out .println ("Newer Docker version available. Updating from " + installedVersion + " to " + latestVersion );
6766 } else {
@@ -88,20 +87,23 @@ private void ensureDockerInstalled() throws IOException, InterruptedException {
8887 String arch = getArch ();
8988 String dockerUrl = String .format (DOCKER_DOWNLOAD_URL , arch , latestVersion );
9089 downloadAndExtract (dockerUrl , DOCKER_DIR );
91- Files .writeString (DOCKER_VERSION_FILE , latestVersion );
90+ Files .write (DOCKER_VERSION_FILE , latestVersion . getBytes () );
9291 }
9392 }
9493
9594 private String getArch () {
9695 String osArch = System .getProperty ("os.arch" );
97- return switch (osArch ) {
98- case "amd64" -> "x86_64" ;
99- case "aarch64" -> "aarch64" ;
100- default -> throw new UnsupportedOperationException ("Unsupported architecture: " + osArch );
101- };
96+ switch (osArch ) {
97+ case "amd64" :
98+ return "x86_64" ;
99+ case "aarch64" :
100+ return "aarch64" ;
101+ default :
102+ throw new UnsupportedOperationException ("Unsupported architecture: " + osArch );
103+ }
102104 }
103105
104- private void ensureRootlessScriptInstalled () throws IOException , InterruptedException {
106+ private void ensureRootlessScriptInstalled () throws IOException {
105107 Path rootlessScriptPath = DOCKER_PATH .getParent ().resolve ("dockerd-rootless.sh" );
106108 if (!Files .exists (rootlessScriptPath )) {
107109 System .out .println ("Downloading dockerd-rootless.sh script..." );
@@ -110,7 +112,7 @@ private void ensureRootlessScriptInstalled() throws IOException, InterruptedExce
110112 }
111113 }
112114
113- private void ensureRootlessKitInstalled () throws IOException , InterruptedException {
115+ private void ensureRootlessKitInstalled () throws IOException {
114116 if (Files .exists (ROOTLESSKIT_PATH .resolve ("rootlesskit" ))) {
115117 return ;
116118 }
@@ -120,7 +122,7 @@ private void ensureRootlessKitInstalled() throws IOException, InterruptedExcepti
120122 System .out .println ("RootlessKit installed successfully." );
121123 }
122124
123- private void ensureSlirp4netnsInstalled () throws IOException , InterruptedException {
125+ private void ensureSlirp4netnsInstalled () throws IOException {
124126 if (Files .exists (SLIRP4NETNS_PATH )) {
125127 return ;
126128 }
@@ -142,7 +144,6 @@ public void start() throws IOException, InterruptedException {
142144 if (isRoot () && !forceRootless ) {
143145 System .out .println ("Running as root, starting dockerd with sudo." );
144146 pb = new ProcessBuilder ("sudo" , DOCKER_PATH .toString (), "-H" , "unix://" + DOCKER_SOCKET_PATH );
145- dockerProcess = pb .start ();
146147 } else {
147148 System .out .println ("Attempting to start in rootless mode using dockerd-rootless.sh." );
148149
@@ -163,8 +164,8 @@ public void start() throws IOException, InterruptedException {
163164
164165 pb .redirectErrorStream (true );
165166 pb .redirectOutput (ProcessBuilder .Redirect .INHERIT );
166- dockerProcess = pb .start ();
167167 }
168+ dockerProcess = pb .start ();
168169
169170 if (!waitForSocket ()) {
170171 throw new RuntimeException ("Docker daemon failed to create socket in time." );
@@ -205,39 +206,37 @@ private boolean isRoot() {
205206 return username != null && username .equals ("root" );
206207 }
207208
208- private void downloadFile (String urlString , Path destinationPath ) throws IOException , InterruptedException {
209+ private void downloadFile (String urlString , Path destinationPath ) throws IOException {
209210 System .out .println ("Downloading " + urlString + " to " + destinationPath );
210- HttpClient client = HttpClient .newBuilder ()
211- .followRedirects (HttpClient .Redirect .ALWAYS )
212- .build ();
213- HttpRequest request = HttpRequest .newBuilder ()
214- .uri (URI .create (urlString ))
215- .build ();
216- HttpResponse <InputStream > response = client .send (request , HttpResponse .BodyHandlers .ofInputStream ());
217-
218- if (response .statusCode () >= 400 ) {
219- throw new IOException ("Failed to download file: " + response .statusCode ());
211+ URL url = new URL (urlString );
212+ HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
213+ connection .setInstanceFollowRedirects (true );
214+ connection .setRequestMethod ("GET" );
215+ connection .connect ();
216+
217+ int responseCode = connection .getResponseCode ();
218+ if (responseCode >= 400 ) {
219+ throw new IOException ("Failed to download file: " + responseCode );
220220 }
221221
222- try (InputStream in = response . body ()) {
222+ try (InputStream in = connection . getInputStream ()) {
223223 Files .copy (in , destinationPath , java .nio .file .StandardCopyOption .REPLACE_EXISTING );
224224 }
225225 }
226226
227227 @ SuppressWarnings ("deprecation" )
228- private void downloadAndExtract (String urlString , Path destinationDir ) throws IOException , InterruptedException {
228+ private void downloadAndExtract (String urlString , Path destinationDir ) throws IOException {
229229 System .out .println ("Downloading and extracting " + urlString + "..." );
230- HttpClient client = HttpClient .newBuilder ()
231- .followRedirects (HttpClient .Redirect .NORMAL )
232- .build ();
233- HttpRequest request = HttpRequest .newBuilder ().uri (URI .create (urlString )).build ();
234- HttpResponse <InputStream > response = client .send (request , HttpResponse .BodyHandlers .ofInputStream ());
235-
236- if (response .statusCode () != 200 ) {
237- throw new IOException ("Failed to download file from " + urlString + ". Status code: " + response .statusCode ());
230+ URL url = new URL (urlString );
231+ HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
232+ connection .setInstanceFollowRedirects (true );
233+ connection .setRequestMethod ("GET" );
234+
235+ if (connection .getResponseCode () != 200 ) {
236+ throw new IOException ("Failed to download file from " + urlString + ". Status code: " + connection .getResponseCode ());
238237 }
239238
240- try (InputStream is = response . body ();
239+ try (InputStream is = connection . getInputStream ();
241240 GzipCompressorInputStream gzis = new GzipCompressorInputStream (is );
242241 TarArchiveInputStream tis = new TarArchiveInputStream (gzis )) {
243242 TarArchiveEntry entry ;
0 commit comments