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