<commit_before>
/*
 * Copyright (C) 2011 <NAME>. All rights reserved.
 *
 * $Id$
 */

package org.dellroad.stuff.io;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

/**
 * Presents an {@link java.io.OutputStream OutputStream} interface given a {@link ReadCallback} that can read from an
 * {@link InputStream}. A separate thread is created to perform the actual reading.
 *
 * <p>
 * If data is written beyond what the reader is willing to consume, an {@link IOException} is thrown.
 *
 * @since 1.0.82
 */
public class NullModemOutputStream extends FilterOutputStream {

    private final PipedInputStream input;

    /**
     * Constructor.
     *
     * @param reader    {@link InputStream} reader callback
     * @param name      name for this instance; used to create the name of the background thread
     */
    public NullModemOutputStream(final ReadCallback reader, String name) {
        super(new PipedOutputStream());

        // Sanity check
        if (reader == null)
            throw new IllegalArgumentException("null reader");

        // Create other end of pipe
        try {
            this.input = new PipedInputStream(this.getPipedOutputStream());
        } catch (IOException e) {
            throw new RuntimeException("unexpected exception", e);
        }

        // Launch reader thread
        Thread thread = new ReaderThread(reader, input, name);
        thread.setDaemon(true);
        thread.start();
    }

    /**
     * Get the wrapped stream cast as a {@link PipedOutputStream}.
     */
    protected PipedOutputStream getPipedOutputStream() {
        return (PipedOutputStream)this.out;
    }

    /**
     * Ensure output stream is closed when this instance is no longer referenced.
     *
     * <p>
     * This ensures the reader thread wakes up (and exits, avoiding a memory leak) when an instance of this class
     * is created but never read from.
     */
    @Override
    protected void finalize() throws Throwable {
        try {
            try {
                this.getPipedOutputStream().close();
            } catch (IOException e) {
                // ignore
            }
        } finally {
            super.finalize();
        }
    }

    /**
     * Callback interface used by {@link NullModemOutputStream}.
     */
    public interface ReadCallback {

        /**
         * Read from the given input stream.
         *
         * <p>
         * This method will be invoked (once) asynchronously in a dedicated reader thread.
         * </p>
         *
         * @throws IOException if an I/O error occurs
         */
        void readFrom(InputStream output) throws IOException;
    }

    /**<commit_msg>Remove unnecessary field.<commit_after>
/*
 * Copyright (C) 2011 <NAME>. All rights reserved.
 *
 * $Id$
 */

package org.dellroad.stuff.io;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

/**
 * Presents an {@link java.io.OutputStream OutputStream} interface given a {@link ReadCallback} that can read from an
 * {@link InputStream}. A separate thread is created to perform the actual reading.
 *
 * <p>
 * If data is written beyond what the reader is willing to consume, an {@link IOException} is thrown.
 *
 * @since 1.0.82
 */
public class NullModemOutputStream extends FilterOutputStream {

    /**
     * Constructor.
     *
     * @param reader    {@link InputStream} reader callback
     * @param name      name for this instance; used to create the name of the background thread
     */
    public NullModemOutputStream(final ReadCallback reader, String name) {
        super(new PipedOutputStream());

        // Sanity check
        if (reader == null)
            throw new IllegalArgumentException("null reader");

        // Create other end of pipe
        PipedInputStream input;
        try {
            input = new PipedInputStream(this.getPipedOutputStream());
        } catch (IOException e) {
            throw new RuntimeException("unexpected exception", e);
        }

        // Launch reader thread
        Thread thread = new ReaderThread(reader, input, name);
        thread.setDaemon(true);
        thread.start();
    }

    /**
     * Get the wrapped stream cast as a {@link PipedOutputStream}.
     */
    protected PipedOutputStream getPipedOutputStream() {
        return (PipedOutputStream)this.out;
    }

    /**
     * Ensure output stream is closed when this instance is no longer referenced.
     *
     * <p>
     * This ensures the reader thread wakes up (and exits, avoiding a memory leak) when an instance of this class
     * is created but never read from.
     */
    @Override
    protected void finalize() throws Throwable {
        try {
            try {
                this.getPipedOutputStream().close();
            } catch (IOException e) {
                // ignore
            }
        } finally {
            super.finalize();
        }
    }

    /**
     * Callback interface used by {@link NullModemOutputStream}.
     */
    public interface ReadCallback {

        /**
         * Read from the given input stream.
         *
         * <p>
         * This method will be invoked (once) asynchronously in a dedicated reader thread.
         * </p>
         *
         * @param input input providing the data written to the corresponding {@link NullModemOutputStream}
         * @throws IOException if an I/O error occurs
         */
        void readFrom(InputStream input) throws IOException;
    }

    /**
{'fraction_non_alphanumeric': 0.09020300088261253, 'fraction_numerical': 0.002824360105913504, 'mean_word_length': 2.1867266591676042, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '6343091', 'n_tokens_mistral': 1479, 'n_tokens_neox': 1435, 'n_words': 516}