001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.cache.file;
018:
019: import java.io.BufferedInputStream;
020: import java.io.BufferedOutputStream;
021: import java.io.DataInputStream;
022: import java.io.FileInputStream;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.OutputStream;
027: import java.net.URL;
028:
029: /*
030: * File Copy Utilities. Some utilities that java.io doesn't give us.
031: *
032: * copy() - copies one file source to another file destination.
033: * copyFromURL)() - copies from a URL source to a file destination.
034: *
035: * NOTE: tried to be a good Commons-citizen and use io out of the sandbox
036: * at the time it was dependent on an older version of commons-lang for a predicate class bs bs
037: *
038: * @author David S. Taylor <a href="mailto:taylor@apache.org">David Sean Taylor</a>
039: * @version $Id: FileCopy.java 516448 2007-03-09 16:25:47Z ate $
040: */
041:
042: public class FileCopy {
043: public static final int BUFFER_SIZE = 4096;
044:
045: /*
046: * Copies one file source to another file destination.
047: *
048: * @param source The source file.
049: * @param destination The destination file.
050: * @throws IOException When an IO error occurs, this exception is thrown.
051: */
052: public static final void copy(String source, String destination)
053: throws IOException {
054: byte[] buffer = new byte[BUFFER_SIZE];
055: BufferedInputStream input;
056: BufferedOutputStream output;
057:
058: input = new BufferedInputStream(new FileInputStream(source));
059: output = new BufferedOutputStream(new FileOutputStream(
060: destination));
061:
062: copyStream(input, output, buffer);
063:
064: input.close();
065: output.close();
066: }
067:
068: /*
069: * Copies from a URL source to a file destination.
070: *
071: * @param source The source URL.
072: * @param destination The destination file.
073: * @throws IOException When an IO error occurs, this exception is thrown.
074: */
075: public static final void copyFromURL(String source,
076: String destination) throws IOException {
077: byte[] buffer = new byte[BUFFER_SIZE];
078: URL url = new URL(source);
079: BufferedInputStream input;
080: BufferedOutputStream output;
081:
082: input = new BufferedInputStream(new DataInputStream(url
083: .openStream()));
084: output = new BufferedOutputStream(new FileOutputStream(
085: destination));
086:
087: copyStream(input, output, buffer);
088:
089: input.close();
090: output.close();
091: }
092:
093: /*
094: * Generic copy from a input stream to an output stream.
095: *
096: * @param input The source input stream.
097: * @param output The destination output stream.
098: * @param buffer The user provided buffer.
099: * @throws IOException When an IO error occurs, this exception is thrown.
100: */
101: public static final void copyStream(InputStream input,
102: OutputStream output, byte[] buffer) throws IOException {
103: int bytesRead;
104:
105: while ((bytesRead = input.read(buffer)) != -1)
106: output.write(buffer, 0, bytesRead);
107: }
108:
109: }
|