1 /* |
1 /* |
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
71 * @author Bill Shannon |
71 * @author Bill Shannon |
72 * @see MimeUtility |
72 * @see MimeUtility |
73 */ |
73 */ |
74 public final class InternetHeaders { |
74 public final class InternetHeaders { |
75 |
75 |
76 private final FinalArrayList headers = new FinalArrayList(); |
76 private final FinalArrayList<hdr> headers = new FinalArrayList<hdr>(); |
77 |
77 |
78 /** |
78 /** |
79 * Lazily cerated view of header lines (Strings). |
79 * Lazily cerated view of header lines (Strings). |
80 */ |
80 */ |
81 private List headerValueView; |
81 private List<String> headerValueView; |
82 |
82 |
83 /** |
83 /** |
84 * Create an empty InternetHeaders object. |
84 * Create an empty InternetHeaders object. |
85 */ |
85 */ |
86 public InternetHeaders() { |
86 public InternetHeaders() { |
117 // to have BodyParts with no header lines. |
117 // to have BodyParts with no header lines. |
118 String line; |
118 String line; |
119 LineInputStream lis = new LineInputStream(is); |
119 LineInputStream lis = new LineInputStream(is); |
120 String prevline = null; // the previous header line, as a string |
120 String prevline = null; // the previous header line, as a string |
121 // a buffer to accumulate the header in, when we know it's needed |
121 // a buffer to accumulate the header in, when we know it's needed |
122 StringBuffer lineBuffer = new StringBuffer(); |
122 StringBuilder lineBuffer = new StringBuilder(); |
123 |
123 |
124 try { |
124 try { |
125 //while ((line = lis.readLine()) != null) { |
125 //while ((line = lis.readLine()) != null) { |
126 do { |
126 do { |
127 line = lis.readLine(); |
127 line = lis.readLine(); |
159 * @param name header name |
159 * @param name header name |
160 * @return array of header values, or null if none |
160 * @return array of header values, or null if none |
161 */ |
161 */ |
162 public String[] getHeader(String name) { |
162 public String[] getHeader(String name) { |
163 // XXX - should we just step through in index order? |
163 // XXX - should we just step through in index order? |
164 FinalArrayList v = new FinalArrayList(); // accumulate return values |
164 FinalArrayList<String> v = new FinalArrayList<String>(); // accumulate return values |
165 |
165 |
166 int len = headers.size(); |
166 int len = headers.size(); |
167 for( int i=0; i<len; i++ ) { |
167 for( int i=0; i<len; i++ ) { |
168 hdr h = (hdr) headers.get(i); |
168 hdr h = headers.get(i); |
169 if (name.equalsIgnoreCase(h.name)) { |
169 if (name.equalsIgnoreCase(h.name)) { |
170 v.add(h.getValue()); |
170 v.add(h.getValue()); |
171 } |
171 } |
172 } |
172 } |
173 if (v.size() == 0) |
173 if (v.size() == 0) |
174 return (null); |
174 return (null); |
175 // convert Vector to an array for return |
175 // convert Vector to an array for return |
176 return (String[]) v.toArray(new String[v.size()]); |
176 return v.toArray(new String[v.size()]); |
177 } |
177 } |
178 |
178 |
179 /** |
179 /** |
180 * Get all the headers for this header name, returned as a single |
180 * Get all the headers for this header name, returned as a single |
181 * String, with headers separated by the delimiter. If the |
181 * String, with headers separated by the delimiter. If the |
217 */ |
217 */ |
218 public void setHeader(String name, String value) { |
218 public void setHeader(String name, String value) { |
219 boolean found = false; |
219 boolean found = false; |
220 |
220 |
221 for (int i = 0; i < headers.size(); i++) { |
221 for (int i = 0; i < headers.size(); i++) { |
222 hdr h = (hdr) headers.get(i); |
222 hdr h = headers.get(i); |
223 if (name.equalsIgnoreCase(h.name)) { |
223 if (name.equalsIgnoreCase(h.name)) { |
224 if (!found) { |
224 if (!found) { |
225 int j; |
225 int j; |
226 if (h.line != null && (j = h.line.indexOf(':')) >= 0) { |
226 if (h.line != null && (j = h.line.indexOf(':')) >= 0) { |
227 h.line = h.line.substring(0, j + 1) + " " + value; |
227 h.line = h.line.substring(0, j + 1) + " " + value; |
250 * @param value header value |
250 * @param value header value |
251 */ |
251 */ |
252 public void addHeader(String name, String value) { |
252 public void addHeader(String name, String value) { |
253 int pos = headers.size(); |
253 int pos = headers.size(); |
254 for (int i = headers.size() - 1; i >= 0; i--) { |
254 for (int i = headers.size() - 1; i >= 0; i--) { |
255 hdr h = (hdr) headers.get(i); |
255 hdr h = headers.get(i); |
256 if (name.equalsIgnoreCase(h.name)) { |
256 if (name.equalsIgnoreCase(h.name)) { |
257 headers.add(i + 1, new hdr(name, value)); |
257 headers.add(i + 1, new hdr(name, value)); |
258 return; |
258 return; |
259 } |
259 } |
260 // marker for default place to add new headers |
260 // marker for default place to add new headers |
269 * |
269 * |
270 * @param name header name |
270 * @param name header name |
271 */ |
271 */ |
272 public void removeHeader(String name) { |
272 public void removeHeader(String name) { |
273 for (int i = 0; i < headers.size(); i++) { |
273 for (int i = 0; i < headers.size(); i++) { |
274 hdr h = (hdr) headers.get(i); |
274 hdr h = headers.get(i); |
275 if (name.equalsIgnoreCase(h.name)) { |
275 if (name.equalsIgnoreCase(h.name)) { |
276 headers.remove(i); |
276 headers.remove(i); |
277 i--; // have to look at i again |
277 i--; // have to look at i again |
278 } |
278 } |
279 } |
279 } |
283 * Return all the headers as an Enumeration of |
283 * Return all the headers as an Enumeration of |
284 * {@link Header} objects. |
284 * {@link Header} objects. |
285 * |
285 * |
286 * @return Header objects |
286 * @return Header objects |
287 */ |
287 */ |
288 public FinalArrayList getAllHeaders() { |
288 public FinalArrayList<hdr> getAllHeaders() { |
289 return headers; // conceptually it should be read-only, but for performance reason I'm not wrapping it here |
289 return headers; // conceptually it should be read-only, but for performance reason I'm not wrapping it here |
290 } |
290 } |
291 |
291 |
292 /** |
292 /** |
293 * Add an RFC822 header line to the header store. |
293 * Add an RFC822 header line to the header store. |
300 */ |
300 */ |
301 public void addHeaderLine(String line) { |
301 public void addHeaderLine(String line) { |
302 try { |
302 try { |
303 char c = line.charAt(0); |
303 char c = line.charAt(0); |
304 if (c == ' ' || c == '\t') { |
304 if (c == ' ' || c == '\t') { |
305 hdr h = (hdr) headers.get(headers.size() - 1); |
305 hdr h = headers.get(headers.size() - 1); |
306 h.line += "\r\n" + line; |
306 h.line += "\r\n" + line; |
307 } else |
307 } else |
308 headers.add(new hdr(line)); |
308 headers.add(new hdr(line)); |
309 } catch (StringIndexOutOfBoundsException e) { |
309 } catch (StringIndexOutOfBoundsException e) { |
310 // line is empty, ignore it |
310 // line is empty, ignore it |
315 } |
315 } |
316 |
316 |
317 /** |
317 /** |
318 * Return all the header lines as a collection |
318 * Return all the header lines as a collection |
319 */ |
319 */ |
320 public List getAllHeaderLines() { |
320 public List<String> getAllHeaderLines() { |
321 if(headerValueView==null) |
321 if(headerValueView==null) |
322 headerValueView = new AbstractList() { |
322 headerValueView = new AbstractList<String>() { |
323 public Object get(int index) { |
323 public String get(int index) { |
324 return ((hdr)headers.get(index)).line; |
324 return headers.get(index).line; |
325 } |
325 } |
326 |
326 |
327 public int size() { |
327 public int size() { |
328 return headers.size(); |
328 return headers.size(); |
329 } |
329 } |