
package com.android.email.mail.internet;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import com.android.email.Email;
import com.android.email.Utility;
import com.android.email.mail.MessagingException;

public class MimeHeader {
    /**
     * Application specific header that contains Store specific information about an attachment.
     * In IMAP this contains the IMAP BODYSTRUCTURE part id so that the ImapStore can later
     * retrieve the attachment at will from the server.
     * The info is recorded from this header on LocalStore.appendMessages and is put back
     * into the MIME data by LocalStore.fetch.
     */
    public static final String HEADER_ANDROID_ATTACHMENT_STORE_DATA = "X-Android-Attachment-StoreData";

    public static final String HEADER_CONTENT_TYPE = "Content-Type";
    public static final String HEADER_CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding";
    public static final String HEADER_CONTENT_DISPOSITION = "Content-Disposition";

    /**
     * Fields that should be omitted when writing the header using writeTo()
     */
    private static final String[] writeOmitFields = {
//        HEADER_ANDROID_ATTACHMENT_DOWNLOADED,
//        HEADER_ANDROID_ATTACHMENT_ID,
        HEADER_ANDROID_ATTACHMENT_STORE_DATA
    };

    protected ArrayList<Field> mFields = new ArrayList<Field>();

    public void clear() {
        mFields.clear();
    }

    public String getFirstHeader(String name) {
        String[] header = getHeader(name);
        if (header == null) {
            return null;
        }
        return header[0];
    }

    public void addHeader(String name, String value)  {
        mFields.add(new Field(name, MimeUtility.foldAndEncode(value)));
    }

    public void setHeader(String name, String value)  {
        if (name == null || value == null) {
            return;
        }
        removeHeader(name);
        addHeader(name, value);
    }

    public List<String> getHeaderNames()
    {
        ArrayList<String> names = new ArrayList<String>();
        for (Field field : mFields) {
            names.add(field.name);
        }
        return names;
    }

    public String[] getHeader(String name)  {
        ArrayList<String> values = new ArrayList<String>();
        for (Field field : mFields) {
            if (field.name.equalsIgnoreCase(name)) {
                values.add(field.value);
            }
        }
        if (values.size() == 0) {
            return null;
        }
        return values.toArray(new String[] {});
    }

    public void removeHeader(String name) {
        ArrayList<Field> removeFields = new ArrayList<Field>();
        for (Field field : mFields) {
            if (field.name.equalsIgnoreCase(name)) {
                removeFields.add(field);
            }
        }
        mFields.removeAll(removeFields);
    }

    public void writeTo(OutputStream out) throws IOException, MessagingException {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out), 1024);
        for (Field field : mFields) {
            if (!Utility.arrayContains(writeOmitFields, field.name)) {
                writer.write(field.name + ": " + field.value + "\r\n");
            }
        }
        writer.flush();
    }

    class Field {
        String name;

        String value;

        public Field(String name, String value) {
            this.name = name;
            this.value = value;
        }
    }
}

@@ -326,16 +326,66 @@
 ception;
+%0Aimport org.apache.james.mime4j.codec.EncoderUtil;
 %0A%0Apublic
@@ -3304,63 +3304,740 @@
     
-writer.write(field.name + %22: %22 + field.value + %22%5Cr%5Cn%22);
+String v = field.value;%0A%0A                if (hasToBeEncoded(v)) %7B%0A                    v = EncoderUtil.encodeEncodedWord(%0A                        field.value,%0A                        EncoderUtil.Usage.WORD_ENTITY%0A                    );%0A                %7D%0A%0A                writer.write(field.name + %22: %22 + v + %22%5Cr%5Cn%22);%0A            %7D%0A        %7D%0A        writer.flush();%0A    %7D%0A%0A    // encode non printable characters except LF/CR codes.%0A    public boolean hasToBeEncoded(String text) %7B%0A        for (int i = 0; i %3C text.length(); i++) %7B%0A            char c = text.charAt(i);%0A            if (c %3C 0x20 %7C%7C 0x7e %3C c) %7B // non printable%0A                if (c != 0x0a && c != 0x0d) %7B // non LF/CR%0A                    return true;%0A                %7D
 %0A   
@@ -4053,32 +4053,33 @@
 %7D%0A        %7D%0A
+%0A
         
 writer.flush
@@ -4062,38 +4062,36 @@
  %7D%0A%0A        
-writer.flush()
+return false
 ;%0A    %7D%0A%0A   

patch from typester to fix UTF8 headers (such as subject) Resolves issue 534