FileDocCategorySizeDatePackage
Header.javaAPI DocAndroid 1.5 API7680Wed May 06 22:41:04 BST 2009org.apache.harmony.luni.internal.net.www.protocol.http

Header

public class Header extends Object implements Cloneable
The general structure for request / response header. It is essentially constructed by hashtable with key indexed in a vector for position lookup.

Fields Summary
private ArrayList
props
private HashMap
keyTable
private String
statusLine
Constructors Summary
public Header()
A generic header structure. Used mostly for request / response header. The key/value pair of the header may be inserted for later use. The key is stored in an array for indexed slot access.

        super();
        this.props = new ArrayList<String>(20);
        this.keyTable = new HashMap<String, LinkedList<String>>(20);
    
public Header(Map map)
The alternative constructor which sets the input map as its initial keyTable.

param
map the initial keyTable as a map

        this(); // initialize fields
        for (Entry<String, List<String>> next : map.entrySet()) {
            String key = next.getKey();
            props.add(key);
            List<String> value = next.getValue();
            LinkedList<String> linkedList = new LinkedList<String>();
            for (String element : value) {
                linkedList.add(element);
                props.add(element);
            }
            keyTable.put(key, linkedList);
        }
    
Methods Summary
public voidadd(java.lang.String key, java.lang.String value)
Add a field with the specified value.

param
key
param
value

        if (key == null) {
            throw new NullPointerException();
        }
        // BEGIN android-changed
        key = key.toLowerCase();
        LinkedList<String> list = keyTable.get(key);
        if (list == null) {
            list = new LinkedList<String>();
            keyTable.put(key, list);
        }
        // END android-changed
        list.add(value);
        props.add(key);
        props.add(value);
    
public java.lang.Objectclone()

        try {
            Header clone = (Header) super.clone();
            clone.props = (ArrayList<String>) props.clone();
            clone.keyTable = new HashMap<String, LinkedList<String>>(20);
            for (Map.Entry<String, LinkedList<String>> next : this.keyTable
                    .entrySet()) {
                LinkedList<String> v = (LinkedList<String>) next.getValue()
                        .clone();
                clone.keyTable.put(next.getKey(), v);
            }
            return clone;
        } catch (CloneNotSupportedException e) {
            return null;
        }
    
public java.lang.Stringget(int pos)
Returns the element at pos, null if no such element exist.

return
java.lang.String the value of the key
param
pos int the position to look for

        if (pos >= 0 && pos < props.size() / 2) {
            return props.get(pos * 2 + 1);
        }
        return null;
    
public java.lang.Stringget(java.lang.String key)
Returns the value corresponding to the specified key, null if no such key exists.

param
key
return

        LinkedList<String> result = keyTable.get(key.toLowerCase());
        if (result == null) {
            return null;
        }
        return result.getLast();
    
public java.util.MapgetFieldMap()
Provides an unmodifiable map with all String header names mapped to their String values. The map keys are Strings and the values are unmodifiable Lists of Strings.

return
an unmodifiable map of the headers

        Map<String, List<String>> result = new HashMap<String, List<String>>(
                keyTable.size());
        for (Map.Entry<String, LinkedList<String>> next : keyTable.entrySet()) {
            List<String> v = next.getValue();
            result.put(next.getKey(), Collections.unmodifiableList(v));
        }
        return Collections.unmodifiableMap(result);
    
public java.lang.StringgetKey(int pos)
Returns the key of this header at pos, null if there are fewer keys in the header

return
the key the desired position
param
pos the position to look for

        if (pos >= 0 && pos < props.size() / 2) {
            return props.get(pos * 2);
        }
        return null;
    
public java.lang.StringgetStatusLine()
Gets the status line in the header request example: GET / HTTP/1.1 response example: HTTP/1.1 200 OK

return
the status line

        return statusLine;
    
public intlength()
Returns the number of keys stored in this header

return

        return props.size() / 2;
    
public voidset(java.lang.String key, java.lang.String value)
Set a field with the specified value. If the field is not found, it is added. If the field is found, the existing value(s) are overwritten.

param
key
param
value

        if (key == null) {
            throw new NullPointerException();
        }
        // BEGIN android-added
        key = key.toLowerCase();
        // END android-added
        LinkedList<String> list = keyTable.get(key);
        if (list == null) {
            add(key, value);
        } else {
            list.clear();
            list.add(value);
            for (int i = 0; i < props.size(); i += 2) {
                String propKey = props.get(i);
                if (propKey != null && key.equals(propKey)) {
                    props.set(i + 1, value);
                }
            }
        }
    
public voidsetStatusLine(java.lang.String statusLine)
Sets the status line in the header request example: GET / HTTP/1.1 response example: HTTP/1.1 200 OK

param
statusLine

        this.statusLine = statusLine;
        /*
         * we add the status line to the list of headers so that it is
         * accessible from java.net.HttpURLConnection.getResponseCode() which
         * calls
         * org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getHeaderField(0)
         * to get it
         */
        props.add(0, null);
        props.add(1, statusLine);