Skip to content

Commit 995203f

Browse files
committed
Refactor stream handling
This commit refactors the stream handling by using try-with-resource for opening streams in `ImageLoader` for Cocoa and GTK.
1 parent 413426e commit 995203f

File tree

2 files changed

+10
-38
lines changed

2 files changed

+10
-38
lines changed

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/ImageLoader.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -168,25 +168,17 @@ public ImageData[] load(InputStream stream) {
168168
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
169169
* </ul>
170170
* @exception SWTException <ul>
171-
* <li>ERROR_IO - if an IO error occurs while reading from the file</li>
171+
* <li>ERROR_IO - if an IO error occurs while reading from the file or closing the stream fails</li>
172172
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li>
173173
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
174174
* </ul>
175175
*/
176176
public ImageData[] load(String filename) {
177177
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
178-
InputStream stream = null;
179-
try {
180-
stream = new FileInputStream(filename);
178+
try (InputStream stream = new FileInputStream(filename)) {
181179
return load(stream);
182180
} catch (IOException e) {
183181
SWT.error(SWT.ERROR_IO, e);
184-
} finally {
185-
try {
186-
if (stream != null) stream.close();
187-
} catch (IOException e) {
188-
// Ignore error
189-
}
190182
}
191183
return null;
192184
}
@@ -251,24 +243,18 @@ public void save(OutputStream stream, int format) {
251243
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
252244
* </ul>
253245
* @exception SWTException <ul>
254-
* <li>ERROR_IO - if an IO error occurs while writing to the file</li>
246+
* <li>ERROR_IO - if an IO error occurs while writing to the file or closing the stream fails</li>
255247
* <li>ERROR_INVALID_IMAGE - if the image data contains invalid data</li>
256248
* <li>ERROR_UNSUPPORTED_FORMAT - if the image data cannot be saved to the requested format</li>
257249
* </ul>
258250
*/
259251
public void save(String filename, int format) {
260252
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
261-
OutputStream stream = null;
262-
try {
263-
stream = new FileOutputStream(filename);
253+
try (OutputStream stream = new FileOutputStream(filename)) {
254+
save(stream, format);
264255
} catch (IOException e) {
265256
SWT.error(SWT.ERROR_IO, e);
266257
}
267-
save(stream, format);
268-
try {
269-
stream.close();
270-
} catch (IOException e) {
271-
}
272258
}
273259

274260
/**

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/ImageLoader.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -284,25 +284,17 @@ boolean isInterlacedPNG(byte [] imageAsByteArray) {
284284
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
285285
* </ul>
286286
* @exception SWTException <ul>
287-
* <li>ERROR_IO - if an IO error occurs while reading from the file</li>
287+
* <li>ERROR_IO - if an IO error occurs while reading from the file or closing the stream fails</li>
288288
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li>
289289
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
290290
* </ul>
291291
*/
292292
public ImageData[] load(String filename) {
293293
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
294-
InputStream stream = null;
295-
try {
296-
stream = new FileInputStream(filename);
294+
try (InputStream stream = new FileInputStream(filename)) {
297295
return load(stream);
298296
} catch (IOException e) {
299297
SWT.error(SWT.ERROR_IO, e);
300-
} finally {
301-
try {
302-
if (stream != null) stream.close();
303-
} catch (IOException e) {
304-
// Ignore error
305-
}
306298
}
307299
return null;
308300
}
@@ -604,24 +596,18 @@ public void save(OutputStream stream, int format) {
604596
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
605597
* </ul>
606598
* @exception SWTException <ul>
607-
* <li>ERROR_IO - if an IO error occurs while writing to the file</li>
599+
* <li>ERROR_IO - if an IO error occurs while writing to the file or closing the stream fails</li>
608600
* <li>ERROR_INVALID_IMAGE - if the image data contains invalid data</li>
609601
* <li>ERROR_UNSUPPORTED_FORMAT - if the image data cannot be saved to the requested format</li>
610602
* </ul>
611603
*/
612604
public void save(String filename, int format) {
613605
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
614-
OutputStream stream = null;
615-
try {
616-
stream = new FileOutputStream(filename);
606+
try (OutputStream stream = new FileOutputStream(filename)) {
607+
save(stream, format);
617608
} catch (IOException e) {
618609
SWT.error(SWT.ERROR_IO, e);
619610
}
620-
save(stream, format);
621-
try {
622-
stream.close();
623-
} catch (IOException e) {
624-
}
625611
}
626612

627613
/**

0 commit comments

Comments
 (0)