001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.util;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.InputStreamReader;
022: import java.io.OutputStream;
023: import java.io.OutputStreamWriter;
024: import java.io.Reader;
025: import java.io.StringReader;
026: import java.io.Writer;
027:
028: /**
029: * <p>
030: * This class provides static utility methods for buffered
031: * copying between sources (<code>InputStream</code>, <code>Reader</code>, <code>String</code> and
032: * <code>byte[]</code>) and destinations (<code>OutputStream</code>, <code>Writer</code>,
033: * <code>String</code> and <code>byte[]</code>).
034: * </p>
035: *
036: * <p>Unless otherwise noted, these <code>copy</code> methods do <em>not</em> flush or close the
037: * streams. Often doing so would require making non-portable assumptions about the streams' origin
038: * and further use. This means that both streams' <code>close()</code> methods must be called after
039: * copying. if one omits this step, then the stream resources (sockets, file descriptors) are
040: * released when the associated Stream is garbage-collected. It is not a good idea to rely on this
041: * mechanism. For a good overview of the distinction between "memory management" and "resource
042: * management", see <a href="http://www.unixreview.com/articles/1998/9804/9804ja/ja.htm">this
043: * UnixReview article</a>.</p>
044: *
045: * <p>For byte-to-char methods, a <code>copy</code> variant allows the encoding
046: * to be selected (otherwise the platform default is used). We would like to
047: * encourage you to always specify the encoding because relying on the platform
048: * default can lead to unexpected results.</p>
049: *
050: * <p>We don't provide special variants for the <code>copy</code> methods that
051: * let you specify the buffer size because in modern VMs the impact on speed
052: * seems to be minimal. We're using a default buffer size of 4 KB.</p>
053: *
054: * <p>The <code>copy</code> methods use an internal buffer when copying. It is therefore advisable
055: * <em>not</em> to deliberately wrap the stream arguments to the <code>copy</code> methods in
056: * <code>Buffered*</code> streams. For example, don't do the
057: * following:</p>
058: *
059: * <code>copy( new BufferedInputStream( in ), new BufferedOutputStream( out ) );</code>
060: *
061: * <p>The rationale is as follows:</p>
062: *
063: * <p>Imagine that an InputStream's read() is a very expensive operation, which would usually suggest
064: * wrapping in a BufferedInputStream. The BufferedInputStream works by issuing infrequent
065: * {@link java.io.InputStream#read(byte[] b, int off, int len)} requests on the underlying InputStream, to
066: * fill an internal buffer, from which further <code>read</code> requests can inexpensively get
067: * their data (until the buffer runs out).</p>
068: * <p>However, the <code>copy</code> methods do the same thing, keeping an internal buffer,
069: * populated by {@link InputStream#read(byte[] b, int off, int len)} requests. Having two buffers
070: * (or three if the destination stream is also buffered) is pointless, and the unnecessary buffer
071: * management hurts performance slightly (about 3%, according to some simple experiments).</p>
072: *
073: * <p>Behold, intrepid explorers; a map of this class:</p>
074: * <pre>
075: * Method Input Output Dependency
076: * ------ ----- ------ -------
077: * 1 copy InputStream OutputStream (primitive)
078: * 2 copy Reader Writer (primitive)
079: *
080: * 3 copy InputStream Writer 2
081: *
082: * 4 copy Reader OutputStream 2
083: *
084: * 5 copy String OutputStream 2
085: * 6 copy String Writer (trivial)
086: *
087: * 7 copy byte[] Writer 3
088: * 8 copy byte[] OutputStream (trivial)
089: * </pre>
090: *
091: * <p>Note that only the first two methods shuffle bytes; the rest use these
092: * two, or (if possible) copy using native Java copy methods. As there are
093: * method variants to specify the encoding, each row may
094: * correspond to up to 2 methods.</p>
095: *
096: * <p>Origin of code: Apache Avalon (Excalibur)</p>
097: *
098: * @author Peter Donald
099: * @author Jeff Turner
100: * @author Matthew Hawthorne
101: */
102: public class CopyUtils {
103: /**
104: * Instances should NOT be constructed in standard programming.
105: */
106: private CopyUtils() {
107: }
108:
109: /**
110: * Copy bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
111: * @param input the byte array to read from
112: * @param output the <code>OutputStream</code> to write to
113: * @throws IOException In case of an I/O problem
114: */
115: public static void copy(byte[] input, OutputStream output)
116: throws IOException {
117: output.write(input);
118: }
119:
120: /**
121: * Copy and convert bytes from a <code>byte[]</code> to chars on a
122: * <code>Writer</code>.
123: * The platform's default encoding is used for the byte-to-char conversion.
124: * @param input the byte array to read from
125: * @param output the <code>Writer</code> to write to
126: * @throws IOException In case of an I/O problem
127: */
128: public static void copy(byte[] input, Writer output)
129: throws IOException {
130: ByteArrayInputStream in = new ByteArrayInputStream(input);
131: copy(in, output);
132: }
133:
134: /**
135: * Copy and convert bytes from a <code>byte[]</code> to chars on a
136: * <code>Writer</code>, using the specified encoding.
137: * @param input the byte array to read from
138: * @param output the <code>Writer</code> to write to
139: * @param encoding The name of a supported character encoding. See the
140: * <a href="http://www.iana.org/assignments/character-sets">IANA
141: * Charset Registry</a> for a list of valid encoding types.
142: * @throws IOException In case of an I/O problem
143: */
144: public static void copy(byte[] input, Writer output, String encoding)
145: throws IOException {
146: ByteArrayInputStream in = new ByteArrayInputStream(input);
147: copy(in, output, encoding);
148: }
149:
150: /**
151: * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
152: * @param input the <code>InputStream</code> to read from
153: * @param output the <code>OutputStream</code> to write to
154: * @return the number of bytes copied
155: * @throws IOException In case of an I/O problem
156: */
157: public static int copy(InputStream input, OutputStream output)
158: throws IOException {
159: byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
160: int count = 0;
161: int n = 0;
162: while (-1 != (n = input.read(buffer))) {
163: output.write(buffer, 0, n);
164: count += n;
165: }
166: return count;
167: }
168:
169: /**
170: * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
171: * @param input the <code>Reader</code> to read from
172: * @param output the <code>Writer</code> to write to
173: * @return the number of characters copied
174: * @throws IOException In case of an I/O problem
175: */
176: public static int copy(Reader input, Writer output)
177: throws IOException {
178: char[] buffer = new char[DEFAULT_BUFFER_SIZE];
179: int count = 0;
180: int n = 0;
181: while (-1 != (n = input.read(buffer))) {
182: output.write(buffer, 0, n);
183: count += n;
184: }
185: return count;
186: }
187:
188: /**
189: * Copy and convert bytes from an <code>InputStream</code> to chars on a
190: * <code>Writer</code>.
191: * The platform's default encoding is used for the byte-to-char conversion.
192: * @param input the <code>InputStream</code> to read from
193: * @param output the <code>Writer</code> to write to
194: * @throws IOException In case of an I/O problem
195: */
196: public static void copy(InputStream input, Writer output)
197: throws IOException {
198: InputStreamReader in = new InputStreamReader(input);
199: copy(in, output);
200: }
201:
202: /**
203: * Copy and convert bytes from an <code>InputStream</code> to chars on a
204: * <code>Writer</code>, using the specified encoding.
205: * @param input the <code>InputStream</code> to read from
206: * @param output the <code>Writer</code> to write to
207: * @param encoding The name of a supported character encoding. See the
208: * <a href="http://www.iana.org/assignments/character-sets">IANA
209: * Charset Registry</a> for a list of valid encoding types.
210: * @throws IOException In case of an I/O problem
211: */
212: public static void copy(InputStream input, Writer output,
213: String encoding) throws IOException {
214: InputStreamReader in = new InputStreamReader(input, encoding);
215: copy(in, output);
216: }
217:
218: /**
219: * Serialize chars from a <code>Reader</code> to bytes on an
220: * <code>OutputStream</code>, and flush the <code>OutputStream</code>.
221: * @param input the <code>Reader</code> to read from
222: * @param output the <code>OutputStream</code> to write to
223: * @throws IOException In case of an I/O problem
224: */
225: public static void copy(Reader input, OutputStream output)
226: throws IOException {
227: OutputStreamWriter out = new OutputStreamWriter(output);
228: copy(input, out);
229: // XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
230: out.flush();
231: }
232:
233: /**
234: * Serialize chars from a <code>String</code> to bytes on an <code>OutputStream</code>, and
235: * flush the <code>OutputStream</code>.
236: * @param input the <code>String</code> to read from
237: * @param output the <code>OutputStream</code> to write to
238: * @throws IOException In case of an I/O problem
239: */
240: public static void copy(String input, OutputStream output)
241: throws IOException {
242: StringReader in = new StringReader(input);
243: OutputStreamWriter out = new OutputStreamWriter(output);
244: copy(in, out);
245: // XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
246: out.flush();
247: }
248:
249: /**
250: * Copy chars from a <code>String</code> to a <code>Writer</code>.
251: * @param input the <code>String</code> to read from
252: * @param output the <code>Writer</code> to write to
253: * @throws IOException In case of an I/O problem
254: */
255: public static void copy(String input, Writer output)
256: throws IOException {
257: output.write(input);
258: }
259:
260: /**
261: * The name says it all.
262: */
263: private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
264: }
|