Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions examples/java/image/writer/ImageWriterExample1.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.io.OutputStream;
import java.text.AttributedString;

import org.apache.commons.io.IOUtils;
import org.apache.xmlgraphics.image.writer.ImageWriter;
import org.apache.xmlgraphics.image.writer.ImageWriterParams;
import org.apache.xmlgraphics.image.writer.ImageWriterRegistry;
Expand Down Expand Up @@ -106,18 +105,15 @@ public void generateBitmapUsingJava2D(File outputFile, String format)
//Paint something
paintSome(g2d, 1);

OutputStream out = new java.io.FileOutputStream(outputFile);
out = new java.io.BufferedOutputStream(out);
try {
try (OutputStream fout = new java.io.FileOutputStream(outputFile);
OutputStream out = new java.io.BufferedOutputStream(fout)) {

ImageWriter writer = ImageWriterRegistry.getInstance().getWriterFor(format);
ImageWriterParams params = new ImageWriterParams();
params.setCompressionMethod(compression);
params.setResolution(72);
writer.writeImage(bimg, out, params);

} finally {
IOUtils.closeQuietly(out);
}
}

Expand Down
7 changes: 2 additions & 5 deletions examples/java/image/writer/ImageWriterExample2.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ public void generateBitmapUsingJava2D(File outputFile, String format)
//String compression = "CCITT T.6";
String compression = "PackBits";

OutputStream out = new java.io.FileOutputStream(outputFile);
out = new java.io.BufferedOutputStream(out);
try {
try (OutputStream fout = new java.io.FileOutputStream(outputFile);
OutputStream out = new java.io.BufferedOutputStream(fout)) {

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

} finally {
IOUtils.closeQuietly(out);
}
}

Expand Down
9 changes: 2 additions & 7 deletions examples/java/java2d/ps/EPSColorsExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;

