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 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);
try {
loadCubeDefinition(name.substring(0,index),fileInputStream); loadCubeDefinition(name.substring(0,index),fileInputStream);
} finally {
fileInputStream.close(); fileInputStream.close();
} }
} }
}
System.err.println(cubeDefinitions.size()+" cube definitions"); System.err.println(cubeDefinitions.size()+" cube definitions");
for (final MagicCubeDefinition cubeDefinition : cubeDefinitions) { for (final MagicCubeDefinition cubeDefinition : cubeDefinitions) {

View File

@ -272,7 +272,12 @@ 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());
@ -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,13 +268,16 @@ 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);
try {
while (true) { 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) {

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