Skip to content

Commit 6850fa3

Browse files
committed
XGC-148: use try-with-resources instead of IOUtils.closeQuietly
Fixes https://issues.apache.org/jira/browse/XGC-148
1 parent 8b25557 commit 6850fa3

20 files changed

Lines changed: 81 additions & 139 deletions

examples/java/image/writer/ImageWriterExample1.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.io.OutputStream;
3030
import java.text.AttributedString;
3131

32-
import org.apache.commons.io.IOUtils;
3332
import org.apache.xmlgraphics.image.writer.ImageWriter;
3433
import org.apache.xmlgraphics.image.writer.ImageWriterParams;
3534
import org.apache.xmlgraphics.image.writer.ImageWriterRegistry;
@@ -106,18 +105,15 @@ public void generateBitmapUsingJava2D(File outputFile, String format)
106105
//Paint something
107106
paintSome(g2d, 1);
108107

109-
OutputStream out = new java.io.FileOutputStream(outputFile);
110-
out = new java.io.BufferedOutputStream(out);
111-
try {
108+
try (OutputStream fout = new java.io.FileOutputStream(outputFile);
109+
OutputStream out = new java.io.BufferedOutputStream(fout)) {
112110

113111
ImageWriter writer = ImageWriterRegistry.getInstance().getWriterFor(format);
114112
ImageWriterParams params = new ImageWriterParams();
115113
params.setCompressionMethod(compression);
116114
params.setResolution(72);
117115
writer.writeImage(bimg, out, params);
118116

119-
} finally {
120-
IOUtils.closeQuietly(out);
121117
}
122118
}
123119

examples/java/image/writer/ImageWriterExample2.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ public void generateBitmapUsingJava2D(File outputFile, String format)
6767
//String compression = "CCITT T.6";
6868
String compression = "PackBits";
6969

70-
OutputStream out = new java.io.FileOutputStream(outputFile);
71-
out = new java.io.BufferedOutputStream(out);
72-
try {
70+
try (OutputStream fout = new java.io.FileOutputStream(outputFile);
71+
OutputStream out = new java.io.BufferedOutputStream(fout)) {
7372

7473
ImageWriter writer = ImageWriterRegistry.getInstance().getWriterFor(format);
7574
ImageWriterParams params = new ImageWriterParams();
@@ -86,8 +85,6 @@ public void generateBitmapUsingJava2D(File outputFile, String format)
8685
+ format);
8786
}
8887

89-
} finally {
90-
IOUtils.closeQuietly(out);
9188
}
9289
}
9390

examples/java/java2d/ps/EPSColorsExample.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
import java.io.IOException;
2626
import java.io.OutputStream;
2727

