Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / gnu / java / net / protocol / http / Headers.java
blob9306fc411c921cf9ad1a1dac3df188d7e2612994
1 /* Headers.java --
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.net.protocol.http;
41 import gnu.java.net.LineInputStream;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.text.DateFormat;
46 import java.text.ParseException;
47 import java.util.Collection;
48 import java.util.Date;
49 import java.util.Iterator;
50 import java.util.LinkedHashMap;
51 import java.util.LinkedHashSet;
52 import java.util.Map;
53 import java.util.Set;
55 /**
56 * A collection of HTTP header names and associated values.
57 * Retrieval of values is case insensitive. An iteration over the keys
58 * returns the header names in the order they were received.
60 * @author Chris Burdess (dog@gnu.org)
62 public class Headers
63 extends LinkedHashMap
66 static final DateFormat dateFormat = new HTTPDateFormat();
68 static class Header
71 final String name;
73 Header(String name)
75 if (name == null || name.length() == 0)
77 throw new IllegalArgumentException(name);
79 this.name = name;
82 public int hashCode()
84 return name.toLowerCase().hashCode();
87 public boolean equals(Object other)
89 if (other instanceof Header)
91 return ((Header) other).name.equalsIgnoreCase(name);
93 return false;
96 public String toString()
98 return name;
103 static class HeaderEntry
104 implements Map.Entry
107 final Map.Entry entry;
109 HeaderEntry(Map.Entry entry)
111 this.entry = entry;
114 public Object getKey()
116 return ((Header) entry.getKey()).name;
119 public Object getValue()
121 return entry.getValue();
124 public Object setValue(Object value)
126 return entry.setValue(value);
129 public int hashCode()
131 return entry.hashCode();
134 public boolean equals(Object other)
136 return entry.equals(other);
139 public String toString()
141 return getKey().toString() + "=" + getValue();
146 public Headers()
150 public boolean containsKey(Object key)
152 return super.containsKey(new Header((String) key));
155 public Object get(Object key)
157 return super.get(new Header((String) key));
161 * Returns the value of the specified header as a string.
163 public String getValue(String header)
165 return (String) super.get(new Header(header));
169 * Returns the value of the specified header as an integer,
170 * or -1 if the header is not present or not an integer.
172 public int getIntValue(String header)
174 String val = getValue(header);
175 if (val == null)
177 return -1;
181 return Integer.parseInt(val);
183 catch (NumberFormatException e)
186 return -1;
190 * Returns the value of the specified header as a long, or -1 if the
191 * header is not present or cannot be parsed as a long.
193 public long getLongValue(String header)
195 String val = getValue(header);
196 if (val == null)
198 return -1;
202 return Long.parseLong(val);
204 catch (NumberFormatException e)
207 return -1;
211 * Returns the value of the specified header as a date,
212 * or <code>null</code> if the header is not present or not a date.
214 public Date getDateValue(String header)
216 String val = getValue(header);
217 if (val == null)
219 return null;
223 return dateFormat.parse(val);
225 catch (ParseException e)
227 return null;
231 public Object put(Object key, Object value)
233 return super.put(new Header((String) key), value);
236 public Object remove(Object key)
238 return super.remove(new Header((String) key));
241 public void putAll(Map t)
243 for (Iterator i = t.keySet().iterator(); i.hasNext(); )
245 String key = (String) i.next();
246 String value = (String) t.get(key);
247 put(key, value);
251 public Set keySet()
253 Set keys = super.keySet();
254 Set ret = new LinkedHashSet();
255 for (Iterator i = keys.iterator(); i.hasNext(); )
257 ret.add(((Header) i.next()).name);
259 return ret;
262 public Set entrySet()
264 Set entries = super.entrySet();
265 Set ret = new LinkedHashSet();
266 for (Iterator i = entries.iterator(); i.hasNext(); )
268 Map.Entry entry = (Map.Entry) i.next();
269 ret.add(new HeaderEntry(entry));
271 return ret;
275 * Parse the specified input stream, adding headers to this collection.
277 public void parse(InputStream in)
278 throws IOException
280 LineInputStream lin = (in instanceof LineInputStream) ?
281 (LineInputStream) in : new LineInputStream(in);
283 String name = null;
284 StringBuilder value = new StringBuilder();
285 while (true)
287 String line = lin.readLine();
288 if (line == null)
290 if (name != null)
292 addValue(name, value.toString());
294 break;
296 int len = line.length();
297 if (len < 2)
299 if (name != null)
301 addValue(name, value.toString());
303 break;
305 char c1 = line.charAt(0);
306 if (c1 == ' ' || c1 == '\t')
308 // Continuation
309 int last = len - 1;
310 if (line.charAt(last) != '\r')
311 ++last;
312 value.append(line.substring(0, last));
314 else
316 if (name != null)
318 addValue(name, value.toString());
321 int di = line.indexOf(':');
322 name = line.substring(0, di);
323 value.setLength(0);
326 di++;
328 while (di < len && line.charAt(di) == ' ');
329 int last = len - 1;
330 if (line.charAt(last) != '\r')
331 ++last;
332 value.append(line.substring(di, last));
337 private void addValue(String name, String value)
339 Header key = new Header(name);
340 String old = (String) super.get(key);
341 if (old == null)
343 super.put(key, value);
345 else
347 super.put(key, old + ", " + value);