Skip to content

Commit 88d5b53

Browse files
Michael5601HannesWell
authored andcommitted
Refactor file-stream handling in ImageLoader
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).
1 parent 5c2611d commit 88d5b53

File tree

3 files changed

+12
-40
lines changed

3 files changed

+12
-40
lines changed

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

+5-19
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void reset() {
143143
* <li>ERROR_NULL_ARGUMENT - if the stream is null</li>
144144
* </ul>
145145
* @exception SWTException <ul>
146-
* <li>ERROR_IO - if an IO error occurs while reading from the stream</li>
146+
* <li>ERROR_IO - if an IO error occurs while reading the stream</li>
147147
* <li>ERROR_INVALID_IMAGE - if the image stream contains invalid data</li>
148148
* <li>ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format</li>
149149
* </ul>
@@ -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 the file</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
}
@@ -258,17 +250,11 @@ public void save(OutputStream stream, int format) {
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

+5-19
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void reset() {
153153
* <li>ERROR_NULL_ARGUMENT - if the stream is null</li>
154154
* </ul>
155155
* @exception SWTException <ul>
156-
* <li>ERROR_IO - if an IO error occurs while reading from the stream</li>
156+
* <li>ERROR_IO - if an IO error occurs while reading the stream</li>
157157
* <li>ERROR_INVALID_IMAGE - if the image stream contains invalid data</li>
158158
* <li>ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format</li>
159159
* </ul>
@@ -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 the file</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
}
@@ -611,17 +603,11 @@ public void save(OutputStream stream, int format) {
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
/**

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void reset() {
143143
* <li>ERROR_NULL_ARGUMENT - if the stream is null</li>
144144
* </ul>
145145
* @exception SWTException <ul>
146-
* <li>ERROR_IO - if an IO error occurs while reading from the stream</li>
146+
* <li>ERROR_IO - if an IO error occurs while reading the stream</li>
147147
* <li>ERROR_INVALID_IMAGE - if the image stream contains invalid data</li>
148148
* <li>ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format</li>
149149
* </ul>
@@ -168,7 +168,7 @@ 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 the file</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>

0 commit comments

Comments
 (0)