28-
import org.apache.commons.io.IOUtils;
29-
3028
import org.apache.xmlgraphics.java2d.color.CIELabColorSpace;
3129
import org.apache.xmlgraphics.java2d.color.ColorSpaces;
3230
import org.apache.xmlgraphics.java2d.color.DeviceCMYKColorSpace;
@@ -45,9 +43,8 @@ public class EPSColorsExample {
4543
* @throws IOException In case of an I/O error
4644
*/
4745
public static void generateEPSusingJava2D(File outputFile) throws IOException {
48-
OutputStream out = new java.io.FileOutputStream(outputFile);
49-
out = new java.io.BufferedOutputStream(out);
50-
try {
46+
try (OutputStream fout = new java.io.FileOutputStream(outputFile);
47+
OutputStream out = new java.io.BufferedOutputStream(fout)) {
5148
//Instantiate the EPSDocumentGraphics2D instance
5249
EPSDocumentGraphics2D g2d = new EPSDocumentGraphics2D(false);
5350
g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
@@ -96,8 +93,6 @@ public static void generateEPSusingJava2D(File outputFile) throws IOException {
9693

9794
//Cleanup
9895
g2d.finish();
99-
} finally {
100-
IOUtils.closeQuietly(out);
10196
}
10297
}
10398

examples/java/java2d/ps/EPSExample1.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.io.IOException;
2727
import java.io.OutputStream;
2828

29-
import org.apache.commons.io.IOUtils;
3029
import org.apache.xmlgraphics.java2d.ps.EPSDocumentGraphics2D;
3130

3231
/**
@@ -42,9 +41,8 @@ public class EPSExample1 {
4241
* @throws IOException In case of an I/O error
4342
*/
4443
public static void generateEPSusingJava2D(File outputFile) throws IOException {
45-
OutputStream out = new java.io.FileOutputStream(outputFile);
46-
out = new java.io.BufferedOutputStream(out);
47-
try {
44+
try (OutputStream fout = new java.io.FileOutputStream(outputFile);
45+
OutputStream out = new java.io.BufferedOutputStream(fout)) {
4846
//Instantiate the EPSDocumentGraphics2D instance
4947
EPSDocumentGraphics2D g2d = new EPSDocumentGraphics2D(false);
5048
g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
@@ -78,8 +76,6 @@ public static void generateEPSusingJava2D(File outputFile) throws IOException {
7876

7977
//Cleanup
8078
g2d.finish();
81-
} finally {
82-
IOUtils.closeQuietly(out);
8379
}
8480
}
8581

examples/java/java2d/ps/TilingPatternExample.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
import java.io.IOException;
3434
import java.io.OutputStream;
3535

36-
import org.apache.commons.io.IOUtils;
37-
3836
import org.apache.xmlgraphics.image.writer.ImageWriterUtil;
3937
import org.apache.xmlgraphics.java2d.ps.PSDocumentGraphics2D;
4038

@@ -71,9 +69,8 @@ public TilingPatternExample() {
7169
* @throws IOException In case of an I/O error
7270
*/
7371
public void generatePSusingJava2D(File outputFile) throws IOException {
74-
OutputStream out = new java.io.FileOutputStream(outputFile);
75-
out = new java.io.BufferedOutputStream(out);
76-
try {
72+
try (OutputStream fout = new java.io.FileOutputStream(outputFile);
73+
OutputStream out = new java.io.BufferedOutputStream(out)) {
7774
//Instantiate the PSDocumentGraphics2D instance
7875
PSDocumentGraphics2D g2d = new PSDocumentGraphics2D(false);
7976
g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
@@ -91,8 +88,6 @@ public void generatePSusingJava2D(File outputFile) throws IOException {
9188

9289
//Cleanup
9390
g2d.finish();
94-
} finally {
95-
IOUtils.closeQuietly(out);
9691
}
9792
}
9893

examples/java/ps/DSCProcessingExample1.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.io.OutputStream;
2525
import java.io.InputStream;
2626

27-
import org.apache.commons.io.IOUtils;
2827
import org.apache.xmlgraphics.ps.dsc.DSCException;
2928
import org.apache.xmlgraphics.ps.dsc.tools.PageExtractor;
3029

@@ -44,20 +43,13 @@ public class DSCProcessingExample1 {
4443
* @throws IOException In case of an I/O error
4544
*/
4645
public void extractPages(File srcFile, File tgtFile, int from, int to) throws IOException {
47-
InputStream in = new java.io.FileInputStream(srcFile);
48-
in = new java.io.BufferedInputStream(in);
49-
try {
50-
OutputStream out = new java.io.FileOutputStream(tgtFile);
51-
out = new java.io.BufferedOutputStream(out);
52-
try {
53-
PageExtractor.extractPages(in, out, from, to);
54-
} catch (DSCException e) {
55-
throw new RuntimeException(e.getMessage());
56-
} finally {
57-
IOUtils.closeQuietly(out);
58-
}
59-
} finally {
60-
IOUtils.closeQuietly(in);
46+
try (InputStream fin = new java.io.FileInputStream(srcFile);
47+
InputStream in = new java.io.BufferedInputStream(fin);
48+
OutputStream fout = new java.io.FileOutputStream(tgtFile);
49+
OutputStream out = new java.io.BufferedOutputStream(fout)) {
50+
PageExtractor.extractPages(in, out, from, to);
51+
} catch (DSCException e) {
52+
throw new RuntimeException(e.getMessage());
6153
}
6254
}
6355

src/main/java/org/apache/xmlgraphics/fonts/Glyphs.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import java.util.Map;
3030
import java.util.StringTokenizer;
3131

32-
import org.apache.commons.io.IOUtils;
33-
3432
/**
3533
* This class provides a number of constants for glyph management.
3634
*/
@@ -651,12 +649,11 @@ private static void addAlternatives(Map map, String[] alternatives) {
651649

652650
private static String[] loadGlyphList(String filename, Map charNameToUnicodeMap) {
653651
List lines = new java.util.ArrayList();
654-
InputStream in = Glyphs.class.getResourceAsStream(filename);
655-
if (in == null) {
656-
throw new RuntimeException("Cannot load " + filename
657-
+ ". The Glyphs class cannot properly be initialized!");
658-
}
659-
try {
652+
try (InputStream in = Glyphs.class.getResourceAsStream(filename)) {
653+
if (in == null) {
654+
throw new RuntimeException("Cannot load " + filename
655+
+ ". The Glyphs class cannot properly be initialized!");
656+
}
660657
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "US-ASCII"));
661658
String line;
662659
try {
@@ -674,8 +671,6 @@ private static String[] loadGlyphList(String filename, Map charNameToUnicodeMap)
674671
} catch (IOException ioe) {
675672
throw new RuntimeException("I/O error while loading " + filename
676673
+ ". The Glyphs class cannot properly be initialized!");
677-
} finally {
678-
IOUtils.closeQuietly(in);
679674
}
680675
String[] arr = new String[lines.size() * 2];
681676
int pos = 0;

src/main/java/org/apache/xmlgraphics/image/loader/impl/AbstractImageSessionContext.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import javax.xml.transform.sax.SAXSource;
4141
import javax.xml.transform.stream.StreamSource;
4242

43-
import org.apache.commons.io.IOUtils;
4443
import org.apache.commons.logging.Log;
4544
import org.apache.commons.logging.LogFactory;
4645

@@ -133,7 +132,14 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
133132
try {
134133
return method.invoke(iin, args);
135134
} finally {
136-
IOUtils.closeQuietly(this.in);
135+
InputStream in = this.in;
136+
if (in != null) {
137+
try {
138+
in.close();
139+
} catch (IOException ignore) {
140+
// ignore
141+
}
142+
}
137143
this.in = null;
138144
}
139145
} else {
@@ -330,7 +336,11 @@ public Source createSource(Source source, String uri) {
330336

331337
if (directFileAccess) {
332338
//Close as the file is reopened in a more optimal way
333-
IOUtils.closeQuietly(in);
339+
try {
340+
in.close();
341+
} catch (IOException ignore) {
342+
// ignore
343+
}
334344
try {
335345
// We let the OS' file system cache do the caching for us
336346
// --> lower Java memory consumption, probably no speed loss

src/main/java/org/apache/xmlgraphics/image/loader/impl/ImageRawStream.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,8 @@ public InputStream createInputStream() {
111111
* @throws IOException if an I/O error occurs
112112
*/
113113
public void writeTo(OutputStream out) throws IOException {
114-
InputStream in = createInputStream();
115-
try {
114+
try (InputStream in = createInputStream()) {
116115
IOUtils.copy(in, out);
117-
} finally {
118-
IOUtils.closeQuietly(in);
119116
}
120117
}
121118

@@ -166,7 +163,14 @@ public synchronized InputStream createInputStream() {
166163
}
167164

168165
public synchronized void close() {
169-
IOUtils.closeQuietly(this.in);
166+
InputStream in = this.in;
167+
if (in != null) {
168+
try {
169+
in.close();
170+
} catch (IOException ignore) {
171+
// ignore
172+
}
173+
}
170174
this.in = null;
171175
}
172176

src/main/java/org/apache/xmlgraphics/image/loader/pipeline/ImageProviderPipeline.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,8 @@ protected Image forceCaching(Image img) throws IOException {
225225

226226
//Read the whole stream and hold it in memory so the image can be cached
227227
ByteArrayOutputStream baout = new ByteArrayOutputStream();
228-
InputStream in = raw.createInputStream();
229-
try {
228+
try (InputStream in = raw.createInputStream()) {
230229
IOUtils.copy(in, baout);
231-
} finally {
232-
IOUtils.closeQuietly(in);
233230
}
234231
final byte[] data = baout.toByteArray();
235232
raw.setInputStreamFactory(new ImageRawStream.ByteArrayStreamFactory(data));

0 commit comments

Comments
 (0)