Skip to content

Commit 1d7064b

Browse files
committed
XGC-148: add xmlgraphics.util.io.IOUtils
1 parent 64b4a65 commit 1d7064b

File tree

16 files changed

+149
-90
lines changed

16 files changed

+149
-90
lines changed

examples/java/image/writer/ImageWriterExample2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
import java.io.IOException;
2727
import java.io.OutputStream;
2828

29-
import org.apache.commons.io.IOUtils;
3029
import org.apache.xmlgraphics.image.writer.ImageWriter;
3130
import org.apache.xmlgraphics.image.writer.ImageWriterParams;
3231
import org.apache.xmlgraphics.image.writer.ImageWriterRegistry;
3332
import org.apache.xmlgraphics.image.writer.MultiImageWriter;
33+
import org.apache.xmlgraphics.util.io.IOUtils;
3434

3535
public class ImageWriterExample2 extends ImageWriterExample1 {
3636

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.apache.xmlgraphics.image.loader.util.ImageUtil;
4949
import org.apache.xmlgraphics.image.loader.util.SoftMapCache;
5050
import org.apache.xmlgraphics.io.XmlSourceUtil;
51+
import org.apache.xmlgraphics.util.io.IOUtils;
5152

5253
/**
5354
* Abstract base class for classes implementing ImageSessionContext. This class provides all the
@@ -132,14 +133,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
132133
try {
133134
return method.invoke(iin, args);
134135
} finally {
135-
InputStream in = this.in;
136-
if (in != null) {
137-
try {
138-
in.close();
139-
} catch (IOException ignore) {
140-
// ignore
141-
}
142-
}
136+
IOUtils.closeQuietly(this.in);
143137
this.in = null;
144138
}
145139
} else {
@@ -336,11 +330,7 @@ public Source createSource(Source source, String uri) {
336330

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

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

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

27-
import org.apache.commons.io.IOUtils;
28-
2927
import org.apache.xmlgraphics.image.loader.ImageFlavor;
3028
import org.apache.xmlgraphics.image.loader.ImageInfo;
3129
import org.apache.xmlgraphics.image.loader.MimeEnabledImageFlavor;
30+
import org.apache.xmlgraphics.util.io.IOUtils;
3231

3332
/**
3433
* This class is an implementation of the Image interface exposing an InputStream for loading the
@@ -163,14 +162,7 @@ public synchronized InputStream createInputStream() {
163162
}
164163

165164
public synchronized void close() {
166-
InputStream in = this.in;
167-
if (in != null) {
168-
try {
169-
in.close();
170-
} catch (IOException ignore) {
171-
// ignore
172-
}
173-
}
165+
IOUtils.closeQuietly(this.in);
174166
this.in = null;
175167
}
176168

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.List;
2727
import java.util.Map;
2828

29-
import org.apache.commons.io.IOUtils;
3029
import org.apache.commons.logging.Log;
3130
import org.apache.commons.logging.LogFactory;
3231

@@ -41,6 +40,7 @@
4140
import org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry;
4241
import org.apache.xmlgraphics.image.loader.spi.ImageLoader;
4342
import org.apache.xmlgraphics.image.loader.util.Penalty;
43+
import org.apache.xmlgraphics.util.io.IOUtils;
4444

4545
/**
4646
* Represents a pipeline of ImageConverters with an ImageLoader at the beginning of the

src/main/java/org/apache/xmlgraphics/io/XmlSourceUtil.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import java.io.ByteArrayInputStream;
2121
import java.io.ByteArrayOutputStream;
22-
import java.io.Closeable;
2322
import java.io.IOException;
2423
import java.io.InputStream;
2524
import java.io.Reader;
@@ -36,6 +35,7 @@
3635
import org.apache.xmlgraphics.image.loader.ImageSource;
3736
import org.apache.xmlgraphics.image.loader.util.ImageInputStreamAdapter;
3837
import org.apache.xmlgraphics.image.loader.util.ImageUtil;
38+
import org.apache.xmlgraphics.util.io.IOUtils;
3939

4040
/**
4141
* A utility class for handling {@link Source} objects, more specficially the streams that back
@@ -136,7 +136,7 @@ public static void removeStreams(Source src) {
136136
public static void closeQuietly(Source src) {
137137
if (src instanceof StreamSource) {
138138
StreamSource streamSource = (StreamSource) src;
139-
closeQuietly(streamSource.getReader());
139+
IOUtils.closeQuietly(streamSource.getReader());
140140
} else if (src instanceof ImageSource) {
141141
if (ImageUtil.getImageInputStream(src) != null) {
142142
try {
@@ -148,24 +148,13 @@ public static void closeQuietly(Source src) {
148148
} else if (src instanceof SAXSource) {
149149
InputSource is = ((SAXSource) src).getInputSource();
150150
if (is != null) {
151-
closeQuietly(is.getByteStream());
152-
closeQuietly(is.getCharacterStream());
151+
IOUtils.closeQuietly(is.getByteStream());
152+
IOUtils.closeQuietly(is.getCharacterStream());
153153
}
154154
}
155155
removeStreams(src);
156156
}
157157

158-
private static void closeQuietly(Closeable in) {
159-
if (in == null) {
160-
return;
161-
}
162-
try {
163-
in.close();
164-
} catch (IOException ignore) {
165-
// ignore
166-
}
167-
}
168-
169158
/**
170159
* Indicates whether the Source object has an InputStream instance.
171160
* @param src the Source object

src/main/java/org/apache/xmlgraphics/ps/PSFontUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
import java.io.InputStream;
2525

2626
import org.apache.commons.io.EndianUtils;
27-
import org.apache.commons.io.IOUtils;
2827

2928
import org.apache.xmlgraphics.fonts.Glyphs;
3029
import org.apache.xmlgraphics.util.io.ASCIIHexOutputStream;
30+
import org.apache.xmlgraphics.util.io.IOUtils;
3131
import org.apache.xmlgraphics.util.io.SubInputStream;
3232

3333
// CSOFF: HideUtilityClassConstructor

src/main/java/org/apache/xmlgraphics/ps/PSGenerator.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@
3232

3333
import javax.xml.transform.Source;
3434

35-
import org.apache.commons.io.IOUtils;
36-
3735
import org.apache.commons.logging.Log;
3836
import org.apache.commons.logging.LogFactory;
3937

4038
import org.apache.xmlgraphics.java2d.color.ColorUtil;
4139
import org.apache.xmlgraphics.java2d.color.ColorWithAlternatives;
4240
import org.apache.xmlgraphics.ps.dsc.ResourceTracker;
4341
import org.apache.xmlgraphics.util.DoubleFormatUtil;
42+
import org.apache.xmlgraphics.util.io.IOUtils;
4443

4544
/**
4645
* This class is used to output PostScript code to an OutputStream. This class assumes that
@@ -842,7 +841,7 @@ public boolean embedIdentityH() throws IOException {
842841
resTracker.registerNeededResource(getProcsetCIDInitResource());
843842
writeDSCComment(DSCConstants.BEGIN_DOCUMENT, IDENTITY_H);
844843
try (InputStream cmap = PSGenerator.class.getResourceAsStream(IDENTITY_H)) {
845-
IOUtils.copyLarge(cmap, out);
844+
IOUtils.copy(cmap, out);
846845
}
847846
writeDSCComment(DSCConstants.END_DOCUMENT);
848847
resTracker.registerSuppliedResource(getIdentityHCMapResource());

src/main/java/org/apache/xmlgraphics/ps/PSImageUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@
3535
import java.io.OutputStream;
3636
import java.util.Arrays;
3737

38-
import org.apache.commons.io.IOUtils;
39-
4038
import org.apache.xmlgraphics.util.io.ASCII85OutputStream;
4139
import org.apache.xmlgraphics.util.io.Finalizable;
4240
import org.apache.xmlgraphics.util.io.FlateEncodeOutputStream;
41+
import org.apache.xmlgraphics.util.io.IOUtils;
4342
import org.apache.xmlgraphics.util.io.RunLengthEncodeOutputStream;
4443

4544
// CSOFF: HideUtilityClassConstructor
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/* $Id$ */
19+
20+
package org.apache.xmlgraphics.util.io;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.Closeable;
24+
import java.io.IOException;
25+
import java.io.InputStream;
26+
import java.io.OutputStream;
27+
import java.io.Reader;
28+
29+
/**
30+
* Utility class providing convenience methods for I/O operations.
31+
*/
32+
public final class IOUtils {
33+
private IOUtils() {
34+
}
35+
36+
/**
37+
* Copies all bytes from the provided input stream to the given output stream.
38+
* The method operates using a buffer and is useful for transferring data
39+
* between streams without modifying the contents.
40+
*
41+
* @param input the {@code InputStream} to read data from, must not be {@code null}
42+
* @param output the {@code OutputStream} to write data to, must not be {@code null}
43+
* @return the total number of bytes copied
44+
* @throws IOException if an I/O error occurs during reading or writing
45+
*/
46+
public static long copy(InputStream input, OutputStream output) throws IOException {
47+
// TODO replace with input.transferTo(output) when Java 9+ is acceptable
48+
byte[] buffer = new byte[4096];
49+
long count = 0;
50+
int n;
51+
while ((n = input.read(buffer)) != -1) {
52+
output.write(buffer, 0, n);
53+
count += n;
54+
}
55+
return count;
56+
}
57+
58+
/**
59+
* Converts the contents of the provided InputStream into a byte array.
60+
* This method reads all the data from the InputStream and writes it into
61+
* a ByteArrayOutputStream, which is then converted to a byte array.
62+
*
63+
* @param input the InputStream to read data from, must not be {@code null}
64+
* @return a byte array containing all the data read from the InputStream
65+
* @throws IOException if an I/O error occurs while reading from the InputStream
66+
*/
67+
public static byte[] toByteArray(InputStream input) throws IOException {
68+
// TODO replace with InputStream.readAllBytes when Java 9+ is acceptable
69+
ByteArrayOutputStream output = new ByteArrayOutputStream();
70+
copy(input, output);
71+
return output.toByteArray();
72+
}
73+
74+
/**
75+
* Converts the contents of the provided Reader into a String.
76+
* This method reads characters from the Reader into a buffer and then
77+
* appends them to a StringBuilder, which is returned as a String.
78+
*
79+
* @param reader the Reader to read data from, must not be null
80+
* @return a String containing all the characters read from the Reader
81+
* @throws IOException if an I/O error occurs while reading from the Reader
82+
*/
83+
public static String toString(Reader reader) throws IOException {
84+
StringBuilder sb = new StringBuilder();
85+
char [] buf = new char[128];
86+
int n;
87+
while ((n = reader.read(buf)) > 0) {
88+
sb.append(buf, 0, n);
89+
}
90+
return sb.toString();
91+
}
92+
93+
/**
94+
* Closes a {@code Closeable} object quietly, suppressing any exceptions
95+
* that might occur during the close operation. If the provided {@code Closeable}
96+
* is {@code null}, this method does nothing.
97+
*
98+
* @param closeable the {@code Closeable} object to close; may be {@code null}.
99+
*/
100+
public static void closeQuietly(Closeable closeable) {
101+
if (closeable == null) {
102+
return;
103+
}
104+
try {
105+
closeable.close();
106+
} catch (Exception ignore) {
107+
//Ignore
108+
}
109+
}
110+
}

src/main/java/org/apache/xmlgraphics/util/uri/DataURLUtil.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
import java.io.StringWriter;
2525
import java.io.Writer;
2626

27-
import org.apache.commons.io.IOUtils;
28-
2927
import org.apache.xmlgraphics.util.WriterOutputStream;
3028
import org.apache.xmlgraphics.util.io.Base64EncodeStream;
29+
import org.apache.xmlgraphics.util.io.IOUtils;
3130

3231
/**
3332
* Utility classes for generating RFC 2397 data URLs.
@@ -64,9 +63,9 @@ public static void writeDataURL(InputStream in, String mediatype, Writer writer)
6463
writer.write(mediatype);
6564
}
6665
writer.write(";base64,");
67-
Base64EncodeStream out = new Base64EncodeStream(
68-
new WriterOutputStream(writer, "US-ASCII"), false);
69-
IOUtils.copy(in, out);
70-
out.close();
66+
try (Base64EncodeStream out = new Base64EncodeStream(
67+
new WriterOutputStream(writer, "US-ASCII"), false)) {
68+
IOUtils.copy(in, out);
69+
}
7170
}
7271
}

0 commit comments

Comments
 (0)