Skip to content

Commit

Permalink
Moved all streams into a try with resources setup
Browse files Browse the repository at this point in the history
Co-authored-by: Patrick Ziegler <ziegler.patrick@hotmail.de>
  • Loading branch information
azoitl and ptziegler committed Oct 20, 2024
1 parent 7181ef2 commit d3b580c
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2004, 2010 IBM Corporation and others.
* Copyright (c) 2004, 2024 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -13,29 +13,21 @@

package org.eclipse.draw2d.test;

import java.io.InputStream;

import org.eclipse.swt.graphics.Image;

public class TestImages {

static final Image depth_8;
static final Image depth_24;

static {
InputStream is = TestImages.class.getResourceAsStream("icons/bits8.gif"); //$NON-NLS-1$
depth_8 = new Image(null, is);
try {
is.close();
} catch (Exception e) {
}

is = TestImages.class.getResourceAsStream("icons/bits24.jpg"); //$NON-NLS-1$
depth_24 = new Image(null, is);
try {
is.close();
} catch (Exception e) {
}
import org.eclipse.jface.resource.ImageDescriptor;

public final class TestImages {

static final Image depth_8 = loadImage("icons/bits8.jpg"); //$NON-NLS-1$
static final Image depth_24 = loadImage("icons/bits24.jpg"); //$NON-NLS-1$

private static Image loadImage(final String imageName) {
return ImageDescriptor.createFromFile(TestImages.class, imageName).createImage();
}

private TestImages() {
throw new UnsupportedOperationException("Utility class shall not be instantiated!"); //$NON-NLS-1$
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,10 @@ private static ActivityDiagram createWakeupModel() {
protected InputStream getInitialContents() {
ActivityDiagram diag = createWakeupModel();
ByteArrayInputStream bais = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);) {
oos.writeObject(diag);
oos.flush();
oos.close();
baos.close();
bais = new ByteArrayInputStream(baos.toByteArray());
bais.close();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2022 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand Down Expand Up @@ -486,9 +486,9 @@ protected void configureZoomManager(ScrollingGraphicalViewer viewer, ZoomManager
}

protected void writeToOutputStream(OutputStream os) throws IOException {
ObjectOutputStream out = new ObjectOutputStream(os);
out.writeObject(getLogicDiagram());
out.close();
try (ObjectOutputStream out = new ObjectOutputStream(os)) {
out.writeObject(getLogicDiagram());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2022 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand Down Expand Up @@ -41,7 +41,7 @@

public class LogicWizardPage1 extends WizardNewFileCreationPage implements SelectionListener {

private IWorkbench workbench;
private final IWorkbench workbench;
private static int exampleCount = 1;
private Button model1 = null;
private Button model2 = null;
Expand Down Expand Up @@ -90,13 +90,10 @@ protected InputStream getInitialContents() {
ld = (LogicDiagram) LogicDiagramFactory.createLargeModel();
}
ByteArrayInputStream bais = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);) {
oos.writeObject(ld);
oos.flush();
oos.close();
baos.close();
bais = new ByteArrayInputStream(baos.toByteArray());
bais.close();
} catch (Exception e) {
Expand All @@ -107,8 +104,7 @@ protected InputStream getInitialContents() {

public boolean finish() {
IFile newFile = createNewFile();
if (newFile == null)
{
if (newFile == null) {
return false; // ie.- creation was unsuccessful
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2022 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -12,15 +12,14 @@
*******************************************************************************/
package org.eclipse.gef.examples.logicdesigner.model;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.graphics.Image;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.PropertyDescriptor;

Expand Down Expand Up @@ -51,13 +50,7 @@ public abstract class LogicSubpart extends LogicElement {
}

protected static Image createImage(Class rsrcClass, String name) {
InputStream stream = rsrcClass.getResourceAsStream(name);
Image image = new Image(null, stream);
try {
stream.close();
} catch (IOException ioe) {
}
return image;
return ImageDescriptor.createFromFile(rsrcClass, name).createImage();
}

protected LogicSubpart() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2004, 2023 Elias Volanakis and others.
* Copyright (c) 2004, 2024 Elias Volanakis and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand Down Expand Up @@ -149,13 +149,11 @@ boolean finish() {
@Override
protected InputStream getInitialContents() {
ByteArrayInputStream bais = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(createDefaultContent()); // argument must be
// Serializable
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);) {
// argument must be Serializable
oos.writeObject(createDefaultContent());
oos.flush();
oos.close();
bais = new ByteArrayInputStream(baos.toByteArray());
} catch (IOException ioe) {
ioe.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2004, 2023 Elias Volanakis and others.
* Copyright (c) 2004, 2024 Elias Volanakis and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand Down Expand Up @@ -123,9 +123,9 @@ public void commandStackChanged(EventObject event) {
}

private void createOutputStream(OutputStream os) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(getModel());
oos.close();
try (ObjectOutputStream oos = new ObjectOutputStream(os)) {
oos.writeObject(getModel());
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2004, 2023 Elias Volanakis and others.
* Copyright (c) 2004, 2024 Elias Volanakis and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -12,13 +12,12 @@
*******************************************************************************/
package org.eclipse.gef.examples.shapes.model;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.graphics.Image;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;
Expand Down Expand Up @@ -90,8 +89,10 @@ public abstract class Shape extends ModelElement {
* @see #setPropertyValue(Object, Object)
*/
static {
descriptors = new IPropertyDescriptor[] { new TextPropertyDescriptor(XPOS_PROP, ShapesExampleMessages.Shape_X), // id and description pair
new TextPropertyDescriptor(YPOS_PROP, ShapesExampleMessages.Shape_Y), new TextPropertyDescriptor(WIDTH_PROP, ShapesExampleMessages.Shape_Width),
// id and description pair
descriptors = new IPropertyDescriptor[] { new TextPropertyDescriptor(XPOS_PROP, ShapesExampleMessages.Shape_X),
new TextPropertyDescriptor(YPOS_PROP, ShapesExampleMessages.Shape_Y),
new TextPropertyDescriptor(WIDTH_PROP, ShapesExampleMessages.Shape_Width),
new TextPropertyDescriptor(HEIGHT_PROP, ShapesExampleMessages.Shape_Height), };
// use a custom cell editor validator for all four array entries
for (IPropertyDescriptor descriptor : descriptors) {
Expand All @@ -108,13 +109,7 @@ public abstract class Shape extends ModelElement {
} // static

protected static Image createImage(String name) {
InputStream stream = ShapesPlugin.class.getResourceAsStream(name);
Image image = new Image(null, stream);
try {
stream.close();
} catch (IOException ioe) {
}
return image;
return ImageDescriptor.createFromFile(ShapesPlugin.class, name).createImage();
}

/** Location of this shape. */
Expand Down

0 comments on commit d3b580c

Please sign in to comment.