Skip to content

Commit

Permalink
Refactor stream handling
Browse files Browse the repository at this point in the history
This commit refactors the stream handling by using try-with-resource for opening streams in `ImageLoader` for Cocoa and GTK.
  • Loading branch information
Michael5601 committed Jan 24, 2025
1 parent 413426e commit 995203f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 from the file or closing the stream fails</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 @@ -251,24 +243,18 @@ public void save(OutputStream stream, int format) {
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_IO - if an IO error occurs while writing to the file</li>
* <li>ERROR_IO - if an IO error occurs while writing to the file or closing the stream fails</li>
* <li>ERROR_INVALID_IMAGE - if the image data contains invalid data</li>
* <li>ERROR_UNSUPPORTED_FORMAT - if the image data cannot be saved to the requested format</li>
* </ul>
*/
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 @@ -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 from the file or closing the stream fails</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 @@ -604,24 +596,18 @@ public void save(OutputStream stream, int format) {
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_IO - if an IO error occurs while writing to the file</li>
* <li>ERROR_IO - if an IO error occurs while writing to the file or closing the stream fails</li>
* <li>ERROR_INVALID_IMAGE - if the image data contains invalid data</li>
* <li>ERROR_UNSUPPORTED_FORMAT - if the image data cannot be saved to the requested format</li>
* </ul>
*/
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

0 comments on commit 995203f

Please sign in to comment.