Skip to content

Commit

Permalink
Refactor file-stream handling in ImageLoader
Browse files Browse the repository at this point in the history
Refactor the file-stream handling by using try-with-resource for opening
and closing them in `ImageLoader` for Cocoa and GTK (Windows already
uses it).
  • Loading branch information
Michael5601 authored and HannesWell committed Jan 24, 2025
1 parent 5c2611d commit 88d5b53
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void reset() {
* <li>ERROR_NULL_ARGUMENT - if the stream is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_IO - if an IO error occurs while reading from the stream</li>
* <li>ERROR_IO - if an IO error occurs while reading the stream</li>
* <li>ERROR_INVALID_IMAGE - if the image stream contains invalid data</li>
* <li>ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format</li>
* </ul>
Expand All @@ -168,25 +168,17 @@ public ImageData[] load(InputStream stream) {
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_IO - if an IO error occurs while reading from the file</li>
* <li>ERROR_IO - if an IO error occurs while reading the file</li>
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li>
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
* </ul>
*/
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 Expand Up @@ -258,17 +250,11 @@ public void save(OutputStream stream, int format) {
*/
public void save(String filename, int format) {
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
OutputStream stream = null;
try {
stream = new FileOutputStream(filename);
try (OutputStream stream = new FileOutputStream(filename)) {
save(stream, format);
} catch (IOException e) {
SWT.error(SWT.ERROR_IO, e);
}
save(stream, format);
try {
stream.close();
} catch (IOException e) {
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void reset() {
* <li>ERROR_NULL_ARGUMENT - if the stream is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_IO - if an IO error occurs while reading from the stream</li>
* <li>ERROR_IO - if an IO error occurs while reading the stream</li>
* <li>ERROR_INVALID_IMAGE - if the image stream contains invalid data</li>
* <li>ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format</li>
* </ul>
Expand Down Expand Up @@ -284,25 +284,17 @@ boolean isInterlacedPNG(byte [] imageAsByteArray) {
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_IO - if an IO error occurs while reading from the file</li>
* <li>ERROR_IO - if an IO error occurs while reading the file</li>
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li>
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
* </ul>
*/
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 Expand Up @@ -611,17 +603,11 @@ public void save(OutputStream stream, int format) {
*/
public void save(String filename, int format) {
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
OutputStream stream = null;
try {
stream = new FileOutputStream(filename);
try (OutputStream stream = new FileOutputStream(filename)) {
save(stream, format);
} catch (IOException e) {
SWT.error(SWT.ERROR_IO, e);
}
save(stream, format);
try {
stream.close();
} catch (IOException e) {
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void reset() {
* <li>ERROR_NULL_ARGUMENT - if the stream is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_IO - if an IO error occurs while reading from the stream</li>
* <li>ERROR_IO - if an IO error occurs while reading the stream</li>
* <li>ERROR_INVALID_IMAGE - if the image stream contains invalid data</li>
* <li>ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format</li>
* </ul>
Expand All @@ -168,7 +168,7 @@ public ImageData[] load(InputStream stream) {
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_IO - if an IO error occurs while reading from the file</li>
* <li>ERROR_IO - if an IO error occurs while reading the file</li>
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li>
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
* </ul>
Expand Down

0 comments on commit 88d5b53

Please sign in to comment.