Skip to content

Commit 861ec2c

Browse files
committed
Reuse IOUtils
1 parent 0ea689c commit 861ec2c

File tree

7 files changed

+26
-52
lines changed

7 files changed

+26
-52
lines changed

commons-vfs2/src/main/java/org/apache/commons/vfs2/cache/LRUFilesCache.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525

2626
import org.apache.commons.collections4.map.AbstractLinkedMap;
2727
import org.apache.commons.collections4.map.LRUMap;
28+
import org.apache.commons.io.IOUtils;
2829
import org.apache.commons.logging.Log;
2930
import org.apache.commons.logging.LogFactory;
3031
import org.apache.commons.vfs2.FileName;
3132
import org.apache.commons.vfs2.FileObject;
3233
import org.apache.commons.vfs2.FileSystem;
33-
import org.apache.commons.vfs2.FileSystemException;
3434
import org.apache.commons.vfs2.VfsLog;
3535
import org.apache.commons.vfs2.util.Messages;
3636

@@ -77,18 +77,12 @@ protected boolean removeLRU(final AbstractLinkedMap.LinkEntry<FileName, FileObje
7777

7878
// System.err.println(">>> " + size() + " removeLRU:" + linkEntry.getKey().toString());
7979
if (super.removeLRU(linkEntry)) {
80-
try {
81-
// force detach
82-
fileObject.close();
83-
} catch (final FileSystemException e) {
84-
VfsLog.warn(getLogger(), log, Messages.getString("vfs.impl/LRUFilesCache-remove-ex.warn"), e);
85-
}
86-
80+
// force detach
81+
IOUtils.closeQuietly(fileObject, e -> VfsLog.warn(getLogger(), log, Messages.getString("vfs.impl/LRUFilesCache-remove-ex.warn"), e));
8782
final Map<?, ?> files = fileSystemCache.get(filesystem);
8883
if (files.isEmpty()) {
8984
fileSystemCache.remove(filesystem);
9085
}
91-
9286
return true;
9387
}
9488

commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/StandardFileSystemManager.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import javax.xml.parsers.DocumentBuilderFactory;
2929
import javax.xml.parsers.ParserConfigurationException;
3030

31+
import org.apache.commons.io.IOUtils;
3132
import org.apache.commons.lang3.ArrayUtils;
3233
import org.apache.commons.lang3.StringUtils;
3334
import org.apache.commons.vfs2.FileSystemException;
@@ -207,13 +208,7 @@ private void configure(final URL configUri) throws FileSystemException {
207208
} catch (final Exception e) {
208209
throw new FileSystemException("vfs.impl/load-config.error", configUri.toString(), e);
209210
} finally {
210-
if (configStream != null) {
211-
try {
212-
configStream.close();
213-
} catch (final IOException e) {
214-
getLogger().warn(e.getLocalizedMessage(), e);
215-
}
216-
}
211+
IOUtils.closeQuietly(configStream, e -> getLogger().warn(e.getLocalizedMessage(), e));
217212
}
218213
}
219214

commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333
import java.util.List;
3434
import java.util.Map;
3535
import java.util.Objects;
36+
import java.util.concurrent.atomic.AtomicReference;
3637
import java.util.stream.Stream;
3738

