<commit_before>/* $Id: IOUtilities.java 302 2009-05-11 13:46:21Z davemckain $
 *
 * Copyright (c) 2009 University of Edinburgh.
 * All Rights Reserved
 */
package uk.ac.ed.ph.snuggletex.internal.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.nio.channels.FileChannel;

/**
 * A collection of vaguely useful utilities for doing common I/O and File-related tasks.
 * 
 * @author  <NAME>
 * @version $Revision: 302 $
 */
public final class IOUtilities {
    
    /** Buffer size when transferring streams */
    public static int BUFFER_SIZE = 32 * 1024;

    /** Maximum size of characters we'll read from a text stream before complaining */
    public static int MAX_TEXT_STREAM_SIZE = 1024 * 1024;

    /**
     * This is a filename filter which ignores CVS control directories and
     * .cvsBLAH and .#BLAH files.
     */
    public static final FilenameFilter CVS_FILE_FILTER = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return !(name.equals("CVS") || name.startsWith(".cvs") || name.startsWith(".#"));
        }
    };

    //----------------------------------------------------------------------------

    /**
     * Simple method to ensure that a given directory exists. If the directory
     * does not exist then it is created, along with all required parents.
     *
     * @throws IOException if creation could not succeed for some reason.
     */
    public static void ensureDirectoryCreated(File directory) throws IOException {
        if (!directory.isDirectory()) {
            if (!directory.mkdirs()) {
                throw new IOException("Could not create directory " + directory);
            }
        }
    }

    /**
     * Simple method to ensure that a given File exists. If the File
     * does not exist then it is created, along with all required parent
     * directories.
     *
     * @throws IOException if creation could not succeed for some reason.
     */
    public static void ensureFileCreated(File file) throws IOException {
        /* Make sure parent exists */
        File parentDirectory = file.getParentFile();
        if (parentDirectory!=null) {
            ensureDirectoryCreated(parentDirectory);
        }
        /* Now create file */
        if (!file.isFile()) {
            if (!file.createNewFile()) {
                throw new IOException("Could not create file " + file);
            }
        }
    }

    //----------------------------------------------------------------------------
    
    /**
     * Ensures that as many of the given {@link Closeable}s are closed. If any fail to close, the
     * first Exception is thrown after an attempt has been made to close the rest.
     * <p>
     * For convenience, any number of the stream may be null, in which case they will be ignored.
     * 
     * @param streams "streams" to close
     * @throws IOException 
     */
    public static void ensureClose(Closeable... streams) throws IOException {
        IOException firstException = null;
        for (Closeable stream : streams) {
            if (stream!=null) {
                try {
                    stream.close();
                }
                catch (IOException e) {
                    firstException = e;<commit_msg>Changed the ensure...Created() methods to return the File passed to it, which can be convenient.<commit_after>/* $Id: IOUtilities.java 302 2009-05-11 13:46:21Z davemckain $
 *
 * Copyright (c) 2009 University of Edinburgh.
 * All Rights Reserved
 */
package uk.ac.ed.ph.snuggletex.internal.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.nio.channels.FileChannel;

/**
 * A collection of vaguely useful utilities for doing common I/O and File-related tasks.
 * 
 * @author  <NAME>
 * @version $Revision: 302 $
 */
public final class IOUtilities {
    
    /** Buffer size when transferring streams */
    public static int BUFFER_SIZE = 32 * 1024;

    /** Maximum size of characters we'll read from a text stream before complaining */
    public static int MAX_TEXT_STREAM_SIZE = 1024 * 1024;

    /**
     * This is a filename filter which ignores CVS control directories and
     * .cvsBLAH and .#BLAH files.
     */
    public static final FilenameFilter CVS_FILE_FILTER = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return !(name.equals("CVS") || name.startsWith(".cvs") || name.startsWith(".#"));
        }
    };

    //----------------------------------------------------------------------------

    /**
     * Simple method to ensure that a given directory exists. If the directory
     * does not exist then it is created, along with all required parents.
     *
     * @throws IOException if creation could not succeed for some reason.
     */
    public static File ensureDirectoryCreated(File directory) throws IOException {
        if (!directory.isDirectory()) {
            if (!directory.mkdirs()) {
                throw new IOException("Could not create directory " + directory);
            }
        }
        return directory;
    }

    /**
     * Simple method to ensure that a given File exists. If the File
     * does not exist then it is created, along with all required parent
     * directories.
     *
     * @throws IOException if creation could not succeed for some reason.
     */
    public static File ensureFileCreated(File file) throws IOException {
        /* Make sure parent exists */
        File parentDirectory = file.getParentFile();
        if (parentDirectory!=null) {
            ensureDirectoryCreated(parentDirectory);
        }
        /* Now create file */
        if (!file.isFile()) {
            if (!file.createNewFile()) {
                throw new IOException("Could not create file " + file);
            }
        }
        return file;
    }

    //----------------------------------------------------------------------------
    
    /**
     * Ensures that as many of the given {@link Closeable}s are closed. If any fail to close, the
     * first Exception is thrown after an attempt has been made to close the rest.
     * <p>
     * For convenience, any number of the stream may be null, in which case they will be ignored.
     * 
     * @param streams "streams" to close
     * @throws IOException 
     */
    public static void ensureClose(Closeable... streams) throws IOException {
        IOException firstException = null;
        for (Closeable stream : streams) {
            if (stream!=null) {
                try {
                    stream.close();
                }
                catch (IOException e) {
                    firstException = e;
{'fraction_non_alphanumeric': 0.1286022084567735, 'fraction_numerical': 0.010234311877188257, 'mean_word_length': 2.101043841336117, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6038308', 'n_tokens_mistral': 1935, 'n_tokens_neox': 1829, 'n_words': 673}