
/*
 * Copyright (C) 2011 Archie L. Cobbs. All rights reserved.
 *
 * $Id$
 */

package org.dellroad.stuff.io;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Presents an {@link InputStream} interface given a callback that can write to an {@link OutputStream}.
 * A separate thread is created to perform the actual writing.
 *
 * @since 1.0.74
 */
public class NullModemInputStream extends PipedInputStream {

    protected final Logger log = LoggerFactory.getLogger(getClass());

    private final CallableWriter writer;
    private final PipedOutputStream output;

    /**
     * Constructor.
     *
     * @param writer    {@link OutputStream} writer callback
     * @param name      name for this instance; used to create the name of the background thread
     */
    public NullModemInputStream(CallableWriter writer, String name) {
        if (writer == null)
            throw new IllegalArgumentException("null writer");
        this.writer = writer;
        try {
            this.output = new PipedOutputStream(this);
        } catch (IOException e) {
            throw new RuntimeException("impossible case", e);
        }
        Thread thread = new Thread(name) {
            @Override
            public void run() {
                NullModemInputStream.this.threadMain();
            }
        };
        thread.setDaemon(true);
        thread.start();
    }

    /**
     * Writer thread main entry point.
     */
    private void threadMain() {
        try {
            this.writer.writeTo(this.output);
            this.output.close();
        } catch (IOException e) {
            // ignore - reader will get another IOException because pipe is about to be broken
        }
    }

    /**
     * Callback interface used by {@link NullModemInputStream}.
     */
    public interface CallableWriter {

        /**
         * Write the output to the given output stream.
         *
         * <p>
         * This method will be invoked asynchronously by a dedicated writer thread.
         * </p>
         *
         * @throws IOException if an I/O error occurs
         */
        void writeTo(OutputStream output) throws IOException;
    }
}


@@ -2294,12 +2294,639 @@
 n;%0A    %7D
+%0A%0A    /**%0A     * Ensure input and output streams are closed when this instance is no longer referenced.%0A     *%0A     * %3Cp%3E%0A     * This avoids a memory leak when an instance of this class is created but never read from.%0A     */%0A    @Override%0A    protected void finalize() throws Throwable %7B%0A        try %7B%0A            try %7B%0A                this.output.close();%0A            %7D catch (IOException e) %7B%0A                // ignore%0A            %7D%0A            try %7B%0A                this.close();%0A            %7D catch (IOException e) %7B%0A                // ignore%0A            %7D%0A        %7D finally %7B%0A            super.finalize();%0A        %7D%0A    %7D
 %0A%7D%0A%0A

Fix memory leak when an instance of NullModemInputStream is created but never read from.