jdk/src/share/classes/sun/net/www/MessageHeader.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 1995-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *      news stream opener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
package sun.net.www;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/** An RFC 844 or MIME message header.  Includes methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    for parsing headers from incoming streams, fetching
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    values, setting values, and printing headers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    Key values of null are legal: they indicate lines in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    the header that don't have a valid key, but do have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    a value (this isn't legal according to the standard,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    but lines like this are everywhere). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
class MessageHeader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private String keys[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private String values[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private int nkeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public MessageHeader () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        grow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public MessageHeader (InputStream is) throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        parseHeader(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Reset a message header (all key/values removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public synchronized void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        keys = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        values = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        nkeys = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        grow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Find the value that corresponds to this key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * It finds only the first occurrence of the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param k the key to find.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @return null if not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public synchronized String findValue(String k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (k == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            for (int i = nkeys; --i >= 0;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                if (keys[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    return values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            for (int i = nkeys; --i >= 0;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                if (k.equalsIgnoreCase(keys[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    return values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // return the location of the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public synchronized int getKey(String k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        for (int i = nkeys; --i >= 0;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if ((keys[i] == k) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                (k != null && k.equalsIgnoreCase(keys[i])))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public synchronized String getKey(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (n < 0 || n >= nkeys) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return keys[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public synchronized String getValue(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (n < 0 || n >= nkeys) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return values[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /** Deprecated: Use multiValueIterator() instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *  Find the next value that corresponds to this key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *  It finds the first value that follows v. To iterate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *  over all the values of a key use:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *  <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *          for(String v=h.findValue(k); v!=null; v=h.findNextValue(k, v)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *              ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *  </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public synchronized String findNextValue(String k, String v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        boolean foundV = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (k == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            for (int i = nkeys; --i >= 0;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                if (keys[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    if (foundV)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        return values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    else if (values[i] == v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                        foundV = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            for (int i = nkeys; --i >= 0;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                if (k.equalsIgnoreCase(keys[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    if (foundV)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                        return values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    else if (values[i] == v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        foundV = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   141
    class HeaderIterator implements Iterator<String> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        int next = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        String key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        boolean haveNext = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        Object lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        public HeaderIterator (String k, Object lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            key = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            this.lock = lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        public boolean hasNext () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                if (haveNext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                while (index < nkeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    if (key.equalsIgnoreCase (keys[index])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        haveNext = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        next = index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    index ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   168
        public String next() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                if (haveNext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    haveNext = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    return values [next];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                if (hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    return next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    throw new NoSuchElementException ("No more elements");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        public void remove () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new UnsupportedOperationException ("remove not allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * return an Iterator that returns all values of a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * key in sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   190
    public Iterator<String> multiValueIterator (String k) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return new HeaderIterator (k, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   194
    public synchronized Map<String, List<String>> getHeaders() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return getHeaders(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   198
    public synchronized Map<String, List<String>> getHeaders(String[] excludeList) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        boolean skipIt = false;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   200
        Map<String, List<String>> m = new HashMap<String, List<String>>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        for (int i = nkeys; --i >= 0;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (excludeList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                // check if the key is in the excludeList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                // if so, don't include it in the Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                for (int j = 0; j < excludeList.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    if ((excludeList[j] != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        (excludeList[j].equalsIgnoreCase(keys[i]))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        skipIt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (!skipIt) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   214
                List<String> l = m.get(keys[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                if (l == null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   216
                    l = new ArrayList<String>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    m.put(keys[i], l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                l.add(values[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                // reset the flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                skipIt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   226
        for (String key : m.keySet()) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   227
            m.put(key, Collections.unmodifiableList(m.get(key)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return Collections.unmodifiableMap(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /** Prints the key-value pairs represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        header.  Also prints the RFC required blank line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        at the end. Omits pairs with a null key. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public synchronized void print(PrintStream p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        for (int i = 0; i < nkeys; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            if (keys[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                p.print(keys[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    (values[i] != null ? ": "+values[i]: "") + "\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        p.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        p.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /** Adds a key value pair to the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        header.  Duplicates are allowed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public synchronized void add(String k, String v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        grow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        keys[nkeys] = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        values[nkeys] = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        nkeys++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /** Prepends a key value pair to the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        header.  Duplicates are allowed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public synchronized void prepend(String k, String v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        grow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        for (int i = nkeys; i > 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            keys[i] = keys[i-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            values[i] = values[i-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        keys[0] = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        values[0] = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        nkeys++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /** Overwrite the previous key/val pair at location 'i'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * with the new k/v.  If the index didn't exist before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * the key/val is simply tacked onto the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public synchronized void set(int i, String k, String v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        grow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (i < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } else if (i >= nkeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            add(k, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            keys[i] = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            values[i] = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /** grow the key/value arrays as needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    private void grow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (keys == null || nkeys >= keys.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            String[] nk = new String[nkeys + 4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            String[] nv = new String[nkeys + 4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if (keys != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                System.arraycopy(keys, 0, nk, 0, nkeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if (values != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                System.arraycopy(values, 0, nv, 0, nkeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            keys = nk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            values = nv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Remove the key from the header. If there are multiple values under
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * the same key, they are all removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Nothing is done if the key doesn't exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * After a remove, the other pairs' order are not changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @param k the key to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public synchronized void remove(String k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if(k == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            for (int i = 0; i < nkeys; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                while (keys[i] == null && i < nkeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    for(int j=i; j<nkeys-1; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                        keys[j] = keys[j+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        values[j] = values[j+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    nkeys--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            for (int i = 0; i < nkeys; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                while (k.equalsIgnoreCase(keys[i]) && i < nkeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    for(int j=i; j<nkeys-1; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                        keys[j] = keys[j+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        values[j] = values[j+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    nkeys--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /** Sets the value of a key.  If the key already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        exists in the header, it's value will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        changed.  Otherwise a new key/value pair will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        be added to the end of the header. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public synchronized void set(String k, String v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        for (int i = nkeys; --i >= 0;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (k.equalsIgnoreCase(keys[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                values[i] = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        add(k, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /** Set's the value of a key only if there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *  key with that value already.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public synchronized void setIfNotSet(String k, String v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if (findValue(k) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            add(k, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /** Convert a message-id string to canonical form (strips off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        leading and trailing <>s) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public static String canonicalID(String id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (id == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        int st = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        int len = id.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        boolean substr = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        while (st < len && ((c = id.charAt(st)) == '<' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                            c <= ' ')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            st++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            substr = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        while (st < len && ((c = id.charAt(len - 1)) == '>' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                            c <= ' ')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            len--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            substr = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        return substr ? id.substring(st, len) : id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /** Parse a MIME header from an input stream. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    public void parseHeader(InputStream is) throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            nkeys = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        mergeHeader(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /** Parse and merge a MIME header from an input stream. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public void mergeHeader(InputStream is) throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (is == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        char s[] = new char[10];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        int firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        while (firstc != '\n' && firstc != '\r' && firstc >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            int keyend = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            boolean inKey = firstc > ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            s[len++] = (char) firstc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    parseloop:{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                while ((c = is.read()) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    switch (c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                      case ':':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                        if (inKey && len > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                            keyend = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                        inKey = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                      case '\t':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                        c = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                      case ' ':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                        inKey = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                      case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                      case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        if (c == '\r' && firstc == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                            firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                            if (firstc == '\r')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                                firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                        if (firstc == '\n' || firstc == '\r' || firstc > ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                            break parseloop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        /* continuation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        c = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    if (len >= s.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        char ns[] = new char[s.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                        System.arraycopy(s, 0, ns, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        s = ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    s[len++] = (char) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                firstc = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            while (len > 0 && s[len - 1] <= ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                len--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            String k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            if (keyend <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                k = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                keyend = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                k = String.copyValueOf(s, 0, keyend);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                if (keyend < len && s[keyend] == ':')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    keyend++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                while (keyend < len && s[keyend] <= ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    keyend++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            String v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            if (keyend >= len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                v = new String();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                v = String.copyValueOf(s, keyend, len - keyend);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            add(k, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public synchronized String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        String result = super.toString() + nkeys + " pairs: ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        for (int i = 0; i < keys.length && i < nkeys; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            result += "{"+keys[i]+": "+values[i]+"}";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
}