KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > pop3 > POP3InputStream


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.pop3;
37
38 import java.io.FilterInputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.InputStream JavaDoc;
41 import java.util.regex.Matcher JavaDoc;
42 import java.util.regex.Pattern JavaDoc;
43
44 import org.columba.ristretto.concurrency.Mutex;
45 import org.columba.ristretto.io.ConnectionDroppedException;
46 import org.columba.ristretto.io.TempSourceFactory;
47 import org.columba.ristretto.parser.ParserException;
48 import org.columba.ristretto.pop3.parser.POP3ResponseParser;
49
50 /**
51  * FilterInputStream that capsules the InputStream from the
52  * socket with a POP3 server.
53  *
54  *
55  * @author Timo Stich <tstich@users.sourceforge.net>
56  */

57 public class POP3InputStream extends FilterInputStream JavaDoc {
58
59     private static final int RETR_BUFFER_SIZE = 1024;
60     private StringBuffer JavaDoc lineBuffer;
61     private static final Pattern JavaDoc sizePattern = Pattern.compile("(\\d+) octets");
62
63     /**
64      * Constructs a POP3InputStream.
65      *
66      * @param in the socket InputStream
67      */

68     public POP3InputStream(InputStream JavaDoc in) {
69         super(in);
70
71         lineBuffer = new StringBuffer JavaDoc();
72     }
73
74     /**
75      * Reads and parses a single line reponse from a POP3 server.
76      *
77      * @return the parsed POP3Response
78      * @throws IOException
79      * @throws POP3Exception
80      */

81     public POP3Response readSingleLineResponse()
82         throws IOException JavaDoc, POP3Exception {
83         readLineInBuffer();
84
85         try {
86             POP3Response response = POP3ResponseParser.parse(lineBuffer);
87
88             return response;
89         } catch (ParserException e) {
90             throw new POP3Exception(e.getMessage());
91         }
92     }
93     
94     /**
95      * Uses the POP3DownloadThread to asynchronously download mails from the POP3 server.
96      *
97      * @param size estimated size of the mail or -1 if unknown.
98      * @param mutex Mutex of the socket which is released after the download finished.
99      * @return the InputStream of the mail
100      * @throws IOException
101      */

102     public InputStream JavaDoc asyncDownload(int size, Mutex mutex) throws IOException JavaDoc {
103         return POP3DownloadThread.asyncDownload( new POP3MultiLineStream(this, size), size, mutex);
104     }
105
106     /**
107      * Blocking download of mails from the POP3 server.
108      *
109      * @param size
110      * @return the InputStream of the mail
111      * @throws IOException
112      */

113     public InputStream JavaDoc syncDownload(int size) throws IOException JavaDoc {
114         return new POP3MultiLineStream(this, size);
115         
116     }
117
118     
119     /**
120      * Reads and parses a multiline POP3 response.
121      *
122      * @return the parsed POP3Response.
123      * @throws IOException
124      * @throws POP3Exception
125      */

126     public POP3Response readMultiLineResponse()
127         throws IOException JavaDoc, POP3Exception {
128         POP3Response response = readSingleLineResponse();
129
130         // Read data only if this response was +OK
131
if (response.isOK()) {
132             int size = -1;
133             // try to find out the size of the stream
134
Matcher JavaDoc matcher = sizePattern.matcher(response.getMessage());
135             if( matcher.find() ) {
136                 size = Integer.parseInt(matcher.group(1));
137             }
138
139             POP3MultiLineStream multiLineStream = new POP3MultiLineStream(this, size);
140             response.setData(TempSourceFactory.createTempSource(multiLineStream, -1));
141         }
142
143         return response;
144     }
145
146     private void readLineInBuffer() throws IOException JavaDoc {
147         // Clear the buffer
148
lineBuffer.delete(0, lineBuffer.length());
149
150         int read = in.read();
151         // read until CRLF
152
while (read != '\r' && read != -1) {
153             lineBuffer.append((char) read);
154             read = in.read();
155         }
156         lineBuffer.append((char)read);
157
158         // read the LF
159
read = in.read();
160         if( read != '\n') throw new ConnectionDroppedException();
161         lineBuffer.append((char) read);
162     }
163
164 }
165
Popular Tags