FileDocCategorySizeDatePackage
Header.javaAPI DocAndroid 1.5 API5317Wed May 06 22:42:46 BST 2009org.apache.james.mime4j.message

Header

public class Header extends Object
The header of an entity (see RFC 2045).
version
$Id: Header.java,v 1.3 2004/10/04 15:36:44 ntherning Exp $

Fields Summary
private List
fields
private HashMap
fieldMap
Constructors Summary
public Header()
Creates a new empty Header.

    
              
      
    
public Header(InputStream is)
Creates a new Header from the specified stream.

param
is the stream to read the header from.

        final MimeStreamParser parser = new MimeStreamParser();
        parser.setContentHandler(new AbstractContentHandler() {
            public void endHeader() {
                parser.stop();
            }
            public void field(String fieldData) {
                addField(Field.parse(fieldData));
            }
        });
        parser.parse(is);
    
Methods Summary
public voidaddField(org.apache.james.mime4j.field.Field field)
Adds a field to the end of the list of fields.

param
field the field to add.

        List values = (List) fieldMap.get(field.getName().toLowerCase());
        if (values == null) {
            values = new LinkedList();
            fieldMap.put(field.getName().toLowerCase(), values);
        }
        values.add(field);
        fields.add(field);
    
public org.apache.james.mime4j.field.FieldgetField(java.lang.String name)
Gets a Field given a field name. If there are multiple such fields defined in this header the first one will be returned.

param
name the field name (e.g. From, Subject).
return
the field or null if none found.

        List l = (List) fieldMap.get(name.toLowerCase());
        if (l != null && !l.isEmpty()) {
            return (Field) l.get(0);
        }
        return null;
    
public java.util.ListgetFields()
Gets the fields of this header. The returned list will not be modifiable.

return
the list of Field objects.

        return Collections.unmodifiableList(fields);
    
public java.util.ListgetFields(java.lang.String name)
Gets all Fields having the specified field name.

param
name the field name (e.g. From, Subject).
return
the list of fields.

        List l = (List) fieldMap.get(name.toLowerCase());
        return Collections.unmodifiableList(l);
    
public java.lang.StringtoString()
Return Header Object as String representation. Each headerline is seperated by "\r\n"

return
headers

        StringBuffer str = new StringBuffer();
        for (Iterator it = fields.iterator(); it.hasNext();) {
            str.append(it.next().toString());
            str.append("\r\n");
        }
        return str.toString();
    
public voidwriteTo(java.io.OutputStream out)
Write the Header to the given OutputStream

param
out the OutputStream to write to
throws
IOException

        String charString = ((ContentTypeField) getField(Field.CONTENT_TYPE)).getCharset();
        
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, CharsetUtil.getCharset(charString)),8192);
        writer.write(toString()+ "\r\n");
        writer.flush();