package org.bouncycastle.mime;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.bouncycastle.util.Iterable;
import org.bouncycastle.util.Strings;
public class
implements Iterable<String>
{
private final Map<String, List> = new TreeMap<String, List>(String.CASE_INSENSITIVE_ORDER);
private final List<String> ;
private final String ;
private String ;
private boolean ;
private String ;
private Map<String, String> ;
private static List<String> (InputStream src)
throws IOException
{
String s;
List<String> headerLines = new ArrayList<String>();
LineReader rd = new LineReader(src);
while ((s = rd.readLine()) != null)
{
if (s.length() == 0)
{
break;
}
headerLines.add(s);
}
return headerLines;
}
public (InputStream source, String defaultContentTransferEncoding)
throws IOException
{
this(parseHeaders(source), defaultContentTransferEncoding);
}
public (List<String> headerLines, String defaultContentTransferEncoding)
{
this.headersAsPresented = headerLines;
String header = "";
for (Iterator it = headerLines.iterator(); it.hasNext();)
{
String line = (String)it.next();
if (line.startsWith(" ") || line.startsWith("\t"))
{
header = header + line.trim();
}
else
{
if (header.length() != 0)
{
this.put(header.substring(0, header.indexOf(':')).trim(), header.substring(header.indexOf(':') + 1).trim());
}
header = line;
}
}
if (header.trim().length() != 0)
{
this.put(header.substring(0, header.indexOf(':')).trim(), header.substring(header.indexOf(':') + 1).trim());
}
String contentTypeHeader = (this.getValues("Content-Type") == null) ? "text/plain" : this.getValues("Content-Type")[0];
int parameterIndex = contentTypeHeader.indexOf(';');
if (parameterIndex < 0)
{
contentType = contentTypeHeader;
contentTypeParameters = Collections.EMPTY_MAP;
}
else
{
contentType = contentTypeHeader.substring(0, parameterIndex);
contentTypeParameters = createContentTypeParameters(contentTypeHeader.substring(parameterIndex + 1).trim());
}
contentTransferEncoding = this.getValues("Content-Transfer-Encoding") == null ? defaultContentTransferEncoding : this.getValues("Content-Transfer-Encoding")[0];
if (contentType.indexOf("multipart") >= 0)
{
multipart = true;
String bound = (String)contentTypeParameters.get("boundary");
boundary = bound.substring(1, bound.length() - 1);
}
else
{
boundary = null;
multipart = false;
}
}
public Map<String, String> ()
{
return contentTypeParameters;
}
private Map<String, String> (String contentTypeParameters)
{
String[] parameterSplit = contentTypeParameters.split(";");
Map<String, String> rv = new LinkedHashMap<String, String>();
for (int i = 0; i != parameterSplit.length; i++)
{
String parameter = parameterSplit[i];
int eqIndex = parameter.indexOf('=');
if (eqIndex < 0)
{
throw new IllegalArgumentException("malformed Content-Type header");
}
rv.put(parameter.substring(0, eqIndex).trim(), parameter.substring(eqIndex + 1).trim());
}
return Collections.unmodifiableMap(rv);
}
public boolean ()
{
return multipart;
}
public String ()
{
return boundary;
}
public String ()
{
return contentType;
}
public String ()
{
return contentTransferEncoding;
}
private void (String field, String value)
{
synchronized (this)
{
KV kv = new KV(field, value);
List<KV> list = (List<KV>)headers.get(field);
if (list == null)
{
list = new ArrayList<KV>();
headers.put(field, list);
}
list.add(kv);
}
}
public Iterator<String> ()
{
return headers.keySet().iterator();
}
public String[] (String header)
{
synchronized (this)
{
List<KV> kvList = (List<KV>)headers.get(header);
if (kvList == null)
{
return null;
}
String[] out = new String[kvList.size()];
for (int t = 0; t < kvList.size(); t++)
{
out[t] = ((KV)kvList.get(t)).value;
}
return out;
}
}
public boolean ()
{
synchronized (this)
{
return headers.isEmpty();
}
}
public boolean (String s)
{
return headers.containsKey(s);
}
public Iterator<String> ()
{
return headers.keySet().iterator();
}
public void (OutputStream outputStream)
throws IOException
{
for (Iterator it = headersAsPresented.iterator(); it.hasNext();)
{
outputStream.write(Strings.toUTF8ByteArray(it.next().toString()));
outputStream.write('\r');
outputStream.write('\n');
}
}
private class
{
public final String ;
public final String ;
public (String key, String value)
{
this.key = key;
this.value = value;
}
public (KV kv)
{
this.key = kv.key;
this.value = kv.value;
}
}
}