From 66114446d16cfdc8bb0de0c2677821132f9c7f36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Kubitz?= Date: Mon, 12 Jun 2023 13:57:22 +0200 Subject: [PATCH] ImageLoader: Use try-with-resource Doesn't try to fix anything. Just code and warnings reduced. --- .../org/eclipse/swt/graphics/ImageLoader.java | 10 +------- .../org/eclipse/swt/snippets/Snippet319.java | 23 +++---------------- 2 files changed, 4 insertions(+), 29 deletions(-) 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 bbe5ac8d518..f309c6186a0 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 @@ -176,18 +176,10 @@ public ImageData[] load(InputStream stream) { */ 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; } diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet319.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet319.java index 829621c5777..12af2f03a8b 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet319.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet319.java @@ -186,10 +186,8 @@ protected boolean validate(Object object) { /* shared methods for converting instances of MyType <-> byte[] */ static byte[] convertToByteArray(MyType type) { - DataOutputStream dataOutStream = null; - try { - ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream(); - dataOutStream = new DataOutputStream(byteOutStream); + try (ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream(); + DataOutputStream dataOutStream = new DataOutputStream(byteOutStream)) { byte[] bytes = type.name.getBytes(); dataOutStream.writeInt(bytes.length); dataOutStream.write(bytes); @@ -197,20 +195,11 @@ static byte[] convertToByteArray(MyType type) { return byteOutStream.toByteArray(); } catch (IOException e) { return null; - } finally { - if (dataOutStream != null) { - try { - dataOutStream.close(); - } catch (IOException e) {} - } } } static MyType restoreFromByteArray(byte[] bytes) { - DataInputStream dataInStream = null; - try { - ByteArrayInputStream byteInStream = new ByteArrayInputStream(bytes); - dataInStream = new DataInputStream(byteInStream); + try (DataInputStream dataInStream = new DataInputStream(new ByteArrayInputStream(bytes))) { int size = dataInStream.readInt(); byte[] name = new byte[size]; dataInStream.read(name); @@ -220,12 +209,6 @@ static MyType restoreFromByteArray(byte[] bytes) { return result; } catch (IOException ex) { return null; - } finally { - if (dataInStream != null) { - try { - dataInStream.close(); - } catch (IOException e) {} - } } }