Permalink
Browse files

Properly close file output streams even on exceptions scenarios.

  • Loading branch information...
luccioman committed Jun 8, 2017
1 parent 4e4dc6c commit 83992751420c2ebb6a15b7f7d81679ecf173b8f0
Showing with 382 additions and 214 deletions.
  1. +10 −7 htroot/ConfigLanguage_p.java
  2. +3 −2 source/net/yacy/cora/federate/solr/instance/EmbeddedInstance.java
  3. +12 −9 source/net/yacy/cora/lod/vocabulary/Tagging.java
  4. +2 −5 source/net/yacy/cora/protocol/ftp/FTPClient.java
  5. +4 −7 source/net/yacy/cora/storage/ZIPWriter.java
  6. +14 −11 source/net/yacy/crawler/data/Transactions.java
  7. +6 −11 source/net/yacy/data/Translator.java
  8. +23 −2 source/net/yacy/document/content/dao/PhpBB3Dao.java
  9. +19 −3 source/net/yacy/document/parser/apkParser.java
  10. +32 −6 source/net/yacy/document/parser/bzipParser.java
  11. +28 −5 source/net/yacy/document/parser/gzipParser.java
  12. +6 −3 source/net/yacy/http/Jetty9HttpServerImpl.java
  13. +20 −13 source/net/yacy/kelondro/blob/Gap.java
  14. +24 −6 source/net/yacy/kelondro/blob/HeapWriter.java
  15. +6 −4 source/net/yacy/kelondro/index/BinSearch.java
  16. +24 −12 source/net/yacy/kelondro/index/RowHandleMap.java
  17. +29 −14 source/net/yacy/kelondro/index/RowHandleSet.java
  18. +6 −3 source/net/yacy/kelondro/util/XMLTables.java
  19. +12 −16 source/net/yacy/peers/operation/yacyRelease.java
  20. +6 −4 source/net/yacy/repository/Blacklist.java
  21. +3 −3 source/net/yacy/search/AutoSearch.java
  22. +9 −16 source/net/yacy/search/Switchboard.java
  23. +16 −9 source/net/yacy/search/index/Fulltext.java
  24. +5 −3 source/net/yacy/server/serverSwitch.java
  25. +14 −11 source/net/yacy/utils/gzip.java
  26. +6 −3 source/net/yacy/utils/tarTools.java
  27. +10 −9 source/net/yacy/utils/translation/TranslatorXliff.java
  28. +6 −3 source/net/yacy/visualization/AnimationGIF.java
  29. +6 −3 source/net/yacy/visualization/AnimationPlotter.java
  30. +8 −4 source/net/yacy/visualization/ChartPlotter.java
  31. +13 −7 source/net/yacy/visualization/RasterPlotter.java
@@ -105,15 +105,18 @@ public static serverObjects respond(@SuppressWarnings("unused") final RequestHea
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());
@@ -120,15 +120,16 @@ private static void initializeCoreConf(final File solr_config, final File contai
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);
}
@@ -165,15 +165,18 @@ public Tagging(String name, File propFile, String objectspace, Map<String, SOTup
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();
}
}
@@ -2860,13 +2860,10 @@ public static void main(final String[] args) {
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) {
@@ -95,12 +95,12 @@ public static void zip(File inputDir, File zipOut) throws IOException {
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 static void zip(File inputDir, File zipOut) throws IOException {
}
}
}
} finally {
zout.close();
out.close();
}
}
@@ -167,24 +167,27 @@ public static boolean store(final SolrInputDocument doc, final boolean concurren
// 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;
@@ -191,19 +191,14 @@ public boolean translateFile(final File sourceFile, final File destFile, final M
}
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;
@@ -270,14 +270,15 @@ public int writeSurrogates(
String versioninfo,
int maxEntriesInFile
) {
FileOutputStream outStream = null;
OutputStreamWriter osw = null;
try {
// generate output file name and attributes
String targethost = new DigestURL(this.urlstub).getHost();
int fc = 0;
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,22 +287,27 @@ public int writeSurrogates(
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);
c++;
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 int writeSurrogates(
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;
}
@@ -74,20 +74,36 @@ public apkParser() {
* - 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;
}
@@ -29,6 +29,7 @@
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 bzipParser() {
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 bzipParser() {
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) {
Oops, something went wrong.

0 comments on commit 8399275

Please sign in to comment.