<commit_before>/*
 * Copyright  1999-2004 The Apache Software Foundation
 *
 *  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.
 *
 */

/* $Id: FileUtil.java,v 1.14 2004/04/30 13:18:42 andreas Exp $  */

package org.apache.lenya.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.util.StringTokenizer;

import org.apache.log4j.Category;


/**
 * DOCUMENT ME!
 */
public class FileUtil {
    private static Category log = Category.getInstance(FileUtil.class);
    
    /**
     * DOCUMENT ME!
     *
     * @param args DOCUMENT ME!
     */
    public static void main(String[] args) {
        if (args.length == 0) {
            System.err.println("Usage: java " + new FileUtil().getClass().getName());

            return;
        }

        if (args[0].equals("--copy")) {
            if (args.length != 3) {
                System.err.println("Usage: --copy source destination");

                return;
            }

            try {
                System.err.println("cp " + args[1] + " " + args[2]);
                copy(args[1], args[2]);
            } catch (FileNotFoundException e) {
                System.err.println(e);
            } catch (IOException e) {
                System.err.println(e);
            }

            return;
        }

        if (args[0].equals("--concatPath")) {
            // FIXME:
            File file = org.apache.lenya.util.FileUtil.file("/root/temp/jpf-1.9/java/lenya/x/xps/samples/invoices/invoices",
                    "../addresses/lenya.xml");
            System.out.println(file.getAbsolutePath());
        } else {
        }
    }

    /**
     * Copying a file
     *
     * @param source_name DOCUMENT ME!
     * @param destination_name DOCUMENT ME!
     *
     * @throws FileNotFoundException DOCUMENT ME!
     * @throws IOException DOCUMENT ME!
     */
    public static void copy(String source_name, String destination_name)
        throws FileNotFoundException, IOException {
        InputStream source = new FileInputStream(source_name);
        File destination_file = new File(destination_name);
        File parent = new File(destination_file.getParent());

        if (!parent.exists()) {
            parent.mkdirs();
            log.warn("Directory has been created: " + parent.getAbsolutePath());
        }

        OutputStream destination = new FileOutputStream(destination_name);
        byte[] bytes_buffer = new byte[1024];
        int bytes_read;

        while ((bytes_read = source.read(bytes_buffer)) >= 0) {
            destination.write(bytes_buffer, 0, bytes_read);
        }
    }
    
    /**
     * Copy a single File or a complete Directory including its Contents.
     *
     * @param src the source File.
     * @param dest the destiantion File.
     *
     * @throws FileNotFoundException if the source File does not exists.
     * @throws IOException if an error occures in the io system.
     */
    public static void copy(File src, File dest)
        throws FileNotFoundException, IOException {
        
        if (src.isFile()) {
            copySingleFile(src, dest);
        } else {
            dest.mkdirs();
            File[] contents = src.listFiles();
            for (int i = 0; i < contents.length; i++) {
                String destPath = dest.getAbsolutePath() + File.separator + contents[i].getName();
                copy(contents[i], new File(destPath));
            }
        }
    }
    
    /**
     * Copy a single File.
     *
     * @param src the source File.
     * @param dest the destiantion File.
     *
     * @throws FileNotFoundException if the source File does not exists.
     * @throws IOException if an error occures in the io system.
     */
    protected static void copySingleFile(File src, File dest)
        throws FileNotFoundException, IOException {

        dest.getParentFile().mkdirs();
        dest.createNewFile();
        FileChannel srcChannel = new FileInputStream(src).getChannel();
        FileChannel destChannel = new FileOutputStream(dest).getChannel();
        srcChannel.transferTo(0, srcChannel.size(), destChannel);
        srcChannel.close();
        destChannel.close();
    }    
    

