/**************************************************************************/ /* /* IOUtil.java /* Copyright (C) 2001 Bjorn Bringert (bjorn@mumblebee.com) /* /* This program is free software; you can redistribute it and/or /* modify it under the terms of the GNU General Public License /* as published by the Free Software Foundation; either version 2 /* of the License, or (at your option) any later version. /* /* This program is distributed in the hope that it will be useful, /* but WITHOUT ANY WARRANTY; without even the implied warranty of /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /* GNU General Public License for more details. /* /* You should have received a copy of the GNU General Public License /* along with this program; if not, write to the Free Software /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. /* /**************************************************************************/ package com.mumblebee.util; import java.io.*; import java.net.URL; /** * Some I/O utility methods. * @author Bjorn Bringert */ public class IOUtil { /** Prevents instantiation. */ private IOUtil() {} /** * Copies a file. * @param src The file to copy from * @param dest The file to copy to * @param overwrite if true, the destination file will be overwritten if it exists. * @throws FileExistsException if overwrite is false and the destination file exists * @throws FileNotFoundException if the source file does not exist * @throws IOException if an I/O error occurs */ public static void copy(File src, File dest, boolean overwrite) throws FileExistsException, FileNotFoundException, IOException { FileInputStream in = new FileInputStream(src); save(in, dest, overwrite); } /** * Saves the contents of a stream to a file and closes the stream. * @param inStream The stream to read from * @param file The file to write to * @param overwrite if true, the file will be overwritten if it exists. * @throws FileExistsException if overwrite is false and the file exists * @throws IOException if an I/O error occurs */ public static void save(InputStream inStream, File file, boolean overwrite) throws FileExistsException, IOException { if (!overwrite && file.exists()) throw new FileExistsException(file + " already exists"); FileOutputStream out = new FileOutputStream(file); copy(inStream, out); out.close(); inStream.close(); } /** * Writes all data from an input stream to an output stream. Doesn't close any * of the streams. * @param in the stream to read from * @param out the stream to write to * @throws IOException if an I/O error occurs */ public static void copy(InputStream in, OutputStream out) throws IOException { byte[] buf = new byte[2048]; int r; while ((r = in.read(buf)) != -1) { out.write(buf, 0, r); } } }