~ubuntu-branches/ubuntu/karmic/commons-io/karmic

« back to all changes in this revision

Viewing changes to src/java/org/apache/commons/io/CopyUtils.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2007-04-13 08:20:49 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070413082049-2hd3s5ixtxsgvnvm
Tags: 1.3.1.dfsg.1-1
* New upstream (closes: #418973)
* java-gcj-compat-dev and cdbs transition
* Removed junit at the moment (closes: #397567)
* No javadoc at the moment

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright 2001-2004 The Apache Software Foundation.
3
 
 * 
4
 
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 
 * you may not use this file except in compliance with the License.
6
 
 * You may obtain a copy of the License at
 
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
7
8
 * 
8
9
 *      http://www.apache.org/licenses/LICENSE-2.0
9
10
 * 
26
27
import java.io.Writer;
27
28
 
28
29
/**
29
 
 * <p>
30
30
 * This class provides static utility methods for buffered
31
 
 * copying between sources (<code>InputStream</code>, <code>Reader</code>, <code>String</code> and
32
 
 * <code>byte[]</code>) and destinations (<code>OutputStream</code>, <code>Writer</code>,
33
 
 * <code>String</code> and <code>byte[]</code>).
34
 
 * </p>
35
 
 *
36
 
 * <p>Unless otherwise noted, these <code>copy</code> methods do <em>not</em> flush or close the
37
 
 * streams. Often doing so would require making non-portable assumptions about the streams' origin
38
 
 * and further use. This means that both streams' <code>close()</code> methods must be called after
39
 
 * copying. if one omits this step, then the stream resources (sockets, file descriptors) are
40
 
 * released when the associated Stream is garbage-collected. It is not a good idea to rely on this
41
 
 * mechanism. For a good overview of the distinction between "memory management" and "resource
42
 
 * management", see <a href="http://www.unixreview.com/articles/1998/9804/9804ja/ja.htm">this
43
 
 * UnixReview article</a>.</p>
44
 
 *
45
 
 * <p>For byte-to-char methods, a <code>copy</code> variant allows the encoding 
46
 
 * to be selected (otherwise the platform default is used). We would like to 
 
31
 * copying between sources (<code>InputStream</code>, <code>Reader</code>,
 
32
 * <code>String</code> and <code>byte[]</code>) and destinations
 
33
 * (<code>OutputStream</code>, <code>Writer</code>, <code>String</code> and
 
34
 * <code>byte[]</code>).
 
35
 * <p>
 
36
 * Unless otherwise noted, these <code>copy</code> methods do <em>not</em>
 
37
 * flush or close the streams. Often doing so would require making non-portable
 
38
 * assumptions about the streams' origin and further use. This means that both
 
39
 * streams' <code>close()</code> methods must be called after copying. if one
 
40
 * omits this step, then the stream resources (sockets, file descriptors) are
 
41
 * released when the associated Stream is garbage-collected. It is not a good
 
42
 * idea to rely on this mechanism. For a good overview of the distinction
 
43
 * between "memory management" and "resource management", see
 
44
 * <a href="http://www.unixreview.com/articles/1998/9804/9804ja/ja.htm">this
 
45
 * UnixReview article</a>.
 
46
 * <p>
 
47
 * For byte-to-char methods, a <code>copy</code> variant allows the encoding
 
48
 * to be selected (otherwise the platform default is used). We would like to
47
49
 * encourage you to always specify the encoding because relying on the platform
48
 
 * default can lead to unexpected results.</p>
49
 
 *
50
 
 * <p>We don't provide special variants for the <code>copy</code> methods that
 
50
 * default can lead to unexpected results.
 
51
 * <p
 
52
 * We don't provide special variants for the <code>copy</code> methods that
51
53
 * let you specify the buffer size because in modern VMs the impact on speed
52
 
 * seems to be minimal. We're using a default buffer size of 4 KB.</p>
53
 
 *
54
 
 * <p>The <code>copy</code> methods use an internal buffer when copying. It is therefore advisable
55
 
 * <em>not</em> to deliberately wrap the stream arguments to the <code>copy</code> methods in
56
 
 * <code>Buffered*</code> streams. For example, don't do the
57
 
 * following:</p>
58
 
 *
59
 
 * <code>copy( new BufferedInputStream( in ), new BufferedOutputStream( out ) );</code>
60
 
 *
61
 
 * <p>The rationale is as follows:</p>
62
 
 *
63
 
 * <p>Imagine that an InputStream's read() is a very expensive operation, which would usually suggest
64
 
 * wrapping in a BufferedInputStream. The BufferedInputStream works by issuing infrequent
65
 
 * {@link java.io.InputStream#read(byte[] b, int off, int len)} requests on the underlying InputStream, to
66
 
 * fill an internal buffer, from which further <code>read</code> requests can inexpensively get
67
 
 * their data (until the buffer runs out).</p>
68
 
 * <p>However, the <code>copy</code> methods do the same thing, keeping an internal buffer,
69
 
 * populated by {@link InputStream#read(byte[] b, int off, int len)} requests. Having two buffers
70
 
 * (or three if the destination stream is also buffered) is pointless, and the unnecessary buffer
71
 
 * management hurts performance slightly (about 3%, according to some simple experiments).</p>
72
 
 *
73
 
 * <p>Behold, intrepid explorers; a map of this class:</p>
 
54
 * seems to be minimal. We're using a default buffer size of 4 KB.
 
55
 * <p>
 
56
 * The <code>copy</code> methods use an internal buffer when copying. It is
 
57
 * therefore advisable <em>not</em> to deliberately wrap the stream arguments
 
58
 * to the <code>copy</code> methods in <code>Buffered*</code> streams. For
 
59
 * example, don't do the following:
 
60
 * <pre>
 
61
 *  copy( new BufferedInputStream( in ), new BufferedOutputStream( out ) );
 
62
 *  </pre>
 
63
 * The rationale is as follows:
 
64
 * <p>
 
65
 * Imagine that an InputStream's read() is a very expensive operation, which
 
66
 * would usually suggest wrapping in a BufferedInputStream. The
 
67
 * BufferedInputStream works by issuing infrequent
 
68
 * {@link java.io.InputStream#read(byte[] b, int off, int len)} requests on the
 
69
 * underlying InputStream, to fill an internal buffer, from which further
 
70
 * <code>read</code> requests can inexpensively get their data (until the buffer
 
71
 * runs out).
 
72
 * <p>
 
73
 * However, the <code>copy</code> methods do the same thing, keeping an
 
74
 * internal buffer, populated by
 
75
 * {@link InputStream#read(byte[] b, int off, int len)} requests. Having two
 
76
 * buffers (or three if the destination stream is also buffered) is pointless,
 
77
 * and the unnecessary buffer management hurts performance slightly (about 3%,
 
78
 * according to some simple experiments).
 
79
 * <p>
 
80
 * Behold, intrepid explorers; a map of this class:
74
81
 * <pre>
75
82
 *       Method      Input               Output          Dependency
76
83
 *       ------      -----               ------          -------
87
94
 * 7     copy        byte[]              Writer          3
88
95
 * 8     copy        byte[]              OutputStream    (trivial)
89
96
 * </pre>
90
 
 *
91
 
 * <p>Note that only the first two methods shuffle bytes; the rest use these
 
97
 * <p>
 
98
 * Note that only the first two methods shuffle bytes; the rest use these
92
99
 * two, or (if possible) copy using native Java copy methods. As there are
93
100
 * method variants to specify the encoding, each row may
94
 
 * correspond to up to 2 methods.</p>
95
 
 *
96
 
 * <p>Origin of code: Apache Avalon (Excalibur)</p>
 
101
 * correspond to up to 2 methods.
 
102
 * <p>
 
103
 * Origin of code: Excalibur.
97
104
 *
98
105
 * @author Peter Donald
99
106
 * @author Jeff Turner
100
107
 * @author Matthew Hawthorne
101
 
 * @version $Id: CopyUtils.java,v 1.6 2004/04/24 23:49:25 bayard Exp $
 
108
 * @version $Id: CopyUtils.java 437680 2006-08-28 11:57:00Z scolebourne $
 
109
 * @deprecated Use IOUtils. Will be removed in 2.0.
 
110
 *  Methods renamed to IOUtils.write() or IOUtils.copy().
 
111
 *  Null handling behaviour changed in IOUtils (null data does not
 
112
 *  throw NullPointerException).
102
113
 */
103
114
public class CopyUtils {
104
115
 
105
116
    /**
106
 
     * The name says it all.
 
117
     * The default size of the buffer.
107
118
     */
108
119
    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
109
120
 
110
121
    /**
111
122
     * Instances should NOT be constructed in standard programming.
112
123
     */
113
 
    public CopyUtils() {}
 
124
    public CopyUtils() { }
114
125
 
115
126
    // ----------------------------------------------------------------
116
127
    // byte[] -> OutputStream
171
182
    // ----------------------------------------------------------------
172
183
 
173
184
    /**
174
 
     * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
 
185
     * Copy bytes from an <code>InputStream</code> to an
 
186
     * <code>OutputStream</code>.
175
187
     * @param input the <code>InputStream</code> to read from
176
188
     * @param output the <code>OutputStream</code> to write to
177
189
     * @return the number of bytes copied
261
273
    // ----------------------------------------------------------------
262
274
 
263
275
    /**
264
 
     * Serialize chars from a <code>Reader</code> to bytes on an 
 
276
     * Serialize chars from a <code>Reader</code> to bytes on an
265
277
     * <code>OutputStream</code>, and flush the <code>OutputStream</code>.
266
278
     * @param input the <code>Reader</code> to read from
267
279
     * @param output the <code>OutputStream</code> to write to
273
285
                throws IOException {
274
286
        OutputStreamWriter out = new OutputStreamWriter(output);
275
287
        copy(input, out);
276
 
        // XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
 
288
        // XXX Unless anyone is planning on rewriting OutputStreamWriter, we
 
289
        // have to flush here.
277
290
        out.flush();
278
291
    }
279
292
 
282
295
    // ----------------------------------------------------------------
283
296
 
284
297
    /**
285
 
     * Serialize chars from a <code>String</code> to bytes on an <code>OutputStream</code>, and
 
298
     * Serialize chars from a <code>String</code> to bytes on an
 
299
     * <code>OutputStream</code>, and
286
300
     * flush the <code>OutputStream</code>.
287
301
     * @param input the <code>String</code> to read from
288
302
     * @param output the <code>OutputStream</code> to write to
295
309
        StringReader in = new StringReader(input);
296
310
        OutputStreamWriter out = new OutputStreamWriter(output);
297
311
        copy(in, out);
298
 
        // XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
 
312
        // XXX Unless anyone is planning on rewriting OutputStreamWriter, we
 
313
        // have to flush here.
299
314
        out.flush();
300
315
    }
301
316
 
314
329
        output.write(input);
315
330
    }
316
331
 
317
 
} // CopyUtils
 
332
}