
package com.fsck.k9.mail.internet;

import com.fsck.k9.helper.Utility;
import com.fsck.k9.mail.MessagingException;
import org.apache.james.mime4j.codec.EncoderUtil;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.*;

public class MimeHeader
{
    private static final String[] EMPTY_STRING_ARRAY = new String[0];

    /**
     * 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";
    public static final String HEADER_CONTENT_ID = "Content-ID";

    /**
     * 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 Set<String> getHeaderNames()
    {
        Set<String> names = new HashSet<String>();
        for (Field field : mFields)
        {
            names.add(field.name);
        }
        return names;
    }

    public String[] getHeader(String name)
    {
        ArrayList<String> values = findHeaders(name);
        if (values.size() == 0)
        {
            return null;
        }
        return values.toArray(EMPTY_STRING_ARRAY);
    }

    public void removeHeader(String name)
    {

        ArrayList<String> removeFields = findHeaders(name);
        mFields.removeAll(removeFields);
    }

    private ArrayList findHeaders(String name) {
	ArrayList<String> values = new ArrayList<String>();
        Iterator<Field> iter = mFields.iterator();
        while (iter.hasNext())
        {
		Field field = iter.next();
		if (field.name.equalsIgnoreCase(name) )
		{
			values.add(field.value);
		}
        }

        return values;
    }


    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))
            {
                String v = field.value;

                if (hasToBeEncoded(v))
                {
                    v = EncoderUtil.encodeEncodedWord(
                            field.value,
                            EncoderUtil.Usage.WORD_ENTITY
                        );
                }

                writer.write(field.name + ": " + v + "\r\n");
            }
        }
        writer.flush();
    }

    // encode non printable characters except LF/CR codes.
    public boolean hasToBeEncoded(String text)
    {
        for (int i = 0; i < text.length(); i++)
        {
            char c = text.charAt(i);
            if (c < 0x20 || 0x7e < c)   // non printable
            {
                if (c != 0x0a && c != 0x0d)   // non LF/CR
                {
                    return true;
                }
            }
        }

        return false;
    }

    class Field
    {
        String name;

        String value;

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

        @Override
        public String toString()
        {
            StringBuilder sb = new StringBuilder("(");
            sb.append(name).append('=').append(value).append(')');
            return sb.toString();
        }
    }
}

@@ -2486,34 +2486,216 @@
 alues = 
-findHeaders(name);
+new ArrayList%3CString%3E();%0A        for (Field field : mFields)%0A        %7B%0A            if (field.name.equalsIgnoreCase(name))%0A            %7B%0A                values.add(field.value);%0A            %7D%0A        %7D
 %0A       
@@ -2870,17 +2870,16 @@
 )%0A    %7B%0A
-%0A
         
@@ -2892,171 +2892,26 @@
 ist%3C
-String%3E removeFields = findHeaders(name);%0A        mFields.removeAll(removeFields);%0A    %7D%0A%0A    private ArrayList findHeaders(String name) %7B%0A%09ArrayList%3CString%3E value
+Field%3E removeField
 s = 
@@ -2916,38 +2916,37 @@
 = new ArrayList%3C
-String
+Field
 %3E();%0A        Ite
@@ -2946,123 +2946,58 @@
     
-Iterator%3CField%3E iter = mFields.iterator();%0A        while (iter.hasNext())%0A        %7B%0A%09%09Field field = iter.next();%0A%09%09
+for (Field field : mFields)%0A        %7B%0A            
 if (
@@ -3033,45 +3033,77 @@
 ame)
- 
 )%0A
-%09%09%7B%0A%09%09%09values.add(field.value);%0A%09%09
+            %7B%0A                removeFields.add(field);%0A            
 %7D%0A  
@@ -3106,33 +3106,32 @@
 %7D%0A        %7D%0A
-%0A
         
 return value
@@ -3118,29 +3118,47 @@
         
-return values
+mFields.removeAll(removeFields)
 ;%0A    %7D%0A
@@ -3158,17 +3158,16 @@
 %0A    %7D%0A%0A
-%0A
     publ

Revert "refactor getHeaders and removeHeaders to use a common method and an"