39+
import org.apache.commons.io.IOUtils;
3840
import org.apache.commons.io.function.Uncheck;
3941
import org.apache.commons.vfs2.Capability;
4042
import org.apache.commons.vfs2.FileContent;
@@ -220,28 +222,21 @@ protected void childrenChanged(final FileName childName, final FileType newType)
220222
*/
221223
@Override
222224
public void close() throws FileSystemException {
223-
FileSystemException exc = null;
224-
225+
AtomicReference<Exception> ref = new AtomicReference<>();
225226
synchronized (fileSystem) {
226227
// Close the content
227-
if (content != null) {
228-
try {
229-
content.close();
230-
content = null;
231-
} catch (final FileSystemException e) {
232-
exc = e;
233-
}
228+
IOUtils.closeQuietly(content, ref::set);
229+
if (ref.get() != null) {
230+
content = null;
234231
}
235-
236232
// Detach from the file
237233
try {
238234
detach();
239235
} catch (final Exception e) {
240-
exc = new FileSystemException("vfs.provider/close.error", fileName, e);
236+
ref.set(e);
241237
}
242-
243-
if (exc != null) {
244-
throw exc;
238+
if (ref.get() != null) {
239+
throw new FileSystemException("vfs.provider/close.error", fileName, ref.get());
245240
}
246241
}
247242
}

commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpRandomAccessContent.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.FilterInputStream;
2121
import java.io.IOException;
2222

23+
import org.apache.commons.io.IOUtils;
2324
import org.apache.commons.vfs2.FileSystemException;
2425
import org.apache.commons.vfs2.provider.AbstractRandomAccessStreamContent;
2526
import org.apache.commons.vfs2.util.RandomAccessMode;
@@ -116,14 +117,10 @@ public void seek(final long pos) throws IOException {
116117
// no change
117118
return;
118119
}
119-
120120
if (pos < 0) {
121121
throw new FileSystemException("vfs.provider/random-access-invalid-position.error", Long.valueOf(pos));
122122
}
123-
if (dis != null) {
124-
close();
125-
}
126-
123+
IOUtils.close(dis);
127124
filePointer = pos;
128125
}
129126
}

commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileSystem.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.zip.ZipEntry;
2828
import java.util.zip.ZipFile;
2929

30+
import org.apache.commons.io.IOUtils;
3031
import org.apache.commons.logging.Log;
3132
import org.apache.commons.logging.LogFactory;
3233
import org.apache.commons.vfs2.Capability;
@@ -129,10 +130,8 @@ protected ZipFileObject createZipFileObject(final AbstractFileName fileName, fin
129130
protected void doCloseCommunicationLink() {
130131
// Release the zip file
131132
try {
132-
if (zipFile != null) {
133-
zipFile.close();
134-
zipFile = null;
135-
}
133+
IOUtils.close(zipFile);
134+
zipFile = null;
136135
} catch (final IOException e) {
137136
// getLogger().warn("vfs.provider.zip/close-zip-file.error :" + file, e);
138137
VfsLog.warn(getLogger(), LOG, "vfs.provider.zip/close-zip-file.error :" + file, e);

commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/DefaultFileContentTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.nio.file.Path;
3232
import java.util.concurrent.atomic.AtomicBoolean;
3333

34+
import org.apache.commons.io.IOUtils;
3435
import org.apache.commons.lang3.ArrayUtils;
3536
import org.apache.commons.lang3.StringUtils;
3637
import org.apache.commons.lang3.function.FailableFunction;
@@ -195,11 +196,7 @@ private <T extends Closeable> void testStreamClosedInADifferentThread(final Fail
195196
final T stream = getStream.apply(file.getContent());
196197
final AtomicBoolean check = new AtomicBoolean();
197198
final Thread thread = new Thread(() -> {
198-
try {
199-
stream.close();
200-
} catch (final IOException exception) {
201-
// ignore
202-
}
199+
IOUtils.closeQuietly(stream);
203200
check.set(true);
204201
});
205202
thread.start();

commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ram/CustomRamProviderTest.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Arrays;
3333
import java.util.List;
3434

35+
import org.apache.commons.io.IOUtils;
3536
import org.apache.commons.vfs2.AllFileSelector;
3637
import org.apache.commons.vfs2.FileContent;
3738
import org.apache.commons.vfs2.FileObject;
@@ -121,15 +122,11 @@ public void setUp() throws Exception {
121122
}
122123

123124
@AfterEach
124-
public void tearDown() {
125-
for (final Closeable closeable : closeables) {
126-
try {
127-
closeable.close();
128-
} catch (final Exception e) {
129-
// ignore
130-
}
125+
public void tearDown() throws IOException {
126+
IOUtils.closeQuietly(closeables);
127+
if (manager != null) {
128+
manager.close();
131129
}
132-
manager.close();
133130
}
134131

135132
@Test

0 commit comments

Comments
 (0)