TerminatedOutputStream.java

/*
 * Copyright (c) 2020, Stein Eldar Johnsen
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package net.morimekta.io.sub;

import java.io.IOException;
import java.io.OutputStream;

/**
 * A wrapping output stream that writes a terminating byte when the stream is
 * closed. It is meant to be paired with a {@link TerminatedInputStream},
 * which treats the terminating byte as end of content. Note that writing
 * the terminating byte value as part of the regular content will cause the
 * corresponding input stream to terminate prematurely. Closing this stream
 * does not close the underlying output stream.
 */
public class TerminatedOutputStream extends OutputStream {
    private       OutputStream out;
    private final int          terminator;

    /**
     * Create a default instance terminating with a '00' byte.
     *
     * @param out Output stream to write terminator on close.
     */
    public TerminatedOutputStream(OutputStream out) {
        this(out, 0);
    }

    /**
     * Create a terminated output stream with specified terminator.
     *
     * @param out        Output stream to write terminator on close.
     * @param terminator Terminator byte code.
     */
    public TerminatedOutputStream(OutputStream out, int terminator) {
        if (terminator < 0 || terminator > 255) {
            throw new IllegalArgumentException("Invalid terminator byte: " + terminator);
        }
        this.out = out;
        this.terminator = terminator;
    }

    @Override
    public void write(int i) throws IOException {
        if (out == null) throw new IOException("Writing to closed stream");
        out.write(i);
    }

    @Override
    public void write(byte[] b) throws IOException {
        if (out == null) throw new IOException("Writing to closed stream");
        out.write(b);
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        if (out == null) throw new IOException("Writing to closed stream");
        out.write(b, off, len);
    }

    @Override
    public void flush() throws IOException {
        if (out != null) {
            out.flush();
        }
    }

    @Override
    public void close() throws IOException {
        if (out != null) {
            out.write(terminator);
            out = null;
        }
    }
}