From 67767346cb402683af48b86e89c9dd82c3422553 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Fri, 24 Jan 2025 10:43:25 +0100 Subject: [PATCH] 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). --- .../org/eclipse/swt/graphics/ImageLoader.java | 24 ++++--------------- .../org/eclipse/swt/graphics/ImageLoader.java | 24 ++++--------------- .../org/eclipse/swt/graphics/ImageLoader.java | 4 ++-- 3 files changed, 12 insertions(+), 40 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/ImageLoader.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/ImageLoader.java index 5b063ab2e78..376d18316f0 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/ImageLoader.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/ImageLoader.java @@ -143,7 +143,7 @@ void reset() { *
  • ERROR_NULL_ARGUMENT - if the stream is null
  • * * @exception SWTException @@ -168,25 +168,17 @@ public ImageData[] load(InputStream stream) { *
  • ERROR_NULL_ARGUMENT - if the file name is null
  • * * @exception SWTException */ 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; } @@ -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) { - } } /** diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/ImageLoader.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/ImageLoader.java index 4fbda1029d7..67336de12bc 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/ImageLoader.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/ImageLoader.java @@ -153,7 +153,7 @@ void reset() { *
  • ERROR_NULL_ARGUMENT - if the stream is null
  • * * @exception SWTException @@ -284,25 +284,17 @@ boolean isInterlacedPNG(byte [] imageAsByteArray) { *
  • ERROR_NULL_ARGUMENT - if the file name is null
  • * * @exception SWTException */ 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; } @@ -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) { - } } /** diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java index a8e4c2f684c..da78f52d244 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java @@ -143,7 +143,7 @@ void reset() { *
  • ERROR_NULL_ARGUMENT - if the stream is null
  • * * @exception SWTException @@ -168,7 +168,7 @@ public ImageData[] load(InputStream stream) { *
  • ERROR_NULL_ARGUMENT - if the file name is null
  • * * @exception SWTException