Skip to content

Commit

Permalink
Properly close file output streams even on exceptions scenarios.
Browse files Browse the repository at this point in the history
  • Loading branch information
luccioman committed Jun 8, 2017
1 parent 4e4dc6c commit 8399275
Show file tree
Hide file tree
Showing 31 changed files with 382 additions and 214 deletions.
17 changes: 10 additions & 7 deletions htroot/ConfigLanguage_p.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
21 changes: 12 additions & 9 deletions source/net/yacy/cora/lod/vocabulary/Tagging.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
7 changes: 2 additions & 5 deletions source/net/yacy/cora/protocol/ftp/FTPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 4 additions & 7 deletions source/net/yacy/cora/storage/ZIPWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -117,9 +117,6 @@ public static void zip(File inputDir, File zipOut) throws IOException {
}
}
}
} finally {
zout.close();
out.close();
}
}

Expand Down
25 changes: 14 additions & 11 deletions source/net/yacy/crawler/data/Transactions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 6 additions & 11 deletions source/net/yacy/data/Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 23 additions & 2 deletions source/net/yacy/document/content/dao/PhpBB3Dao.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down
22 changes: 19 additions & 3 deletions source/net/yacy/document/parser/apkParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,36 @@ public Document[] parse(
* - 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;
}
Expand Down
38 changes: 32 additions & 6 deletions source/net/yacy/document/parser/bzipParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

Expand Down Expand Up @@ -74,23 +75,48 @@ public Document[] parse(

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(
Expand All @@ -114,7 +140,7 @@ public Document[] parse(
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) {
Expand Down
Loading

0 comments on commit 8399275

Please sign in to comment.