1 21 22 27 28 package com.sun.mail.pop3; 29 30 import java.io.*; 31 import java.util.Enumeration ; 32 import javax.mail.*; 33 import javax.mail.internet.*; 34 import javax.mail.event.*; 35 36 42 public class POP3Message extends MimeMessage { 43 44 49 50 static final String UNKNOWN = "UNKNOWN"; 52 53 private POP3Folder folder; private int hdrSize = -1; 55 private int msgSize = -1; 56 String uid = UNKNOWN; 58 public POP3Message(Folder folder, int msgno) 59 throws MessagingException { 60 super(folder, msgno); 61 this.folder = (POP3Folder)folder; 62 } 63 64 70 public void setFlags(Flags newFlags, boolean set) 71 throws MessagingException { 72 Flags oldFlags = (Flags)flags.clone(); 73 super.setFlags(newFlags, set); 74 if (!flags.equals(oldFlags)) 75 folder.notifyMessageChangedListeners( 76 MessageChangedEvent.FLAGS_CHANGED, this); 77 } 78 79 90 public int getSize() throws MessagingException { 91 if (msgSize >= 0) 93 return msgSize; 94 try { 95 synchronized (this) { 96 if (msgSize < 0) { 97 105 if (headers == null) 106 loadHeaders(); 107 if (contentStream != null) 108 msgSize = contentStream.available(); 109 else 110 msgSize = folder.getProtocol().list(msgnum) - hdrSize; 111 } 112 return msgSize; 113 } 114 } catch (EOFException eex) { 115 folder.close(false); 116 throw new FolderClosedException(folder, eex.toString()); 117 } catch (IOException ex) { 118 throw new MessagingException("error getting size", ex); 119 } 120 } 121 122 128 protected InputStream getContentStream() 129 throws MessagingException { 130 try { 131 synchronized(this) { 132 if (contentStream == null) { 133 InputStream rawcontent = folder.getProtocol().retr(msgnum, 134 msgSize > 0 ? msgSize + hdrSize : 0); 135 if (rawcontent == null) { 136 expunged = true; 137 throw new MessageRemovedException(); } 139 if (headers == null || 140 ((POP3Store)(folder.getStore())).forgetTopHeaders) { 141 headers = new InternetHeaders(rawcontent); 142 hdrSize = 143 (int)((SharedInputStream)rawcontent).getPosition(); 144 } else { 145 157 int offset = 0; 158 for (;;) { 159 int len = 0; int c1; 161 while ((c1 = rawcontent.read()) >= 0) { 162 if (c1 == '\n') break; 164 else if (c1 == '\r') { 165 if (rawcontent.available() > 0) { 167 rawcontent.mark(1); 168 if (rawcontent.read() != '\n') 169 rawcontent.reset(); 170 } 171 break; } 173 174 len++; 176 } 177 179 if (rawcontent.available() == 0) 181 break; 182 183 if (len == 0) 185 break; 186 } 187 hdrSize = 188 (int)((SharedInputStream)rawcontent).getPosition(); 189 } 190 contentStream = 191 ((SharedInputStream)rawcontent).newStream(hdrSize, -1); 192 rawcontent = null; } 194 } 195 } catch (EOFException eex) { 196 folder.close(false); 197 throw new FolderClosedException(folder, eex.toString()); 198 } catch (IOException ex) { 199 throw new MessagingException("error fetching POP3 content", ex); 200 } 201 return super.getContentStream(); 202 } 203 204 212 public void invalidate(boolean invalidateHeaders) { 213 content = null; 214 contentStream = null; 215 msgSize = -1; 216 if (invalidateHeaders) { 217 headers = null; 218 hdrSize = -1; 219 } 220 } 221 222 230 public InputStream top(int n) throws MessagingException { 231 try { 232 synchronized (this) { 233 return folder.getProtocol().top(msgnum, n); 234 } 235 } catch (EOFException eex) { 236 folder.close(false); 237 throw new FolderClosedException(folder, eex.toString()); 238 } catch (IOException ex) { 239 throw new MessagingException("error getting size", ex); 240 } 241 } 242 243 253 public String [] getHeader(String name) 254 throws MessagingException { 255 if (headers == null) 256 loadHeaders(); 257 return headers.getHeader(name); 258 } 259 260 272 public String getHeader(String name, String delimiter) 273 throws MessagingException { 274 if (headers == null) 275 loadHeaders(); 276 return headers.getHeader(name, delimiter); 277 } 278 279 291 public void setHeader(String name, String value) 292 throws MessagingException { 293 throw new IllegalWriteException("POP3 messages are read-only"); 295 } 296 297 309 public void addHeader(String name, String value) 310 throws MessagingException { 311 throw new IllegalWriteException("POP3 messages are read-only"); 313 } 314 315 324 public void removeHeader(String name) 325 throws MessagingException { 326 throw new IllegalWriteException("POP3 messages are read-only"); 328 } 329 330 342 public Enumeration getAllHeaders() throws MessagingException { 343 if (headers == null) 344 loadHeaders(); 345 return headers.getAllHeaders(); 346 } 347 348 354 public Enumeration getMatchingHeaders(String [] names) 355 throws MessagingException { 356 if (headers == null) 357 loadHeaders(); 358 return headers.getMatchingHeaders(names); 359 } 360 361 367 public Enumeration getNonMatchingHeaders(String [] names) 368 throws MessagingException { 369 if (headers == null) 370 loadHeaders(); 371 return headers.getNonMatchingHeaders(names); 372 } 373 374 383 public void addHeaderLine(String line) throws MessagingException { 384 throw new IllegalWriteException("POP3 messages are read-only"); 386 } 387 388 395 public Enumeration getAllHeaderLines() throws MessagingException { 396 if (headers == null) 397 loadHeaders(); 398 return headers.getAllHeaderLines(); 399 } 400 401 408 public Enumeration getMatchingHeaderLines(String [] names) 409 throws MessagingException { 410 if (headers == null) 411 loadHeaders(); 412 return headers.getMatchingHeaderLines(names); 413 } 414 415 422 public Enumeration getNonMatchingHeaderLines(String [] names) 423 throws MessagingException { 424 if (headers == null) 425 loadHeaders(); 426 return headers.getNonMatchingHeaderLines(names); 427 } 428 429 436 public void saveChanges() throws MessagingException { 437 throw new IllegalWriteException("POP3 messages are read-only"); 439 } 440 441 445 private void loadHeaders() throws MessagingException { 446 try { 447 synchronized (this) { 448 if (headers != null) return; 450 InputStream hdrs = null; 451 if (((POP3Store)(folder.getStore())).disableTop || 452 (hdrs = folder.getProtocol().top(msgnum, 0)) == null) { 453 InputStream cs = getContentStream(); 457 cs.close(); 458 } else { 459 hdrSize = hdrs.available(); 460 headers = new InternetHeaders(hdrs); 461 } 462 } 463 } catch (EOFException eex) { 464 folder.close(false); 465 throw new FolderClosedException(folder, eex.toString()); 466 } catch (IOException ex) { 467 throw new MessagingException("error loading POP3 headers", ex); 468 } 469 } 470 } 471 | Popular Tags |