    /**
     * Returns a file by specifying an absolute directory name and a relative file name
     *
     * @param absoluteDir DOCUMENT ME!
     * @param relativeFile DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public static File file(String absoluteDir, String relativeFile) {
        File file = new File(fileName(absoluteDir, relativeFile));

        return file;
    }<commit_msg>Exchanged usage of nio.Channel.tranferTo with ...excalibur.io.FileUtil since Linux 2.6.x Kernels have an issue with jdk 1.4.x. See http://swforum.sun.com/jive/thread.jspa?threadID=46416 Of course transferTo is most fucking quick :)<commit_after>/*
 * Copyright  1999-2004 The Apache Software Foundation
 *
 *  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.
 *
 */

/* $Id: FileUtil.java,v 1.15 2004/07/21 15:55:10 roku Exp $  */

package org.apache.lenya.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.StringTokenizer;

import org.apache.log4j.Category;


/**
 * DOCUMENT ME!
 */
public class FileUtil {
    private static Category log = Category.getInstance(FileUtil.class);
    
    /**
     * DOCUMENT ME!
     *
     * @param args DOCUMENT ME!
     */
    public static void main(String[] args) {
        if (args.length == 0) {
            System.err.println("Usage: java " + new FileUtil().getClass().getName());

            return;
        }

        if (args[0].equals("--copy")) {
            if (args.length != 3) {
                System.err.println("Usage: --copy source destination");

                return;
            }

            try {
                System.err.println("cp " + args[1] + " " + args[2]);
                copy(args[1], args[2]);
            } catch (FileNotFoundException e) {
                System.err.println(e);
            } catch (IOException e) {
                System.err.println(e);
            }

            return;
        }

        if (args[0].equals("--concatPath")) {
            // FIXME:
            File file = org.apache.lenya.util.FileUtil.file("/root/temp/jpf-1.9/java/lenya/x/xps/samples/invoices/invoices",
                    "../addresses/lenya.xml");
            System.out.println(file.getAbsolutePath());
        } else {
        }
    }

    /**
     * Copying a file
     *
     * @param source_name DOCUMENT ME!
     * @param destination_name DOCUMENT ME!
     *
     * @throws FileNotFoundException DOCUMENT ME!
     * @throws IOException DOCUMENT ME!
     */
    public static void copy(String source_name, String destination_name)
        throws FileNotFoundException, IOException {
        InputStream source = new FileInputStream(source_name);
        File destination_file = new File(destination_name);
        File parent = new File(destination_file.getParent());

        if (!parent.exists()) {
            parent.mkdirs();
            log.warn("Directory has been created: " + parent.getAbsolutePath());
        }

        OutputStream destination = new FileOutputStream(destination_name);
        byte[] bytes_buffer = new byte[1024];
        int bytes_read;

        while ((bytes_read = source.read(bytes_buffer)) >= 0) {
            destination.write(bytes_buffer, 0, bytes_read);
        }
    }
    
    /**
     * Copy a single File or a complete Directory including its Contents.
     *
     * @param src the source File.
     * @param dest the destiantion File.
     *
     * @throws FileNotFoundException if the source File does not exists.
     * @throws IOException if an error occures in the io system.
     */
    public static void copy(File src, File dest)
        throws FileNotFoundException, IOException {
        
        if (src.isFile()) {
            copySingleFile(src, dest);
        } else {
            dest.mkdirs();
            File[] contents = src.listFiles();
            for (int i = 0; i < contents.length; i++) {
                String destPath = dest.getAbsolutePath() + File.separator + contents[i].getName();
                copy(contents[i], new File(destPath));
            }
        }
    }
    
    /**
     * Copy a single File.
     *
     * @param src the source File.
     * @param dest the destiantion File.
     *
     * @throws FileNotFoundException if the source File does not exists.
     * @throws IOException if an error occures in the io system.
     */
    protected static void copySingleFile(File src, File dest)
        throws FileNotFoundException, IOException {

        dest.getParentFile().mkdirs();
        dest.createNewFile();        
        org.apache.avalon.excalibur.io.FileUtil.copyFile(src, dest);
    }    
    

    /**
     * Returns a file by specifying an absolute directory name and a relative file name
     *
     * @param absoluteDir DOCUMENT ME!
     * @param relativeFile DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public static File file(String absoluteDir, String relativeFile) {
        File file = new File(fileName(absoluteDir, relativeFile));

        return file;
    }
{'fraction_non_alphanumeric': 0.10632839224629419, 'fraction_numerical': 0.009882174078297225, 'mean_word_length': 2.184568835098336, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2392232', 'n_tokens_mistral': 2982, 'n_tokens_neox': 2851, 'n_words': 840}