make sure file streams are closed by finally blocks

master
melvin 2011-06-24 11:31:05 +08:00
parent 3423d67e9c
commit db5ee83239
4 changed files with 42 additions and 16 deletions

View File

@ -97,9 +97,12 @@ public class CubeDefinitions {
final String name=file.getName(); final String name=file.getName();
final int index=name.indexOf(CUBE_FILE_EXTENSION); final int index=name.indexOf(CUBE_FILE_EXTENSION);
final InputStream fileInputStream=new FileInputStream(file); final InputStream fileInputStream=new FileInputStream(file);
loadCubeDefinition(name.substring(0,index),fileInputStream); try {
fileInputStream.close(); loadCubeDefinition(name.substring(0,index),fileInputStream);
} finally {
fileInputStream.close();
}
} }
} }

View File

@ -272,13 +272,18 @@ public class GeneralConfig {
public void load() { public void load() {
try { try {
final Properties properties=new Properties(); final Properties properties=new Properties();
properties.load(new FileInputStream(getConfigFile())); final FileInputStream fis = new FileInputStream(getConfigFile());
try {
properties.load(fis);
} finally {
fis.close();
}
load(properties); load(properties);
} catch (final IOException ex) { } catch (final IOException ex) {
System.err.println("ERROR! unable to load " + getConfigFile()); System.err.println("ERROR! unable to load " + getConfigFile());
System.err.println(ex.getMessage()); System.err.println(ex.getMessage());
ex.printStackTrace(); ex.printStackTrace();
} }
} }
public void save(final Properties properties) { public void save(final Properties properties) {
@ -307,7 +312,12 @@ public class GeneralConfig {
try { try {
final Properties properties=new Properties(); final Properties properties=new Properties();
save(properties); save(properties);
properties.store(new FileOutputStream(getConfigFile()),"General configuration"); final FileOutputStream fos = new FileOutputStream(getConfigFile());
try {
properties.store(fos,"General configuration");
} finally {
fos.close();
}
System.err.println("Saved general config"); System.err.println("Saved general config");
} catch (final IOException ex) { } catch (final IOException ex) {
System.err.println("ERROR! unable to save general config"); System.err.println("ERROR! unable to save general config");

View File

@ -268,14 +268,17 @@ public class IconImages {
final byte data[]=new byte[1<<16]; final byte data[]=new byte[1<<16];
int size=0; int size=0;
final InputStream inputStream=IconImages.class.getResourceAsStream("icons/"+name); final InputStream inputStream=IconImages.class.getResourceAsStream("icons/"+name);
while (true) { try {
while (true) {
final int len=inputStream.read(data,size,data.length-size); final int len=inputStream.read(data,size,data.length-size);
if (len<0) { if (len<0) {
break; break;
} }
size+=len; size+=len;
} }
} finally {
inputStream.close();
}
return new ImageIcon(Arrays.copyOf(data,size)); return new ImageIcon(Arrays.copyOf(data,size));
} catch (final Throwable th) { } catch (final Throwable th) {
return MISSING2; return MISSING2;

View File

@ -169,7 +169,12 @@ public class TournamentConfig {
public void load() { public void load() {
try { try {
final Properties properties=new Properties(); final Properties properties=new Properties();
properties.load(new FileInputStream(getConfigFile())); final FileInputStream fis = new FileInputStream(getConfigFile());
try {
properties.load(fis);
} finally {
fis.close();
}
load(properties); load(properties);
System.err.println("Loaded tournament config"); System.err.println("Loaded tournament config");
} catch (final IOException ex) { } catch (final IOException ex) {
@ -193,7 +198,12 @@ public class TournamentConfig {
try { try {
final Properties properties=new Properties(); final Properties properties=new Properties();
save(properties); save(properties);
properties.store(new FileOutputStream(getConfigFile()),"Tournament configuration"); final FileOutputStream fos = new FileOutputStream(getConfigFile());
try {
properties.store(fos,"Tournament configuration");
} finally {
fos.close();
}
System.err.println("Saved tournament config"); System.err.println("Saved tournament config");
} catch (final IOException ex) { } catch (final IOException ex) {
System.err.println("ERROR! Unable to save tournament config"); System.err.println("ERROR! Unable to save tournament config");