/**

   Copyright 2010 OpenEngSB Division, Vienna University of Technology

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */
package org.openengsb.util;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class IO {

    private static Log log = LogFactory.getLog(IO.class);

    /**
     * Constructs a new {@code BufferedReader} that is reading from the
     * specified file.
     */
    public static BufferedReader newBufferedReader(final String filename) throws FileNotFoundException {
        return newBufferedReader(new FileInputStream(filename));
    }

    /**
     * Constructs a new {@code BufferedReader} that is readinf from the
     * specified {@code InputStream}
     */
    public static BufferedReader newBufferedReader(final InputStream in) {
        return new BufferedReader(new InputStreamReader(in));
    }

    /**
     * Constructs an relative path with an absolute base path and an absolute
     * path that is deeper inside the base path.
     */
    public static String relativize(final String base, final String path) {
        return new File(base).toURI().relativize(new File(path).toURI()).getPath();
    }

    /**
     * Writes a String into a file on a specific file path.
     */
    public static void write(final String toWrite, final String filePath) throws IOException {
        write(toWrite, new File(filePath));
    }

    /**
     * Writes a String into a specific file uri.
     */
    public static void write(final String toWrite, final URI fileUri) throws IOException {
        write(toWrite, new File(fileUri));
    }

    /**
     * Writes a String into a specific file.
     */
    public static void write(final String toWrite, final File file) throws IOException {
        final FileWriter fw = new FileWriter(file);
        fw.write(toWrite);
        fw.flush();
        fw.close();
    }

    /**
     * @see ClassLoader#getResource(String)
     */
    public static URL getResource(final String resourceName) {
        return ClassLoader.getSystemClassLoader().getResource(resourceName);
    }

    /**
     * @see ClassLoader#getResourceAsStream(String)
     * @param resourceName
     * @return
     */
    public static InputStream getResourceAsStream(final String resourceName) {
        return ClassLoader.getSystemClassLoader().getResourceAsStream(resourceName);
    }

    /**
     * Wait for an {@code Enter} at the stdin.
     */
    public static void waitForKeyPress() {
        final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        try {
            reader.readLine();
        } catch (final IOException e) {
            // no op
        }
    }

    /**
     * Copies a file from an stream to an file.
     */
    public static void copyFile(final InputStream f1, final File f2) throws IOException {
        IO.log.debug("Copy [" + f1.toString() + "] to [" + f2.getAbsolutePath() + "]...");
        final InputStream in = f1;
        final OutputStream out = new FileOutputStream(f2);

        final byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }

    /**
     * Copies one file into another one.
     */
    public static void copyFile(final File f1, final File f2) throws IOException {
        IO.log.debug("Copy [" + f1.getAbsolutePath() + "] to [" + f2.getAbsolutePath() + "]...");
        final InputStream in = new FileInputStream(f1);
        final OutputStream out = new FileOutputStream(f2);

        final byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }

    /**
     * Close quietly
     */
    public static void closeQuietly(Closeable closeable) {
        if (closeable == null) {
            return;
        }

        try {
            closeable.close();
        } catch (IOException e) {
        }
    }

    /**
     * Scans a directory recursively for files and return all files found in an
     * {@link List}.
     */
    public static List<File> recursiveScanDirectoryForFiles(final File directory) {
        final List<File> retVal = new ArrayList<File>();
        recursiveScan(directory, retVal);
        return retVal;
    }

    /**
     * Internal helper method to recursively scan a directory for files.
     */
    private static void recursiveScan(final File directory, final List<File> actualList) {
        if (!directory.isDirectory()) {
            return;
        }
        for (final File file : directory.listFiles()) {
            if (file.isDirectory()) {
                recursiveScan(file, actualList);
            } else {
                actualList.add(file);
            }
        }
    }

}

@@ -689,34 +689,8 @@
 er;%0A
-import java.io.Closeable;%0A
 impo
@@ -706,16 +706,16 @@
 o.File;%0A
+
 import j
@@ -780,41 +780,8 @@
 on;%0A
-import java.io.FileOutputStream;%0A
 impo
@@ -851,32 +851,32 @@
 io.InputStream;%0A
+
 import java.io.I
@@ -897,37 +897,8 @@
 er;%0A
-import java.io.OutputStream;%0A
 impo
@@ -3214,1673 +3214,8 @@
  %7D%0A%0A
-    /**%0A     * Wait for an %7B@code Enter%7D at the stdin.%0A     */%0A    public static void waitForKeyPress() %7B%0A        final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));%0A        try %7B%0A            reader.readLine();%0A        %7D catch (final IOException e) %7B%0A            // no op%0A        %7D%0A    %7D%0A%0A    /**%0A     * Copies a file from an stream to an file.%0A     */%0A    public static void copyFile(final InputStream f1, final File f2) throws IOException %7B%0A        IO.log.debug(%22Copy %5B%22 + f1.toString() + %22%5D to %5B%22 + f2.getAbsolutePath() + %22%5D...%22);%0A        final InputStream in = f1;%0A        final OutputStream out = new FileOutputStream(f2);%0A%0A        final byte%5B%5D buf = new byte%5B1024%5D;%0A        int len;%0A        while ((len = in.read(buf)) %3E 0) %7B%0A            out.write(buf, 0, len);%0A        %7D%0A        in.close();%0A        out.close();%0A    %7D%0A%0A    /**%0A     * Copies one file into another one.%0A     */%0A    public static void copyFile(final File f1, final File f2) throws IOException %7B%0A        IO.log.debug(%22Copy %5B%22 + f1.getAbsolutePath() + %22%5D to %5B%22 + f2.getAbsolutePath() + %22%5D...%22);%0A        final InputStream in = new FileInputStream(f1);%0A        final OutputStream out = new FileOutputStream(f2);%0A%0A        final byte%5B%5D buf = new byte%5B1024%5D;%0A        int len;%0A        while ((len = in.read(buf)) %3E 0) %7B%0A            out.write(buf, 0, len);%0A        %7D%0A        in.close();%0A        out.close();%0A    %7D%0A%0A    /**%0A     * Close quietly%0A     */%0A    public static void closeQuietly(Closeable closeable) %7B%0A        if (closeable == null) %7B%0A            return;%0A        %7D%0A%0A        try %7B%0A            closeable.close();%0A        %7D catch (IOException e) %7B%0A        %7D%0A    %7D%0A%0A
     

remove copyFile