source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/mail/SmtpResponseReader.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 2.8 KB
Line 
1/*
2 * Copyright 2000-2002,2004-2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.mail;
19
20import java.io.InputStream;
21import java.io.IOException;
22import java.io.BufferedReader;
23import java.io.InputStreamReader;
24
25/**
26 * A wrapper around the raw input from the SMTP server that assembles
27 * multi line responses into a single String.
28 *
29 * <p>The same rules used here would apply to FTP and other Telnet
30 * based protocols as well.</p>
31 *
32 */
33public class SmtpResponseReader {
34
35 protected BufferedReader reader = null;
36 private StringBuffer result = new StringBuffer();
37
38 /**
39 * Wrap this input stream.
40 * @param in the stream to wrap.
41 */
42 public SmtpResponseReader(InputStream in) {
43 reader = new BufferedReader(new InputStreamReader(in));
44 }
45
46 /**
47 * Read until the server indicates that the response is complete.
48 *
49 * @return Responsecode (3 digits) + Blank + Text from all
50 * response line concatenated (with blanks replacing the \r\n
51 * sequences).
52 * @throws IOException on error.
53 */
54 public String getResponse() throws IOException {
55 result.setLength(0);
56 String line = reader.readLine();
57 if (line != null && line.length() >= 3) {
58 result.append(line.substring(0, 3));
59 result.append(" ");
60 }
61
62 while (line != null) {
63 append(line);
64 if (!hasMoreLines(line)) {
65 break;
66 }
67 line = reader.readLine();
68 }
69 return result.toString().trim();
70 }
71
72 /**
73 * Closes the underlying stream.
74 * @throws IOException on error.
75 */
76 public void close() throws IOException {
77 reader.close();
78 }
79
80 /**
81 * Should we expect more input?
82 * @param line the line to check.
83 * @return true if there are more lines to check.
84 */
85 protected boolean hasMoreLines(String line) {
86 return line.length() > 3 && line.charAt(3) == '-';
87 }
88
89 /**
90 * Append the text from this line of the resonse.
91 */
92 private void append(String line) {
93 if (line.length() > 4) {
94 result.append(line.substring(4));
95 result.append(" ");
96 }
97 }
98}
Note: See TracBrowser for help on using the repository browser.