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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.util;
1819import java.io.*;
20import java.net.URL;
2122/*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 */3031publicclassFileCopy {
3233publicstaticfinalint BUFFER_SIZE = 4096;
3435/*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 */42publicstaticfinalvoid copy(String source, String destination)
43 throws IOException
44 {
45 byte[] buffer = new byte[BUFFER_SIZE];
46 BufferedInputStream input;
47 BufferedOutputStream output;
4849 input = new BufferedInputStream(new FileInputStream(source));
50 output = new BufferedOutputStream(new FileOutputStream(destination));
5152 copyStream(input, output, buffer);
5354 input.close();
55 output.close();
56 }
5758/*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 */65publicstaticfinalvoid 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;
727374 input = new BufferedInputStream(new DataInputStream(url.openStream()));
75 output = new BufferedOutputStream(new FileOutputStream(destination));
7677 copyStream(input, output, buffer);
7879 input.close();
80 output.close();
81 }
8283/*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 */91publicstaticfinalvoid copyStream(InputStream input,
92 OutputStream output,
93 byte[] buffer)
94 throws IOException
95 {
96int bytesRead;
9798while((bytesRead = input.read(buffer)) != -1)
99 output.write(buffer, 0, bytesRead);
100 }
101102 }