TeeServletOutputStream.java 3.24 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.ServletOutputStream
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.httpcache.utils;

import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class TeeServletOutputStream
extends ServletOutputStream {
    private final Logger logger;
    private final ServletOutputStream out;
    private final String name;
    private OutputStream output;
    private boolean opened;
    private boolean error;
    private boolean closed;

    public TeeServletOutputStream(ServletOutputStream out, String name) {
        this.logger = LoggerFactory.getLogger(this.getClass());
        this.out = out;
        this.name = name;
    }

    public void write(byte[] b, int off, int len) throws IOException {
        this.out.write(b, off, len);
        try {
            OutputStream output = this.getOutputStream();
            if (output != null) {
                output.write(b, off, len);
            }
        }
        catch (IOException e) {
            this.logger.warn(String.format("Unable to write to %s.", this.name), (Throwable)e);
            this.error = true;
        }
    }

    public void write(int c) throws IOException {
        this.out.write(c);
        try {
            OutputStream output = this.getOutputStream();
            if (output != null) {
                output.write(c);
            }
        }
        catch (IOException e) {
            this.logger.warn(String.format("Unable to write to %s.", this.name), (Throwable)e);
            this.error = true;
        }
    }

    public void flush() throws IOException {
        this.out.flush();
        try {
            OutputStream output = this.getOutputStream();
            if (output != null) {
                output.flush();
            }
        }
        catch (IOException e) {
            this.logger.warn(String.format("Unable to flush %s.", this.name), (Throwable)e);
            this.error = true;
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void close() throws IOException {
        if (this.opened && !this.closed) {
            try {
                this.output.close();
            }
            catch (IOException e) {
                this.logger.warn(String.format("Unable to close %s.", this.name), (Throwable)e);
                this.error = true;
            }
            finally {
                this.closed = true;
            }
        }
        this.out.close();
    }

    public boolean hasError() {
        return this.error;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private OutputStream getOutputStream() throws IOException {
        if (this.error || this.closed) {
            return null;
        }
        if (this.output == null && !this.opened) {
            try {
                this.output = this.createOutputStream();
            }
            finally {
                this.opened = true;
            }
        }
        return this.output;
    }

    protected abstract OutputStream createOutputStream() throws IOException;
}