import org.apache.xmlgraphics.java2d.color.CIELabColorSpace;
import org.apache.xmlgraphics.java2d.color.ColorSpaces;
import org.apache.xmlgraphics.java2d.color.DeviceCMYKColorSpace;
Expand All @@ -45,9 +43,8 @@ public class EPSColorsExample {
* @throws IOException In case of an I/O error
*/
public static void generateEPSusingJava2D(File outputFile) throws IOException {
OutputStream out = new java.io.FileOutputStream(outputFile);
out = new java.io.BufferedOutputStream(out);
try {
try (OutputStream fout = new java.io.FileOutputStream(outputFile);
OutputStream out = new java.io.BufferedOutputStream(fout)) {
//Instantiate the EPSDocumentGraphics2D instance
EPSDocumentGraphics2D g2d = new EPSDocumentGraphics2D(false);
g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
Expand Down Expand Up @@ -96,8 +93,6 @@ public static void generateEPSusingJava2D(File outputFile) throws IOException {

//Cleanup
g2d.finish();
} finally {
IOUtils.closeQuietly(out);
}
}

Expand Down
8 changes: 2 additions & 6 deletions examples/java/java2d/ps/EPSExample1.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;
import org.apache.xmlgraphics.java2d.ps.EPSDocumentGraphics2D;

/**
Expand All @@ -42,9 +41,8 @@ public class EPSExample1 {
* @throws IOException In case of an I/O error
*/
public static void generateEPSusingJava2D(File outputFile) throws IOException {
OutputStream out = new java.io.FileOutputStream(outputFile);
out = new java.io.BufferedOutputStream(out);
try {
try (OutputStream fout = new java.io.FileOutputStream(outputFile);
OutputStream out = new java.io.BufferedOutputStream(fout)) {
//Instantiate the EPSDocumentGraphics2D instance
EPSDocumentGraphics2D g2d = new EPSDocumentGraphics2D(false);
g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
Expand Down Expand Up @@ -78,8 +76,6 @@ public static void generateEPSusingJava2D(File outputFile) throws IOException {

//Cleanup
g2d.finish();
} finally {
IOUtils.closeQuietly(out);
}
}

Expand Down
9 changes: 2 additions & 7 deletions examples/java/java2d/ps/TilingPatternExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;

import org.apache.xmlgraphics.image.writer.ImageWriterUtil;
import org.apache.xmlgraphics.java2d.ps.PSDocumentGraphics2D;

Expand Down Expand Up @@ -71,9 +69,8 @@ public TilingPatternExample() {
* @throws IOException In case of an I/O error
*/
public void generatePSusingJava2D(File outputFile) throws IOException {
OutputStream out = new java.io.FileOutputStream(outputFile);
out = new java.io.BufferedOutputStream(out);
try {
try (OutputStream fout = new java.io.FileOutputStream(outputFile);
OutputStream out = new java.io.BufferedOutputStream(out)) {
//Instantiate the PSDocumentGraphics2D instance
PSDocumentGraphics2D g2d = new PSDocumentGraphics2D(false);
g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
Expand All @@ -91,8 +88,6 @@ public void generatePSusingJava2D(File outputFile) throws IOException {

//Cleanup
g2d.finish();
} finally {
IOUtils.closeQuietly(out);
}
}

Expand Down
22 changes: 7 additions & 15 deletions examples/java/ps/DSCProcessingExample1.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.io.OutputStream;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.xmlgraphics.ps.dsc.DSCException;
import org.apache.xmlgraphics.ps.dsc.tools.PageExtractor;

Expand All @@ -44,20 +43,13 @@ public class DSCProcessingExample1 {
* @throws IOException In case of an I/O error
*/
public void extractPages(File srcFile, File tgtFile, int from, int to) throws IOException {
InputStream in = new java.io.FileInputStream(srcFile);
in = new java.io.BufferedInputStream(in);
try {
OutputStream out = new java.io.FileOutputStream(tgtFile);
out = new java.io.BufferedOutputStream(out);
try {
PageExtractor.extractPages(in, out, from, to);
} catch (DSCException e) {
throw new RuntimeException(e.getMessage());
} finally {
IOUtils.closeQuietly(out);
}
} finally {
IOUtils.closeQuietly(in);
try (InputStream fin = new java.io.FileInputStream(srcFile);
InputStream in = new java.io.BufferedInputStream(fin);
OutputStream fout = new java.io.FileOutputStream(tgtFile);
OutputStream out = new java.io.BufferedOutputStream(fout)) {
PageExtractor.extractPages(in, out, from, to);
} catch (DSCException e) {
throw new RuntimeException(e.getMessage());
}
}

Expand Down
15 changes: 5 additions & 10 deletions src/main/java/org/apache/xmlgraphics/fonts/Glyphs.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import java.util.Map;
import java.util.StringTokenizer;

import org.apache.commons.io.IOUtils;

/**
* This class provides a number of constants for glyph management.
*/
Expand Down Expand Up @@ -651,12 +649,11 @@ private static void addAlternatives(Map map, String[] alternatives) {

private static String[] loadGlyphList(String filename, Map charNameToUnicodeMap) {
List lines = new java.util.ArrayList();
InputStream in = Glyphs.class.getResourceAsStream(filename);
if (in == null) {
throw new RuntimeException("Cannot load " + filename
+ ". The Glyphs class cannot properly be initialized!");
}
try {
try (InputStream in = Glyphs.class.getResourceAsStream(filename)) {
if (in == null) {
throw new RuntimeException("Cannot load " + filename
+ ". The Glyphs class cannot properly be initialized!");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "US-ASCII"));
String line;
try {
Expand All @@ -674,8 +671,6 @@ private static String[] loadGlyphList(String filename, Map charNameToUnicodeMap)
} catch (IOException ioe) {
throw new RuntimeException("I/O error while loading " + filename
+ ". The Glyphs class cannot properly be initialized!");
} finally {
IOUtils.closeQuietly(in);
}
String[] arr = new String[lines.size() * 2];
int pos = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -133,7 +132,14 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
try {
return method.invoke(iin, args);
} finally {
IOUtils.closeQuietly(this.in);
InputStream in = this.in;
if (in != null) {
try {
in.close();
} catch (IOException ignore) {
// ignore
}
}
this.in = null;
}
} else {
Expand Down Expand Up @@ -330,7 +336,11 @@ public Source createSource(Source source, String uri) {

if (directFileAccess) {
//Close as the file is reopened in a more optimal way
IOUtils.closeQuietly(in);
try {
in.close();
Comment thread
vlsi marked this conversation as resolved.
Outdated
} catch (IOException ignore) {
// ignore
}
try {
// We let the OS' file system cache do the caching for us
// --> lower Java memory consumption, probably no speed loss
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,8 @@ public InputStream createInputStream() {
* @throws IOException if an I/O error occurs
*/
public void writeTo(OutputStream out) throws IOException {
InputStream in = createInputStream();
try {
try (InputStream in = createInputStream()) {
IOUtils.copy(in, out);
} finally {
IOUtils.closeQuietly(in);
}
}

Expand Down Expand Up @@ -166,7 +163,14 @@ public synchronized InputStream createInputStream() {
}

public synchronized void close() {
IOUtils.closeQuietly(this.in);
InputStream in = this.in;
if (in != null) {
try {
in.close();
} catch (IOException ignore) {
// ignore
}
}
this.in = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,8 @@ protected Image forceCaching(Image img) throws IOException {

//Read the whole stream and hold it in memory so the image can be cached
ByteArrayOutputStream baout = new ByteArrayOutputStream();
InputStream in = raw.createInputStream();
try {
try (InputStream in = raw.createInputStream()) {
IOUtils.copy(in, baout);
} finally {
IOUtils.closeQuietly(in);
}
final byte[] data = baout.toByteArray();
raw.setInputStreamFactory(new ImageRawStream.ByteArrayStreamFactory(data));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;
import java.nio.file.Files;

/**
* Convenience methods around ImageWriter for the most important tasks.
Expand Down Expand Up @@ -68,14 +67,11 @@ public static void saveAsPNG(RenderedImage bitmap, int resolution, File outputFi
public static void saveAsFile(RenderedImage bitmap,
int resolution, File outputFile, String mime)
throws IOException {
OutputStream out = new java.io.FileOutputStream(outputFile);
try {
try (OutputStream out = Files.newOutputStream(outputFile.toPath())) {
ImageWriter writer = ImageWriterRegistry.getInstance().getWriterFor(mime);
ImageWriterParams params = new ImageWriterParams();
params.setResolution(resolution);
writer.writeImage(bitmap, out, params);
} finally {
IOUtils.closeQuietly(out);
}
}

Expand Down
20 changes: 15 additions & 5 deletions src/main/java/org/apache/xmlgraphics/io/XmlSourceUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
Expand All @@ -32,8 +33,6 @@

import org.xml.sax.InputSource;

import org.apache.commons.io.IOUtils;

import org.apache.xmlgraphics.image.loader.ImageSource;
import org.apache.xmlgraphics.image.loader.util.ImageInputStreamAdapter;
import org.apache.xmlgraphics.image.loader.util.ImageUtil;
Expand Down Expand Up @@ -137,7 +136,7 @@ public static void removeStreams(Source src) {
public static void closeQuietly(Source src) {
if (src instanceof StreamSource) {
StreamSource streamSource = (StreamSource) src;
IOUtils.closeQuietly(streamSource.getReader());
closeQuietly(streamSource.getReader());
} else if (src instanceof ImageSource) {
if (ImageUtil.getImageInputStream(src) != null) {
try {
Expand All @@ -149,13 +148,24 @@ public static void closeQuietly(Source src) {
} else if (src instanceof SAXSource) {
InputSource is = ((SAXSource) src).getInputSource();
if (is != null) {
IOUtils.closeQuietly(is.getByteStream());
IOUtils.closeQuietly(is.getCharacterStream());
closeQuietly(is.getByteStream());
closeQuietly(is.getCharacterStream());
}
}
removeStreams(src);
}

private static void closeQuietly(Closeable in) {
if (in == null) {
return;
}
try {
in.close();
} catch (IOException ignore) {
// ignore
}
}

/**
* Indicates whether the Source object has an InputStream instance.
* @param src the Source object
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/org/apache/xmlgraphics/ps/PSGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -841,11 +841,8 @@ public boolean embedIdentityH() throws IOException {
} else {
resTracker.registerNeededResource(getProcsetCIDInitResource());
writeDSCComment(DSCConstants.BEGIN_DOCUMENT, IDENTITY_H);
InputStream cmap = PSGenerator.class.getResourceAsStream(IDENTITY_H);
try {
try (InputStream cmap = PSGenerator.class.getResourceAsStream(IDENTITY_H)) {
IOUtils.copyLarge(cmap, out);
} finally {
IOUtils.closeQuietly(cmap);
}
writeDSCComment(DSCConstants.END_DOCUMENT);
resTracker.registerSuppliedResource(getIdentityHCMapResource());
Expand Down
Loading
Loading