Close stream on error and log exception

This commit is contained in:
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,38 +123,30 @@ public class GB {
dir.mkdirs(); dir.mkdirs();
} }
} }
FileOutputStream fos; try (FileOutputStream fos = new FileOutputStream(dir + "/" + filename)) {
try { ByteBuffer headerbuf = ByteBuffer.allocate(FILE_HEADER_SIZE + INFO_HEADER_SIZE + screenshot.clut.length);
fos = new FileOutputStream(dir + "/" + filename); headerbuf.order(ByteOrder.LITTLE_ENDIAN);
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
ByteBuffer headerbuf = ByteBuffer.allocate(FILE_HEADER_SIZE + INFO_HEADER_SIZE + screenshot.clut.length); // file header
headerbuf.order(ByteOrder.LITTLE_ENDIAN); headerbuf.put((byte) 'B');
headerbuf.put((byte) 'M');
headerbuf.putInt(0); // size in bytes (unconpressed = 0)
headerbuf.putInt(0); // reserved
headerbuf.putInt(FILE_HEADER_SIZE + INFO_HEADER_SIZE + screenshot.clut.length);
// file header // info header
headerbuf.put((byte) 'B'); headerbuf.putInt(INFO_HEADER_SIZE);
headerbuf.put((byte) 'M'); headerbuf.putInt(screenshot.width);
headerbuf.putInt(0); // size in bytes (unconpressed = 0) headerbuf.putInt(-screenshot.height);
headerbuf.putInt(0); // reserved headerbuf.putShort((short) 1); // planes
headerbuf.putInt(FILE_HEADER_SIZE + INFO_HEADER_SIZE + screenshot.clut.length); headerbuf.putShort((short) 1); // bit count
headerbuf.putInt(0); // compression
// info header headerbuf.putInt(0); // length of pixeldata in byte (uncompressed=0)
headerbuf.putInt(INFO_HEADER_SIZE); headerbuf.putInt(0); // pixels per meter (x)
headerbuf.putInt(screenshot.width); headerbuf.putInt(0); // pixels per meter (y)
headerbuf.putInt(-screenshot.height); headerbuf.putInt(2); // number of colors in CLUT
headerbuf.putShort((short) 1); // planes headerbuf.putInt(2); // numbers of used colors
headerbuf.putShort((short) 1); // bit count headerbuf.put(screenshot.clut);
headerbuf.putInt(0); // compression
headerbuf.putInt(0); // length of pixeldata in byte (uncompressed=0)
headerbuf.putInt(0); // pixels per meter (x)
headerbuf.putInt(0); // pixels per meter (y)
headerbuf.putInt(2); // number of colors in CLUT
headerbuf.putInt(2); // numbers of used colors
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;
} }
} }