1 21 22 27 28 package com.sun.mail.imap.protocol; 29 30 import java.io.*; 31 import java.util.*; 32 import com.sun.mail.util.*; 33 import com.sun.mail.iap.*; 34 35 42 43 public class FetchResponse extends IMAPResponse { 44 private Item[] items; 45 46 public FetchResponse(Protocol p) 47 throws IOException, ProtocolException { 48 super(p); 49 parse(); 50 } 51 52 public FetchResponse(IMAPResponse r) 53 throws IOException, ProtocolException { 54 super(r); 55 parse(); 56 } 57 58 public int getItemCount() { 59 return items.length; 60 } 61 62 public Item getItem(int index) { 63 return items[index]; 64 } 65 66 public Item getItem(Class c) { 67 for (int i = 0; i < items.length; i++) { 68 if (c.isInstance(items[i])) 69 return items[i]; 70 } 71 72 return null; 73 } 74 75 public static Item getItem(Response[] r, int msgno, Class c) { 76 if (r == null) 77 return null; 78 79 for (int i = 0; i < r.length; i++) { 80 81 if (r[i] == null || 82 !(r[i] instanceof FetchResponse) || 83 ((FetchResponse)r[i]).getNumber() != msgno) 84 continue; 85 86 FetchResponse f = (FetchResponse)r[i]; 87 for (int j = 0; j < f.items.length; j++) { 88 if (c.isInstance(f.items[j])) 89 return f.items[j]; 90 } 91 } 92 93 return null; 94 } 95 96 private final static char[] HEADER = {'.','H','E','A','D','E','R'}; 97 private final static char[] TEXT = {'.','T','E','X','T'}; 98 99 100 private void parse() throws ParsingException { 101 skipSpaces(); 102 if (buffer[index] != '(') 103 throw new ParsingException( 104 "error in FETCH parsing, missing '(' at index " + index); 105 106 Vector v = new Vector(); 107 Item i = null; 108 do { 109 index++; 111 if (index >= size) 112 throw new ParsingException( 113 "error in FETCH parsing, ran off end of buffer, size " + size); 114 115 switch(buffer[index]) { 116 case 'E': 117 if (match(ENVELOPE.name)) { 118 index += ENVELOPE.name.length; i = new ENVELOPE(this); 120 } 121 break; 122 case 'F': 123 if (match(FLAGS.name)) { 124 index += FLAGS.name.length; i = new FLAGS((IMAPResponse)this); 126 } 127 break; 128 case 'I': 129 if (match(INTERNALDATE.name)) { 130 index += INTERNALDATE.name.length; i = new INTERNALDATE(this); 132 } 133 break; 134 case 'B': 135 if (match(BODY.name)) { 136 if (buffer[index+4] == '[') { 137 index += BODY.name.length; i = new BODY(this); 139 } 140 else { 141 if (match(BODYSTRUCTURE.name)) 142 index += BODYSTRUCTURE.name.length; 143 else 145 index += BODY.name.length; i = new BODYSTRUCTURE(this); 147 } 148 } 149 break; 150 case 'R': 151 if (match(RFC822SIZE.name)) { 152 index += RFC822SIZE.name.length; i = new RFC822SIZE(this); 154 } 155 else { 156 if (match(RFC822DATA.name)) { 157 index += RFC822DATA.name.length; 158 if (match(HEADER)) 159 index += HEADER.length; else if (match(TEXT)) 161 index += TEXT.length; i = new RFC822DATA(this); 163 } 164 } 165 break; 166 case 'U': 167 if (match(UID.name)) { 168 index += UID.name.length; 169 i = new UID(this); 170 } 171 break; 172 default: 173 } 174 if (i != null) 175 v.addElement(i); 176 } while (buffer[index] != ')'); 177 178 index++; items = new Item[v.size()]; 180 v.copyInto(items); 181 } 182 183 187 private boolean match(char[] itemName) { 188 int len = itemName.length; 189 for (int i = 0, j = index; i < len;) 190 if (Character.toUpperCase((char)buffer[j++]) != itemName[i++]) 193 return false; 194 return true; 195 } 196 } 197 | Popular Tags |