<commit_before>package com.googlecode.totallylazy;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import static com.googlecode.totallylazy.Bytes.bytes;
import static com.googlecode.totallylazy.Callables.doThen;
import static com.googlecode.totallylazy.Callables.returns;
import static com.googlecode.totallylazy.Closeables.using;
import static com.googlecode.totallylazy.Predicates.is;
import static com.googlecode.totallylazy.Sequences.sequence;
import static java.lang.String.format;
import static java.lang.System.getProperty;
import static java.util.UUID.randomUUID;

public class Files {
    public static final File TEMP_DIR = new File(getProperty("java.io.tmpdir"));

    public static String relativePath(File folder, File file) {
        return folder.toURI().relativize(file.toURI()).getPath();
    }

    public static Predicate<? super File> isFile() {
        return new Predicate<File>() {
            public boolean matches(File file) {
                return file.isFile();
            }
        };
    }

    public static Predicate<? super File> hasSuffix(final String suffix) {
        return new Predicate<File>() {
            public boolean matches(File file) {
                return file.getName().endsWith("." + suffix);
            }
        };
    }

    public static Predicate<? super File> isDirectory() {
        return new Predicate<File>() {
            public boolean matches(File file) {
                return file.isDirectory();
            }
        };
    }

    public static Function1<File, String> name() {
        return new Function1<File, String>() {
            public String call(File file) throws Exception {
                return file.getName();
            }
        };
    }

    public static Function1<File, String> path() {
        return new Function1<File, String>() {
            public String call(File file) throws Exception {
                return file.getPath();
            }
        };
    }

    public static Function1<File, File> parent() {
        return new Function1<File, File>() {
            public File call(File file) throws Exception {
                return file.getParentFile();
            }
        };
    }

    public static File temporaryDirectory() {
        return TEMP_DIR;
    }

    public static File temporaryDirectory(String name) {
        return directory(TEMP_DIR, name);
    }

    public static File emptyTemporaryDirectory(String name) {
        File directory = directory(TEMP_DIR, name);
        deleteFiles(directory);
        return directory;
    }

    public static boolean delete(File file) {
        return deleteFiles(file) && file.delete();
    }

    public static boolean deleteFiles(File file) {
        return recursiveFiles(file).map(delete()).forAll(is(true));
    }

    public static File temporaryFile() {
        return temporaryFile(TEMP_DIR);
    }

    public static File temporaryFile(File directory) {
        try {
            File file = new File(directory, randomFilename());
            file.deleteOnExit();
            file.createNewFile();
            return file;
        } catch (IOException e) {
            throw LazyException.lazyException(e);
        }
    }

    public static File workingDirectory() {
        return new File(getProperty("user.dir"));
    }

    public static String randomFilename() {
        return randomUUID().toString();
    }

    public static Sequence<File> files(File directory) {
        return sequence(directory.listFiles());
    }

    public static Sequence<File> recursiveFiles(final File directory) {
        return files(directory).flatMap(recursiveFiles());
    }

    public static Function1<File, Iterable<File>> recursiveFiles() {
        return new Function1<File, Iterable<File>>() {
            public Iterable<File> call(File file) throws Exception {
                return file.isDirectory() ? recursiveFiles(file).add(file) : sequence(file);
            }
        };
    }

    public static File write(byte[] bytes, File file) {
        return write(bytes, file, false);
    }

    public static File append(byte[] bytes, File file) {
        return write(bytes, file, true);
    }

    public static File write(byte[] bytes, File file, boolean append) {
        try {
            return using(new FileOutputStream(file, append), doThen(Runnables.write(bytes), returns(file)));
        } catch (FileNotFoundException e) {
            throw LazyException.lazyException(e);
        }
    }

    public static Function1<InputStream, Void> write(final File output) {
        return new Function1<InputStream, Void>() {
            @Override
            public Void call(InputStream inputStream) throws Exception {
                write(bytes(inputStream), output);
                return null;
            }
        };
    }

    public static Function1<File, Boolean> delete() {
        return new Function1<File, Boolean>() {
            public Boolean call(File file) throws Exception {
                return file.delete();
            }
        };
    }

