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