<commit_before>package org.analogweb.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;

import org.analogweb.util.logging.Log;
import org.analogweb.util.logging.Logs;

/**
 * @author snowgoose
 */
public final class IOUtils {

    private static final Log log = Logs.getLog(IOUtils.class);

    public static void closeQuietly(Closeable closeable) {
        if (closeable == null) {
            return;
        }
        try {
            closeable.close();
        } catch (IOException e) {
            // nop.
            log.log("DU000007", e, closeable);
        }
    }

    public static int copyQuietly(InputStream input, OutputStream output) {
        try {
            return copyInternal(new BufferedInputStream(input), new BufferedOutputStream(output));
        } catch (IOException e) {
            log.log("DU000008", e, input);
            return -1;
        }
    }

    public static int copy(InputStream input, OutputStream output) throws IOException {
        return copyInternal(new BufferedInputStream(input), new BufferedOutputStream(output));
    }

    private static int copyInternal(InputStream input, OutputStream output) throws IOException {
        Assertion.notNull(input, InputStream.class.getName());
        Assertion.notNull(output, OutputStream.class.getName());
        try {
            int count = 0;
            int i;
            while ((i = input.read()) != -1) {
                output.write(i);
                count++;
            }
            output.flush();
            return count;
        } finally {
            closeQuietly(input);
        }
    }

    public static String toString(InputStream in) throws IOException {
        return toString(new InputStreamReader(in));
    }

    public static String toString(Reader reader) throws IOException {
        return toString(reader, 4096);
    }

    public static String toString(Reader reader, int bufferSize) throws IOException {
        StringBuilder sb = new StringBuilder();
        char[] buffer = new char[bufferSize];
        int n = 0;
        while (-1 != (n = reader.read(buffer))) {
            sb.append(buffer, 0, n);
        }
        return sb.toString();
    }

    public static int avairable(InputStream in) {
        try {
            return in.available();
        } catch (IOException e) {
            return -1;
        }
    }
}<commit_msg>Improve IOUtils performance.<commit_after>package org.analogweb.util;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

import org.analogweb.util.logging.Log;
import org.analogweb.util.logging.Logs;

/**
 * @author snowgoose
 */
public final class IOUtils {

    private static final Log log = Logs.getLog(IOUtils.class);

    public static void closeQuietly(Closeable closeable) {
        if (closeable == null) {
            return;
        }
        try {
            closeable.close();
        } catch (IOException e) {
            // nop.
            log.log("DU000007", e, closeable);
        }
    }

    public static int copyQuietly(InputStream input, OutputStream output) {
        try {
            return copy(input, output, 8192);
        } catch (IOException e) {
            log.log("DU000008", e, input);
            return -1;
        }
    }

    public static int copy(InputStream input, OutputStream output) throws IOException {
        return copy(input, output, 8192);
    }

    public static int copy(InputStream input, OutputStream output, int bufferSize)
            throws IOException {
        Assertion.notNull(input, InputStream.class.getName());
        Assertion.notNull(output, OutputStream.class.getName());
        ReadableByteChannel in = Channels.newChannel(input);
        WritableByteChannel out = Channels.newChannel(output);
        try {
            int count = 0;
            ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
            while (in.read(buffer) != -1) {
                buffer.flip();
                count += out.write(buffer);
                buffer.clear();
            }
            output.flush();
            return count;
        } finally {
            closeQuietly(input);
        }
    }

    public static String toString(InputStream in) throws IOException {
        return toString(new InputStreamReader(in));
    }

    public static String toString(Reader reader) throws IOException {
        return toString(reader, 4096);
    }

    public static String toString(Reader reader, int bufferSize) throws IOException {
        StringBuilder sb = new StringBuilder();
        CharBuffer buffer = CharBuffer.allocate(bufferSize);
        while (-1 != reader.read(buffer)) {
            buffer.flip();
            sb.append(buffer);
            buffer.clear();
        }
        return sb.toString();
    }

    public static int avairable(InputStream in) {
        try {
            return in.available();
        } catch (IOException e) {
            return -1;
        }
    }
}
{'fraction_non_alphanumeric': 0.10079379730478125, 'fraction_numerical': 0.009599409267122024, 'mean_word_length': 2.192692987625221, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2131539', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1436, 'n_words': 369}