asfgit closed pull request #322: Migrate to Files.new* creation of I/O streams
during IDE boot
URL: https://github.com/apache/incubator-netbeans/pull/322
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/core.netigso/nbproject/project.properties
b/core.netigso/nbproject/project.properties
index f00a0003a..d5937298e 100644
--- a/core.netigso/nbproject/project.properties
+++ b/core.netigso/nbproject/project.properties
@@ -16,7 +16,7 @@
# under the License.
is.autoload=true
-javac.source=1.6
+javac.source=1.7
javac.compilerargs=-Xlint -Xlint:-serial
javadoc.arch=${basedir}/arch.xml
javadoc.apichanges=${basedir}/apichanges.xml
diff --git a/core.netigso/src/org/netbeans/core/netigso/Netigso.java
b/core.netigso/src/org/netbeans/core/netigso/Netigso.java
index 3d3ae68e6..bad7d6b04 100644
--- a/core.netigso/src/org/netbeans/core/netigso/Netigso.java
+++ b/core.netigso/src/org/netbeans/core/netigso/Netigso.java
@@ -22,10 +22,11 @@
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
import java.security.ProtectionDomain;
import java.util.Arrays;
import java.util.Collection;
@@ -36,7 +37,6 @@
import java.util.Map;
import java.util.Properties;
import java.util.Set;
-import java.util.StringTokenizer;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
@@ -453,9 +453,11 @@ private void fakeOneModule(Module m, Bundle original)
throws IOException {
} else if (symbolicName != null) { // NOI18N
if (original != null) {
LOG.log(Level.FINE, "Updating bundle {0}",
original.getLocation());
- FileInputStream is = new FileInputStream(m.getJarFile());
- original.update(is);
- is.close();
+ try (InputStream is =
Files.newInputStream(m.getJarFile().toPath())) {
+ original.update(is);
+ } catch (InvalidPathException ex) {
+ throw new IOException(ex);
+ }
b = original;
} else {
BundleContext bc = framework.getBundleContext();
diff --git
a/core.network/src/org/netbeans/core/network/proxy/NbProxySelector.java
b/core.network/src/org/netbeans/core/network/proxy/NbProxySelector.java
index 394c39a7a..440ff507c 100644
--- a/core.network/src/org/netbeans/core/network/proxy/NbProxySelector.java
+++ b/core.network/src/org/netbeans/core/network/proxy/NbProxySelector.java
@@ -21,10 +21,11 @@
import java.io.BufferedInputStream;
import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -421,10 +422,9 @@ static boolean useSystemProxies() {
}
fname = netProperties.getCanonicalPath();
- InputStream in = new FileInputStream(fname);
- BufferedInputStream bin = new BufferedInputStream(in);
- props.load(bin);
- bin.close();
+ try (InputStream bin = new
BufferedInputStream(Files.newInputStream(Paths.get(fname)))) {
+ props.load(bin);
+ }
String val = props.getProperty(propertyKey);
val = System.getProperty(propertyKey, val);
diff --git
a/core.network/src/org/netbeans/core/network/proxy/kde/KdeNetworkProxy.java
b/core.network/src/org/netbeans/core/network/proxy/kde/KdeNetworkProxy.java
index 553d1e59f..505fb9a8b 100644
--- a/core.network/src/org/netbeans/core/network/proxy/kde/KdeNetworkProxy.java
+++ b/core.network/src/org/netbeans/core/network/proxy/kde/KdeNetworkProxy.java
@@ -19,12 +19,11 @@
package org.netbeans.core.network.proxy.kde;
import java.io.BufferedReader;
-import java.io.DataInputStream;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
-import java.io.InputStreamReader;
+import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
@@ -133,10 +132,7 @@ public NetworkProxySettings getNetworkProxySettings() {
Map<String, String> map = new HashMap<String, String>();
if (kioslavercFile.exists()) {
- try {
- FileInputStream fis = new FileInputStream(kioslavercFile);
- DataInputStream dis = new DataInputStream(fis);
- BufferedReader br = new BufferedReader(new
InputStreamReader(dis));
+ try (BufferedReader br =
Files.newBufferedReader(kioslavercFile.toPath())) {
String line;
boolean inGroup = false;
while ((line = br.readLine()) != null) {
@@ -153,11 +149,8 @@ public NetworkProxySettings getNetworkProxySettings() {
inGroup = true;
}
}
- dis.close();
- } catch (FileNotFoundException fnfe) {
- LOGGER.log(Level.SEVERE, "Cannot read file: ", fnfe);
- } catch (IOException ioe) {
- LOGGER.log(Level.SEVERE, "Cannot read file: ", ioe);
+ } catch (IOException | InvalidPathException ex) {
+ LOGGER.log(Level.SEVERE, "Cannot read file: ", ex);
}
} else {
LOGGER.log(Level.WARNING, "KDE system proxy resolver: The
kioslaverc file not found ({0})", KIOSLAVERC_PATH);
diff --git a/core.osgi/nbproject/project.properties
b/core.osgi/nbproject/project.properties
index 0624d2c37..0fd506d48 100644
--- a/core.osgi/nbproject/project.properties
+++ b/core.osgi/nbproject/project.properties
@@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.
is.autoload=true
-javac.source=1.6
+javac.source=1.7
javac.compilerargs=-Xlint -Xlint:-serial
cp.extra=\
${nb_all}/libs.osgi/external/osgi.core-5.0.0.jar:\
diff --git a/core.osgi/src/org/netbeans/core/osgi/OSGiInstalledFileLocator.java
b/core.osgi/src/org/netbeans/core/osgi/OSGiInstalledFileLocator.java
index f25b1e133..bfc9a960c 100644
--- a/core.osgi/src/org/netbeans/core/osgi/OSGiInstalledFileLocator.java
+++ b/core.osgi/src/org/netbeans/core/osgi/OSGiInstalledFileLocator.java
@@ -20,12 +20,13 @@
package org.netbeans.core.osgi;
import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
@@ -122,27 +123,20 @@ public OSGiInstalledFileLocator(BundleContext context) {
if (!dir.isDirectory() && !dir.mkdirs()) {
throw new IOException("Could not make " + dir);
}
- InputStream is = resource.openStream();
- try {
- OutputStream os = new FileOutputStream(f2);
- try {
- byte[] buf = new byte[4096];
- int read;
- while ((read = is.read(buf)) != -1) {
- os.write(buf, 0, read);
- }
- } finally {
- os.close();
+ try (InputStream is = resource.openStream();
+ OutputStream os =
Files.newOutputStream(f2.toPath())) {
+ byte[] buf = new byte[4096];
+ int read;
+ while ((read = is.read(buf)) != -1) {
+ os.write(buf, 0, read);
}
- } finally {
- is.close();
}
if (execFiles.contains(name)) {
f2.setExecutable(true);
}
}
return f;
- } catch (IOException x) {
+ } catch (IOException | InvalidPathException x) {
Exceptions.printStackTrace(x);
}
}
diff --git a/core.output2/nbproject/project.properties
b/core.output2/nbproject/project.properties
index 25c2885b1..5db5e5d61 100644
--- a/core.output2/nbproject/project.properties
+++ b/core.output2/nbproject/project.properties
@@ -17,7 +17,7 @@
is.autoload=true
javac.compilerargs=-Xlint -Xlint:-serial
-javac.source=1.6
+javac.source=1.7
javadoc.arch=${basedir}/arch.xml
test.config.stableBTD.includes=**/*Test.class
diff --git a/core.output2/src/org/netbeans/core/output2/AbstractLines.java
b/core.output2/src/org/netbeans/core/output2/AbstractLines.java
index 44b2d4c2f..2189ff5f2 100644
--- a/core.output2/src/org/netbeans/core/output2/AbstractLines.java
+++ b/core.output2/src/org/netbeans/core/output2/AbstractLines.java
@@ -22,13 +22,15 @@
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -869,7 +871,6 @@ public void saveAs(String path) throws IOException {
if (storage == null) {
throw new IOException ("Data has already been disposed"); //NOI18N
}
- FileOutputStream fos = new FileOutputStream(path);
try {
String encoding = System.getProperty ("file.encoding"); //NOI18N
if (encoding == null) {
@@ -878,30 +879,30 @@ public void saveAs(String path) throws IOException {
Charset charset = Charset.forName (encoding); //NOI18N
CharsetEncoder encoder = charset.newEncoder ();
String ls = System.getProperty("line.separator");
- FileChannel ch = fos.getChannel();
- ByteBuffer lsbb = encoder.encode(CharBuffer.wrap(ls));
- for (int i = 0; i < getLineCount(); i++) {
- int lineStart = getCharLineStart(i);
- int lineLength = length(i);
- BufferResource<CharBuffer> br = getCharBuffer(lineStart,
- lineLength);
- try {
- CharBuffer cb = br.getBuffer();
- ByteBuffer bb = encoder.encode(cb);
- ch.write(bb);
- if (i != getLineCount() - 1) {
- lsbb.rewind();
- ch.write(lsbb);
- }
- } finally {
- if (br != null) {
- br.releaseBuffer();
+ Path filePath = Paths.get(path);
+ try (FileChannel ch = FileChannel.open(filePath,
StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
+ ByteBuffer lsbb = encoder.encode(CharBuffer.wrap(ls));
+ for (int i = 0; i < getLineCount(); i++) {
+ int lineStart = getCharLineStart(i);
+ int lineLength = length(i);
+ BufferResource<CharBuffer> br = getCharBuffer(lineStart,
+ lineLength);
+ try {
+ CharBuffer cb = br.getBuffer();
+ ByteBuffer bb = encoder.encode(cb);
+ ch.write(bb);
+ if (i != getLineCount() - 1) {
+ lsbb.rewind();
+ ch.write(lsbb);
+ }
+ } finally {
+ if (br != null) {
+ br.releaseBuffer();
+ }
}
}
}
- ch.close();
} finally {
- fos.close();
FileUtil.refreshFor(new java.io.File(path));
}
}
diff --git a/core.output2/src/org/netbeans/core/output2/Controller.java
b/core.output2/src/org/netbeans/core/output2/Controller.java
index 56982b995..806fae907 100644
--- a/core.output2/src/org/netbeans/core/output2/Controller.java
+++ b/core.output2/src/org/netbeans/core/output2/Controller.java
@@ -23,10 +23,10 @@
import java.awt.Font;
import java.io.CharConversionException;
import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.ref.WeakReference;
+import java.nio.file.Files;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
@@ -525,7 +525,7 @@ private static OutputStream getLogStream() {
f.delete();
}
f.createNewFile();
- logStream = new FileOutputStream(f);
+ logStream = Files.newOutputStream(f.toPath());
} catch (Exception e) {
e.printStackTrace();
logStream = System.err;
diff --git a/core.startup.base/nbproject/project.properties
b/core.startup.base/nbproject/project.properties
index 2bda232f3..6cea6afaf 100644
--- a/core.startup.base/nbproject/project.properties
+++ b/core.startup.base/nbproject/project.properties
@@ -14,7 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-javac.source=1.6
+javac.source=1.7
javac.compilerargs=-Xlint -Xlint:-serial
spec.version.base=1.63.0
module.jar.dir=core
diff --git
a/core.startup.base/src/org/netbeans/core/startup/layers/ArchiveURLMapper.java
b/core.startup.base/src/org/netbeans/core/startup/layers/ArchiveURLMapper.java
index 0fbceb5fc..bc308cea7 100644
---
a/core.startup.base/src/org/netbeans/core/startup/layers/ArchiveURLMapper.java
+++
b/core.startup.base/src/org/netbeans/core/startup/layers/ArchiveURLMapper.java
@@ -20,7 +20,6 @@
package org.netbeans.core.startup.layers;
import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -30,6 +29,8 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
+import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
@@ -241,16 +242,10 @@ private static File copyJAR(FileObject fo, URI
archiveFileURI, boolean replace)
copy = copy.getCanonicalFile();
copy.deleteOnExit();
}
- InputStream is = fo.getInputStream();
- try {
- OutputStream os = new FileOutputStream(copy);
- try {
- FileUtil.copy(is, os);
- } finally {
- os.close();
- }
- } finally {
- is.close();
+ try (InputStream is = fo.getInputStream(); OutputStream os =
Files.newOutputStream(copy.toPath())) {
+ FileUtil.copy(is, os);
+ } catch (InvalidPathException ex) {
+ throw new IOException(ex);
}
copiedJARs.put(archiveFileURI, copy);
}
diff --git
a/core.startup.base/src/org/netbeans/core/startup/layers/NbinstURLStreamHandler.java
b/core.startup.base/src/org/netbeans/core/startup/layers/NbinstURLStreamHandler.java
index a697cbf1c..db3899b8c 100644
---
a/core.startup.base/src/org/netbeans/core/startup/layers/NbinstURLStreamHandler.java
+++
b/core.startup.base/src/org/netbeans/core/startup/layers/NbinstURLStreamHandler.java
@@ -20,7 +20,6 @@
package org.netbeans.core.startup.layers;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@@ -29,6 +28,8 @@
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.net.UnknownServiceException;
+import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.Exceptions;
@@ -140,7 +141,11 @@ public int getContentLength() {
public InputStream getInputStream() throws IOException {
this.connect();
if (iStream == null) {
- iStream = new FileInputStream(f);
+ try {
+ iStream = Files.newInputStream(f.toPath());
+ } catch (InvalidPathException ex) {
+ throw new IOException(ex);
+ }
}
return iStream;
}
diff --git
a/core.startup.base/test/unit/src/org/netbeans/core/startup/layers/SystemFileSystemTest.java
b/core.startup.base/test/unit/src/org/netbeans/core/startup/layers/SystemFileSystemTest.java
index e5b6f1f7d..0274f3bb4 100644
---
a/core.startup.base/test/unit/src/org/netbeans/core/startup/layers/SystemFileSystemTest.java
+++
b/core.startup.base/test/unit/src/org/netbeans/core/startup/layers/SystemFileSystemTest.java
@@ -34,7 +34,6 @@
import org.netbeans.core.startup.Main;
import org.netbeans.core.startup.MainLookup;
import org.netbeans.SetupHid;
-import org.netbeans.core.startup.layers.SystemFileSystem;
import org.openide.filesystems.FileAttributeEvent;
import org.openide.filesystems.FileChangeListener;
import org.openide.filesystems.FileEvent;
@@ -236,17 +235,18 @@ public void testCanHideFilesFromModules() throws
Exception {
}
private static void write(FileObject fo, String txt) throws IOException {
- OutputStream os = fo.getOutputStream();
- os.write(txt.getBytes());
- os.close();
+ try (OutputStream os = fo.getOutputStream()) {
+ os.write(txt.getBytes());
+ }
}
private static String read(FileObject fo) throws IOException {
byte[] arr = new byte[(int)fo.getSize()];
- InputStream is = fo.getInputStream();
- int len = is.read(arr);
- assertEquals("Not enough read", arr.length, len);
- return new String(arr);
+ try (InputStream is = fo.getInputStream()) {
+ int len = is.read(arr);
+ assertEquals("Not enough read", arr.length, len);
+ return new String(arr);
+ }
}
public FileSystem convert(FileSystem obj) {
diff --git
a/core.startup/src/org/netbeans/core/startup/logging/MessagesHandler.java
b/core.startup/src/org/netbeans/core/startup/logging/MessagesHandler.java
index 6cc31e76d..5f59e9a41 100644
--- a/core.startup/src/org/netbeans/core/startup/logging/MessagesHandler.java
+++ b/core.startup/src/org/netbeans/core/startup/logging/MessagesHandler.java
@@ -19,8 +19,9 @@
package org.netbeans.core.startup.logging;
import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
import java.util.Arrays;
import java.util.Objects;
import java.util.logging.Level;
@@ -84,8 +85,8 @@ private boolean checkRotate(boolean always) {
private void initStream() {
try {
- setOutputStream(new FileOutputStream(files[0], false));
- } catch (FileNotFoundException ex) {
+ setOutputStream(Files.newOutputStream(files[0].toPath()));
+ } catch (IOException | InvalidPathException ex) {
setOutputStream(System.err);
}
}
diff --git a/core.startup/src/org/netbeans/core/startup/logging/NbLogging.java
b/core.startup/src/org/netbeans/core/startup/logging/NbLogging.java
index 198a90013..3a50fe328 100644
--- a/core.startup/src/org/netbeans/core/startup/logging/NbLogging.java
+++ b/core.startup/src/org/netbeans/core/startup/logging/NbLogging.java
@@ -18,10 +18,14 @@
*/
package org.netbeans.core.startup.logging;
+import static java.nio.file.StandardOpenOption.APPEND;
+import static java.nio.file.StandardOpenOption.CREATE;
+
import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.PrintStream;
+import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
import java.util.logging.Handler;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
@@ -43,8 +47,8 @@
try {
File debugLog = new File(System.getProperty("java.io.tmpdir"),
"TopLogging.log"); // NOI18N
System.err.println("Logging sent to: " + debugLog); // NOI18N
- _D = new PrintStream(new FileOutputStream(debugLog), true);
- } catch (FileNotFoundException x) {
+ _D = new PrintStream(Files.newOutputStream(debugLog.toPath(),
CREATE, APPEND));
+ } catch (IOException | InvalidPathException x) {
x.printStackTrace();
}
}
diff --git a/o.n.bootstrap/src/org/netbeans/JarClassLoader.java
b/o.n.bootstrap/src/org/netbeans/JarClassLoader.java
index 5268c7c45..b1cdcc833 100644
--- a/o.n.bootstrap/src/org/netbeans/JarClassLoader.java
+++ b/o.n.bootstrap/src/org/netbeans/JarClassLoader.java
@@ -21,9 +21,7 @@
import java.io.ByteArrayInputStream;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -38,6 +36,8 @@
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
+import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
import java.security.CodeSource;
import java.security.PermissionCollection;
import java.security.Policy;
@@ -701,22 +701,17 @@ protected void destroy() throws IOException {
File temp = File.createTempFile(prefix, suffix);
temp.deleteOnExit();
- InputStream is = new FileInputStream(orig);
- try {
- OutputStream os = new FileOutputStream(temp);
- try {
- byte[] buf = new byte[4096];
- int j;
- while ((j = is.read(buf)) != -1) {
- os.write(buf, 0, j);
- }
- } finally {
- os.close();
+ try (InputStream is = Files.newInputStream(orig.toPath());
+ OutputStream os = Files.newOutputStream(temp.toPath())) {
+ byte[] buf = new byte[4096];
+ int j;
+ while ((j = is.read(buf)) != -1) {
+ os.write(buf, 0, j);
}
- } finally {
- is.close();
+ } catch (InvalidPathException ex) {
+ throw new IOException(ex);
}
-
+
doCloseJar();
file = temp;
dead = true;
@@ -864,9 +859,9 @@ public Manifest getManifest() {
File maniF = new File(new File(dir, "META-INF"), "MANIFEST.MF");
mf = new Manifest();
if (maniF.canRead()) {
- try (InputStream istm = new FileInputStream(maniF)) {
+ try (InputStream istm = Files.newInputStream(maniF.toPath())) {
mf.read(istm);
- } catch (IOException ex) {
+ } catch (IOException | InvalidPathException ex) {
Exceptions.printStackTrace(ex);
}
}
@@ -888,15 +883,14 @@ protected URL doGetResource(String name) throws
MalformedURLException {
int len = (int)clsFile.length();
byte[] data = new byte[len];
- InputStream is = new FileInputStream(clsFile);
- try {
+ try (InputStream is = Files.newInputStream(clsFile.toPath())) {
int count = 0;
while (count < len) {
count += is.read(data, count, len - count);
}
return data;
- } finally {
- is.close();
+ } catch (InvalidPathException ex) {
+ throw new IOException(ex);
}
}
diff --git a/o.n.bootstrap/src/org/netbeans/Stamps.java
b/o.n.bootstrap/src/org/netbeans/Stamps.java
index 7777f1d50..e41c90897 100644
--- a/o.n.bootstrap/src/org/netbeans/Stamps.java
+++ b/o.n.bootstrap/src/org/netbeans/Stamps.java
@@ -26,9 +26,6 @@
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -36,6 +33,10 @@
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
+import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
+import java.nio.file.NoSuchFileException;
+import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -185,8 +186,7 @@ private ByteBuffer asByteBuffer(String cache, boolean
direct, boolean mmap) {
return null;
}
- try {
- FileChannel fc = new FileInputStream(cacheFile).getChannel();
+ try (FileChannel fc = FileChannel.open(cacheFile.toPath(),
StandardOpenOption.READ)){
ByteBuffer master;
if (mmap) {
master = fc.map(FileChannel.MapMode.READ_ONLY, 0, len[0]);
@@ -201,10 +201,8 @@ private ByteBuffer asByteBuffer(String cache, boolean
direct, boolean mmap) {
master.flip();
}
- fc.close();
-
return master;
- } catch (IOException ex) {
+ } catch (IOException | InvalidPathException ex) {
LOG.log(Level.WARNING, "Cannot read cache " + cacheFile, ex); //
NOI18N
return null;
}
@@ -429,31 +427,25 @@ private static boolean compareAndUpdateFile(File file,
String content, AtomicLon
try {
byte[] expected = content.getBytes("UTF-8"); // NOI18N
byte[] read = new byte[expected.length];
- FileInputStream is = null;
boolean areCachesOK;
boolean writeFile;
long lastMod;
- try {
- is = new FileInputStream(file);
+ try (InputStream is = Files.newInputStream(file.toPath())) {
int len = is.read(read);
areCachesOK = len == read.length && is.available() == 0 &&
Arrays.equals(expected, read);
writeFile = !areCachesOK;
lastMod = file.lastModified();
- } catch (FileNotFoundException notFoundEx) {
+ } catch (NoSuchFileException notFoundEx) {
// ok, running for the first time, no need to invalidate the
cache
areCachesOK = true;
writeFile = true;
lastMod = result.get();
- } finally {
- if (is != null) {
- is.close();
- }
}
if (writeFile) {
file.getParentFile().mkdirs();
- FileOutputStream os = new FileOutputStream(file);
- os.write(expected);
- os.close();
+ try (OutputStream os = Files.newOutputStream(file.toPath())) {
+ os.write(expected);
+ }
if (areCachesOK) {
file.setLastModified(lastMod);
}
@@ -463,7 +455,7 @@ private static boolean compareAndUpdateFile(File file,
String content, AtomicLon
}
}
return areCachesOK;
- } catch (IOException ex) {
+ } catch (IOException | InvalidPathException ex) {
ex.printStackTrace();
return false;
}
@@ -552,7 +544,6 @@ static void checkPopulateCache() {
return;
}
ZipInputStream zip = null;
- FileOutputStream os = null;
try {
byte[] arr = new byte[4096];
LOG.log(Level.FINE, "Found populate.zip about to extract it into
{0}", cache);
@@ -567,18 +558,18 @@ static void checkPopulateCache() {
}
File f = new File(cache, en.getName().replace('/',
File.separatorChar));
f.getParentFile().mkdirs();
- os = new FileOutputStream(f);
- for (;;) {
- int len = zip.read(arr);
- if (len == -1) {
- break;
+ try (OutputStream os = Files.newOutputStream(f.toPath())) {
+ for (;;) {
+ int len = zip.read(arr);
+ if (len == -1) {
+ break;
+ }
+ os.write(arr, 0, len);
}
- os.write(arr, 0, len);
}
- os.close();
}
zip.close();
- } catch (IOException ex) {
+ } catch (IOException | InvalidPathException ex) {
LOG.log(Level.INFO, "Failed to populate {0}", cache);
}
}
@@ -591,22 +582,12 @@ private static boolean clustersChanged() {
final String clustersCache = "all-clusters.dat"; // NOI18N
File f = fileImpl(clustersCache, null, -1); // no timestamp check
if (f != null) {
- DataInputStream dis = null;
- try {
- dis = new DataInputStream(new FileInputStream(f));
+ try (DataInputStream dis = new
DataInputStream(Files.newInputStream(f.toPath()))) {
if (Clusters.compareDirs(dis)) {
return false;
}
- } catch (IOException ex) {
+ } catch (IOException | InvalidPathException ex) {
return clustersChanged = true;
- } finally {
- if (dis != null) {
- try {
- dis.close();
- } catch (IOException ex) {
- LOG.log(Level.INFO, null, ex);
- }
- }
}
} else {
// missing cluster file signals caches are OK, for
@@ -700,7 +681,8 @@ public boolean store(AtomicInteger delay) {
cacheFile.getParentFile().mkdirs();
LOG.log(Level.FINE, "Storing cache {0}", cacheFile);
- os = new FileOutputStream(cacheFile, append); //append new
entries only
+ //append new entries only
+ os = Files.newOutputStream(cacheFile.toPath(),
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
DataOutputStream dos = new DataOutputStream(new
BufferedOutputStream(this, 1024 * 1024));
this.delay = delay;
@@ -708,7 +690,7 @@ public boolean store(AtomicInteger delay) {
updater.flushCaches(dos);
dos.close();
LOG.log(Level.FINE, "Done Storing cache {0}", cacheFile);
- } catch (IOException ex) {
+ } catch (IOException | InvalidPathException ex) {
LOG.log(Level.WARNING, "Error saving cache {0}", cacheFile);
LOG.log(Level.INFO, ex.getMessage(), ex); // NOI18N
delete = true;
diff --git a/o.n.bootstrap/src/org/netbeans/Util.java
b/o.n.bootstrap/src/org/netbeans/Util.java
index 1647aef51..e2b03a706 100644
--- a/o.n.bootstrap/src/org/netbeans/Util.java
+++ b/o.n.bootstrap/src/org/netbeans/Util.java
@@ -20,6 +20,8 @@
package org.netbeans;
import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
import java.util.*;
import java.util.ArrayList;
import java.util.logging.Level;
@@ -59,20 +61,15 @@ static File makeTempJar(File moduleFile) throws IOException
{
String suffix = "-test.jar"; // NOI18N
File physicalModuleFile = File.createTempFile(prefix, suffix);
physicalModuleFile.deleteOnExit();
- InputStream is = new FileInputStream(moduleFile);
- try {
- OutputStream os = new FileOutputStream(physicalModuleFile);
- try {
- byte[] buf = new byte[4096];
- int i;
- while ((i = is.read(buf)) != -1) {
- os.write(buf, 0, i);
- }
- } finally {
- os.close();
- }
- } finally {
- is.close();
+ try (InputStream is = Files.newInputStream(moduleFile.toPath());
+ OutputStream os =
Files.newOutputStream(physicalModuleFile.toPath())) {
+ byte[] buf = new byte[4096];
+ int i;
+ while ((i = is.read(buf)) != -1) {
+ os.write(buf, 0, i);
+ }
+ } catch (InvalidPathException ex) {
+ throw new IOException(ex);
}
err.fine("Made " + physicalModuleFile);
return physicalModuleFile;
diff --git a/openide.filesystems/src/org/openide/filesystems/JarFileSystem.java
b/openide.filesystems/src/org/openide/filesystems/JarFileSystem.java
index 45fceada7..df168191f 100644
--- a/openide.filesystems/src/org/openide/filesystems/JarFileSystem.java
+++ b/openide.filesystems/src/org/openide/filesystems/JarFileSystem.java
@@ -23,9 +23,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
@@ -35,6 +33,8 @@
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
+import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
@@ -503,24 +503,16 @@ private InputStream getTemporaryInputStream(JarFile jf,
JarEntry je, boolean for
if (createContent || forceRecreate) {
// JDK 1.3 contains bug #4336753
//is = j.getInputStream (je);
- InputStream is = getInputStream4336753(jf, je);
-
- try {
- OutputStream os = new FileOutputStream(f);
-
- try {
- FileUtil.copy(is, os);
- } finally {
- os.close();
- }
- } finally {
- is.close();
+ try (InputStream is = getInputStream4336753(jf, je); OutputStream
os = Files.newOutputStream(f.toPath())) {
+ FileUtil.copy(is, os);
+ } catch (InvalidPathException ex) {
+ throw new IOException(ex);
}
}
f.deleteOnExit();
- return new FileInputStream(f);
+ return Files.newInputStream(f.toPath());
}
private static String temporaryName(String filePath, String entryPath) {
diff --git
a/openide.filesystems/src/org/openide/filesystems/LocalFileSystem.java
b/openide.filesystems/src/org/openide/filesystems/LocalFileSystem.java
index b41e618bc..7266cbfb7 100644
--- a/openide.filesystems/src/org/openide/filesystems/LocalFileSystem.java
+++ b/openide.filesystems/src/org/openide/filesystems/LocalFileSystem.java
@@ -33,6 +33,7 @@
import java.io.OutputStream;
import java.io.SyncFailedException;
import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Random;
@@ -401,13 +402,16 @@ protected InputStream inputStream(String name) throws
java.io.FileNotFoundExcept
File file = null;
try {
- fis = new BufferedInputStream(new FileInputStream(file =
getFile(name)));
- } catch (FileNotFoundException exc) {
+ file = getFile(name);
+ fis = new BufferedInputStream(Files.newInputStream(file.toPath()));
+ } catch (IOException | InvalidPathException exc) {
+ FileNotFoundException fnfException = new
FileNotFoundException(exc.getMessage());
if ((file == null) || !file.exists()) {
- ExternalUtil.annotate(exc,
NbBundle.getMessage(LocalFileSystem.class, "EXC_FileOutsideModified",
getFile(name)));
+ ExternalUtil.annotate(fnfException,
+ NbBundle.getMessage(LocalFileSystem.class,
"EXC_FileOutsideModified", getFile(name)));
}
- throw exc;
+ throw fnfException;
}
return fis;
@@ -418,14 +422,17 @@ protected OutputStream outputStream(final String name)
throws java.io.IOExceptio
if (!f.exists()) {
f.getParentFile().mkdirs();
}
- OutputStream retVal = new BufferedOutputStream(new
FileOutputStream(f));
+ try {
+ OutputStream retVal = new
BufferedOutputStream(Files.newOutputStream(f.toPath()));
- // workaround for #42624
- if (BaseUtilities.isMac()) {
- retVal = getOutputStreamForMac42624(retVal, name);
+ // workaround for #42624
+ if (BaseUtilities.isMac()) {
+ retVal = getOutputStreamForMac42624(retVal, name);
+ }
+ return retVal;
+ } catch (InvalidPathException ex) {
+ throw new IOException(ex);
}
-
- return retVal;
}
private OutputStream getOutputStreamForMac42624(final OutputStream
originalStream, final String name) {
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists