1 2 24 25 26 27 28 package com.lutris.mime; 29 import java.io.IOException ; 30 import java.io.PushbackReader ; 31 import java.io.StringReader ; 32 import java.util.Enumeration ; 33 import java.util.Hashtable ; 34 35 44 public class MimeHeader { 45 48 private Hashtable myParams; 49 50 53 private String myValue; 54 55 59 private String myHeaderType; 60 61 64 private String rawHeaderLine; 65 66 70 public 71 MimeHeader(String headerLine) 72 { 73 myParams = new Hashtable (); 74 rawHeaderLine = headerLine; 75 int pos = headerLine.indexOf(':'); 76 if (pos < 0) { 77 myHeaderType = null; 78 } else { 79 myHeaderType = headerLine.substring(0, pos).trim().toLowerCase(); 80 headerLine = headerLine.substring(pos+1); 81 } 82 pos = headerLine.indexOf(';'); 83 if (pos < 0) { 84 pos = headerLine.indexOf('('); 86 if (pos < 0) { 87 myValue = headerLine.trim(); 88 } else { 89 myValue = headerLine.substring(0, pos).trim(); 90 } 91 } else { 92 myValue = headerLine.substring(0, pos).trim(); 93 } 94 } 95 96 103 protected 104 void putParameter(String name, String value) 105 { 106 name = name.toLowerCase(); 107 String [] oldvals = (String []) myParams.get(name); 108 int num = oldvals == null ? 0 : oldvals.length; 109 String [] newvals = new String [num+1]; 110 for (int i=0; i < num; i++) { 111 newvals[i] = oldvals[i]; 112 oldvals[i] = null; 113 } 114 newvals[num] = value; 115 oldvals = null; 116 myParams.put(name, newvals); 117 } 118 119 134 public 135 String getValue() 136 { 137 return myValue; 138 } 139 140 154 public 155 String getHeaderType() 156 { 157 return myHeaderType; 158 } 159 160 165 public 166 String getRawHeaderLine() 167 { 168 return rawHeaderLine; 169 } 170 171 187 public 188 String getParameter(String name) 189 { 190 name = name.toLowerCase(); 191 String [] vals = (String []) myParams.get(name); 192 if (vals == null) return null; 193 if (vals.length < 1) return null; 194 return vals[vals.length - 1]; 195 } 196 197 213 public String [] getParameters(String name) 214 { 215 name = name.toLowerCase(); 216 String [] vals = (String []) myParams.get(name); 217 if (vals == null) return null; 218 if (vals.length < 1) return null; 219 return vals; 220 } 221 222 229 public Enumeration getParameterNames() 230 { 231 return myParams.keys(); 232 } 233 234 private static 235 void skipComment(PushbackReader input) 236 throws IOException 237 { 238 int ch; 239 while ((ch = input.read()) >= 0) if (ch == ')') return; 240 } 241 242 private final 243 String parseToken(PushbackReader input, boolean trim) 244 throws IOException 245 { 246 StringBuffer buf = new StringBuffer (); 247 int ch; 248 if (trim) { 249 while ((ch = input.read()) >= 0) if ((ch > ' ') && (ch < 0xff)) { 250 input.unread(ch); 251 if (ch == '(') { 252 skipComment(input); 253 } 254 else 255 break; 256 } 257 } 258 while ((ch = input.read()) >= 0) { 259 switch (ch) { 260 case '=': case ';': 261 input.unread(ch); 262 return buf.toString(); 263 case '(': 264 skipComment(input); 265 break; 266 default: 267 if ((ch > ' ') && (ch < 0xff)) 268 buf.append((char)ch); 269 else { 270 input.unread(ch); 271 return buf.toString(); 272 } 273 break; 274 } 275 } 276 return buf.toString(); 277 } 278 279 private final 280 String parseQuotedString(PushbackReader input) 281 throws IOException 282 { 283 StringBuffer buf = new StringBuffer (); 284 int ch; 285 while ((ch = input.read()) >= 0) { 286 switch (ch) { 287 case '"': 288 return buf.toString(); 289 290 case '\\': 291 if ((ch = input.read()) >= 0) { 292 if ((ch!='\\')&&(ch!='"')) { 293 buf.append('\\'); 294 } 295 buf.append((char)ch); 296 } else { 297 buf.append('\\'); 298 return buf.toString(); 299 } 300 break; 301 302 default: 303 buf.append((char)ch); 304 break; 305 } 306 } 307 return buf.toString(); 308 } 309 310 320 protected 321 void parseParameters(String headerLine) 322 { 323 try { 324 StringReader reader = new StringReader (headerLine); 325 PushbackReader input = new PushbackReader (reader); 326 int ch; 327 String key, value; 328 while (true) { 329 key = parseToken(input, true); 330 ch = input.read(); 331 if (ch < 0) break; 332 switch (ch) { 333 case '=': 334 switch (ch = input.read()) { 335 case '"': 336 value = parseQuotedString(input); 337 if (key.length() > 0) { 338 putParameter(key, value); 339 } 340 break; 341 default: 342 input.unread(ch); 343 value = parseToken(input, false); 344 if (key.length() > 0) { 345 putParameter(key, value); 346 } 347 break; 348 } 349 while (((ch = input.read()) >= 0) && (ch != ';')) { 350 if (ch == '(') { 351 skipComment(input); 352 } 353 } 354 break; 355 case ';': 356 break; 358 default: 359 return; 361 } } return; 364 } 365 catch (IOException e) { 366 return; 367 } 368 } 369 } 370 371 | Popular Tags |