From 995203f7879cb538ca2a38a8453c540bd8653264 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Fri, 24 Jan 2025 10:43:25 +0100 Subject: [PATCH] Refactor stream handling This commit refactors the stream handling by using try-with-resource for opening streams in `ImageLoader` for Cocoa and GTK. --- .../org/eclipse/swt/graphics/ImageLoader.java | 24 ++++--------------- .../org/eclipse/swt/graphics/ImageLoader.java | 24 ++++--------------- 2 files changed, 10 insertions(+), 38 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..569d2d0e7f4 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 @@ -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; } @@ -251,24 +243,18 @@ public void save(OutputStream stream, int format) { *
  • ERROR_NULL_ARGUMENT - if the file name is null
  • * * @exception SWTException */ 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..3fd9614c586 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 @@ -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; } @@ -604,24 +596,18 @@ public void save(OutputStream stream, int format) { *
  • ERROR_NULL_ARGUMENT - if the file name is null
  • * * @exception SWTException */ 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) { - } } /**