// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.mime.encoding;

import java.io.IOException;
import java.io.InputStream;
import java.io.FilterInputStream;

public class QuotedPrintableInputStream extends FilterInputStream
{
    public QuotedPrintableInputStream(final InputStream in) {
        super(in);
    }
    
    @Override
    public int read(final byte[] array, final int n, final int n2) throws IOException {
        int i;
        for (i = 0; i != n2; ++i) {
            final int read = this.read();
            if (read < 0) {
                break;
            }
            array[i + n] = (byte)read;
        }
        if (i == 0) {
            return -1;
        }
        return i;
    }
    
    @Override
    public int read() throws IOException {
        int i = this.in.read();
        if (i == -1) {
            return -1;
        }
        while (i == 61) {
            final int read = this.in.read();
            if (read == -1) {
                throw new IllegalStateException("Quoted '=' at end of stream");
            }
            if (read == 13) {
                int n = this.in.read();
                if (n == 10) {
                    n = this.in.read();
                }
                i = n;
            }
            else {
                if (read != 10) {
                    int n2;
                    if (read >= 48 && read <= 57) {
                        n2 = read - 48;
                    }
                    else {
                        if (read < 65 || read > 70) {
                            throw new IllegalStateException("Expecting '0123456789ABCDEF after quote that was not immediately followed by LF or CRLF");
                        }
                        n2 = 10 + (read - 65);
                    }
                    final int n3 = n2 << 4;
                    final int read2 = this.in.read();
                    int n4;
                    if (read2 >= 48 && read2 <= 57) {
                        n4 = (n3 | read2 - 48);
                    }
                    else {
                        if (read2 < 65 || read2 > 70) {
                            throw new IllegalStateException("Expecting second '0123456789ABCDEF after quote that was not immediately followed by LF or CRLF");
                        }
                        n4 = (n3 | 10 + (read2 - 65));
                    }
                    return n4;
                }
                i = this.in.read();
            }
        }
        return i;
    }
}
