Tighten up file closing on exceptions

This commit is contained in:
Mike Primm 2011-10-19 12:20:56 +08:00 committed by mikeprimm
parent 5c064eab2b
commit d70ea37b31
3 changed files with 59 additions and 10 deletions

View File

@ -93,10 +93,12 @@ public class JsonFileClientUpdateComponent extends ClientUpdateComponent {
int retrycnt = 0; int retrycnt = 0;
boolean done = false; boolean done = false;
while(!done) { while(!done) {
FileOutputStream fos = null;
try { try {
FileOutputStream fos = new FileOutputStream(outputTempFile); fos = new FileOutputStream(outputTempFile);
fos.write(clientConfiguration.toJSONString().getBytes("UTF-8")); fos.write(clientConfiguration.toJSONString().getBytes("UTF-8"));
fos.close(); fos.close();
fos = null;
outputFile.delete(); outputFile.delete();
outputTempFile.renameTo(outputFile); outputTempFile.renameTo(outputFile);
done = true; done = true;
@ -109,6 +111,14 @@ public class JsonFileClientUpdateComponent extends ClientUpdateComponent {
Log.severe("Exception while writing JSON-configuration-file.", ioe); Log.severe("Exception while writing JSON-configuration-file.", ioe);
done = true; done = true;
} }
} finally {
if(fos != null) {
try {
fos.close();
} catch (IOException iox) {
}
fos = null;
}
} }
} }
} }
@ -132,10 +142,12 @@ public class JsonFileClientUpdateComponent extends ClientUpdateComponent {
int retrycnt = 0; int retrycnt = 0;
boolean done = false; boolean done = false;
while(!done) { while(!done) {
FileOutputStream fos = null;
try { try {
FileOutputStream fos = new FileOutputStream(outputTempFile); fos = new FileOutputStream(outputTempFile);
fos.write(Json.stringifyJson(update).getBytes("UTF-8")); fos.write(Json.stringifyJson(update).getBytes("UTF-8"));
fos.close(); fos.close();
fos = null;
outputFile.delete(); outputFile.delete();
outputTempFile.renameTo(outputFile); outputTempFile.renameTo(outputFile);
done = true; done = true;
@ -148,6 +160,14 @@ public class JsonFileClientUpdateComponent extends ClientUpdateComponent {
Log.severe("Exception while writing JSON-file.", ioe); Log.severe("Exception while writing JSON-file.", ioe);
done = true; done = true;
} }
} finally {
if(fos != null) {
try {
fos.close();
} catch (IOException iox) {
}
fos = null;
}
} }
} }
plugin.events.<ClientUpdateEvent>trigger("clientupdatewritten", clientUpdate); plugin.events.<ClientUpdateEvent>trigger("clientupdatewritten", clientUpdate);
@ -160,14 +180,23 @@ public class JsonFileClientUpdateComponent extends ClientUpdateComponent {
File webchatFile = getStandaloneFile("dynmap_webchat.json"); File webchatFile = getStandaloneFile("dynmap_webchat.json");
if (webchatFile.exists() && lastTimestamp != 0) { if (webchatFile.exists() && lastTimestamp != 0) {
JSONArray jsonMsgs = null; JSONArray jsonMsgs = null;
Reader inputFileReader = null;
try { try {
Reader inputFileReader = new InputStreamReader(new FileInputStream(webchatFile), cs_utf8); inputFileReader = new InputStreamReader(new FileInputStream(webchatFile), cs_utf8);
jsonMsgs = (JSONArray) parser.parse(inputFileReader); jsonMsgs = (JSONArray) parser.parse(inputFileReader);
inputFileReader.close();
} catch (IOException ex) { } catch (IOException ex) {
Log.severe("Exception while reading JSON-file.", ex); Log.severe("Exception while reading JSON-file.", ex);
} catch (ParseException ex) { } catch (ParseException ex) {
Log.severe("Exception while parsing JSON-file.", ex); Log.severe("Exception while parsing JSON-file.", ex);
} finally {
if(inputFileReader != null) {
try {
inputFileReader.close();
} catch (IOException iox) {
}
inputFileReader = null;
}
} }
if (jsonMsgs != null) { if (jsonMsgs != null) {

View File

@ -47,14 +47,21 @@ public class FactionsConfigHandler {
return rslt; return rslt;
} }
JSONObject fact = null; JSONObject fact = null;
Reader inputFileReader = null;
try { try {
Reader inputFileReader = new InputStreamReader(new FileInputStream(faction), cs_utf8); inputFileReader = new InputStreamReader(new FileInputStream(faction), cs_utf8);
fact = (JSONObject) parser.parse(inputFileReader); fact = (JSONObject) parser.parse(inputFileReader);
inputFileReader.close();
} catch (IOException ex) { } catch (IOException ex) {
Log.severe("Exception while reading factions.json.", ex); Log.severe("Exception while reading factions.json.", ex);
} catch (ParseException ex) { } catch (ParseException ex) {
Log.severe("Exception while parsing factions.json.", ex); Log.severe("Exception while parsing factions.json.", ex);
} finally {
if(inputFileReader != null) {
try {
inputFileReader.close();
} catch (IOException iox) {}
inputFileReader = null;
}
} }
if(fact == null) if(fact == null)
return rslt; return rslt;
@ -66,13 +73,19 @@ public class FactionsConfigHandler {
} }
JSONObject blocks = null; JSONObject blocks = null;
try { try {
Reader inputFileReader = new InputStreamReader(new FileInputStream(board), cs_utf8); inputFileReader = new InputStreamReader(new FileInputStream(board), cs_utf8);
blocks = (JSONObject) parser.parse(inputFileReader); blocks = (JSONObject) parser.parse(inputFileReader);
inputFileReader.close();
} catch (IOException ex) { } catch (IOException ex) {
Log.severe("Exception while reading board.json.", ex); Log.severe("Exception while reading board.json.", ex);
} catch (ParseException ex) { } catch (ParseException ex) {
Log.severe("Exception while parsing board.json.", ex); Log.severe("Exception while parsing board.json.", ex);
} finally {
if(inputFileReader != null) {
try {
inputFileReader.close();
} catch (IOException iox) {}
inputFileReader = null;
}
} }
if(blocks == null) if(blocks == null)
return rslt; return rslt;

View File

@ -157,14 +157,21 @@ public class RegionsComponent extends ClientComponent {
else { else {
outputFile = new File(plugin.getDataFolder(), webWorldPath.toString()); outputFile = new File(plugin.getDataFolder(), webWorldPath.toString());
} }
FileOutputStream fos = null;
try { try {
FileOutputStream fos = new FileOutputStream(outputFile); fos = new FileOutputStream(outputFile);
fos.write(Json.stringifyJson(regionData).getBytes()); fos.write(Json.stringifyJson(regionData).getBytes());
fos.close();
} catch (FileNotFoundException ex) { } catch (FileNotFoundException ex) {
Log.severe("Exception while writing JSON-file.", ex); Log.severe("Exception while writing JSON-file.", ex);
} catch (IOException ioe) { } catch (IOException ioe) {
Log.severe("Exception while writing JSON-file.", ioe); Log.severe("Exception while writing JSON-file.", ioe);
} finally {
if(fos != null) {
try {
fos.close();
} catch (IOException iox) {}
fos = null;
}
} }
} }