Skip to content

Commit e5fdc38

Browse files
committed
Use try-with-resources
- Longer lines and less dynamic string building - Favor US English spelling
1 parent bb6b3d3 commit e5fdc38

1 file changed

Lines changed: 44 additions & 88 deletions

File tree

src/main/java/org/apache/commons/io/FileSystemUtils.java

Lines changed: 44 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public class FileSystemUtils {
113113
* the command line.
114114
* This method does not normalize the result, and typically returns
115115
* bytes on Windows, 512 byte units on OS X and kilobytes on Unix.
116-
* As this is not very useful, this method is deprecated in favour
116+
* As this is not very useful, this method is deprecated in favor
117117
* of {@link #freeSpaceKb(String)} which returns a result in kilobytes.
118118
* <p>
119119
* Note that some OS's are NOT currently supported, including OS/390,
@@ -128,7 +128,7 @@ public class FileSystemUtils {
128128
* @param path the path to get free space for, not null, not empty on Unix
129129
* @return the amount of free drive space on the drive or volume
130130
* @throws IllegalArgumentException if the path is invalid
131-
* @throws IllegalStateException if an error occurred in initialisation
131+
* @throws IllegalStateException if an error occurred in initialization
132132
* @throws IOException if an error occurs when finding the free space
133133
* @since 1.1, enhanced OS support in 1.2 and 1.3
134134
* @deprecated Use freeSpaceKb(String)
@@ -148,7 +148,7 @@ public static long freeSpace(final String path) throws IOException {
148148
* freeSpaceKb(FileUtils.current().getAbsolutePath())
149149
* </pre>
150150
* @return the amount of free drive space on the drive or volume in kilobytes
151-
* @throws IllegalStateException if an error occurred in initialisation
151+
* @throws IllegalStateException if an error occurred in initialization
152152
* @throws IOException if an error occurs when finding the free space
153153
* @since 2.0
154154
* @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
@@ -169,7 +169,7 @@ public static long freeSpaceKb() throws IOException {
169169
* @param timeout The timeout amount in milliseconds or no timeout if the value
170170
* is zero or less
171171
* @return the amount of free drive space on the drive or volume in kilobytes
172-
* @throws IllegalStateException if an error occurred in initialisation
172+
* @throws IllegalStateException if an error occurred in initialization
173173
* @throws IOException if an error occurs when finding the free space
174174
* @since 2.0
175175
* @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
@@ -198,7 +198,7 @@ public static long freeSpaceKb(final long timeout) throws IOException {
198198
* @param path the path to get free space for, not null, not empty on Unix
199199
* @return the amount of free drive space on the drive or volume in kilobytes
200200
* @throws IllegalArgumentException if the path is invalid
201-
* @throws IllegalStateException if an error occurred in initialisation
201+
* @throws IllegalStateException if an error occurred in initialization
202202
* @throws IOException if an error occurs when finding the free space
203203
* @since 1.2, enhanced OS support in 1.3
204204
* @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
@@ -230,7 +230,7 @@ public static long freeSpaceKb(final String path) throws IOException {
230230
* is zero or less
231231
* @return the amount of free drive space on the drive or volume in kilobytes
232232
* @throws IllegalArgumentException if the path is invalid
233-
* @throws IllegalStateException if an error occurred in initialisation
233+
* @throws IllegalStateException if an error occurred in initialization
234234
* @throws IOException if an error occurs when finding the free space
235235
* @since 2.0
236236
* @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
@@ -291,7 +291,7 @@ long freeSpaceOS(final String path, final int os, final boolean kb, final Durati
291291
* @param timeout The timeout amount in milliseconds or no timeout if the value
292292
* is zero or less
293293
* @return the amount of free drive space on the volume
294-
* @throws IOException if an error occurs
294+
* @throws IOException If an I/O error occurs
295295
*/
296296
long freeSpaceUnix(final String path, final boolean kb, final boolean posix, final Duration timeout)
297297
throws IOException {
@@ -307,16 +307,13 @@ long freeSpaceUnix(final String path, final boolean kb, final boolean posix, fin
307307
if (posix) {
308308
flags += "P";
309309
}
310-
final String[] cmdAttribs =
311-
flags.length() > 1 ? new String[] {DF, flags, path} : new String[] {DF, path};
310+
final String[] cmdAttribs = flags.length() > 1 ? new String[] { DF, flags, path } : new String[] { DF, path };
312311

313312
// perform the command, asking for up to 3 lines (header, interesting, overflow)
314313
final List<String> lines = performCommand(cmdAttribs, 3, timeout);
315314
if (lines.size() < 2) {
316315
// unknown problem, throw exception
317-
throw new IOException(
318-
"Command line '" + DF + "' did not return info as expected " +
319-
"for path '" + path + "'- response was " + lines);
316+
throw new IOException("Command line '" + DF + "' did not return info as expected for path '" + path + "'- response was " + lines);
320317
}
321318
final String line2 = lines.get(1); // the line we're interested in
322319

@@ -325,9 +322,7 @@ long freeSpaceUnix(final String path, final boolean kb, final boolean posix, fin
325322
if (tok.countTokens() < 4) {
326323
// could be long Filesystem, thus data on third line
327324
if (tok.countTokens() != 1 || lines.size() < 3) {
328-
throw new IOException(
329-
"Command line '" + DF + "' did not return data as expected " +
330-
"for path '" + path + "'- check path is valid");
325+
throw new IOException("Command line '" + DF + "' did not return data as expected for path '" + path + "'- check path is valid");
331326
}
332327
final String line3 = lines.get(2); // the line may be interested in
333328
tok = new StringTokenizer(line3, " ");
@@ -347,7 +342,7 @@ long freeSpaceUnix(final String path, final boolean kb, final boolean posix, fin
347342
* @param timeout The timeout amount in milliseconds or no timeout if the value
348343
* is zero or less
349344
* @return the amount of free drive space on the drive
350-
* @throws IOException if an error occurs
345+
* @throws IOException If an I/O error occurs
351346
*/
352347
long freeSpaceWindows(final String path, final Duration timeout) throws IOException {
353348
String normPath = FilenameUtils.normalize(path, false);
@@ -359,7 +354,7 @@ long freeSpaceWindows(final String path, final Duration timeout) throws IOExcept
359354
}
360355

361356
// build and run the 'dir' command
362-
final String[] cmdAttribs = {"cmd.exe", "/C", "dir /a /-c " + normPath};
357+
final String[] cmdAttribs = { "cmd.exe", "/C", "dir /a /-c " + normPath };
363358

364359
// read in the output of the command to an ArrayList
365360
final List<String> lines = performCommand(cmdAttribs, Integer.MAX_VALUE, timeout);
@@ -375,17 +370,15 @@ long freeSpaceWindows(final String path, final Duration timeout) throws IOExcept
375370
}
376371
}
377372
// all lines are blank
378-
throw new IOException(
379-
"Command line 'dir /-c' did not return any info " +
380-
"for path '" + normPath + "'");
373+
throw new IOException("Command line 'dir /-c' did not return any info for path '" + normPath + "'");
381374
}
382375

383376
/**
384377
* Opens the process to the operating system.
385378
*
386379
* @param cmdAttribs the command line parameters
387380
* @return the process
388-
* @throws IOException if an error occurs
381+
* @throws IOException If an I/O error occurs
389382
*/
390383
Process openProcess(final String[] cmdAttribs) throws IOException {
391384
return Runtime.getRuntime().exec(cmdAttribs);
@@ -397,22 +390,18 @@ Process openProcess(final String[] cmdAttribs) throws IOException {
397390
* @param freeSpace the free space string
398391
* @param path the path
399392
* @return the number of bytes
400-
* @throws IOException if an error occurs
393+
* @throws IOException If an I/O error occurs
401394
*/
402395
long parseBytes(final String freeSpace, final String path) throws IOException {
403396
try {
404397
final long bytes = Long.parseLong(freeSpace);
405398
if (bytes < 0) {
406-
throw new IOException(
407-
"Command line '" + DF + "' did not find free space in response " +
408-
"for path '" + path + "'- check path is valid");
399+
throw new IOException("Command line '" + DF + "' did not find free space in response for path '" + path + "'- check path is valid");
409400
}
410401
return bytes;
411402

412403
} catch (final NumberFormatException ex) {
413-
throw new IOException(
414-
"Command line '" + DF + "' did not return numeric data as expected " +
415-
"for path '" + path + "'- check path is valid", ex);
404+
throw new IOException("Command line '" + DF + "' did not return numeric data as expected for path '" + path + "'- check path is valid", ex);
416405
}
417406
}
418407

@@ -422,7 +411,7 @@ long parseBytes(final String freeSpace, final String path) throws IOException {
422411
* @param line the line to parse
423412
* @param path the path that was sent
424413
* @return the number of bytes
425-
* @throws IOException if an error occurs
414+
* @throws IOException If an I/O error occurs
426415
*/
427416
long parseDir(final String line, final String path) throws IOException {
428417
// read from the end of the line to find the last numeric
@@ -435,27 +424,25 @@ long parseDir(final String line, final String path) throws IOException {
435424
innerLoop1: while (j >= 0) {
436425
final char c = line.charAt(j);
437426
if (Character.isDigit(c)) {
438-
// found the last numeric character, this is the end of
439-
// the free space bytes count
440-
bytesEnd = j + 1;
441-
break innerLoop1;
427+
// found the last numeric character, this is the end of
428+
// the free space bytes count
429+
bytesEnd = j + 1;
430+
break innerLoop1;
442431
}
443432
j--;
444433
}
445434
innerLoop2: while (j >= 0) {
446435
final char c = line.charAt(j);
447436
if (!Character.isDigit(c) && c != ',' && c != '.') {
448-
// found the next non-numeric character, this is the
449-
// beginning of the free space bytes count
450-
bytesStart = j + 1;
451-
break innerLoop2;
437+
// found the next non-numeric character, this is the
438+
// beginning of the free space bytes count
439+
bytesStart = j + 1;
440+
break innerLoop2;
452441
}
453442
j--;
454443
}
455444
if (j < 0) {
456-
throw new IOException(
457-
"Command line 'dir /-c' did not return valid info " +
458-
"for path '" + path + "'");
445+
throw new IOException("Command line 'dir /-c' did not return valid info for path '" + path + "'");
459446
}
460447

461448
// remove commas and dots in the bytes count
@@ -479,73 +466,42 @@ long parseDir(final String line, final String path) throws IOException {
479466
* @throws IOException if an error occurs
480467
*/
481468
List<String> performCommand(final String[] cmdAttribs, final int max, final Duration timeout) throws IOException {
482-
// this method does what it can to avoid the 'Too many open files' error
469+
//
470+
// This method does what it can to avoid the 'Too many open files' error
483471
// based on trial and error and these links:
484472
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4784692
485473
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4801027
486474
// http://forum.java.sun.com/thread.jspa?threadID=533029&messageID=2572018
487475
// however, it's still not perfect as the JDK support is so poor
488476
// (see commons-exec or Ant for a better multithreaded multi-OS solution)
489-
490-
final List<String> lines;
491-
Process proc = null;
492-
InputStream in = null;
493-
OutputStream out = null;
494-
InputStream err = null;
495-
BufferedReader inr = null;
496-
try {
497-
498-
final Thread monitor = ThreadMonitor.start(timeout);
499-
500-
proc = openProcess(cmdAttribs);
501-
in = proc.getInputStream();
502-
out = proc.getOutputStream();
503-
err = proc.getErrorStream();
504-
// default Charset is most likely appropriate here
505-
inr = new BufferedReader(new InputStreamReader(in, Charset.defaultCharset()));
506-
507-
lines = inr.lines().limit(max).map(line -> line.toLowerCase(Locale.getDefault()).trim()).collect(Collectors.toList());
508-
477+
//
478+
final Process proc = openProcess(cmdAttribs);
479+
final Thread monitor = ThreadMonitor.start(timeout);
480+
try (InputStream in = proc.getInputStream();
481+
OutputStream out = proc.getOutputStream();
482+
// default Charset is most likely appropriate here
483+
InputStream err = proc.getErrorStream();
484+
// If in is null here, InputStreamReader throws NullPointerException
485+
BufferedReader inr = new BufferedReader(new InputStreamReader(in, Charset.defaultCharset()))) {
486+
487+
final List<String> lines = inr.lines().limit(max).map(line -> line.toLowerCase(Locale.getDefault()).trim()).collect(Collectors.toList());
509488
proc.waitFor();
510-
511489
ThreadMonitor.stop(monitor);
512490

513491
if (proc.exitValue() != 0) {
514-
// OS command problem, throw exception
492+
// Command problem, throw exception
515493
throw new IOException("Command line returned OS error code '" + proc.exitValue() + "' for command " + Arrays.asList(cmdAttribs));
516494
}
517495
if (lines.isEmpty()) {
518-
// unknown problem, throw exception
519-
throw new IOException("Command line did not return any info " + "for command " + Arrays.asList(cmdAttribs));
520-
}
521-
522-
inr.close();
523-
inr = null;
524-
525-
in.close();
526-
in = null;
527-
528-
if (out != null) {
529-
out.close();
530-
out = null;
531-
}
532-
533-
if (err != null) {
534-
err.close();
535-
err = null;
496+
// Unknown problem, throw exception
497+
throw new IOException("Command line did not return any info for command " + Arrays.asList(cmdAttribs));
536498
}
537499

538500
return lines;
539501

540502
} catch (final InterruptedException ex) {
541-
throw new IOException(
542-
"Command line threw an InterruptedException " +
543-
"for command " + Arrays.asList(cmdAttribs) + " timeout=" + timeout, ex);
503+
throw new IOException("Command line threw an InterruptedException for command " + Arrays.asList(cmdAttribs) + " timeout=" + timeout, ex);
544504
} finally {
545-
IOUtils.closeQuietly(in);
546-
IOUtils.closeQuietly(out);
547-
IOUtils.closeQuietly(err);
548-
IOUtils.closeQuietly(inr);
549505
if (proc != null) {
550506
proc.destroy();
551507
}

0 commit comments

Comments
 (0)