@@ -106,10 +106,8 @@ public static File download(String name, String urlPath, @Nullable BiConsumer<Lo
106106 * Decompress a bzip2 file into a new file.
107107 * The method is needed because Micromamba is distributed as a .tar.bz2 file and
108108 * many distributions do not have tools readily available to extract the required files.
109- * @param source
110- * .bzip2 file
111- * @param destination
112- * destination folder where the contents of the file are going to be decompressed
109+ * @param source .bzip2 file
110+ * @param destination destination folder where the contents of the file are going to be decompressed
113111 * @throws FileNotFoundException if the .bzip2 file is not found or does not exist
114112 * @throws IOException if the source file already exists or there is any error with the decompression
115113 * @throws InterruptedException if the thread where the decompression is happening is interrupted
@@ -126,10 +124,8 @@ public static void unBZip2(File source, File destination) throws FileNotFoundExc
126124 /**
127125 * Copies the content of an InputStream into an OutputStream.
128126 *
129- * @param input
130- * the InputStream to copy
131- * @param output
132- * the target, may be null to simulate output to dev/null on Linux and NUL on Windows
127+ * @param input the InputStream to copy
128+ * @param output the target, may be null to simulate output to dev/null on Linux and NUL on Windows
133129 * @return the number of bytes copied
134130 * @throws IOException if an error occurs copying the streams
135131 * @throws InterruptedException if the thread where this is happening is interrupted
@@ -161,8 +157,7 @@ public static void unpack(final File inputFile, final File outputDir) throws Fil
161157 /**
162158 * Decompress a zip file into a directory.
163159 * @param source .zip file
164- * @param destination
165- * destination folder where the contents of the file are going to be decompressed
160+ * @param destination destination folder where the contents of the file are going to be decompressed
166161 * @throws FileNotFoundException if the .zip file is not found or does not exist
167162 * @throws IOException if the source file already exists or there is any error with the decompression
168163 * @throws InterruptedException if the thread where the decompression is happening is interrupted
@@ -214,26 +209,27 @@ public static void unTar(final File inputFile, final File outputDir) throws File
214209 InputStream is = new FileInputStream (inputFile );
215210 TarArchiveInputStream debInputStream = (TarArchiveInputStream ) new ArchiveStreamFactory ().createArchiveInputStream ("tar" , is );
216211 ) {
217- TarArchiveEntry entry = null ;
218- while ((entry = debInputStream .getNextEntry ()) != null ) {
219- final File outputFile = new File (outputDir , entry .getName ());
220- if (entry .isDirectory ()) {
221- if (!outputFile .exists ()) {
222- if (!outputFile .mkdirs ()) {
223- throw new IOException (String .format ("Couldn't create directory %s." , outputFile .getAbsolutePath ()));
224- }
225- }
226- } else {
227- if (!outputFile .getParentFile ().exists ()) {
228- if (!outputFile .getParentFile ().mkdirs ())
229- throw new IOException ("Failed to create directory " + outputFile .getParentFile ().getAbsolutePath ());
230- }
231- try (OutputStream outputFileStream = new FileOutputStream (outputFile )) {
232- copy (debInputStream , outputFileStream );
233- }
234- }
235- }
236- } catch (ArchiveException e ) {
212+ TarArchiveEntry entry = null ;
213+ while ((entry = debInputStream .getNextEntry ()) != null ) {
214+ final File outputFile = new File (outputDir , entry .getName ());
215+ if (entry .isDirectory ()) {
216+ if (!outputFile .exists ()) {
217+ if (!outputFile .mkdirs ()) {
218+ throw new IOException (String .format ("Couldn't create directory %s." , outputFile .getAbsolutePath ()));
219+ }
220+ }
221+ } else {
222+ if (!outputFile .getParentFile ().exists ()) {
223+ if (!outputFile .getParentFile ().mkdirs ())
224+ throw new IOException ("Failed to create directory " + outputFile .getParentFile ().getAbsolutePath ());
225+ }
226+ try (OutputStream outputFileStream = new FileOutputStream (outputFile )) {
227+ copy (debInputStream , outputFileStream );
228+ }
229+ }
230+ }
231+ }
232+ catch (ArchiveException e ) {
237233 throw new IOException (e );
238234 }
239235
@@ -251,30 +247,31 @@ public static void unTar(final File inputFile, final File outputDir) throws File
251247 */
252248 public static void unTarGz (final File inputFile , final File outputDir ) throws FileNotFoundException , IOException , InterruptedException {
253249 try (
254- InputStream is = new FileInputStream (inputFile );
255- InputStream gzipIs = new GzipCompressorInputStream (new BufferedInputStream (is ));
256- TarArchiveInputStream tarInputStream = (TarArchiveInputStream ) new ArchiveStreamFactory ().createArchiveInputStream ("tar" , gzipIs );
257- ) {
258- TarArchiveEntry entry = null ;
259- while ((entry = tarInputStream .getNextEntry ()) != null ) {
260- final File outputFile = new File (outputDir , entry .getName ());
261- if (entry .isDirectory ()) {
262- if (!outputFile .exists ()) {
263- if (!outputFile .mkdirs ()) {
264- throw new IOException (String .format ("Couldn't create directory %s." , outputFile .getAbsolutePath ()));
265- }
266- }
267- } else {
268- if (!outputFile .getParentFile ().exists ()) {
269- if (!outputFile .getParentFile ().mkdirs ())
270- throw new IOException ("Failed to create directory " + outputFile .getParentFile ().getAbsolutePath ());
271- }
272- try (OutputStream outputFileStream = new FileOutputStream (outputFile )) {
273- copy (tarInputStream , outputFileStream );
274- }
275- }
276- }
277- } catch (ArchiveException e ) {
250+ InputStream is = new FileInputStream (inputFile );
251+ InputStream gzipIs = new GzipCompressorInputStream (new BufferedInputStream (is ));
252+ TarArchiveInputStream tarInputStream = (TarArchiveInputStream ) new ArchiveStreamFactory ().createArchiveInputStream ("tar" , gzipIs );
253+ ) {
254+ TarArchiveEntry entry = null ;
255+ while ((entry = tarInputStream .getNextEntry ()) != null ) {
256+ final File outputFile = new File (outputDir , entry .getName ());
257+ if (entry .isDirectory ()) {
258+ if (!outputFile .exists ()) {
259+ if (!outputFile .mkdirs ()) {
260+ throw new IOException (String .format ("Couldn't create directory %s." , outputFile .getAbsolutePath ()));
261+ }
262+ }
263+ } else {
264+ if (!outputFile .getParentFile ().exists ()) {
265+ if (!outputFile .getParentFile ().mkdirs ())
266+ throw new IOException ("Failed to create directory " + outputFile .getParentFile ().getAbsolutePath ());
267+ }
268+ try (OutputStream outputFileStream = new FileOutputStream (outputFile )) {
269+ copy (tarInputStream , outputFileStream );
270+ }
271+ }
272+ }
273+ }
274+ catch (ArchiveException e ) {
278275 throw new IOException (e );
279276 }
280277 }
@@ -286,50 +283,53 @@ public static void unTarGz(final File inputFile, final File outputDir) throws Fi
286283 * - {@link HttpURLConnection#HTTP_MOVED_PERM}
287284 * - {@link HttpURLConnection#HTTP_SEE_OTHER}
288285 *
289- * If that is not the response code or the connection does not work, the url
286+ * If that is not the response code or the connection does not work, the URL
290287 * returned will be the same as the provided.
291- * If the method is used corretly, it will return the URL to which the original URL
292- * has been redirected
293- * @param url
294- * original url. Connecting to that url must give a 301, 302 or 303 response code
295- * @return the redirected url
296- * @throws MalformedURLException if the url does not fulfil the requirements for an url to be correct
297- * @throws URISyntaxException if the url is incorrect or there is no internet connection
288+ * If the method is used correctly, it will return the URL to which the original URL
289+ * has been redirected.
290+ * @param url original URL. Connecting to that URL must give a 301, 302 or 303 response code
291+ * @return the redirected URL
292+ * @throws MalformedURLException if the URL does not fulfil the requirements for an URL to be correct
293+ * @throws URISyntaxException if the URL is incorrect or there is no internet connection
298294 */
299295 public static URL redirectedURL (URL url ) throws MalformedURLException , URISyntaxException {
300296 int statusCode ;
301297 HttpURLConnection conn ;
302298 try {
303299 conn = (HttpURLConnection ) url .openConnection ();
304300 statusCode = conn .getResponseCode ();
305- } catch (IOException ex ) {
301+ }
302+ catch (IOException ex ) {
306303 return url ;
307304 }
308305 if (statusCode < 300 || statusCode > 308 )
309306 return url ;
310307 String newURL = conn .getHeaderField ("Location" );
311308 try {
312309 return redirectedURL (new URL (newURL ));
313- } catch (MalformedURLException ex ) {
310+ }
311+ catch (MalformedURLException ex ) {
312+ // Continue with other approaches.
314313 }
315314 try {
316315 if (newURL .startsWith ("//" ))
317316 return redirectedURL (new URL ("http:" + newURL ));
318317 else
319318 throw new MalformedURLException ();
320- } catch (MalformedURLException ex ) {
321319 }
322- URI uri = url .toURI ();
323- String scheme = uri .getScheme ();
324- String host = uri .getHost ();
325- String mainDomain = scheme + "://" + host ;
320+ catch (MalformedURLException ex ) {
321+ // Keep going with other approaches.
322+ }
323+ URI uri = url .toURI ();
324+ String scheme = uri .getScheme ();
325+ String host = uri .getHost ();
326+ String mainDomain = scheme + "://" + host ;
326327 return redirectedURL (new URL (mainDomain + newURL ));
327328 }
328329
329330 /**
330- * Get the size of the file stored in the given URL
331- * @param url
332- * url where the file is stored
331+ * Gets the size of the file stored in the given URL.
332+ * @param url url where the file is stored
333333 * @return the size of the file
334334 */
335335 public static long getFileSize (URL url ) {
@@ -344,9 +344,11 @@ public static long getFileSize(URL url) {
344344 long size = conn .getContentLengthLong ();
345345 conn .disconnect ();
346346 return size ;
347- } catch (IOException e ) {
347+ }
348+ catch (IOException e ) {
348349 throw new RuntimeException (e );
349- } catch (Exception ex ) {
350+ }
351+ catch (Exception ex ) {
350352 ex .printStackTrace ();
351353 String msg = "Unable to connect to " + url .toString ();
352354 System .out .println (msg );
0 commit comments