001package jmri.jmrix;
002
003import java.io.IOException;
004import java.io.OutputStream;
005
006/**
007 * An output stream where the stream can be replaced on the fly.
008 *
009 * @author Daniel Bergqvist (C) 2024
010 */
011public class ReplaceableOutputStream extends OutputStream {
012
013    private volatile OutputStream _stream;
014
015    public void replaceStream(OutputStream stream) {
016        this._stream = stream;
017    }
018
019    @Override
020    public void write(int b) throws IOException {
021        _stream.write(b);
022    }
023
024    /** {@inheritDoc} */
025    @Override
026    public void write(byte b[]) throws IOException {
027        _stream.write(b);
028    }
029
030    /** {@inheritDoc} */
031    @Override
032    public void write(byte b[], int off, int len) throws IOException {
033        _stream.write(b, off, len);
034    }
035
036    /** {@inheritDoc} */
037    @Override
038    public void flush() throws IOException {
039        _stream.flush();
040    }
041
042    /** {@inheritDoc} */
043    @Override
044    public void close() throws IOException {
045        _stream.close();
046    }
047
048}