1 /*
2 * Copyright 2000-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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.apache.jetspeed.util;
18
19 import java.io.*;
20 import java.net.URL;
21
22 /*
23 * File Copy Utilities. Some utilities that java.io doesn't give us.
24 *
25 * copy() - copies one file source to another file destination.
26 * copyFromURL)() - copies from a URL source to a file destination.
27 *
28 * @author David S. Taylor <a href="mailto:taylor@apache.org">David Sean Taylor</a>
29 */
30
31 public class FileCopy {
32
33 public static final int BUFFER_SIZE = 4096;
34
35 /*
36 * Copies one file source to another file destination.
37 *
38 * @param source The source file.
39 * @param destination The destination file.
40 * @throws IOException When an IO error occurs, this exception is thrown.
41 */
42 public static final void copy(String source, String destination)
43 throws IOException
44 {
45 byte[] buffer = new byte[BUFFER_SIZE];
46 BufferedInputStream input;
47 BufferedOutputStream output;
48
49 input = new BufferedInputStream(new FileInputStream(source));
50 output = new BufferedOutputStream(new FileOutputStream(destination));
51
52 copyStream(input, output, buffer);
53
54 input.close();
55 output.close();
56 }
57
58 /*
59 * Copies from a URL source to a file destination.
60 *
61 * @param source The source URL.
62 * @param destination The destination file.
63 * @throws IOException When an IO error occurs, this exception is thrown.
64 */
65 public static final void copyFromURL(String source, String destination)
66 throws IOException
67 {
68 byte[] buffer = new byte[BUFFER_SIZE];
69 URL url = new URL(source);
70 BufferedInputStream input;
71 BufferedOutputStream output;
72
73
74 input = new BufferedInputStream(new DataInputStream(url.openStream()));
75 output = new BufferedOutputStream(new FileOutputStream(destination));
76
77 copyStream(input, output, buffer);
78
79 input.close();
80 output.close();
81 }
82
83 /*
84 * Generic copy from a input stream to an output stream.
85 *
86 * @param input The source input stream.
87 * @param output The destination output stream.
88 * @param buffer The user provided buffer.
89 * @throws IOException When an IO error occurs, this exception is thrown.
90 */
91 public static final void copyStream(InputStream input,
92 OutputStream output,
93 byte[] buffer)
94 throws IOException
95 {
96 int bytesRead;
97
98 while((bytesRead = input.read(buffer)) != -1)
99 output.write(buffer, 0, bytesRead);
100 }
101
102 }