KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > mail > SmtpResponseReader


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.mail;
20
21 import java.io.InputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.BufferedReader JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25
26 /**
27  * A wrapper around the raw input from the SMTP server that assembles
28  * multi line responses into a single String.
29  *
30  * <p>The same rules used here would apply to FTP and other Telnet
31  * based protocols as well.</p>
32  *
33  */

34 public class SmtpResponseReader {
35     // CheckStyle:VisibilityModifier OFF - bc
36
protected BufferedReader JavaDoc reader = null;
37     // CheckStyle:VisibilityModifier ON
38
private StringBuffer JavaDoc result = new StringBuffer JavaDoc();
39
40     /**
41      * Wrap this input stream.
42      * @param in the stream to wrap.
43      */

44     public SmtpResponseReader(InputStream JavaDoc in) {
45         reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
46     }
47
48     /**
49      * Read until the server indicates that the response is complete.
50      *
51      * @return Responsecode (3 digits) + Blank + Text from all
52      * response line concatenated (with blanks replacing the \r\n
53      * sequences).
54      * @throws IOException on error.
55      */

56     public String JavaDoc getResponse() throws IOException JavaDoc {
57         result.setLength(0);
58         String JavaDoc line = reader.readLine();
59         if (line != null && line.length() >= 3) {
60             result.append(line.substring(0, 3));
61             result.append(" ");
62         }
63
64         while (line != null) {
65             append(line);
66             if (!hasMoreLines(line)) {
67                 break;
68             }
69             line = reader.readLine();
70         }
71         return result.toString().trim();
72     }
73
74     /**
75      * Closes the underlying stream.
76      * @throws IOException on error.
77      */

78     public void close() throws IOException JavaDoc {
79         reader.close();
80     }
81
82     /**
83      * Should we expect more input?
84      * @param line the line to check.
85      * @return true if there are more lines to check.
86      */

87     protected boolean hasMoreLines(String JavaDoc line) {
88         return line.length() > 3 && line.charAt(3) == '-';
89     }
90
91     /**
92      * Append the text from this line of the resonse.
93      */

94     private void append(String JavaDoc line) {
95         if (line.length() > 4) {
96             result.append(line.substring(4));
97             result.append(" ");
98         }
99     }
100 }
101
Popular Tags