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 int index=name.indexOf(CUBE_FILE_EXTENSION);
final InputStream fileInputStream=new FileInputStream(file);
loadCubeDefinition(name.substring(0,index),fileInputStream);
fileInputStream.close();
final InputStream fileInputStream=new FileInputStream(file);
try {
loadCubeDefinition(name.substring(0,index),fileInputStream);
} finally {
fileInputStream.close();
}
}
}

View File

@ -272,13 +272,18 @@ 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());
System.err.println(ex.getMessage());
ex.printStackTrace();
}
}
}
public void save(final Properties properties) {
@ -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,14 +268,17 @@ public class IconImages {
final byte data[]=new byte[1<<16];
int size=0;
final InputStream inputStream=IconImages.class.getResourceAsStream("icons/"+name);
while (true) {
final int len=inputStream.read(data,size,data.length-size);
if (len<0) {
break;
}
size+=len;
}
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) {
return MISSING2;

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");