Skip to content

Commit 1bd3a1f

Browse files
author
Gary Gregory
committed
PMD: Remove unnecessary final
1 parent 6d6096f commit 1bd3a1f

79 files changed

Lines changed: 384 additions & 385 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ public static void copyToFile(final InputStream inputStream, final File file) th
10681068
* @throws IOException if an IO error occurs during copying
10691069
*/
10701070
public static void copyURLToFile(final URL source, final File destination) throws IOException {
1071-
try (final InputStream stream = source.openStream()) {
1071+
try (InputStream stream = source.openStream()) {
10721072
final Path path = destination.toPath();
10731073
PathUtils.createParentDirectories(path);
10741074
Files.copy(stream, path, StandardCopyOption.REPLACE_EXISTING);
@@ -1096,10 +1096,10 @@ public static void copyURLToFile(final URL source, final File destination) throw
10961096
*/
10971097
public static void copyURLToFile(final URL source, final File destination, final int connectionTimeoutMillis, final int readTimeoutMillis)
10981098
throws IOException {
1099-
try (final CloseableURLConnection urlConnection = CloseableURLConnection.open(source)) {
1099+
try (CloseableURLConnection urlConnection = CloseableURLConnection.open(source)) {
11001100
urlConnection.setConnectTimeout(connectionTimeoutMillis);
11011101
urlConnection.setReadTimeout(readTimeoutMillis);
1102-
try (final InputStream stream = urlConnection.getInputStream()) {
1102+
try (InputStream stream = urlConnection.getInputStream()) {
11031103
copyInputStreamToFile(stream, destination);
11041104
}
11051105
}

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,10 +2485,9 @@ public static BufferedReader toBufferedReader(final Reader reader, final int siz
24852485
*/
24862486
public static byte[] toByteArray(final InputStream inputStream) throws IOException {
24872487
// We use a ThresholdingOutputStream to avoid reading AND writing more than Integer.MAX_VALUE.
2488-
try (final UnsynchronizedByteArrayOutputStream ubaOutput = new UnsynchronizedByteArrayOutputStream();
2489-
final ThresholdingOutputStream thresholdOuput = new ThresholdingOutputStream(Integer.MAX_VALUE, os -> {
2490-
throw new IllegalArgumentException(
2491-
String.format("Cannot read more than %,d into a byte array", Integer.MAX_VALUE));
2488+
try (UnsynchronizedByteArrayOutputStream ubaOutput = new UnsynchronizedByteArrayOutputStream();
2489+
ThresholdingOutputStream thresholdOuput = new ThresholdingOutputStream(Integer.MAX_VALUE, os -> {
2490+
throw new IllegalArgumentException(String.format("Cannot read more than %,d into a byte array", Integer.MAX_VALUE));
24922491
}, os -> ubaOutput)) {
24932492
copy(inputStream, thresholdOuput);
24942493
return ubaOutput.toByteArray();
@@ -2589,7 +2588,7 @@ public static byte[] toByteArray(final Reader reader) throws IOException {
25892588
* @since 2.3
25902589
*/
25912590
public static byte[] toByteArray(final Reader reader, final Charset charset) throws IOException {
2592-
try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) {
2591+
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
25932592
copy(reader, output, charset);
25942593
return output.toByteArray();
25952594
}
@@ -2662,7 +2661,7 @@ public static byte[] toByteArray(final URI uri) throws IOException {
26622661
* @since 2.4
26632662
*/
26642663
public static byte[] toByteArray(final URL url) throws IOException {
2665-
try (final CloseableURLConnection urlConnection = CloseableURLConnection.open(url)) {
2664+
try (CloseableURLConnection urlConnection = CloseableURLConnection.open(url)) {
26662665
return IOUtils.toByteArray(urlConnection);
26672666
}
26682667
}
@@ -2930,7 +2929,7 @@ public static String toString(final InputStream input) throws IOException {
29302929
* @since 2.3
29312930
*/
29322931
public static String toString(final InputStream input, final Charset charset) throws IOException {
2933-
try (final StringBuilderWriter sw = new StringBuilderWriter()) {
2932+
try (StringBuilderWriter sw = new StringBuilderWriter()) {
29342933
copy(input, sw, charset);
29352934
return sw.toString();
29362935
}
@@ -2975,7 +2974,7 @@ public static String toString(final InputStream input, final String charsetName)
29752974
* @throws IOException if an I/O error occurs
29762975
*/
29772976
public static String toString(final Reader reader) throws IOException {
2978-
try (final StringBuilderWriter sw = new StringBuilderWriter()) {
2977+
try (StringBuilderWriter sw = new StringBuilderWriter()) {
29792978
copy(reader, sw);
29802979
return sw.toString();
29812980
}

src/main/java/org/apache/commons/io/file/PathUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public static PathCounters copyDirectory(final Path sourceDirectory, final Path
277277
* @see Files#copy(InputStream, Path, CopyOption...)
278278
*/
279279
public static Path copyFile(final URL sourceFile, final Path targetFile, final CopyOption... copyOptions) throws IOException {
280-
try (final InputStream inputStream = sourceFile.openStream()) {
280+
try (InputStream inputStream = sourceFile.openStream()) {
281281
Files.copy(inputStream, targetFile, copyOptions);
282282
return targetFile;
283283
}
@@ -308,7 +308,7 @@ public static Path copyFileToDirectory(final Path sourceFile, final Path targetD
308308
* @see Files#copy(InputStream, Path, CopyOption...)
309309
*/
310310
public static Path copyFileToDirectory(final URL sourceFile, final Path targetDirectory, final CopyOption... copyOptions) throws IOException {
311-
try (final InputStream inputStream = sourceFile.openStream()) {
311+
try (InputStream inputStream = sourceFile.openStream()) {
312312
Files.copy(inputStream, targetDirectory.resolve(sourceFile.getFile()), copyOptions);
313313
return targetDirectory;
314314
}
@@ -721,8 +721,8 @@ public static boolean fileContentEquals(final Path path1, final Path path2, fina
721721
// same file
722722
return true;
723723
}
724-
try (final InputStream inputStream1 = Files.newInputStream(nPath1, openOptions);
725-
final InputStream inputStream2 = Files.newInputStream(nPath2, openOptions)) {
724+
try (InputStream inputStream1 = Files.newInputStream(nPath1, openOptions);
725+
InputStream inputStream2 = Files.newInputStream(nPath2, openOptions)) {
726726
return IOUtils.contentEquals(inputStream1, inputStream2);
727727
}
728728
}

src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public boolean accept(final File file) {
292292
public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
293293
if (file != null && Files.isRegularFile(file) && Files.isReadable(file)) {
294294
try {
295-
try (final FileChannel fileChannel = FileChannel.open(file)) {
295+
try (FileChannel fileChannel = FileChannel.open(file)) {
296296
final ByteBuffer byteBuffer = ByteBuffer.allocate(this.magicNumbers.length);
297297
final int read = fileChannel.read(byteBuffer);
298298
if (read != magicNumbers.length) {

src/main/java/org/apache/commons/io/input/TimestampedObserver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*
3232
* <pre>
3333
* final TimestampedObserver timetampedObserver = new TimestampedObserver();
34-
* try (final ObservableInputStream inputStream = new ObservableInputStream(...),
34+
* try (ObservableInputStream inputStream = new ObservableInputStream(...),
3535
* timetampedObserver)) {
3636
* ...
3737
* }

src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static InputStream toBufferedInputStream(final InputStream input)
7878
*/
7979
public static InputStream toBufferedInputStream(final InputStream input, final int size)
8080
throws IOException {
81-
try (final ByteArrayOutputStream output = new ByteArrayOutputStream(size)) {
81+
try (ByteArrayOutputStream output = new ByteArrayOutputStream(size)) {
8282
output.write(input);
8383
return output.toInputStream();
8484
}

src/main/java/org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static InputStream toBufferedInputStream(final InputStream input) throws
7171
*/
7272
public static InputStream toBufferedInputStream(final InputStream input, final int size) throws IOException {
7373
// It does not matter if a ByteArrayOutputStream is not closed as close() is a no-op
74-
try (final UnsynchronizedByteArrayOutputStream output = new UnsynchronizedByteArrayOutputStream(size)) {
74+
try (UnsynchronizedByteArrayOutputStream output = new UnsynchronizedByteArrayOutputStream(size)) {
7575
output.write(input);
7676
return output.toInputStream();
7777
}

src/test/java/org/apache/commons/io/FileCleaningTrackerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void testFileCleanerDirectory_ForceStrategy() throws Exception {
127127
throw new IOException("Cannot create file " + testFile
128128
+ " as the parent directory does not exist");
129129
}
130-
try (final BufferedOutputStream output =
130+
try (BufferedOutputStream output =
131131
new BufferedOutputStream(Files.newOutputStream(testFile.toPath()))) {
132132
TestUtils.generateTestData(output, 100);
133133
}

src/test/java/org/apache/commons/io/FileDeleteStrategyTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void testDeleteForce() throws Exception {
4949
throw new IOException("Cannot create file " + subFile
5050
+ " as the parent directory does not exist");
5151
}
52-
try (final BufferedOutputStream output =
52+
try (BufferedOutputStream output =
5353
new BufferedOutputStream(Files.newOutputStream(subFile.toPath()))) {
5454
TestUtils.generateTestData(output, 16);
5555
}
@@ -75,7 +75,7 @@ public void testDeleteNormal() throws Exception {
7575
throw new IOException("Cannot create file " + subFile
7676
+ " as the parent directory does not exist");
7777
}
78-
try (final BufferedOutputStream output =
78+
try (BufferedOutputStream output =
7979
new BufferedOutputStream(Files.newOutputStream(subFile.toPath()))) {
8080
TestUtils.generateTestData(output, 16);
8181
}
@@ -114,7 +114,7 @@ public void testDeleteQuietlyNormal() throws Exception {
114114
throw new IOException("Cannot create file " + subFile
115115
+ " as the parent directory does not exist");
116116
}
117-
try (final BufferedOutputStream output =
117+
try (BufferedOutputStream output =
118118
new BufferedOutputStream(Files.newOutputStream(subFile.toPath()))) {
119119
TestUtils.generateTestData(output, 16);
120120
}

src/test/java/org/apache/commons/io/FileUtilsCopyDirectoryToDirectoryTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void copyDirectoryToDirectoryThrowsIllegalArgumentExceptionWithCorrectMes
7474
@Test
7575
public void copyDirectoryToDirectoryThrowsIllegalExceptionWithCorrectMessageWhenSrcDirIsNotDirectory()
7676
throws IOException {
77-
try (final TempFile srcDir = TempFile.create("notadireotry", null)) {
77+
try (TempFile srcDir = TempFile.create("notadireotry", null)) {
7878
final File destDir = new File(temporaryFolder, "destinationDirectory");
7979
destDir.mkdirs();
8080
final String expectedMessage = String.format("Parameter 'sourceDir' is not a directory: '%s'", srcDir);
@@ -100,7 +100,7 @@ public void copyDirectoryToDirectoryThrowsNullPointerExceptionWithCorrectMessage
100100

101101
@Test
102102
public void copyFileAndCheckAcl() throws IOException {
103-
try (final TempFile sourcePath = TempFile.create("TempOutput", ".bin")) {
103+
try (TempFile sourcePath = TempFile.create("TempOutput", ".bin")) {
104104
final Path destPath = Paths.get(temporaryFolder.getAbsolutePath(), "SomeFile.bin");
105105
// Test copy attributes without replace FIRST.
106106
FileUtils.copyFile(sourcePath.toFile(), destPath.toFile(), true, StandardCopyOption.COPY_ATTRIBUTES);

0 commit comments

Comments
 (0)