    public static Function1<String, File> asFile() {
        return new Function1<String, File>() {
            public File call(String name) throws Exception {
                return new File(name);
            }<commit_msg>Made Files.write not buffer content into byte[] (merge)<commit_after>package com.googlecode.totallylazy;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import static com.googlecode.totallylazy.Bytes.bytes;
import static com.googlecode.totallylazy.Callables.doThen;
import static com.googlecode.totallylazy.Callables.returns;
import static com.googlecode.totallylazy.Closeables.using;
import static com.googlecode.totallylazy.Predicates.is;
import static com.googlecode.totallylazy.Sequences.sequence;
import static java.lang.String.format;
import static java.lang.System.getProperty;
import static java.util.UUID.randomUUID;

public class Files {
    public static final File TEMP_DIR = new File(getProperty("java.io.tmpdir"));

    public static String relativePath(File folder, File file) {
        return folder.toURI().relativize(file.toURI()).getPath();
    }

    public static Predicate<? super File> isFile() {
        return new Predicate<File>() {
            public boolean matches(File file) {
                return file.isFile();
            }
        };
    }

    public static Predicate<? super File> hasSuffix(final String suffix) {
        return new Predicate<File>() {
            public boolean matches(File file) {
                return file.getName().endsWith("." + suffix);
            }
        };
    }

    public static Predicate<? super File> isDirectory() {
        return new Predicate<File>() {
            public boolean matches(File file) {
                return file.isDirectory();
            }
        };
    }

    public static Function1<File, String> name() {
        return new Function1<File, String>() {
            public String call(File file) throws Exception {
                return file.getName();
            }
        };
    }

    public static Function1<File, String> path() {
        return new Function1<File, String>() {
            public String call(File file) throws Exception {
                return file.getPath();
            }
        };
    }

    public static Function1<File, File> parent() {
        return new Function1<File, File>() {
            public File call(File file) throws Exception {
                return file.getParentFile();
            }
        };
    }

    public static File temporaryDirectory() {
        return TEMP_DIR;
    }

    public static File temporaryDirectory(String name) {
        return directory(TEMP_DIR, name);
    }

    public static File emptyTemporaryDirectory(String name) {
        File directory = directory(TEMP_DIR, name);
        deleteFiles(directory);
        return directory;
    }

    public static boolean delete(File file) {
        return deleteFiles(file) && file.delete();
    }

    public static boolean deleteFiles(File file) {
        return recursiveFiles(file).map(delete()).forAll(is(true));
    }

    public static File temporaryFile() {
        return temporaryFile(TEMP_DIR);
    }

    public static File temporaryFile(File directory) {
        try {
            File file = new File(directory, randomFilename());
            file.deleteOnExit();
            file.createNewFile();
            return file;
        } catch (IOException e) {
            throw LazyException.lazyException(e);
        }
    }

    public static File workingDirectory() {
        return new File(getProperty("user.dir"));
    }

    public static String randomFilename() {
        return randomUUID().toString();
    }

    public static Sequence<File> files(File directory) {
        return sequence(directory.listFiles());
    }

    public static Sequence<File> recursiveFiles(final File directory) {
        return files(directory).flatMap(recursiveFiles());
    }

    public static Function1<File, Iterable<File>> recursiveFiles() {
        return new Function1<File, Iterable<File>>() {
            public Iterable<File> call(File file) throws Exception {
                return file.isDirectory() ? recursiveFiles(file).add(file) : sequence(file);
            }
        };
    }

    public static File write(byte[] bytes, File file) {
        return write(bytes, file, false);
    }

    public static File append(byte[] bytes, File file) {
        return write(bytes, file, true);
    }

    public static File write(byte[] bytes, File file, boolean append) {
        try {
            return using(new FileOutputStream(file, append), doThen(Runnables.write(bytes), returns(file)));
        } catch (FileNotFoundException e) {
            throw LazyException.lazyException(e);
        }
    }

    public static Function1<InputStream, Void> write(final File output) {
        return new Function1<InputStream, Void>() {
            @Override
            public Void call(InputStream inputStream) throws Exception {
                Streams.copyAndClose(inputStream, new FileOutputStream(output));
                return null;
            }
        };
    }

    public static Function1<File, Boolean> delete() {
        return new Function1<File, Boolean>() {
            public Boolean call(File file) throws Exception {
                return file.delete();
            }
        };
    }

    public static Function1<String, File> asFile() {
        return new Function1<String, File>() {
            public File call(String name) throws Exception {
                return new File(name);
            }
{'fraction_non_alphanumeric': 0.10012955765315565, 'fraction_numerical': 0.002591153063113085, 'mean_word_length': 2.3489308955686394, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2545902', 'n_tokens_mistral': 2815, 'n_tokens_neox': 2737, 'n_words': 750}