Close stream on error and log exception

master
cpfeiffer 2015-06-29 22:38:37 +02:00
parent bf29814294
commit ef2bbf13c7
1 changed files with 24 additions and 32 deletions

View File

@ -123,14 +123,7 @@ public class GB {
dir.mkdirs();
}
}
FileOutputStream fos;
try {
fos = new FileOutputStream(dir + "/" + filename);
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
try (FileOutputStream fos = new FileOutputStream(dir + "/" + filename)) {
ByteBuffer headerbuf = ByteBuffer.allocate(FILE_HEADER_SIZE + INFO_HEADER_SIZE + screenshot.clut.length);
headerbuf.order(ByteOrder.LITTLE_ENDIAN);
@ -154,7 +147,6 @@ public class GB {
headerbuf.putInt(2); // number of colors in CLUT
headerbuf.putInt(2); // numbers of used colors
headerbuf.put(screenshot.clut);
try {
fos.write(headerbuf.array());
int rowbytes = screenshot.width / 8;
byte[] pad = new byte[rowbytes % 4];
@ -162,11 +154,11 @@ public class GB {
fos.write(screenshot.data, rowbytes * i, rowbytes);
fos.write(pad);
}
fos.close();
} catch (IOException e) {
e.printStackTrace();
LOG.error("Error saving screenshot", e);
return false;
}
return true;
}
}