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

package org.bouncycastle.mime;

import org.bouncycastle.mime.encoding.QuotedPrintableInputStream;
import org.bouncycastle.mime.encoding.Base64InputStream;
import java.io.IOException;
import java.io.InputStream;

public class BasicMimeParser implements MimeParser
{
    private final InputStream src;
    private final MimeParserContext parserContext;
    private final String defaultContentTransferEncoding;
    private Headers headers;
    private boolean isMultipart;
    private final String boundary;
    
    public BasicMimeParser(final InputStream inputStream) throws IOException {
        this(null, new Headers(inputStream, "7bit"), inputStream);
    }
    
    public BasicMimeParser(final MimeParserContext mimeParserContext, final InputStream inputStream) throws IOException {
        this(mimeParserContext, new Headers(inputStream, mimeParserContext.getDefaultContentTransferEncoding()), inputStream);
    }
    
    public BasicMimeParser(final Headers headers, final InputStream inputStream) {
        this(null, headers, inputStream);
    }
    
    public BasicMimeParser(final MimeParserContext parserContext, final Headers headers, final InputStream src) {
        this.isMultipart = false;
        if (headers.isMultipart()) {
            this.isMultipart = true;
            this.boundary = headers.getBoundary();
        }
        else {
            this.boundary = null;
        }
        this.headers = headers;
        this.parserContext = parserContext;
        this.src = src;
        this.defaultContentTransferEncoding = ((parserContext != null) ? parserContext.getDefaultContentTransferEncoding() : "7bit");
    }
    
    @Override
    public void parse(final MimeParserListener mimeParserListener) throws IOException {
        final MimeContext context = mimeParserListener.createContext(this.parserContext, this.headers);
        if (this.isMultipart) {
            final MimeMultipartContext mimeMultipartContext = (MimeMultipartContext)context;
            final String string = "--" + this.boundary;
            int n = 0;
            int n2 = 0;
            String line;
            while ((line = new LineReader(this.src).readLine()) != null && !"--".equals(line)) {
                if (n != 0) {
                    final BoundaryLimitedInputStream boundaryLimitedInputStream = new BoundaryLimitedInputStream(this.src, this.boundary);
                    final Headers headers = new Headers(boundaryLimitedInputStream, this.defaultContentTransferEncoding);
                    final InputStream applyContext = mimeMultipartContext.createContext(n2++).applyContext(headers, boundaryLimitedInputStream);
                    mimeParserListener.object(this.parserContext, headers, this.processStream(headers, applyContext));
                    if (applyContext.read() >= 0) {
                        throw new IOException("MIME object not fully processed");
                    }
                    continue;
                }
                else {
                    if (!string.equals(line)) {
                        continue;
                    }
                    n = 1;
                    final BoundaryLimitedInputStream boundaryLimitedInputStream2 = new BoundaryLimitedInputStream(this.src, this.boundary);
                    final Headers headers2 = new Headers(boundaryLimitedInputStream2, this.defaultContentTransferEncoding);
                    final InputStream applyContext2 = mimeMultipartContext.createContext(n2++).applyContext(headers2, boundaryLimitedInputStream2);
                    mimeParserListener.object(this.parserContext, headers2, this.processStream(headers2, applyContext2));
                    if (applyContext2.read() >= 0) {
                        throw new IOException("MIME object not fully processed");
                    }
                    continue;
                }
            }
        }
        else {
            mimeParserListener.object(this.parserContext, this.headers, this.processStream(this.headers, context.applyContext(this.headers, this.src)));
        }
    }
    
    public boolean isMultipart() {
        return this.isMultipart;
    }
    
    private InputStream processStream(final Headers headers, final InputStream inputStream) {
        if (headers.getContentTransferEncoding().equals("base64")) {
            return new Base64InputStream(inputStream);
        }
        if (headers.getContentTransferEncoding().equals("quoted-printable")) {
            return new QuotedPrintableInputStream(inputStream);
        }
        return inputStream;
    }
}
