@@ -117,8 +117,7 @@ public CompletableFuture<ResolutionResult<String>> readString(final URL source,
117117 return this .download (source , this .baseDirectory .resolve (relativePath ), path -> {
118118 final HttpResponse .BodyHandler <String > reader = HttpResponse .BodyHandlers .ofString (StandardCharsets .UTF_8 );
119119 if (this .writeToDisk ) {
120- final HttpResponse .BodyHandler <Path > downloader = HttpResponse .BodyHandlers .ofFile (path );
121- return info -> new TeeSubscriber <>(reader .apply (info ), downloader .apply (info ));
120+ return JdkHttpClientDownloader .downloading (reader , path );
122121 } else {
123122 return reader ;
124123 }
@@ -132,8 +131,7 @@ public CompletableFuture<ResolutionResult<String>> readStringAndValidate(
132131 return this .downloadValidating (source , this .baseDirectory .resolve (relativePath ), algorithm , hash , path -> {
133132 final HttpResponse .BodyHandler <String > reader = HttpResponse .BodyHandlers .ofString (StandardCharsets .UTF_8 );
134133 if (this .writeToDisk ) {
135- final HttpResponse .BodyHandler <Path > downloader = HttpResponse .BodyHandlers .ofFile (path );
136- return info -> new TeeSubscriber <>(reader .apply (info ), downloader .apply (info ));
134+ return JdkHttpClientDownloader .downloading (reader , path );
137135 } else {
138136 return reader ;
139137 }
@@ -149,8 +147,7 @@ public CompletableFuture<ResolutionResult<byte[]>> readBytes(final URL source, f
149147 return this .download (source , this .baseDirectory .resolve (relativePath ), path -> {
150148 final HttpResponse .BodyHandler <byte []> reader = HttpResponse .BodyHandlers .ofByteArray ();
151149 if (this .writeToDisk ) {
152- final HttpResponse .BodyHandler <Path > downloader = HttpResponse .BodyHandlers .ofFile (path );
153- return info -> new TeeSubscriber <>(reader .apply (info ), downloader .apply (info ));
150+ return JdkHttpClientDownloader .downloading (reader , path );
154151 } else {
155152 return reader ;
156153 }
@@ -164,8 +161,7 @@ public CompletableFuture<ResolutionResult<byte[]>> readBytesAndValidate(
164161 return this .downloadValidating (source , this .baseDirectory .resolve (relativePath ), algorithm , hash , path -> {
165162 final HttpResponse .BodyHandler <byte []> reader = HttpResponse .BodyHandlers .ofByteArray ();
166163 if (this .writeToDisk ) {
167- final HttpResponse .BodyHandler <Path > downloader = HttpResponse .BodyHandlers .ofFile (path );
168- return info -> new TeeSubscriber <>(reader .apply (info ), downloader .apply (info ));
164+ return JdkHttpClientDownloader .downloading (reader , path );
169165 } else {
170166 return reader ;
171167 }
@@ -181,7 +177,7 @@ public CompletableFuture<ResolutionResult<Path>> download(final URL source, fina
181177 return this .download (
182178 source ,
183179 this .baseDirectory .resolve (destination ),
184- HttpResponse . BodyHandlers :: ofFile ,
180+ JdkHttpClientDownloader :: downloading ,
185181 CompletableFuture ::completedFuture
186182 );
187183 }
@@ -195,7 +191,7 @@ public CompletableFuture<ResolutionResult<Path>> downloadAndValidate(
195191 this .baseDirectory .resolve (destination ),
196192 algorithm ,
197193 hash ,
198- HttpResponse . BodyHandlers :: ofFile ,
194+ JdkHttpClientDownloader :: downloading ,
199195 CompletableFuture ::completedFuture
200196 );
201197 }
@@ -314,11 +310,21 @@ public void close() throws IOException {
314310
315311 // body subscribers
316312 static <T > HttpResponse .BodyHandler <T > validating (final HttpResponse .BodyHandler <T > original , final HashAlgorithm algo , final String expectedHash ) {
317- return info -> JdkHttpClientDownloader . validating ( original .apply (info ), algo , expectedHash );
313+ return info -> new ValidatingBodySubscriber <>( algo , original .apply (info ), expectedHash );
318314 }
319315
320- static <T > HttpResponse .BodySubscriber <T > validating (final HttpResponse .BodySubscriber <T > original , final HashAlgorithm algo , final String expectedHash ) {
321- return new ValidatingBodySubscriber <>(algo , original , expectedHash );
316+ static HttpResponse .BodyHandler <Path > downloading (final Path path ) {
317+ try {
318+ FileUtils .createDirectoriesSymlinkSafe (path .getParent ());
319+ } catch (final IOException ex ) {
320+ JdkHttpClientDownloader .LOGGER .log (System .Logger .Level .WARNING , "Failed to create directory {} before downloading" , path .getParent (), ex );
321+ }
322+ return HttpResponse .BodyHandlers .ofFile (path );
323+ }
324+
325+ static <T > HttpResponse .BodyHandler <T > downloading (final HttpResponse .BodyHandler <T > original , final Path path ) {
326+ final HttpResponse .BodyHandler <Path > downloader = JdkHttpClientDownloader .downloading (path );
327+ return info -> new TeeSubscriber <>(original .apply (info ), downloader .apply (info ));
322328 }
323329
324330}
0 commit comments