KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > imap > protocol > IMAPResponse


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)IMAPResponse.java 1.4 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

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 /**
36  * This class represents a response obtained from the input stream
37  * of an IMAP server.
38  *
39  * @version 1.4, 05/08/29
40  * @author John Mani
41  */

42
43 public class IMAPResponse extends Response {
44     private String JavaDoc key;
45     private int number;
46
47     public IMAPResponse(Protocol c) throws IOException, ProtocolException {
48     super(c);
49
50     // continue parsing if this is an untagged response
51
if (isUnTagged() && !isOK() && !isNO() && !isBAD() && !isBYE()) {
52         key = readAtom();
53
54         // Is this response of the form "* <number> <command>"
55
try {
56         number = Integer.parseInt(key);
57         key = readAtom();
58         } catch (NumberFormatException JavaDoc ne) { }
59     }
60     }
61
62     /**
63      * Copy constructor.
64      */

65     public IMAPResponse(IMAPResponse r) {
66     super((Response)r);
67     key = r.key;
68     number = r.number;
69     }
70
71     /**
72      * Read a list of space-separated "flag_extension" sequences and
73      * return the list as a array of Strings. An empty list is returned
74      * as null. This is an IMAP-ism, and perhaps this method should
75      * moved into the IMAP layer.
76      */

77     public String JavaDoc[] readSimpleList() {
78     skipSpaces();
79
80     if (buffer[index] != '(') // not what we expected
81
return null;
82     index++; // skip '('
83

84     Vector v = new Vector();
85     int start;
86     for (start = index; buffer[index] != ')'; index++) {
87         if (buffer[index] == ' ') { // got one item
88
v.addElement(ASCIIUtility.toString(buffer, start, index));
89         start = index+1; // index gets incremented at the top
90
}
91     }
92     if (index > start) // get the last item
93
v.addElement(ASCIIUtility.toString(buffer, start, index));
94     index++; // skip ')'
95

96     int size = v.size();
97     if (size > 0) {
98         String JavaDoc[] s = new String JavaDoc[size];
99         v.copyInto(s);
100         return s;
101     } else // empty list
102
return null;
103     }
104
105     public String JavaDoc getKey() {
106     return key;
107     }
108
109     public boolean keyEquals(String JavaDoc k) {
110     if (key != null && key.equalsIgnoreCase(k))
111         return true;
112     else
113         return false;
114     }
115
116     public int getNumber() {
117     return number;
118     }
119
120     public static IMAPResponse readResponse(Protocol p)
121             throws IOException, ProtocolException {
122     IMAPResponse r = new IMAPResponse(p);
123     if (r.keyEquals("FETCH"))
124         r = new FetchResponse(r);
125     return r;
126     }
127 }
128
Popular Tags