KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > imap > IMAPDownloadThread


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.imap;
37
38 import java.io.File JavaDoc;
39 import java.io.FileInputStream JavaDoc;
40 import java.io.FileOutputStream JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.InputStream JavaDoc;
43 import java.io.OutputStream JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 import org.columba.ristretto.concurrency.Mutex;
47 import org.columba.ristretto.io.AsyncInputStream;
48 import org.columba.ristretto.io.MemBuffer;
49 import org.columba.ristretto.io.MemBufferInputStream;
50 import org.columba.ristretto.io.MemBufferOutputStream;
51 import org.columba.ristretto.io.TempSourceFactory;
52
53 /**
54  * Thread for asynchronous download from the IMAP
55  * server. It is invoked by the IMAPProtocol if
56  * a asychrounous method is called.
57  *
58  * @author Timo Stich <tstich@users.sourceforge.net>
59  */

60 public class IMAPDownloadThread implements Runnable JavaDoc {
61     private static final int BUFFER_SIZE = 1024;
62     
63     /** JDK 1.4+ logging framework logger, used for logging. */
64     private static final Logger JavaDoc LOG = Logger.getLogger("org.columba.ristretto.imap.protocol");
65
66     private AsyncInputStream partner;
67
68     private InputStream JavaDoc source;
69     private OutputStream JavaDoc out;
70
71     private byte[] buffer = new byte[BUFFER_SIZE];
72
73     private int size;
74     private int read;
75
76     private Mutex mutex;
77
78
79     /**
80      * @see java.lang.Runnable#run()
81      */

82     public void run() {
83         int lastRead;
84         long startTime = System.currentTimeMillis();
85         
86         try {
87             while( read < size ) {
88                 lastRead = source.read(buffer,0, size - read < BUFFER_SIZE ? size - read : BUFFER_SIZE);
89                 out.write(buffer, 0, lastRead);
90                 partner.grow(lastRead);
91                 read += lastRead;
92             }
93         } catch (IOException JavaDoc e) {
94             partner.exceptionOccured(e);
95             // This unblocks the waiting reader
96
partner.grow(size - read);
97         }
98
99         try {
100             //now read the rest from the response
101
int rest = 0;
102             while( rest != '\n' && rest != -1) {
103                 rest = source.read();
104             }
105
106             // and the a004 OK FETCH completed
107
rest = 0;
108             while( rest != '\n' && rest != -1) {
109                 rest = source.read();
110             }
111         } catch (IOException JavaDoc e1) {
112             LOG.warning( e1.getLocalizedMessage() );
113         }
114         
115         LOG.finer("Needed " + (System.currentTimeMillis() - startTime) + " ms for downloading " + size + " bytes.");
116         
117         if( mutex!= null ) {
118             mutex.release();
119         }
120     }
121     /**
122      * Constructs the DownloadThread.java.
123      *
124      * @param partner
125      * @param source
126      * @param size
127      */

128     private IMAPDownloadThread(AsyncInputStream partner, InputStream JavaDoc source,
129             OutputStream JavaDoc out, int size, Mutex mutex) {
130         this.partner = partner;
131         this.source = source;
132         this.size = size;
133         this.out = out;
134         this.mutex = mutex;
135     }
136     
137     /**
138      * Constructs a new IMAPDownloadThread and starts
139      * the download.
140      *
141      * @param input the InputStream from the server.
142      * @param size the size of the part in bytes.
143      * @param mutex that manages the access on the input.
144      * @return the InputStream of the downloaded part.
145      * @throws IOException
146      */

147     public static AsyncInputStream asyncDownload(InputStream JavaDoc input, int size, Mutex mutex) throws IOException JavaDoc {
148         InputStream JavaDoc literalSource;
149         OutputStream JavaDoc out;
150         
151         if( TempSourceFactory.useMemoryTemp(size)) {
152             MemBuffer literalBuffer = new MemBuffer(size);
153             
154             literalSource = new MemBufferInputStream(literalBuffer);
155             out = new MemBufferOutputStream(literalBuffer);
156         } else {
157             File JavaDoc tempFile = TempSourceFactory.createTempFile();
158             
159             // First create file that has correct size
160
byte[] zeros = new byte[10000];
161             out = new FileOutputStream JavaDoc(tempFile);
162             try {
163                 int i;
164                 for( i=0; i<size; i+=10000) {
165                     out.write(zeros);
166                 }
167                 out.write(zeros,0,size % 10000);
168             } finally {
169                 out.close();
170             }
171             
172             literalSource = new FileInputStream JavaDoc(tempFile);
173             out = new FileOutputStream JavaDoc(tempFile);
174         }
175
176         
177         AsyncInputStream asyncStream = new AsyncInputStream(literalSource, size);
178      
179         IMAPDownloadThread thread = new IMAPDownloadThread( asyncStream, input, out, size, mutex );
180         
181         new Thread JavaDoc( thread ).start();
182         
183         return asyncStream;
184     }
185 }
186
Popular Tags