package com.github.mustachejava.util;

import java.io.IOException;
import java.io.Writer;

/* JADX INFO: loaded from: classes.dex */
public class IndentWriter extends Writer {
    private final String indent;
    public final Writer inner;
    private boolean prependIndent = false;

    public IndentWriter(Writer writer, String str) {
        this.inner = writer;
        this.indent = str;
    }

    @Override // java.io.Writer
    public void write(char[] cArr, int i, int i2) throws IOException {
        int i3 = i;
        int i4 = i3;
        while (i3 < i2) {
            if (cArr[i3] == '\n') {
                int i5 = i3 + 1;
                writeLine(cArr, i4, i5 - i4);
                this.prependIndent = true;
                i4 = i5;
            }
            i3++;
        }
        writeLine(cArr, i4, i2 - (i4 - i));
    }

    public void flushIndent() throws IOException {
        if (this.prependIndent) {
            this.inner.append((CharSequence) this.indent);
            this.prependIndent = false;
        }
    }

    private void writeLine(char[] cArr, int i, int i2) throws IOException {
        if (i2 <= 0) {
            return;
        }
        flushIndent();
        this.inner.write(cArr, i, i2);
    }

    @Override // java.io.Writer, java.io.Flushable
    public void flush() throws IOException {
        this.inner.flush();
    }

    @Override // java.io.Writer, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        this.inner.close();
    }
}
