Skip to content

Commit 8c89228

Browse files
committed
Added try/catch/finally to inputstreams that close the streams properly
1 parent 1b28804 commit 8c89228

File tree

6 files changed

+90
-71
lines changed

6 files changed

+90
-71
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@ src_module/.classpath
44
src_module/.project
55
src_module/theme/
66
src_module/userlib/
7-
src_module/.classpath
8-
src_module/.project
97
*.launch
108
*.tmp
119
*.lock
1210
.idea/
1311

1412
dist/
15-
test/
1613

1714
node_modules/
1815
.editorconfig

src_module/javasource/imagecrop/actions/CopyFileContent.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This file was generated by Mendix Business Modeler.
1+
// This file was generated by Mendix Modeler.
22
//
33
// WARNING: Only the following code will be retained when actions are regenerated:
44
// - the import list
@@ -9,11 +9,10 @@
99

1010
package imagecrop.actions;
1111

12-
import system.proxies.FileDocument;
1312
import com.mendix.core.Core;
1413
import com.mendix.systemwideinterfaces.core.IContext;
15-
import com.mendix.webui.CustomJavaAction;
1614
import com.mendix.systemwideinterfaces.core.IMendixObject;
15+
import com.mendix.webui.CustomJavaAction;
1716

1817
/**
1918
*

src_module/javasource/imagecrop/actions/CreateBWImage.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This file was generated by Mendix Business Modeler.
1+
// This file was generated by Mendix Modeler.
22
//
33
// WARNING: Only the following code will be retained when actions are regenerated:
44
// - the import list
@@ -45,8 +45,10 @@ public CreateBWImage(IContext context, IMendixObject ColorImage, IMendixObject B
4545
public Boolean executeAction() throws Exception
4646
{
4747
// BEGIN USER CODE
48+
InputStream is = null;
49+
InputStream stream = null;
4850
try {
49-
InputStream is = Core.getImage(getContext(), this.ColorImage, false);
51+
is = Core.getImage(getContext(), this.ColorImage, false);
5052
BufferedImage original = ImageIO.read(is);
5153
// BufferedImage binarized = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
5254

@@ -94,12 +96,17 @@ public Boolean executeAction() throws Exception
9496

9597
ByteArrayOutputStream os = new ByteArrayOutputStream();
9698
ImageIO.write(original, formatName, os);
97-
InputStream stream = new ByteArrayInputStream(os.toByteArray());
99+
stream = new ByteArrayInputStream(os.toByteArray());
98100
Core.storeImageDocumentContent(getContext(), this.BWImage, stream,
99101
this.thumbnailWidth.intValue(), this.thumbnailHeight.intValue());
100102
}
101103
catch (IOException e) {
102104
Core.getLogger(this.toString()).error(e);
105+
} finally {
106+
if (is != null)
107+
is.close();
108+
if (stream != null)
109+
stream.close();
103110
}
104111

105112
return true;

src_module/javasource/imagecrop/actions/CropImage.java

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This file was generated by Mendix Business Modeler.
1+
// This file was generated by Mendix Modeler.
22
//
33
// WARNING: Only the following code will be retained when actions are regenerated:
44
// - the import list
@@ -12,6 +12,7 @@
1212
import java.awt.image.BufferedImage;
1313
import java.io.ByteArrayInputStream;
1414
import java.io.ByteArrayOutputStream;
15+
import java.io.IOException;
1516
import java.io.InputStream;
1617
import javax.imageio.ImageIO;
1718
import com.mendix.core.Core;
@@ -22,17 +23,16 @@
2223
/**
2324
*
2425
*/
25-
public class CropImage extends CustomJavaAction<Boolean>
26-
{
26+
public class CropImage extends CustomJavaAction<Boolean> {
2727
private IMendixObject __cropImgObj;
2828
private imagecrop.proxies.CropImage cropImgObj;
2929
private Long imageWidth;
3030
private Long imageHeight;
3131
private Long thumbnailWidth;
3232
private Long thumbnailHeight;
3333

34-
public CropImage(IContext context, IMendixObject cropImgObj, Long imageWidth, Long imageHeight, Long thumbnailWidth, Long thumbnailHeight)
35-
{
34+
public CropImage(IContext context, IMendixObject cropImgObj, Long imageWidth, Long imageHeight, Long thumbnailWidth,
35+
Long thumbnailHeight) {
3636
super(context);
3737
this.__cropImgObj = cropImgObj;
3838
this.imageWidth = imageWidth;
@@ -42,48 +42,59 @@ public CropImage(IContext context, IMendixObject cropImgObj, Long imageWidth, Lo
4242
}
4343

4444
@Override
45-
public Boolean executeAction() throws Exception
46-
{
47-
this.cropImgObj = __cropImgObj == null ? null : imagecrop.proxies.CropImage.initialize(getContext(), __cropImgObj);
45+
public Boolean executeAction() throws Exception {
46+
this.cropImgObj = __cropImgObj == null ? null
47+
: imagecrop.proxies.CropImage.initialize(getContext(), __cropImgObj);
4848

4949
// BEGIN USER CODE
5050
int cropHeight = this.imageHeight.intValue();
5151
int cropWidth = this.imageWidth.intValue();
52-
52+
5353
int x1 = this.cropImgObj.getcrop_x1();
5454
int y1 = this.cropImgObj.getcrop_y1();
5555
int x2 = this.cropImgObj.getcrop_x2();
5656
int y2 = this.cropImgObj.getcrop_y2();
57-
57+
5858
if (x2 > 0 && y2 > 0) {
5959
if (cropHeight == 0 && cropWidth == 0) {
6060
cropHeight = this.cropImgObj.getcrop_height();
6161
cropWidth = this.cropImgObj.getcrop_width();
6262
} else if (cropHeight == 0) {
6363
float ratio = (float) cropWidth / this.cropImgObj.getcrop_width().floatValue();
64-
cropHeight = Math.round(this.cropImgObj.getcrop_height()*ratio);
64+
cropHeight = Math.round(this.cropImgObj.getcrop_height() * ratio);
6565
} else if (cropWidth == 0) {
6666
float ratio = (float) cropHeight / this.cropImgObj.getcrop_height().floatValue();
67-
cropWidth = Math.round(this.cropImgObj.getcrop_width()*ratio);
67+
cropWidth = Math.round(this.cropImgObj.getcrop_width() * ratio);
6868
}
69-
InputStream is = Core.getImage(getContext(), this.cropImgObj.getMendixObject(), false);
70-
BufferedImage originalImage = ImageIO.read(is);
7169

72-
BufferedImage alteredImage = new BufferedImage(cropWidth, cropHeight, BufferedImage.TYPE_INT_RGB);
73-
alteredImage.getGraphics().drawImage(originalImage, 0, 0, cropWidth, cropHeight, x1, y1, x2, y2, null);
70+
InputStream is = null;
71+
InputStream stream = null;
7472

73+
try {
74+
is = Core.getImage(getContext(), this.cropImgObj.getMendixObject(), false);
75+
BufferedImage originalImage = ImageIO.read(is);
7576

76-
String formatName = ScaleImage.getFormatName( Core.getImage(getContext(), this.cropImgObj.getMendixObject(), false) );
77-
78-
ByteArrayOutputStream os = new ByteArrayOutputStream();
79-
ImageIO.write(alteredImage, formatName, os);
80-
InputStream stream = new ByteArrayInputStream(os.toByteArray());
81-
Core.storeImageDocumentContent(getContext(), this.cropImgObj.getMendixObject(), stream,
82-
this.thumbnailWidth.intValue(), this.thumbnailHeight.intValue());
83-
77+
BufferedImage alteredImage = new BufferedImage(cropWidth, cropHeight, BufferedImage.TYPE_INT_RGB);
78+
alteredImage.getGraphics().drawImage(originalImage, 0, 0, cropWidth, cropHeight, x1, y1, x2, y2, null);
79+
80+
String formatName = ScaleImage
81+
.getFormatName(Core.getImage(getContext(), this.cropImgObj.getMendixObject(), false));
82+
83+
ByteArrayOutputStream os = new ByteArrayOutputStream();
84+
ImageIO.write(alteredImage, formatName, os);
85+
stream = new ByteArrayInputStream(os.toByteArray());
86+
Core.storeImageDocumentContent(getContext(), this.cropImgObj.getMendixObject(), stream,
87+
this.thumbnailWidth.intValue(), this.thumbnailHeight.intValue());
88+
} catch (IOException e) {
89+
Core.getLogger(this.toString()).error(e);
90+
} finally {
91+
if (is != null)
92+
is.close();
93+
if (stream != null)
94+
stream.close();
95+
}
8496
return true;
85-
}
86-
else
97+
} else
8798
return false;
8899
// END USER CODE
89100
}
@@ -92,8 +103,7 @@ public Boolean executeAction() throws Exception
92103
* Returns a string representation of this action
93104
*/
94105
@Override
95-
public String toString()
96-
{
106+
public String toString() {
97107
return "CropImage";
98108
}
99109

src_module/javasource/imagecrop/actions/ScaleImage.java

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This file was generated by Mendix Business Modeler.
1+
// This file was generated by Mendix Modeler.
22
//
33
// WARNING: Only the following code will be retained when actions are regenerated:
44
// - the import list
@@ -26,53 +26,61 @@
2626
/**
2727
*
2828
*/
29-
public class ScaleImage extends CustomJavaAction<Boolean>
30-
{
29+
public class ScaleImage extends CustomJavaAction<Boolean> {
3130
private IMendixObject __cropImgObj;
3231
private imagecrop.proxies.CropImage cropImgObj;
3332
private Long thumbnailWidth;
3433
private Long thumbnailHeight;
3534

36-
public ScaleImage(IContext context, IMendixObject cropImgObj, Long thumbnailWidth, Long thumbnailHeight)
37-
{
35+
public ScaleImage(IContext context, IMendixObject cropImgObj, Long thumbnailWidth, Long thumbnailHeight) {
3836
super(context);
3937
this.__cropImgObj = cropImgObj;
4038
this.thumbnailWidth = thumbnailWidth;
4139
this.thumbnailHeight = thumbnailHeight;
4240
}
4341

4442
@Override
45-
public Boolean executeAction() throws Exception
46-
{
47-
this.cropImgObj = __cropImgObj == null ? null : imagecrop.proxies.CropImage.initialize(getContext(), __cropImgObj);
43+
public Boolean executeAction() throws Exception {
44+
this.cropImgObj = __cropImgObj == null ? null
45+
: imagecrop.proxies.CropImage.initialize(getContext(), __cropImgObj);
4846

4947
// BEGIN USER CODE
5048
int x1 = this.cropImgObj.getcrop_x1();
5149
int x2 = this.cropImgObj.getcrop_x2();
5250
int y1 = this.cropImgObj.getcrop_y1();
5351
int y2 = this.cropImgObj.getcrop_y2();
5452

55-
if ( x2 > 0 && y2 > 0 ) {
53+
if (x2 > 0 && y2 > 0) {
5654
int newWidth = x2 - x1, newHeight = y2 - y1;
57-
58-
InputStream is = Core.getImage(getContext(), this.cropImgObj.getMendixObject(), false);
59-
BufferedImage originalImage = ImageIO.read(is);
60-
61-
BufferedImage alteredImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
62-
alteredImage.getGraphics().drawImage(originalImage, 0, 0, newWidth, newHeight, null);
63-
6455

65-
String formatName = getFormatName( Core.getImage(getContext(), this.cropImgObj.getMendixObject(), false) );
56+
InputStream is = null;
57+
InputStream stream = null;
6658

67-
ByteArrayOutputStream os = new ByteArrayOutputStream();
68-
ImageIO.write(alteredImage, formatName, os);
69-
InputStream stream = new ByteArrayInputStream(os.toByteArray());
70-
Core.storeImageDocumentContent(getContext(), this.cropImgObj.getMendixObject(), stream,
71-
this.thumbnailWidth.intValue(), this.thumbnailHeight.intValue());
59+
try {
60+
is = Core.getImage(getContext(), this.cropImgObj.getMendixObject(), false);
61+
BufferedImage originalImage = ImageIO.read(is);
7262

63+
BufferedImage alteredImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
64+
alteredImage.getGraphics().drawImage(originalImage, 0, 0, newWidth, newHeight, null);
65+
66+
String formatName = getFormatName(
67+
Core.getImage(getContext(), this.cropImgObj.getMendixObject(), false));
68+
69+
ByteArrayOutputStream os = new ByteArrayOutputStream();
70+
ImageIO.write(alteredImage, formatName, os);
71+
stream = new ByteArrayInputStream(os.toByteArray());
72+
Core.storeImageDocumentContent(getContext(), this.cropImgObj.getMendixObject(), stream,
73+
this.thumbnailWidth.intValue(), this.thumbnailHeight.intValue());
74+
} catch (IOException e) {
75+
Core.getLogger(this.toString()).error(e);
76+
} finally {
77+
if (is != null)
78+
is.close();
79+
if (stream != null)
80+
stream.close();
81+
}
7382
return true;
74-
}
75-
else
83+
} else
7684
return false;
7785
// END USER CODE
7886
}
@@ -81,30 +89,28 @@ public Boolean executeAction() throws Exception
8189
* Returns a string representation of this action
8290
*/
8391
@Override
84-
public String toString()
85-
{
92+
public String toString() {
8693
return "ScaleImage";
8794
}
8895

8996
// BEGIN EXTRA CODE
90-
public static String getFormatName( Object o ) {
97+
public static String getFormatName(Object o) {
9198
try {
9299
ImageInputStream iis = ImageIO.createImageInputStream(o);
93100
Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
94-
if ( !iter.hasNext() ) {
101+
if (!iter.hasNext()) {
95102
return null;
96103
}
97104
ImageReader reader = (ImageReader) iter.next();
98105
iis.close();
99106
String formatName = reader.getFormatName();
100-
if( formatName == null )
107+
if (formatName == null)
101108
formatName = "jpeg";
102-
109+
103110
return formatName;
111+
} catch (IOException e) {
104112
}
105-
catch( IOException e ) {
106-
}
107-
113+
108114
return "jpeg";
109115
}
110116
// END EXTRA CODE

src_module/javasource/imagecrop/actions/SetInitialImageProps.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This file was generated by Mendix Business Modeler.
1+
// This file was generated by Mendix Modeler.
22
//
33
// WARNING: Only the following code will be retained when actions are regenerated:
44
// - the import list

0 commit comments

Comments
 (0)