Skip to content

Commit

Permalink
ImageLoader: Use try-with-resource
Browse files Browse the repository at this point in the history
Doesn't try to fix anything. Just code and warnings reduced.
  • Loading branch information
EcljpseB0T authored and jukzi committed Jun 13, 2023
1 parent 28e331b commit 6611444
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,10 @@ public ImageData[] load(InputStream stream) {
*/
public ImageData[] load(String filename) {
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
InputStream stream = null;
try {
stream = new FileInputStream(filename);
try (InputStream stream = new FileInputStream(filename)) {
return load(stream);
} catch (IOException e) {
SWT.error(SWT.ERROR_IO, e);
} finally {
try {
if (stream != null) stream.close();
} catch (IOException e) {
// Ignore error
}
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,31 +186,20 @@ protected boolean validate(Object object) {
/* shared methods for converting instances of MyType <-> byte[] */

static byte[] convertToByteArray(MyType type) {
DataOutputStream dataOutStream = null;
try {
ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
dataOutStream = new DataOutputStream(byteOutStream);
try (ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
DataOutputStream dataOutStream = new DataOutputStream(byteOutStream)) {
byte[] bytes = type.name.getBytes();
dataOutStream.writeInt(bytes.length);
dataOutStream.write(bytes);
dataOutStream.writeLong(type.time);
return byteOutStream.toByteArray();
} catch (IOException e) {
return null;
} finally {
if (dataOutStream != null) {
try {
dataOutStream.close();
} catch (IOException e) {}
}
}
}

static MyType restoreFromByteArray(byte[] bytes) {
DataInputStream dataInStream = null;
try {
ByteArrayInputStream byteInStream = new ByteArrayInputStream(bytes);
dataInStream = new DataInputStream(byteInStream);
try (DataInputStream dataInStream = new DataInputStream(new ByteArrayInputStream(bytes))) {
int size = dataInStream.readInt();
byte[] name = new byte[size];
dataInStream.read(name);
Expand All @@ -220,12 +209,6 @@ static MyType restoreFromByteArray(byte[] bytes) {
return result;
} catch (IOException ex) {
return null;
} finally {
if (dataInStream != null) {
try {
dataInStream.close();
} catch (IOException e) {}
}
}
}

Expand Down

0 comments on commit 6611444

Please sign in to comment.