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

import java.io.FilterWriter;
import java.io.IOException;
import java.io.Writer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class TeeWriter
extends FilterWriter {
    private final Logger logger;
    private final String name;
    private Writer writer;
    private boolean opened;
    private boolean error;
    private boolean closed;

    public TeeWriter(Writer out, String name) {
        super(out);
        this.logger = LoggerFactory.getLogger(this.getClass());
        this.name = name;
    }

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

    public void write(char[] cbuf, int off, int len) throws IOException {
        super.write(cbuf, off, len);
        try {
            Writer writer = this.getWriter();
            if (writer != null) {
                writer.write(cbuf, 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(String str, int off, int len) throws IOException {
        super.write(str, off, len);
        try {
            Writer writer = this.getWriter();
            if (writer != null) {
                writer.write(str, off, len);
            }
        }
        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 {
        super.flush();
        try {
            Writer writer = this.getWriter();
            if (writer != null) {
                writer.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.writer.close();
            }
            catch (IOException e) {
                this.logger.warn(String.format("Unable to close %s.", this.name), (Throwable)e);
                this.error = true;
            }
            finally {
                this.closed = true;
            }
        }
        super.close();
    }

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

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

    protected abstract Writer createWriter() throws IOException;
}