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

package org.bouncycastle.mime;

import org.bouncycastle.util.Strings;
import java.io.OutputStream;
import java.util.LinkedHashMap;
import java.util.Iterator;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeMap;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import org.bouncycastle.util.Iterable;

public class Headers implements Iterable<String>
{
    private final Map<String, List> headers;
    private final List<String> headersAsPresented;
    private final String contentTransferEncoding;
    private String boundary;
    private boolean multipart;
    private String contentType;
    private Map<String, String> contentTypeParameters;
    
    private static List<String> parseHeaders(final InputStream inputStream) throws IOException {
        final ArrayList list = new ArrayList();
        String line;
        while ((line = new LineReader(inputStream).readLine()) != null && line.length() != 0) {
            list.add(line);
        }
        return list;
    }
    
    public Headers(String substring, final String s) {
        this.headers = new TreeMap<String, List>(String.CASE_INSENSITIVE_ORDER);
        (this.headersAsPresented = new ArrayList<String>()).add("Content-Type: " + substring);
        this.put("Content-Type", substring);
        final String s2 = (this.getValues("Content-Type") == null) ? "text/plain" : this.getValues("Content-Type")[0];
        final int index = s2.indexOf(59);
        if (index < 0) {
            substring = s2;
            this.contentTypeParameters = Collections.EMPTY_MAP;
        }
        else {
            substring = s2.substring(0, index);
            this.contentTypeParameters = this.createContentTypeParameters(s2.substring(index + 1).trim());
        }
        this.contentTransferEncoding = ((this.getValues("Content-Transfer-Encoding") == null) ? s : this.getValues("Content-Transfer-Encoding")[0]);
        if (substring.indexOf("multipart") >= 0) {
            this.multipart = true;
            final String boundary = this.contentTypeParameters.get("boundary");
            if (boundary.startsWith("\"") && boundary.endsWith("\"")) {
                this.boundary = boundary.substring(1, boundary.length() - 1);
            }
            else {
                this.boundary = boundary;
            }
        }
        else {
            this.boundary = null;
            this.multipart = false;
        }
    }
    
    public Headers(final InputStream inputStream, final String s) throws IOException {
        this(parseHeaders(inputStream), s);
    }
    
    public Headers(final List<String> headersAsPresented, final String s) {
        this.headers = new TreeMap<String, List>(String.CASE_INSENSITIVE_ORDER);
        this.headersAsPresented = headersAsPresented;
        String string = "";
        for (final String s2 : headersAsPresented) {
            if (s2.startsWith(" ") || s2.startsWith("\t")) {
                string += s2.trim();
            }
            else {
                if (string.length() != 0) {
                    this.put(string.substring(0, string.indexOf(58)).trim(), string.substring(string.indexOf(58) + 1).trim());
                }
                string = s2;
            }
        }
        if (string.trim().length() != 0) {
            this.put(string.substring(0, string.indexOf(58)).trim(), string.substring(string.indexOf(58) + 1).trim());
        }
        final String contentType = (this.getValues("Content-Type") == null) ? "text/plain" : this.getValues("Content-Type")[0];
        final int index = contentType.indexOf(59);
        if (index < 0) {
            this.contentType = contentType;
            this.contentTypeParameters = Collections.EMPTY_MAP;
        }
        else {
            this.contentType = contentType.substring(0, index);
            this.contentTypeParameters = this.createContentTypeParameters(contentType.substring(index + 1).trim());
        }
        this.contentTransferEncoding = ((this.getValues("Content-Transfer-Encoding") == null) ? s : this.getValues("Content-Transfer-Encoding")[0]);
        if (this.contentType.indexOf("multipart") >= 0) {
            this.multipart = true;
            final String s3 = this.contentTypeParameters.get("boundary");
            this.boundary = s3.substring(1, s3.length() - 1);
        }
        else {
            this.boundary = null;
            this.multipart = false;
        }
    }
    
    public Map<String, String> getContentTypeAttributes() {
        return this.contentTypeParameters;
    }
    
    private Map<String, String> createContentTypeParameters(final String s) {
        final String[] split = s.split(";");
        final LinkedHashMap m = new LinkedHashMap();
        for (int i = 0; i != split.length; ++i) {
            final String s2 = split[i];
            final int index = s2.indexOf(61);
            if (index < 0) {
                throw new IllegalArgumentException("malformed Content-Type header");
            }
            m.put(s2.substring(0, index).trim(), s2.substring(index + 1).trim());
        }
        return (Map<String, String>)Collections.unmodifiableMap((Map<?, ?>)m);
    }
    
    public boolean isMultipart() {
        return this.multipart;
    }
    
    public String getBoundary() {
        return this.boundary;
    }
    
    public String getContentType() {
        return this.contentType;
    }
    
    public String getContentTransferEncoding() {
        return this.contentTransferEncoding;
    }
    
    private void put(final String s, final String s2) {
        synchronized (this) {
            final KV kv = new KV(s, s2);
            List list = this.headers.get(s);
            if (list == null) {
                list = new ArrayList();
                this.headers.put(s, list);
            }
            list.add(kv);
        }
    }
    
    public Iterator<String> getNames() {
        return this.headers.keySet().iterator();
    }
    
    public String[] getValues(final String s) {
        synchronized (this) {
            final List list = this.headers.get(s);
            if (list == null) {
                return null;
            }
            final String[] array = new String[list.size()];
            for (int i = 0; i < list.size(); ++i) {
                array[i] = ((KV)list.get(i)).value;
            }
            return array;
        }
    }
    
    public boolean isEmpty() {
        synchronized (this) {
            return this.headers.isEmpty();
        }
    }
    
    public boolean containsKey(final String s) {
        return this.headers.containsKey(s);
    }
    
    @Override
    public Iterator<String> iterator() {
        return this.headers.keySet().iterator();
    }
    
    public void dumpHeaders(final OutputStream outputStream) throws IOException {
        final Iterator<String> iterator = this.headersAsPresented.iterator();
        while (iterator.hasNext()) {
            outputStream.write(Strings.toUTF8ByteArray(iterator.next().toString()));
            outputStream.write(13);
            outputStream.write(10);
        }
    }
    
    private static class KV
    {
        public final String key;
        public final String value;
        
        public KV(final String key, final String value) {
            this.key = key;
            this.value = value;
        }
        
        public KV(final KV kv) {
            this.key = kv.key;
            this.value = kv.value;
        }
    }
}
