1 21 22 27 28 package com.sun.mail.iap; 29 30 import java.io.*; 31 import java.util.*; 32 import com.sun.mail.util.*; 33 34 41 42 public class Response { 43 protected int index; protected int pindex; protected int size; protected byte[] buffer = null; 47 protected int type = 0; 48 protected String tag = null; 49 50 private static final int increment = 100; 51 52 public final static int TAG_MASK = 0x03; 55 public final static int CONTINUATION = 0x01; 56 public final static int TAGGED = 0x02; 57 public final static int UNTAGGED = 0x03; 58 59 public final static int TYPE_MASK = 0x1C; 62 public final static int OK = 0x04; 63 public final static int NO = 0x08; 64 public final static int BAD = 0x0C; 65 public final static int BYE = 0x10; 66 67 public final static int SYNTHETIC = 0x20; 69 70 public Response(String s) { 71 buffer = ASCIIUtility.getBytes(s); 72 size = buffer.length; 73 parse(); 74 } 75 76 80 public Response(Protocol p) throws IOException, ProtocolException { 81 82 ByteArray response = p.getInputStream().readResponse(); 84 buffer = response.getBytes(); 85 size = response.getCount() - 2; 87 parse(); 88 } 89 90 93 public Response(Response r) { 94 index = r.index; 95 size = r.size; 96 buffer = r.buffer; 97 type = r.type; 98 tag = r.tag; 99 } 100 101 105 public static Response byeResponse(Exception ex) { 106 String err = "* BYE JavaMail Exception: " + ex.toString(); 107 err = err.replace('\r', ' ').replace('\n', ' '); 108 Response r = new Response(err); 109 r.type |= SYNTHETIC; 110 return r; 111 } 112 113 private void parse() { 114 index = 0; 116 if (buffer[index] == '+') { type |= CONTINUATION; 118 index += 1; return; } else if (buffer[index] == '*') { type |= UNTAGGED; 122 index += 1; } else { type |= TAGGED; 125 tag = readAtom(); } 127 128 int mark = index; String s = readAtom(); if (s.equalsIgnoreCase("OK")) 131 type |= OK; 132 else if (s.equalsIgnoreCase("NO")) 133 type |= NO; 134 else if (s.equalsIgnoreCase("BAD")) 135 type |= BAD; 136 else if (s.equalsIgnoreCase("BYE")) 137 type |= BYE; 138 else 139 index = mark; 141 pindex = index; 142 return; 143 } 144 145 public void skipSpaces() { 146 while (index < size && buffer[index] == ' ') 147 index++; 148 } 149 150 153 public void skipToken() { 154 while (index < size && buffer[index] != ' ') 155 index++; 156 } 157 158 public void skip(int count) { 159 index += count; 160 } 161 162 public byte peekByte() { 163 if (index < size) 164 return buffer[index]; 165 else 166 return 0; } 168 169 173 public byte readByte() { 174 if (index < size) 175 return buffer[index++]; 176 else 177 return 0; } 179 180 185 public String readAtom() { 186 return readAtom('\0'); 187 } 188 189 193 public String readAtom(char delim) { 194 skipSpaces(); 195 196 if (index >= size) return null; 198 199 203 byte b; 204 int start = index; 205 while (index < size && ((b = buffer[index]) > ' ') && 206 b != '(' && b != ')' && b != '%' && b != '*' && 207 b != '"' && b != '\\' && b != 0x7f && 208 (delim == '\0' || b != delim)) 209 index++; 210 211 return ASCIIUtility.toString(buffer, start, index); 212 } 213 214 219 public String readString(char delim) { 220 skipSpaces(); 221 222 if (index >= size) return null; 224 225 int start = index; 226 while (index < size && buffer[index] != delim) 227 index++; 228 229 return ASCIIUtility.toString(buffer, start, index); 230 } 231 232 public String [] readStringList() { 233 skipSpaces(); 234 235 if (buffer[index] != '(') return null; 237 index++; 239 Vector v = new Vector(); 240 do { 241 v.addElement(readString()); 242 } while (buffer[index++] != ')'); 243 244 int size = v.size(); 245 if (size > 0) { 246 String [] s = new String [size]; 247 v.copyInto(s); 248 return s; 249 } else return null; 251 } 252 253 260 public int readNumber() { 261 skipSpaces(); 263 264 int start = index; 265 while (index < size && Character.isDigit((char)buffer[index])) 266 index++; 267 268 if (index > start) { 269 try { 270 return ASCIIUtility.parseInt(buffer, start, index); 271 } catch (NumberFormatException nex) { } 272 } 273 274 return -1; 275 } 276 277 284 public long readLong() { 285 skipSpaces(); 287 288 int start = index; 289 while (index < size && Character.isDigit((char)buffer[index])) 290 index++; 291 292 if (index > start) { 293 try { 294 return ASCIIUtility.parseLong(buffer, start, index); 295 } catch (NumberFormatException nex) { } 296 } 297 298 return -1; 299 } 300 301 309 public String readString() { 310 return (String )parseString(false, true); 311 } 312 313 321 public ByteArrayInputStream readBytes() { 322 ByteArray ba = readByteArray(); 323 if (ba != null) 324 return ba.toByteArrayInputStream(); 325 else 326 return null; 327 } 328 329 337 public ByteArray readByteArray() { 338 342 if (isContinuation()) { 343 skipSpaces(); 344 return new ByteArray(buffer, index, size - index); 345 } 346 return (ByteArray)parseString(false, false); 347 } 348 349 360 public String readAtomString() { 361 return (String )parseString(true, true); 362 } 363 364 369 private Object parseString(boolean parseAtoms, boolean returnString) { 370 byte b; 371 372 skipSpaces(); 374 375 b = buffer[index]; 376 if (b == '"') { index++; int start = index; 379 int copyto = index; 380 381 while ((b = buffer[index]) != '"') { 382 if (b == '\\') index++; 384 if (index != copyto) { buffer[copyto] = buffer[index]; 388 } 389 copyto++; 390 index++; 391 } 392 index++; 394 if (returnString) 395 return ASCIIUtility.toString(buffer, start, copyto); 396 else 397 return new ByteArray(buffer, start, copyto-start); 398 } else if (b == '{') { int start = ++index; 401 while (buffer[index] != '}') 402 index++; 403 404 int count = 0; 405 try { 406 count = ASCIIUtility.parseInt(buffer, start, index); 407 } catch (NumberFormatException nex) { 408 return null; 410 } 411 412 start = index + 3; index = start + count; 415 if (returnString) return ASCIIUtility.toString(buffer, start, start + count); 417 else 418 return new ByteArray(buffer, start, count); 419 } else if (parseAtoms) { int start = index; String s = readAtom(); 423 if (returnString) 424 return s; 425 else return new ByteArray(buffer, start, index); 427 } else if (b == 'N' || b == 'n') { index += 3; return null; 430 } 431 return null; } 433 434 public int getType() { 435 return type; 436 } 437 438 public boolean isContinuation() { 439 return ((type & TAG_MASK) == CONTINUATION); 440 } 441 442 public boolean isTagged() { 443 return ((type & TAG_MASK) == TAGGED); 444 } 445 446 public boolean isUnTagged() { 447 return ((type & TAG_MASK) == UNTAGGED); 448 } 449 450 public boolean isOK() { 451 return ((type & TYPE_MASK) == OK); 452 } 453 454 public boolean isNO() { 455 return ((type & TYPE_MASK) == NO); 456 } 457 458 public boolean isBAD() { 459 return ((type & TYPE_MASK) == BAD); 460 } 461 462 public boolean isBYE() { 463 return ((type & TYPE_MASK) == BYE); 464 } 465 466 public boolean isSynthetic() { 467 return ((type & SYNTHETIC) == SYNTHETIC); 468 } 469 470 474 public String getTag() { 475 return tag; 476 } 477 478 482 public String getRest() { 483 skipSpaces(); 484 return ASCIIUtility.toString(buffer, index, size); 485 } 486 487 490 public void reset() { 491 index = pindex; 492 } 493 494 public String toString() { 495 return ASCIIUtility.toString(buffer, 0, size); 496 } 497 498 } 499 | Popular Tags |