Skip to content

Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance

Properly close file output streams even on exceptions scenarios.

luccioman83992751420c
diff --git a/htroot/ConfigLanguage_p.java b/htroot/ConfigLanguage_p.java
index f93efd8d0..8851e9121 100644
--- a/htroot/ConfigLanguage_p.java
+++ b/htroot/ConfigLanguage_p.java
@@ -105,15 +105,18 @@ public class ConfigLanguage_p {
try {
final DigestURL u = new DigestURL(url);
it = FileUtils.strings(u.get(ClientIdentification.yacyInternetCrawlerAgent, null, null));
+ TranslatorXliff tx = new TranslatorXliff();
+ File langFile = tx.getScratchFile(new File(langPath, u.getFileName()));
try {
- TranslatorXliff tx = new TranslatorXliff();
- File langFile = tx.getScratchFile(new File(langPath, u.getFileName()));
- final OutputStreamWriter bw = new OutputStreamWriter(new FileOutputStream(langFile), StandardCharsets.UTF_8.name());
-
- while (it.hasNext()) {
- bw.write(it.next() + "\n");
+ try (
+ /* Automatically closed by this try-with-resources statement */
+ final OutputStreamWriter bw = new OutputStreamWriter(new FileOutputStream(langFile), StandardCharsets.UTF_8.name());
+ ) {
+ while (it.hasNext()) {
+ bw.write(it.next() + "\n");
+ }
}
- bw.close();
+
// convert downloaded xliff to internal lng file
final String ext = Files.getFileExtension(langFile.getName());
diff --git a/source/net/yacy/cora/federate/solr/instance/EmbeddedInstance.java b/source/net/yacy/cora/federate/solr/instance/EmbeddedInstance.java
index 79de0c513..c8b211ad9 100644
--- a/source/net/yacy/cora/federate/solr/instance/EmbeddedInstance.java
+++ b/source/net/yacy/cora/federate/solr/instance/EmbeddedInstance.java
@@ -120,15 +120,16 @@ public class EmbeddedInstance implements SolrInstance {
File core_properties = new File(corePath, "core.properties");
if (!core_properties.exists()) {
// create the file
- try {
+ try (
+ /* Automatically closed by this try-with-resources statement */
FileOutputStream fos = new FileOutputStream(core_properties);
+ ) {
fos.write(ASCII.getBytes("name=" + coreName + "\n"));
fos.write(ASCII.getBytes("shard=${shard:}\n"));
fos.write(ASCII.getBytes("collection=${collection:" + coreName + "}\n"));
fos.write(ASCII.getBytes("config=${solrconfig:solrconfig.xml}\n"));
fos.write(ASCII.getBytes("schema=${schema:schema.xml}\n"));
fos.write(ASCII.getBytes("coreNodeName=${coreNodeName:}\n"));
- fos.close();
} catch (IOException e) {
ConcurrentLog.logException(e);
}
diff --git a/source/net/yacy/cora/lod/vocabulary/Tagging.java b/source/net/yacy/cora/lod/vocabulary/Tagging.java
index e385de395..4d2ca307e 100644
--- a/source/net/yacy/cora/lod/vocabulary/Tagging.java
+++ b/source/net/yacy/cora/lod/vocabulary/Tagging.java
@@ -165,15 +165,18 @@ public class Tagging {
synonyms.add(synonym);
}
} else {
- //
- BufferedWriter w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(propFile), StandardCharsets.UTF_8.name()));
- if (objectspace != null && objectspace.length() > 0) w.write("#objectspace:" + objectspace + "\n");
- for (Map.Entry<String, SOTuple> e: table.entrySet()) {
- String s = e.getValue() == null ? "" : e.getValue().getSynonymsCSV();
- String o = e.getValue() == null ? "" : e.getValue().getObjectlink();
- w.write(e.getKey() + (s == null || s.isEmpty() ? "" : ":" + e.getValue().getSynonymsCSV()) + (o == null || o.isEmpty() || o.equals(objectspace + e.getKey()) ? "" : "#" + o) + "\n");
- }
- w.close();
+ try (
+ /* Resources automatically closed by this try-with-resources statement */
+ final FileOutputStream outStream = new FileOutputStream(propFile);
+ final BufferedWriter w = new BufferedWriter(new OutputStreamWriter(outStream, StandardCharsets.UTF_8.name()));
+ ) {
+ if (objectspace != null && objectspace.length() > 0) w.write("#objectspace:" + objectspace + "\n");
+ for (Map.Entry<String, SOTuple> e: table.entrySet()) {
+ String s = e.getValue() == null ? "" : e.getValue().getSynonymsCSV();
+ String o = e.getValue() == null ? "" : e.getValue().getObjectlink();
+ w.write(e.getKey() + (s == null || s.isEmpty() ? "" : ":" + e.getValue().getSynonymsCSV()) + (o == null || o.isEmpty() || o.equals(objectspace + e.getKey()) ? "" : "#" + o) + "\n");
+ }
+ }
init();
}
}
diff --git a/source/net/yacy/cora/protocol/ftp/FTPClient.java b/source/net/yacy/cora/protocol/ftp/FTPClient.java
index 18e06481c..063027575 100644
--- a/source/net/yacy/cora/protocol/ftp/FTPClient.java
+++ b/source/net/yacy/cora/protocol/ftp/FTPClient.java
@@ -2860,13 +2860,10 @@ public class FTPClient {
if (args[0].equals("-dir")) {
dir(args[1], args[2], ANONYMOUS, "anomic@");
} else if (args[0].equals("-htmldir")) {
- try {
+ final File file = new File("dirindex.html");
+ try (FileOutputStream fos = new FileOutputStream(file);) {
final StringBuilder page = dirhtml(args[1], 21, args[2], ANONYMOUS, "anomic@");
- final File file = new File("dirindex.html");
- FileOutputStream fos;
- fos = new FileOutputStream(file);
fos.write(UTF8.getBytes(page.toString()));
- fos.close();
} catch (final FileNotFoundException e) {
log.warn(e);
} catch (final IOException e) {
diff --git a/source/net/yacy/cora/storage/ZIPWriter.java b/source/net/yacy/cora/storage/ZIPWriter.java
index 9e055f647..93cc1b69f 100644
--- a/source/net/yacy/cora/storage/ZIPWriter.java
+++ b/source/net/yacy/cora/storage/ZIPWriter.java
@@ -95,12 +95,12 @@ public class ZIPWriter extends AbstractMap<String, ZipEntry> implements Map<Stri
URI base = inputDir.toURI();
Deque<File> queue = new LinkedList<File>();
queue.push(inputDir);
- OutputStream out = new FileOutputStream(zipOut);
- ZipOutputStream zout = null;
+
byte[] buffer = new byte[1024];
int readCount;
- try {
- zout = new ZipOutputStream(out);
+ try (/* Resources automatically closed by this try-with-resources statement */
+ final OutputStream out = new FileOutputStream(zipOut);
+ final ZipOutputStream zout = new ZipOutputStream(out)) {
while (!queue.isEmpty()) {
inputDir = queue.pop();
for (File lf : inputDir.listFiles()) {
@@ -117,9 +117,6 @@ public class ZIPWriter extends AbstractMap<String, ZipEntry> implements Map<Stri
}
}
}
- } finally {
- zout.close();
- out.close();
}
}
diff --git a/source/net/yacy/crawler/data/Transactions.java b/source/net/yacy/crawler/data/Transactions.java
index c4f84eb1e..f5b777a8c 100644
--- a/source/net/yacy/crawler/data/Transactions.java
+++ b/source/net/yacy/crawler/data/Transactions.java
@@ -167,24 +167,27 @@ public class Transactions {
// STORE METADATA FOR THE IMAGE
File metadataPath = Transactions.definePath(url, depth, date, "xml", Transactions.State.INVENTORY);
metadataPath.getParentFile().mkdirs();
- try {
- if (doc != null) {
- FileOutputStream fos = new FileOutputStream(metadataPath);
- OutputStreamWriter osw = new OutputStreamWriter(fos);
+ if (doc != null) {
+ try (
+ /* Resources automatically closed by this try-with-resources statement */
+ final FileOutputStream fos = new FileOutputStream(metadataPath);
+ final OutputStreamWriter osw = new OutputStreamWriter(fos);
+ ) {
osw.write(XML_PREFIX);
osw.write(WHITESPACE); osw.write("\n-->\n"); // placeholder for transaction information properties (a hack to attach metadata to metadata)
osw.write("<result name=\"response\" numFound=\"1\" start=\"0\">\n");
EnhancedXMLResponseWriter.writeDoc(osw, doc);
osw.write("</result>\n");
osw.write("</response>\n");
- osw.close();
- fos.close();
- Transactions.announceStorage(url, depth, date, State.INVENTORY);
- }
- } catch (IOException e) {
- ConcurrentLog.logException(e);
- success = false;
+ } catch (IOException e) {
+ ConcurrentLog.logException(e);
+ success = false;
+ }
+ if(success) {
+ Transactions.announceStorage(url, depth, date, State.INVENTORY);
+ }
}
+
}
return success;
diff --git a/source/net/yacy/data/Translator.java b/source/net/yacy/data/Translator.java
index 8c9007c5b..57e2a5b62 100644
--- a/source/net/yacy/data/Translator.java
+++ b/source/net/yacy/data/Translator.java
@@ -191,19 +191,14 @@ public class Translator {
}
String processedContent = translate(content, translationList);
- BufferedWriter bw = null;
- try{
- bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile), StandardCharsets.UTF_8));
+ try (
+ /* Resources automatically closed by this try-with-resources statement */
+ final FileOutputStream outStream = new FileOutputStream(destFile);
+ BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outStream, StandardCharsets.UTF_8));
+ ) {
bw.write(processedContent);
- bw.close();
- }catch(final IOException e){
+ } catch(final Exception e){
return false;
- } finally {
- if (bw != null) {
- try {
- bw.close();
- } catch (final Exception e) {}
- }
}
return true;
diff --git a/source/net/yacy/document/content/dao/PhpBB3Dao.java b/source/net/yacy/document/content/dao/PhpBB3Dao.java
index bdcdd77bf..a6bb5a9de 100644
--- a/source/net/yacy/document/content/dao/PhpBB3Dao.java
+++ b/source/net/yacy/document/content/dao/PhpBB3Dao.java
@@ -270,6 +270,8 @@ public class PhpBB3Dao implements Dao {
String versioninfo,
int maxEntriesInFile
) {
+ FileOutputStream outStream = null;
+ OutputStreamWriter osw = null;
try {
// generate output file name and attributes
String targethost = new DigestURL(this.urlstub).getHost();
@@ -277,7 +279,6 @@ public class PhpBB3Dao implements Dao {
File outputfiletmp = null, outputfile = null;
// write the result from the query concurrently in a file
- OutputStreamWriter osw = null;
DCEntry e;
int c = 0;
while ((e = queue.take()) != DCEntry.poison) {
@@ -286,7 +287,8 @@ public class PhpBB3Dao implements Dao {
outputfile = new File(targetdir, targethost + "." + versioninfo + "." + fc + ".xml");
if (outputfiletmp.exists()) outputfiletmp.delete();
if (outputfile.exists()) outputfile.delete();
- osw = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(outputfiletmp)), StandardCharsets.UTF_8);
+ outStream = new FileOutputStream(outputfiletmp);
+ osw = new OutputStreamWriter(new BufferedOutputStream(outStream), StandardCharsets.UTF_8);
osw.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + SurrogateReader.SURROGATES_MAIN_ELEMENT_OPEN + "\n");
}
e.writeXML(osw);
@@ -294,14 +296,18 @@ public class PhpBB3Dao implements Dao {
if (c >= maxEntriesInFile) {
osw.write("</surrogates>\n");
osw.close();
+ outStream.close();
outputfiletmp.renameTo(outputfile);
osw = null;
+ outStream = null;
c = 0;
fc++;
}
}
osw.write(SurrogateReader.SURROGATES_MAIN_ELEMENT_CLOSE + "\n");
osw.close();
+ outStream.close();
+ osw = null;
outputfiletmp.renameTo(outputfile);
return fc + 1;
} catch (final MalformedURLException e) {
@@ -312,6 +318,21 @@ public class PhpBB3Dao implements Dao {
ConcurrentLog.logException(e);
} catch (final InterruptedException e) {
ConcurrentLog.logException(e);
+ } finally {
+ if(osw != null) {
+ try {
+ osw.close();
+ } catch (IOException e) {
+ ConcurrentLog.logException(e);
+ }
+ }
+ if(outStream != null) {
+ try {
+ outStream.close();
+ } catch (IOException e) {
+ ConcurrentLog.logException(e);
+ }
+ }
}
return 0;
}
diff --git a/source/net/yacy/document/parser/apkParser.java b/source/net/yacy/document/parser/apkParser.java
index f1d0b2129..60f5b1048 100644
--- a/source/net/yacy/document/parser/apkParser.java
+++ b/source/net/yacy/document/parser/apkParser.java
@@ -74,20 +74,36 @@ public class apkParser extends AbstractParser implements Parser {
* - strings from resources
*/
Document[] docs = null;
+ File tempFile = null;
+ FileOutputStream out = null;
try {
- File tempFile = File.createTempFile("apk" + System.currentTimeMillis(), "jar");
- final FileOutputStream out = new FileOutputStream(tempFile);
+ tempFile = File.createTempFile("apk" + System.currentTimeMillis(), "jar");
+ out = new FileOutputStream(tempFile);
int read = 0;
final byte[] data = new byte[1024];
while((read = source.read(data, 0, 1024)) != -1) {
out.write(data, 0, read);
}
out.close();
+ out = null;
JarFile jf = new JarFile(tempFile);
docs = parse(location, mimeType, charset, jf);
- tempFile.delete();
} catch (IOException e) {
ConcurrentLog.logException(e);
+ } finally {
+ try {
+ if (out != null) {
+ out.close();
+ }
+ } catch (IOException e) {
+ ConcurrentLog.logException(e);
+ } finally {
+ if (tempFile != null) {
+ if (!tempFile.delete()) {
+ log.warn("Could not delete temporary file " + tempFile);
+ }
+ }
+ }
}
return docs;
}
diff --git a/source/net/yacy/document/parser/bzipParser.java b/source/net/yacy/document/parser/bzipParser.java
index 922eba542..bcbc83b36 100644
--- a/source/net/yacy/document/parser/bzipParser.java
+++ b/source/net/yacy/document/parser/bzipParser.java
@@ -29,6 +29,7 @@ package net.yacy.document.parser;
import java.io.File;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
@@ -74,23 +75,48 @@ public class bzipParser extends AbstractParser implements Parser {
File tempFile = null;
Document maindoc = null;
+ int read = 0;
+ final byte[] data = new byte[1024];
+ BZip2CompressorInputStream zippedContent = null;
+ FileOutputStream out = null;
try {
- int read = 0;
- final byte[] data = new byte[1024];
// BZip2CompressorInputStream checks filecontent (magic start-bytes "BZh") and throws ioexception if no match
- final BZip2CompressorInputStream zippedContent = new BZip2CompressorInputStream(source);
+ zippedContent = new BZip2CompressorInputStream(source);
tempFile = File.createTempFile("bunzip","tmp");
// creating a temp file to store the uncompressed data
- final FileOutputStream out = new FileOutputStream(tempFile);
+ out = new FileOutputStream(tempFile);
// reading bzip file and store it uncompressed
while((read = zippedContent.read(data, 0, 1024)) != -1) {
out.write(data, 0, read);
}
- zippedContent.close();
out.close();
+ out = null;
+
+ } catch(Exception e) {
+ if (tempFile != null) {
+ FileUtils.deletedelete(tempFile);
+ }
+ throw new Parser.Failure("Unexpected error while parsing bzip file. " + e.getMessage(), location);
+ } finally {
+ if(zippedContent != null) {
+ try {
+ zippedContent.close();
+ } catch (IOException ignored) {
+ log.warn("Could not close bzip input stream");
+ }
+ }
+ if(out != null) {
+ try {
+ out.close();
+ } catch (IOException e) {
+ throw new Parser.Failure("Unexpected error while parsing bzip file. " + e.getMessage(), location);
+ }
+ }
+ }
+ try {
final String filename = location.getFileName();
// create maindoc for this bzip container, register with supplied url & mime
maindoc = new Document(
@@ -114,7 +140,7 @@ public class bzipParser extends AbstractParser implements Parser {
new Date());
// creating a new parser class to parse the unzipped content
final String contentfilename = BZip2Utils.getUncompressedFilename(location.getFileName());
- final String mime = TextParser.mimeOf(DigestURL.getFileExtension(contentfilename));
+ final String mime = TextParser.mimeOf(MultiProtocolURL.getFileExtension(contentfilename));
final Document[] docs = TextParser.parseSource(location, mime, null, scraper, timezoneOffset, 999, tempFile);
if (docs != null) maindoc.addSubDocuments(docs);
} catch (final Exception e) {
diff --git a/source/net/yacy/document/parser/gzipParser.java b/source/net/yacy/document/parser/gzipParser.java
index 95a6ad77a..5bdb90d69 100644
--- a/source/net/yacy/document/parser/gzipParser.java
+++ b/source/net/yacy/document/parser/gzipParser.java
@@ -29,6 +29,7 @@ package net.yacy.document.parser;
import java.io.File;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.zip.GZIPInputStream;
@@ -72,23 +73,45 @@ public class gzipParser extends AbstractParser implements Parser {
File tempFile = null;
Document maindoc = null;
+ GZIPInputStream zippedContent = null;
+ FileOutputStream out = null;
try {
int read = 0;
final byte[] data = new byte[1024];
- final GZIPInputStream zippedContent = new GZIPInputStream(source);
+ zippedContent = new GZIPInputStream(source);
tempFile = File.createTempFile("gunzip","tmp");
// creating a temp file to store the uncompressed data
- final FileOutputStream out = new FileOutputStream(tempFile);
+ out = new FileOutputStream(tempFile);
// reading gzip file and store it uncompressed
while ((read = zippedContent.read(data, 0, 1024)) != -1) {
out.write(data, 0, read);
}
- zippedContent.close();
- out.close();
+ } catch(Exception e) {
+ if (tempFile != null) {
+ FileUtils.deletedelete(tempFile);
+ }
+ throw new Parser.Failure("Unexpected error while parsing gzip file. " + e.getMessage(), location);
+ } finally {
+ if(zippedContent != null) {
+ try {
+ zippedContent.close();
+ } catch (IOException ignored) {
+ log.warn("Could not close gzip input stream");
+ }
+ }
+ if(out != null) {
+ try {
+ out.close();
+ } catch (IOException e) {
+ throw new Parser.Failure("Unexpected error while parsing gzip file. " + e.getMessage(), location);
+ }
+ }
+ }
+ try {
final String filename = location.getFileName();
// create maindoc for this gzip container, register with supplied url & mime
maindoc = new Document(
@@ -112,7 +135,7 @@ public class gzipParser extends AbstractParser implements Parser {
new Date());
// creating a new parser class to parse the unzipped content
final String contentfilename = GzipUtils.getUncompressedFilename(location.getFileName());
- final String mime = TextParser.mimeOf(DigestURL.getFileExtension(contentfilename));
+ final String mime = TextParser.mimeOf(MultiProtocolURL.getFileExtension(contentfilename));
Document[] docs = TextParser.parseSource(location, mime, null, scraper, timezoneOffset, 999, tempFile);
if (docs != null) maindoc.addSubDocuments(docs);
} catch (final Exception e) {
diff --git a/source/net/yacy/http/Jetty9HttpServerImpl.java b/source/net/yacy/http/Jetty9HttpServerImpl.java
index 138d11fe0..4571cb34e 100644
--- a/source/net/yacy/http/Jetty9HttpServerImpl.java
+++ b/source/net/yacy/http/Jetty9HttpServerImpl.java
@@ -407,9 +407,12 @@ public class Jetty9HttpServerImpl implements YaCyHttpServer {
// creating an empty java keystore
final KeyStore ks = KeyStore.getInstance("JKS");
ks.load(null,keyStorePwd.toCharArray());
- final FileOutputStream ksOut = new FileOutputStream(keyStoreFileName);
- ks.store(ksOut, keyStorePwd.toCharArray());
- ksOut.close();
+ try (
+ /* Automatically closed by this try-with-resources statement */
+ final FileOutputStream ksOut = new FileOutputStream(keyStoreFileName);
+ ) {
+ ks.store(ksOut, keyStorePwd.toCharArray());
+ }
// storing path to keystore into config file
sb.setConfig("keyStore", keyStoreFileName);
diff --git a/source/net/yacy/kelondro/blob/Gap.java b/source/net/yacy/kelondro/blob/Gap.java
index d63c7a8a5..2d5cc08f0 100644
--- a/source/net/yacy/kelondro/blob/Gap.java
+++ b/source/net/yacy/kelondro/blob/Gap.java
@@ -105,21 +105,28 @@ public class Gap extends TreeMap<Long, Integer> {
File tmp = new File(file.getParentFile(), file.getName() + ".prt");
Iterator<Map.Entry<Long, Integer>> i = this.entrySet().iterator();
DataOutputStream os;
- try {
- os = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(tmp), (Integer.SIZE + Long.SIZE) * 1024)); // = 16*1024*recordsize
- } catch (final OutOfMemoryError e) {
- os = new DataOutputStream(new FileOutputStream(tmp));
- }
int c = 0;
- Map.Entry<Long, Integer> e;
- while (i.hasNext()) {
- e = i.next();
- os.writeLong(e.getKey().longValue());
- os.writeInt(e.getValue().intValue());
- c++;
+ try (/* Resource automatically closed by this try-with-resources statement */
+ final FileOutputStream fileStream = new FileOutputStream(tmp);
+ ) {
+ try {
+ os = new DataOutputStream(new BufferedOutputStream(fileStream, (Integer.SIZE + Long.SIZE) * 1024)); // = 16*1024*recordsize
+ } catch (final OutOfMemoryError e) {
+ os = new DataOutputStream(fileStream);
+ }
+ try {
+ Map.Entry<Long, Integer> e;
+ while (i.hasNext()) {
+ e = i.next();
+ os.writeLong(e.getKey().longValue());
+ os.writeInt(e.getValue().intValue());
+ c++;
+ }
+ os.flush();
+ } finally {
+ os.close();
+ }
}
- os.flush();
- os.close();
tmp.renameTo(file);
assert file.exists() : file.toString();
assert !tmp.exists() : tmp.toString();
diff --git a/source/net/yacy/kelondro/blob/HeapWriter.java b/source/net/yacy/kelondro/blob/HeapWriter.java
index 6ef001733..83c10bdd6 100644
--- a/source/net/yacy/kelondro/blob/HeapWriter.java
+++ b/source/net/yacy/kelondro/blob/HeapWriter.java
@@ -83,11 +83,21 @@ public final class HeapWriter {
this.heapFileREADY = readyHeapFile;
this.keylength = keylength;
this.index = new RowHandleMap(keylength, ordering, 8, 100000, readyHeapFile.getAbsolutePath());
+ final FileOutputStream fileStream = new FileOutputStream(temporaryHeapFile);
try {
- this.os = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(temporaryHeapFile), outBuffer));
- } catch (final OutOfMemoryError e) {
- // try this again without buffer
- this.os = new DataOutputStream(new FileOutputStream(temporaryHeapFile));
+ try {
+ this.os = new DataOutputStream(new BufferedOutputStream(fileStream, outBuffer));
+ } catch (final OutOfMemoryError e) {
+ // try this again without buffer
+ this.os = new DataOutputStream(fileStream);
+ }
+ } catch(Exception e) {
+ try {
+ fileStream.close();
+ } catch(IOException ignored) {
+ log.warn("Could not close output stream on file " + temporaryHeapFile);
+ }
+ throw e;
}
this.seek = 0;
}
@@ -122,8 +132,16 @@ public final class HeapWriter {
*/
public synchronized void close(boolean writeIDX) throws IOException {
// close the file
- this.os.flush();
- this.os.close();
+ try {
+ this.os.flush();
+ } catch(Exception e) {
+ // on flush error, free ram index resources before transmitting the exception
+ this.index.close();
+ this.index = null;
+ throw e;
+ } finally {
+ this.os.close();
+ }
this.os = null;
// rename the file into final name
diff --git a/source/net/yacy/kelondro/index/BinSearch.java b/source/net/yacy/kelondro/index/BinSearch.java
index 61426c4de..16581ffde 100644
--- a/source/net/yacy/kelondro/index/BinSearch.java
+++ b/source/net/yacy/kelondro/index/BinSearch.java
@@ -98,10 +98,12 @@ public final class BinSearch {
}
public final void write(File f) throws IOException {
- FileOutputStream os = new FileOutputStream(f);
- os.write(this.chunks);
- os.flush();
- os.close();
+ try ( /* Resource automatically closed by this try-with-resources statement */
+ FileOutputStream os = new FileOutputStream(f);
+ ) {
+ os.write(this.chunks);
+ os.flush();
+ }
}
public static void main(final String[] args) {
diff --git a/source/net/yacy/kelondro/index/RowHandleMap.java b/source/net/yacy/kelondro/index/RowHandleMap.java
index a58c3c5c5..9b845cbe2 100644
--- a/source/net/yacy/kelondro/index/RowHandleMap.java
+++ b/source/net/yacy/kelondro/index/RowHandleMap.java
@@ -195,20 +195,32 @@ public final class RowHandleMap implements HandleMap, Iterable<Map.Entry<byte[],
// everything much faster, but this is not an option here.
final File tmp = new File(file.getParentFile(), file.getName() + ".prt");
final Iterator<Row.Entry> i = this.index.rows(true, null);
- OutputStream os;
+ int c = 0;
+ final FileOutputStream fileStream = new FileOutputStream(tmp);
+ OutputStream os = null;
try {
- os = new BufferedOutputStream(new FileOutputStream(tmp), 4 * 1024 * 1024);
- } catch (final OutOfMemoryError e) {
- os = new FileOutputStream(tmp);
- }
- if (file.getName().endsWith(".gz")) os = new GZIPOutputStream(os, 65536){{def.setLevel(Deflater.BEST_COMPRESSION);}};
- int c = 0;
- while (i.hasNext()) {
- os.write(i.next().bytes());
- c++;
+ try {
+ os = new BufferedOutputStream(fileStream, 4 * 1024 * 1024);
+ } catch (final OutOfMemoryError e) {
+ os = fileStream;
+ }
+ if (file.getName().endsWith(".gz")) os = new GZIPOutputStream(os, 65536){{def.setLevel(Deflater.BEST_COMPRESSION);}};
+ while (i.hasNext()) {
+ os.write(i.next().bytes());
+ c++;
+ }
+ os.flush();
+ } finally {
+ try {
+ if(os != null) {
+ os.close();
+ }
+ } finally {
+ if(fileStream != os) {
+ fileStream.close();
+ }
+ }
}
- os.flush();
- os.close();
tmp.renameTo(file);
assert file.exists() : file.toString();
assert !tmp.exists() : tmp.toString();
diff --git a/source/net/yacy/kelondro/index/RowHandleSet.java b/source/net/yacy/kelondro/index/RowHandleSet.java
index 7b2fb7720..15ef4067a 100644
--- a/source/net/yacy/kelondro/index/RowHandleSet.java
+++ b/source/net/yacy/kelondro/index/RowHandleSet.java
@@ -141,19 +141,32 @@ public final class RowHandleSet implements HandleSet, Iterable<byte[]>, Cloneabl
// otherwise we could just write the byte[] from the in kelondroRowSet which would make
// everything much faster, but this is not an option here.
final Iterator<Row.Entry> i = this.index.rows(true, null);
- OutputStream os;
+ int c = 0;
+ final FileOutputStream fileStream = new FileOutputStream(file);
+ OutputStream os = null;
try {
- os = new BufferedOutputStream(new FileOutputStream(file), 1024 * 1024);
- } catch (final OutOfMemoryError e) {
- os = new FileOutputStream(file);
- }
- int c = 0;
- while (i.hasNext()) {
- os.write(i.next().bytes());
- c++;
+
+ try {
+ os = new BufferedOutputStream(fileStream, 1024 * 1024);
+ } catch (final OutOfMemoryError e) {
+ os = fileStream;
+ }
+ while (i.hasNext()) {
+ os.write(i.next().bytes());
+ c++;
+ }
+ os.flush();
+ } finally {
+ try {
+ if(os != null) {
+ os.close();
+ }
+ } finally {
+ if(fileStream != os) {
+ fileStream.close();
+ }
+ }
}
- os.flush();
- os.close();
return c;
}
@@ -391,9 +404,11 @@ public final class RowHandleSet implements HandleSet, Iterable<byte[]>, Cloneabl
try {
// write to file
File f = File.createTempFile("HandleSet", "stream");
- ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
- out.writeObject(s);
- out.close();
+ try(/* Resource automatically closed by thus try-with-resource statement */
+ ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
+ ) {
+ out.writeObject(s);
+ }
// read from file
ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
diff --git a/source/net/yacy/kelondro/util/XMLTables.java b/source/net/yacy/kelondro/util/XMLTables.java
index ae2a05289..39830a5d8 100644
--- a/source/net/yacy/kelondro/util/XMLTables.java
+++ b/source/net/yacy/kelondro/util/XMLTables.java
@@ -88,9 +88,12 @@ public class XMLTables {
final File tmpFile = new File(this.propFile.toString() + ".prt");
// write file
- final XMLEncoder xmlenc = new XMLEncoder(new FileOutputStream(tmpFile));
- xmlenc.writeObject(tables);
- xmlenc.close();
+ try(/* Resource automatically closed by this try-with-resources statement */
+ final XMLEncoder xmlenc = new XMLEncoder(new FileOutputStream(tmpFile));
+ ) {
+ xmlenc.writeObject(tables);
+ }
+
// delete old file and rename tmp-file to old file's name
FileUtils.deletedelete(this.propFile);
diff --git a/source/net/yacy/peers/operation/yacyRelease.java b/source/net/yacy/peers/operation/yacyRelease.java
index 6a139d867..ad96c1406 100644
--- a/source/net/yacy/peers/operation/yacyRelease.java
+++ b/source/net/yacy/peers/operation/yacyRelease.java
@@ -32,7 +32,6 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
@@ -331,20 +330,19 @@ public final class yacyRelease extends yacyVersion {
}
if (this.publicKey != null && signatureBytes != null) {
// copy to file and check signature
- SignatureOutputStream verifyOutput = null;
- try {
- verifyOutput = new SignatureOutputStream(new FileOutputStream(download), CryptoLib.signAlgorithm, this.publicKey);
- FileUtils.copy(response.getContent(), new BufferedOutputStream(verifyOutput));
+ try (
+ /* Resources automatically closed by this try-with-resources statement */
+ final FileOutputStream fileOutStream = new FileOutputStream(download);
+ final SignatureOutputStream verifyOutput = new SignatureOutputStream(fileOutStream, CryptoLib.signAlgorithm, this.publicKey);
+ final BufferedOutputStream bufferedStream = new BufferedOutputStream(verifyOutput);
+ ) {
+ FileUtils.copy(response.getContent(), bufferedStream);
if (!verifyOutput.verify(signatureBytes)) throw new IOException("Bad Signature!");
} catch (final NoSuchAlgorithmException e) {
throw new IOException("No such algorithm");
} catch (final SignatureException e) {
throw new IOException("Signature exception");
- } finally {
- if (verifyOutput != null) {
- verifyOutput.close();
- }
}
// Save signature
final File signatureFile = new File(download.getAbsoluteFile() + ".sig");
@@ -352,14 +350,12 @@ public final class yacyRelease extends yacyVersion {
if ((!signatureFile.exists()) || (signatureFile.length() == 0)) throw new IOException("create signature file failed");
} else {
// just copy into file
- OutputStream downloadOutStream = null;
- try {
- downloadOutStream = new BufferedOutputStream(new FileOutputStream(download));
+ try (
+ /* Resources automatically closed by this try-with-resources statement */
+ final FileOutputStream fileOutStream = new FileOutputStream(download);
+ final BufferedOutputStream downloadOutStream = new BufferedOutputStream(fileOutStream);
+ ) {
FileUtils.copy(response.getContent(), downloadOutStream);
- } finally {
- if(downloadOutStream != null) {
- downloadOutStream.close();
- }
}
}
if ((!download.exists()) || (download.length() == 0)) throw new IOException("wget of url " + getUrl() + " failed");
diff --git a/source/net/yacy/repository/Blacklist.java b/source/net/yacy/repository/Blacklist.java
index 03efe4c1b..dda98c9b7 100644
--- a/source/net/yacy/repository/Blacklist.java
+++ b/source/net/yacy/repository/Blacklist.java
@@ -779,15 +779,17 @@ public class Blacklist {
}
private final void saveDHTCache(final BlacklistType type) {
- try {
- final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(DHTCacheFile(type)));
+ try (
+ /* Resources automatically closed by this try-with-resources statement */
+ final FileOutputStream fileOutStream =new FileOutputStream(DHTCacheFile(type));
+ final ObjectOutputStream out = new ObjectOutputStream(fileOutStream);
+ ) {
HandleSet s = getCacheUrlHashsSet(type);
if (s != null) {
out.writeObject(getCacheUrlHashsSet(type));
- out.close();
}
-
} catch (final IOException e) {
+ /* Catch but trace in log any IO exception occurring in write or automatic closing */
ConcurrentLog.logException(e);
}
}
diff --git a/source/net/yacy/search/AutoSearch.java b/source/net/yacy/search/AutoSearch.java
index 4fc0ac57c..194c9f387 100644
--- a/source/net/yacy/search/AutoSearch.java
+++ b/source/net/yacy/search/AutoSearch.java
@@ -124,14 +124,14 @@ public class AutoSearch extends AbstractBusyThread {
pfile.delete();
}
} else {
- try {
+ try (/* Resource automatically closed by this try-with-resources statement */
+ final OutputStream fileOut = new FileOutputStream(pfile);
+ ) {
Properties prop = new Properties();
for (String s : querystack) {
prop.put("query" + s.hashCode(), s);
}
- OutputStream fileOut = new FileOutputStream(pfile);
prop.store(fileOut, "AutoSearch query list");
- fileOut.close();
} catch (FileNotFoundException ex) {
ConcurrentLog.warn(AutoSearch.class.getName(), "can not create file " + pfile.getAbsolutePath());
} catch (IOException ex) {
diff --git a/source/net/yacy/search/Switchboard.java b/source/net/yacy/search/Switchboard.java
index 9eef2fd10..0637bef3e 100644
--- a/source/net/yacy/search/Switchboard.java
+++ b/source/net/yacy/search/Switchboard.java
@@ -2144,28 +2144,21 @@ public final class Switchboard extends serverSwitch {
if ( !outfile.getName().endsWith(".gz") ) {
final String gzname = outfile.getName() + ".gz";
final File gzfile = new File(outfile.getParentFile(), gzname);
- try {
- final OutputStream os = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(gzfile), 65536){{def.setLevel(Deflater.BEST_COMPRESSION);}});
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(outfile));
- try {
- FileUtils.copy(bis, os);
- } finally {
- try {
- os.close();
- } finally {
- try {
- bis.close();
- } catch(IOException ignored) {
- log.warn("Could not close input stream on file " + outfile);
- }
- }
- }
+ try (
+ /* Resources automatically closed by this try-with-resources statement */
+ final FileOutputStream fileOutStream = new FileOutputStream(gzfile);
+ final OutputStream os = new BufferedOutputStream(new GZIPOutputStream(fileOutStream, 65536){{def.setLevel(Deflater.BEST_COMPRESSION);}});
+ final FileInputStream fileInStream = new FileInputStream(outfile);
+ final BufferedInputStream bis = new BufferedInputStream(fileInStream);
+ ) {
+ FileUtils.copy(bis, os);
if ( gzfile.exists() ) {
FileUtils.deletedelete(outfile);
}
} catch (final FileNotFoundException e ) {
ConcurrentLog.logException(e);
} catch (final IOException e ) {
+ /* Catch but log any IO exception that can occur on copy, automatic closing or streams creation */
ConcurrentLog.logException(e);
}
}
diff --git a/source/net/yacy/search/index/Fulltext.java b/source/net/yacy/search/index/Fulltext.java
index 102977b73..4ef4ee611 100644
--- a/source/net/yacy/search/index/Fulltext.java
+++ b/source/net/yacy/search/index/Fulltext.java
@@ -784,12 +784,22 @@ public final class Fulltext {
@Override
public void run() {
- try {
+ try {
final File parentf = this.f.getParentFile();
- if (parentf != null) parentf.mkdirs();
- OutputStream os = new FileOutputStream(this.format == ExportFormat.solr ? new File(this.f.getAbsolutePath() + ".gz") : this.f);
- if (this.format == ExportFormat.solr) os = new GZIPOutputStream(os, 65536){{def.setLevel(Deflater.BEST_COMPRESSION);}};
- final PrintWriter pw = new PrintWriter(new BufferedOutputStream(os));
+ if (parentf != null) {
+ parentf.mkdirs();
+ }
+ } catch(Exception e) {
+ ConcurrentLog.logException(e);
+ this.failure = e.getMessage();
+ return;
+ }
+
+ try (/* Resources automatically closed by this try-with-resources statement */
+ final OutputStream os = new FileOutputStream(this.format == ExportFormat.solr ? new File(this.f.getAbsolutePath() + ".gz") : this.f);
+ final OutputStream wrappedStream = ((this.format == ExportFormat.solr)) ? new GZIPOutputStream(os, 65536){{def.setLevel(Deflater.BEST_COMPRESSION);}} : os;
+ final PrintWriter pw = new PrintWriter(new BufferedOutputStream(wrappedStream));
+ ) {
if (this.format == ExportFormat.html) {
pw.println("<html><head></head><body>");
}
@@ -888,11 +898,8 @@ public final class Fulltext {
pw.println("</result>");
pw.println("</response>");
}
- pw.close();
- } catch (final IOException e) {
- ConcurrentLog.logException(e);
- this.failure = e.getMessage();
} catch (final Exception e) {
+ /* Catch but log any IO exception that can occur on copy, automatic closing or streams creation */
ConcurrentLog.logException(e);
this.failure = e.getMessage();
}
diff --git a/source/net/yacy/server/serverSwitch.java b/source/net/yacy/server/serverSwitch.java
index 83e158d1f..eae42215b 100644
--- a/source/net/yacy/server/serverSwitch.java
+++ b/source/net/yacy/server/serverSwitch.java
@@ -698,9 +698,11 @@ public class serverSwitch {
}
// save locally in case next fetch fails
if (file != null) {
- FileOutputStream f = new FileOutputStream(file);
- f.write(data);
- f.close();
+ try(/* Automatically closed by this try-with-resource statement */
+ FileOutputStream f = new FileOutputStream(file);
+ ) {
+ f.write(data);
+ }
}
return new InputStreamReader(new BufferedInputStream(
new ByteArrayInputStream(data)));
diff --git a/source/net/yacy/utils/gzip.java b/source/net/yacy/utils/gzip.java
index 21487b6c9..e95f82d04 100644
--- a/source/net/yacy/utils/gzip.java
+++ b/source/net/yacy/utils/gzip.java
@@ -86,12 +86,14 @@ public class gzip {
}
public static void gunzipFile(final File inFile, final File outFile) {
- try {
- final InputStream fin = new GZIPInputStream(new BufferedInputStream(new FileInputStream(inFile)));
- final OutputStream fout = new BufferedOutputStream(new FileOutputStream(outFile));
+ try (
+ /* Resources automatically closed by this try-with-resources statement */
+ final FileInputStream fileInStream = new FileInputStream(inFile);
+ final InputStream fin = new GZIPInputStream(new BufferedInputStream(fileInStream));
+ final FileOutputStream fileOutStream = new FileOutputStream(outFile);
+ final OutputStream fout = new BufferedOutputStream(fileOutStream);
+ ) {
copy(fout, fin, 1024);
- fin.close();
- fout.close();
} catch (final FileNotFoundException e) {
logger.warn("ERROR: file '" + inFile + "' not found", e);
} catch (final IOException e) {
@@ -145,13 +147,14 @@ public class gzip {
// some static helper methods
public static void saveGzip(final File f, final byte[] content) throws IOException {
- java.util.zip.GZIPOutputStream gzipout = null;
- try {
- f.getParentFile().mkdirs();
- gzipout = new java.util.zip.GZIPOutputStream(new FileOutputStream(f), 65536){{def.setLevel(Deflater.BEST_COMPRESSION);}};
+ f.getParentFile().mkdirs();
+
+ try (
+ /* Resources automatically closed by this try-with-resources statement */
+ final FileOutputStream fileOutStream = new FileOutputStream(f);
+ final java.util.zip.GZIPOutputStream gzipout = new java.util.zip.GZIPOutputStream(fileOutStream, 65536){{def.setLevel(Deflater.BEST_COMPRESSION);}};
+ ) {
gzipout.write(content, 0, content.length);
- } finally {
- if (gzipout!=null)try{gzipout.close();}catch(final Exception e){}
}
}
diff --git a/source/net/yacy/utils/tarTools.java b/source/net/yacy/utils/tarTools.java
index c16297967..9deb1ddb3 100644
--- a/source/net/yacy/utils/tarTools.java
+++ b/source/net/yacy/utils/tarTools.java
@@ -134,9 +134,12 @@ public class tarTools {
final File destPath = new File(untarDir + File.separator + tarEntry.getName());
if (!tarEntry.isDirectory()) {
new File(destPath.getParent()).mkdirs(); // create missing subdirectories
- final FileOutputStream fout = new FileOutputStream(destPath);
- IOUtils.copyLarge(tin,fout,0,tarEntry.getSize());
- fout.close();
+ try (
+ /* Automatically closed by this try-with-resources statement */
+ final FileOutputStream fout = new FileOutputStream(destPath);
+ ) {
+ IOUtils.copyLarge(tin, fout, 0, tarEntry.getSize());
+ }
} else {
destPath.mkdir();
}
diff --git a/source/net/yacy/utils/translation/TranslatorXliff.java b/source/net/yacy/utils/translation/TranslatorXliff.java
index e44628709..534433f71 100644
--- a/source/net/yacy/utils/translation/TranslatorXliff.java
+++ b/source/net/yacy/utils/translation/TranslatorXliff.java
@@ -217,10 +217,12 @@ public class TranslatorXliff extends Translator {
public boolean saveAsXliff(final String targetLanguageCode, File xliffFile, Map<String, Map<String, String>> lng) {
final String sourceLanguage = "en"; // source language is always English
- OutputStreamWriter output;
- try {
- output = new OutputStreamWriter(new FileOutputStream(xliffFile), StandardCharsets.UTF_8.name());
+ try (
+ /* Resources automatically closed by this try-with-resources statement */
+ final FileOutputStream fileOutStream = new FileOutputStream(xliffFile);
+ final OutputStreamWriter output = new OutputStreamWriter(fileOutStream, StandardCharsets.UTF_8.name());
+ ) {
output.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
output.write("<xliff version='1.2' xmlns='urn:oasis:names:tc:xliff:document:1.2'> \n");
for (String afilemap : lng.keySet()) {
@@ -252,7 +254,6 @@ public class TranslatorXliff extends Translator {
}
output.write("</xliff>\n");
- output.close();
} catch (Exception e) {
return false;
}
@@ -302,10 +303,11 @@ public class TranslatorXliff extends Translator {
*/
public boolean saveAsLngFile(final String targetLanguageCode, File lngFile, Map<String, Map<String, String>> lng) {
- OutputStreamWriter output;
-
- try {
- output = new OutputStreamWriter(new FileOutputStream(lngFile), StandardCharsets.UTF_8.name());
+ try (
+ /* Resources automatically closed by this try-with-resources statement */
+ final FileOutputStream fileOutStream = new FileOutputStream(lngFile);
+ final OutputStreamWriter output = new OutputStreamWriter(fileOutStream, StandardCharsets.UTF_8.name());
+ ) {
output.write("# " + (targetLanguageCode == null ? "master" : targetLanguageCode) + ".lng\n");
output.write("# -----------------------\n");
output.write("# This is a part of YaCy, a peer-to-peer based web search engine\n\n");
@@ -325,7 +327,6 @@ public class TranslatorXliff extends Translator {
}
}
output.write("# EOF");
- output.close();
} catch (Exception e) {
return false;
}
diff --git a/source/net/yacy/visualization/AnimationGIF.java b/source/net/yacy/visualization/AnimationGIF.java
index 6361bd989..78ecc4cb6 100644
--- a/source/net/yacy/visualization/AnimationGIF.java
+++ b/source/net/yacy/visualization/AnimationGIF.java
@@ -204,9 +204,12 @@ public class AnimationGIF {
for (int i = 0; i < framescount; i++) {
generator.addImage(generateTestImage(320, 160, r, i * 2 * Math.PI / framescount), 10, 0);
}
- FileOutputStream fos = new FileOutputStream(new File("/tmp/giftest.gif"));
- fos.write(generator.get());
- fos.close();
+ try (
+ /* Automatically closed by this try-with-resources statement */
+ FileOutputStream fos = new FileOutputStream(new File(System.getProperty("java.io.tmpdir") + File.separator + "giftest.gif"));
+ ) {
+ fos.write(generator.get());
+ }
} catch (final IOException e) {
e.printStackTrace();
}
diff --git a/source/net/yacy/visualization/AnimationPlotter.java b/source/net/yacy/visualization/AnimationPlotter.java
index 4cf21c023..565fc7019 100644
--- a/source/net/yacy/visualization/AnimationPlotter.java
+++ b/source/net/yacy/visualization/AnimationPlotter.java
@@ -60,9 +60,12 @@ public class AnimationPlotter {
for (int i = 0; i < this.frames.size(); i++) {
Frame frame = this.frames.get(i);
File file = new File(path, filestub + "_" + intformat(i) + '.' + type);
- final FileOutputStream fos = new FileOutputStream(file);
- ImageIO.write(frame.image, type, fos);
- fos.close();
+ try (
+ /* Automatically closed by this try-with-resources statement */
+ final FileOutputStream fos = new FileOutputStream(file);
+ ) {
+ ImageIO.write(frame.image, type, fos);
+ }
}
}
diff --git a/source/net/yacy/visualization/ChartPlotter.java b/source/net/yacy/visualization/ChartPlotter.java
index 47e70e8f7..5dfc6f974 100644
--- a/source/net/yacy/visualization/ChartPlotter.java
+++ b/source/net/yacy/visualization/ChartPlotter.java
@@ -201,13 +201,17 @@ public class ChartPlotter extends RasterPlotter {
//ip.print(100, 100, 0, "1234", false);
//ip.print(100, 100, 90, "TEXT", true);
//ip.print(100, 100, 90, "1234", false);
- final File file = new File("/Users/admin/Desktop/testimage.png");
- try {
+ final File file = new File(System.getProperty("java.io.tmpdir") + File.separator + "testimage.png");
+ try (
+ /* Automatically closed by this try-with-resources statement */
final FileOutputStream fos = new FileOutputStream(file);
+ ) {
+ System.out.println("Writing file " + file);
fos.write(RasterPlotter.exportImage(ip.getImage(), "png").getBytes());
//ImageIO.write(ip.getImage(), "png", fos);
- fos.close();
- } catch (final IOException e) {}
+ } catch (final IOException e) {
+ e.printStackTrace();
+ }
ConcurrentLog.shutdown();
}
diff --git a/source/net/yacy/visualization/RasterPlotter.java b/source/net/yacy/visualization/RasterPlotter.java
index 2134188bc..41f13b856 100644
--- a/source/net/yacy/visualization/RasterPlotter.java
+++ b/source/net/yacy/visualization/RasterPlotter.java
@@ -950,9 +950,12 @@ public class RasterPlotter {
* @throws IOException
*/
public void save(File file, String type) throws IOException {
- final FileOutputStream fos = new FileOutputStream(file);
- ImageIO.write(this.image, type, fos);
- fos.close();
+ try (
+ /* Automatically closed by this try-with-resources statement */
+ final FileOutputStream fos = new FileOutputStream(file);
+ ) {
+ ImageIO.write(this.image, type, fos);
+ }
}
/**
@@ -1054,12 +1057,15 @@ public class RasterPlotter {
final RasterPlotter m = new RasterPlotter(200, 300, DrawMode.MODE_SUB, "FFFFFF");
demoPaint(m);
- final File file = new File("/Users/admin/Desktop/testimage.png");
- try {
+ final File file = new File(System.getProperty("java.io.tmpdir") + File.separator + "testimage.png");
+ try ( /* Automatically closed by this try-with-resources statement */
final FileOutputStream fos = new FileOutputStream(file);
+ ) {
+ System.out.println("Writing file " + file);
ImageIO.write(m.getImage(), "png", fos);
- fos.close();
- } catch (final IOException e) {}
+ } catch (final IOException e) {
+ e.printStackTrace();
+ }
ConcurrentLog.shutdown();
// open file automatically, works only on Mac OS X