001: /**
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */package com.tc.util.io;
005:
006: import org.apache.commons.io.IOUtils;
007:
008: import java.io.File;
009: import java.io.FileInputStream;
010: import java.io.FileOutputStream;
011: import java.io.IOException;
012: import java.util.Iterator;
013: import java.util.LinkedList;
014: import java.util.List;
015:
016: public class TCFileUtils {
017:
018: /**
019: * Given a resource path, returns the File object of that resource
020: */
021: public static File getResourceFile(String resource) {
022: return org.apache.commons.io.FileUtils.toFile(TCFileUtils.class
023: .getResource(resource));
024: }
025:
026: /**
027: * deletes all files with matching extension. Does not recurse into sub directories.
028: */
029: public static void forceDelete(File directory, String extension)
030: throws IOException {
031: Iterator files = org.apache.commons.io.FileUtils.listFiles(
032: directory, new String[] { extension }, false)
033: .iterator();
034: while (files.hasNext()) {
035: File f = (File) files.next();
036: org.apache.commons.io.FileUtils.forceDelete(f);
037: }
038: }
039:
040: /**
041: * copy one file to another. Can also copy directories
042: */
043: public static void copyFile(File src, File dest) throws IOException {
044: List queue = new LinkedList();
045: queue.add(new CopyTask(src.getCanonicalFile(), dest
046: .getCanonicalFile()));
047:
048: while (queue.size() > 0) {
049: CopyTask item = (CopyTask) queue.remove(0);
050: if (item.getSrc().isDirectory()) {
051: File destDir = item.getDest();
052: destDir.mkdirs();
053:
054: if (!destDir.isDirectory()) {
055: throw new IOException(
056: "Destination directory does not exist: "
057: + destDir);
058: }
059:
060: String[] list = item.getSrc().list();
061: for (int i = 0; i < list.length; i++) {
062: File _src = new File(item.getSrc(), list[i]);
063: File _dest = new File(item.getDest(), list[i]);
064: queue.add(new CopyTask(_src, _dest));
065: }
066: } else if (item.getSrc().isFile()) {
067: try {
068: doCopy(item.getSrc(), item.getDest());
069: } catch (IOException e) {
070: System.err.println("Error copying: ["
071: + item.getSrc() + "] to [" + item.getDest()
072: + "]");
073: }
074: } else {
075: throw new IOException(item.getSrc()
076: + " is neither a file or a directory");
077: }
078: }
079:
080: }
081:
082: private static void doCopy(File src, File dest) throws IOException {
083: FileInputStream in = null;
084: FileOutputStream out = null;
085: byte[] buffer = new byte[1024 * 8];
086: int count;
087: try {
088: in = new FileInputStream(src);
089: out = new FileOutputStream(dest);
090: while ((count = in.read(buffer)) >= 0) {
091: out.write(buffer, 0, count);
092: }
093: } finally {
094: IOUtils.closeQuietly(in);
095: IOUtils.closeQuietly(out);
096: }
097: }
098:
099: private static class CopyTask {
100: private final File src;
101: private final File dest;
102:
103: public CopyTask(File src, File dest) {
104: this .src = src;
105: this .dest = dest;
106: }
107:
108: public File getSrc() {
109: return src;
110: }
111:
112: public File getDest() {
113: return dest;
114: }
115: }
116: }
|