001package jmri.util;
002
003import java.io.IOException;
004import java.io.PipedInputStream;
005import java.io.PipedOutputStream;
006
007/**
008 * Makes a workaround for standard {@link PipedOutputStream} wait.
009 * <p>The {@link PipedInputStream#read()}, in case the receive buffer is
010 * empty at the time of the call, waits for up to 1000ms.
011 * {@link PipedOutputStream#write(int)} does call <code>sink.receive</code>,
012 * but does not <code>notify()</code> the sink object so that read's
013 * wait() terminates.
014 * <p>
015 * As a result, the read side of the pipe waits full 1000ms even though data
016 * become available during the wait.
017 * <p>
018 * The workaround is to simply {@link PipedOutputStream#flush} after write,
019 * which returns from wait()s immediately.
020 *
021 * @author Svata Dedic Copyright (C) 2020
022 */
023
024public class ImmediatePipedOutputStream extends PipedOutputStream {
025    @Override
026    public void write(byte[] b, int off, int len) throws IOException {
027        super.write(b, off, len);
028        flush();
029    }
030
031    @Override
032    public void write(int b) throws IOException {
033        super.write(b);
034        flush();
035    }
036}