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

@ -98,10 +98,13 @@ public class CubeDefinitions {
final String name=file.getName();
final int index=name.indexOf(CUBE_FILE_EXTENSION);
final InputStream fileInputStream=new FileInputStream(file);
try {
loadCubeDefinition(name.substring(0,index),fileInputStream);
} finally {
fileInputStream.close();
}
}
}
System.err.println(cubeDefinitions.size()+" cube definitions");
for (final MagicCubeDefinition cubeDefinition : cubeDefinitions) {

View File

@ -272,7 +272,12 @@ public class GeneralConfig {
public void load() {
try {
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);
} catch (final IOException ex) {
System.err.println("ERROR! unable to load " + getConfigFile());
@ -307,7 +312,12 @@ public class GeneralConfig {
try {
final Properties properties=new 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");
} catch (final IOException ex) {
System.err.println("ERROR! unable to save general config");

View File

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

View File

@ -169,7 +169,12 @@ public class TournamentConfig {
public void load() {
try {
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);
System.err.println("Loaded tournament config");
} catch (final IOException ex) {
@ -193,7 +198,12 @@ public class TournamentConfig {
try {
final Properties properties=new 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");
} catch (final IOException ex) {
System.err.println("ERROR! Unable to save tournament config");