package org.bouncycastle.openpgp;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import org.bouncycastle.util.Arrays;

/* JADX INFO: loaded from: classes.dex */
public class PGPCanonicalizedDataGenerator implements StreamGenerator {
    public static final String CONSOLE = "_CONSOLE";
    public static final Date NOW = PGPLiteralData.NOW;
    public static final char TEXT = 't';
    public static final char UTF8 = 'u';
    private PGPLiteralDataGenerator lGen;
    private boolean oldFormat;

    /* JADX INFO: loaded from: classes3.dex */
    private static class ArrayCRLFGeneratorStream extends CRLFGeneratorStream {
        private final char format;
        private final PGPLiteralDataGenerator lGen;
        private final Date modificationTime;
        private final String name;
        private final OutputStream out;

        public ArrayCRLFGeneratorStream(OutputStream outputStream, PGPLiteralDataGenerator pGPLiteralDataGenerator, char c, String str, Date date) {
            super(new ErasableOutputStream(), c == 'b');
            this.out = outputStream;
            this.lGen = pGPLiteralDataGenerator;
            this.format = c;
            this.name = str;
            this.modificationTime = date;
        }

        @Override // org.bouncycastle.openpgp.PGPCanonicalizedDataGenerator.CRLFGeneratorStream, java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            super.close();
            ErasableOutputStream erasableOutputStream = (ErasableOutputStream) this.crlfOut;
            byte[] buf = erasableOutputStream.getBuf();
            int size = erasableOutputStream.size();
            OutputStream outputStreamOpen = this.lGen.open(this.out, this.format, this.name, size, this.modificationTime);
            outputStreamOpen.write(buf, 0, size);
            outputStreamOpen.close();
            erasableOutputStream.erase();
        }
    }

    /* JADX INFO: loaded from: classes3.dex */
    static class CRLFGeneratorStream extends OutputStream {
        protected final OutputStream crlfOut;
        private final boolean isBinary;
        private int lastB = 0;

        public CRLFGeneratorStream(OutputStream outputStream, boolean z) {
            this.crlfOut = outputStream;
            this.isBinary = z;
        }

        @Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            if (!this.isBinary && this.lastB == 13) {
                this.crlfOut.write(10);
            }
            this.crlfOut.close();
        }

        @Override // java.io.OutputStream
        public void write(int i) throws IOException {
            if (!this.isBinary) {
                if (i == 10 && this.lastB != 13) {
                    this.crlfOut.write(13);
                } else if (this.lastB == 13 && i != 10) {
                    this.crlfOut.write(10);
                }
                this.lastB = i;
            }
            this.crlfOut.write(i);
        }
    }

    /* JADX INFO: loaded from: classes2.dex */
    private static final class ErasableOutputStream extends ByteArrayOutputStream {
        public void erase() {
            Arrays.fill(this.buf, (byte) 0);
            reset();
        }

        public byte[] getBuf() {
            return this.buf;
        }
    }

    private static class FileCRLFGeneratorStream extends CRLFGeneratorStream {
        private final File backingFile;
        private final char format;
        private final PGPLiteralDataGenerator lGen;
        private final Date modificationTime;
        private final String name;
        private final OutputStream out;

        public FileCRLFGeneratorStream(OutputStream outputStream, PGPLiteralDataGenerator pGPLiteralDataGenerator, char c, String str, Date date, File file) throws FileNotFoundException {
            super(new BufferedOutputStream(new FileOutputStream(file)), c == 'b');
            this.out = outputStream;
            this.lGen = pGPLiteralDataGenerator;
            this.format = c;
            this.name = str;
            this.modificationTime = date;
            this.backingFile = file;
        }

        @Override // org.bouncycastle.openpgp.PGPCanonicalizedDataGenerator.CRLFGeneratorStream, java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            super.close();
            PGPUtil.pipeFileContents(this.backingFile, this.lGen.open(this.out, this.format, this.name, this.backingFile.length(), this.modificationTime), 32678);
        }
    }

    /* JADX INFO: loaded from: classes2.dex */
    private static class IndefiniteCRLFGeneratorStream extends CRLFGeneratorStream {
        public IndefiniteCRLFGeneratorStream(OutputStream outputStream, PGPLiteralDataGenerator pGPLiteralDataGenerator, char c, String str, Date date, byte[] bArr) throws IOException {
            super(pGPLiteralDataGenerator.open(outputStream, c, str, date, bArr), c == 'b');
        }

        @Override // org.bouncycastle.openpgp.PGPCanonicalizedDataGenerator.CRLFGeneratorStream, java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            super.close();
        }
    }

    public PGPCanonicalizedDataGenerator() {
        this.oldFormat = false;
    }

    public PGPCanonicalizedDataGenerator(boolean z) {
        this.oldFormat = false;
        this.oldFormat = z;
    }

    @Override // org.bouncycastle.openpgp.StreamGenerator
    public void close() throws IOException {
        if (this.lGen != null) {
            this.lGen.close();
            this.lGen = null;
        }
    }

    public OutputStream open(OutputStream outputStream, char c, File file) throws IOException {
        return open(outputStream, c, file.getName(), new Date(file.lastModified()));
    }

    public OutputStream open(OutputStream outputStream, char c, String str, Date date) throws IOException {
        if (this.lGen != null) {
            throw new IllegalStateException("generator already in open state");
        }
        this.lGen = new PGPLiteralDataGenerator(this.oldFormat);
        return new ArrayCRLFGeneratorStream(outputStream, this.lGen, c, str, new Date(date.getTime()));
    }

    public OutputStream open(OutputStream outputStream, char c, String str, Date date, File file) throws IOException {
        if (this.lGen != null) {
            throw new IllegalStateException("generator already in open state");
        }
        this.lGen = new PGPLiteralDataGenerator(this.oldFormat);
        return new FileCRLFGeneratorStream(outputStream, this.lGen, c, str, new Date(date.getTime()), file);
    }

    public OutputStream open(OutputStream outputStream, char c, String str, Date date, byte[] bArr) throws IOException {
        if (this.lGen != null) {
            throw new IllegalStateException("generator already in open state");
        }
        this.lGen = new PGPLiteralDataGenerator(this.oldFormat);
        return new IndefiniteCRLFGeneratorStream(outputStream, this.lGen, c, str, new Date(date.getTime()), bArr);
    }
}
