Skip to content

Commit a6355cf

Browse files
committed
Use try-with-resources in filesystem FileStore
1 parent e5dadef commit a6355cf

File tree

2 files changed

+21
-54
lines changed
  • resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core

2 files changed

+21
-54
lines changed

resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/provider/FileStore.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,22 @@
1414
*******************************************************************************/
1515
package org.eclipse.core.filesystem.provider;
1616

17-
import java.io.*;
17+
import java.io.IOException;
18+
import java.io.InputStream;
19+
import java.io.OutputStream;
1820
import java.net.URI;
19-
import org.eclipse.core.filesystem.*;
20-
import org.eclipse.core.internal.filesystem.*;
21-
import org.eclipse.core.runtime.*;
21+
import org.eclipse.core.filesystem.EFS;
22+
import org.eclipse.core.filesystem.IFileInfo;
23+
import org.eclipse.core.filesystem.IFileStore;
24+
import org.eclipse.core.filesystem.IFileSystem;
25+
import org.eclipse.core.internal.filesystem.FileCache;
26+
import org.eclipse.core.internal.filesystem.Messages;
27+
import org.eclipse.core.internal.filesystem.Policy;
28+
import org.eclipse.core.runtime.CoreException;
29+
import org.eclipse.core.runtime.IPath;
30+
import org.eclipse.core.runtime.IProgressMonitor;
31+
import org.eclipse.core.runtime.PlatformObject;
32+
import org.eclipse.core.runtime.SubMonitor;
2233
import org.eclipse.osgi.util.NLS;
2334

2435
/**
@@ -57,7 +68,7 @@ public abstract class FileStore extends PlatformObject implements IFileStore {
5768
private static final void transferStreams(InputStream source, OutputStream destination, long length, String path, IProgressMonitor monitor) throws CoreException {
5869
byte[] buffer = new byte[8192];
5970
SubMonitor subMonitor = SubMonitor.convert(monitor, length >= 0 ? 1 + (int) (length / buffer.length) : 1000);
60-
try {
71+
try (source; destination) {
6172
while (true) {
6273
int bytesRead = -1;
6374
try {
@@ -78,9 +89,7 @@ private static final void transferStreams(InputStream source, OutputStream desti
7889
}
7990
subMonitor.worked(1);
8091
}
81-
} finally {
82-
Policy.safeClose(source);
83-
Policy.safeClose(destination);
92+
} catch (IOException e) { // ignore
8493
}
8594
}
8695

@@ -193,16 +202,13 @@ protected void copyFile(IFileInfo sourceInfo, IFileStore destination, int option
193202
long length = sourceInfo.getLength();
194203
String sourcePath = toString();
195204
SubMonitor subMonitor = SubMonitor.convert(monitor, NLS.bind(Messages.copying, sourcePath), 100);
196-
InputStream in = null;
197-
OutputStream out = null;
198-
try {
199-
in = openInputStream(EFS.NONE, subMonitor.newChild(1));
200-
out = destination.openOutputStream(EFS.NONE, subMonitor.newChild(1));
205+
try (InputStream in = openInputStream(EFS.NONE, subMonitor.newChild(1)); //
206+
OutputStream out = destination.openOutputStream(EFS.NONE, subMonitor.newChild(1));) {
201207
transferStreams(in, out, length, sourcePath, subMonitor.newChild(98));
202208
transferAttributes(sourceInfo, destination);
209+
} catch (IOException e) {
210+
// ignore
203211
} catch (CoreException e) {
204-
Policy.safeClose(in);
205-
Policy.safeClose(out);
206212
//if we failed to write, try to cleanup the half written file
207213
if (!destination.fetchInfo(0, null).exists())
208214
destination.delete(EFS.NONE, null);

resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/Policy.java

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
*******************************************************************************/
1414
package org.eclipse.core.internal.filesystem;
1515

16-
import java.io.*;
17-
import java.util.Date;
1816
import org.eclipse.core.internal.runtime.RuntimeLog;
1917
import org.eclipse.core.runtime.CoreException;
2018
import org.eclipse.core.runtime.Status;
@@ -25,20 +23,6 @@
2523
public class Policy {
2624
public static final String PI_FILE_SYSTEM = "org.eclipse.core.filesystem"; //$NON-NLS-1$
2725

28-
/**
29-
* Print a debug message to the console.
30-
* Pre-pend the message with the current date and the name of the current thread.
31-
*/
32-
public static void debug(String message) {
33-
StringBuilder buffer = new StringBuilder();
34-
buffer.append(new Date(System.currentTimeMillis()));
35-
buffer.append(" - ["); //$NON-NLS-1$
36-
buffer.append(Thread.currentThread().getName());
37-
buffer.append("] "); //$NON-NLS-1$
38-
buffer.append(message);
39-
System.out.println(buffer.toString());
40-
}
41-
4226
public static void error(int code, String message) throws CoreException {
4327
error(code, message, null);
4428
}
@@ -54,27 +38,4 @@ public static void log(int severity, String message, Throwable t) {
5438
RuntimeLog.log(new Status(severity, PI_FILE_SYSTEM, 1, message, t));
5539
}
5640

57-
/**
58-
* Closes a stream and ignores any resulting exception.
59-
*/
60-
public static void safeClose(InputStream in) {
61-
try {
62-
if (in != null)
63-
in.close();
64-
} catch (IOException e) {
65-
//ignore
66-
}
67-
}
68-
69-
/**
70-
* Closes a stream and ignores any resulting exception.
71-
*/
72-
public static void safeClose(OutputStream out) {
73-
try {
74-
if (out != null)
75-
out.close();
76-
} catch (IOException e) {
77-
//ignore
78-
}
79-
}
8041
}

0 commit comments

Comments
 (0)