1 36 package org.columba.ristretto.pop3; 37 38 import java.io.File ; 39 import java.io.FileInputStream ; 40 import java.io.FileOutputStream ; 41 import java.io.IOException ; 42 import java.io.InputStream ; 43 import java.io.OutputStream ; 44 import java.util.logging.Logger ; 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 59 public class POP3DownloadThread implements Runnable { 60 private static final int BUFFER_SIZE = 1024; 61 62 63 private static final Logger LOG = Logger.getLogger("org.columba.ristretto.pop3.protocol"); 64 65 private AsyncInputStream partner; 66 67 private InputStream source; 68 private OutputStream out; 69 70 private byte[] buffer = new byte[BUFFER_SIZE]; 71 72 private int size; 73 private int read; 74 75 private Mutex mutex; 76 77 78 81 public void run() { 82 int lastRead; 83 long startTime = System.currentTimeMillis(); 84 85 86 try { 87 lastRead = source.read(buffer); 88 while( lastRead != -1 ) { 89 90 if( read + lastRead > size ) { 91 partner.setSize( read + lastRead); 93 LOG.warning("message should be " + size + " but is " + (read + lastRead) + " bytes"); 94 } 95 96 out.write(buffer, 0, lastRead); 97 partner.grow(lastRead); 98 read += lastRead; 99 100 lastRead = source.read(buffer); 101 } 102 } catch (IOException e) { 103 partner.exceptionOccured(e); 104 partner.grow(size - read); 106 } 107 108 109 if( read < size ) { 110 partner.setSize(read); 112 113 partner.grow(1); 115 116 LOG.warning("message should be " + size + " but is " + read + " bytes"); 117 } 118 119 LOG.finer("Needed " + (System.currentTimeMillis() - startTime) + " ms for downloading " + size + " bytes."); 120 121 if( mutex!= null ) { 122 mutex.release(); 123 } 124 } 125 132 private POP3DownloadThread(AsyncInputStream partner, InputStream source, 133 OutputStream out, int size, Mutex mutex) { 134 this.partner = partner; 135 this.source = source; 136 this.size = size; 137 this.out = out; 138 this.mutex = mutex; 139 } 140 141 142 151 public static AsyncInputStream asyncDownload(InputStream source, int size, Mutex mutex) throws IOException { 152 InputStream literalSource; 153 OutputStream out; 154 155 if( TempSourceFactory.useMemoryTemp(size)) { 156 MemBuffer literalBuffer = new MemBuffer(size); 157 out = new MemBufferOutputStream(literalBuffer); 158 literalSource = new MemBufferInputStream(literalBuffer); 159 } else { 160 File tempFile = TempSourceFactory.createTempFile(); 161 162 byte[] zeros = new byte[10000]; 164 out = new FileOutputStream (tempFile); 165 try { 166 int i; 167 for( i=0; i<size; i+=10000) { 168 out.write(zeros); 169 } 170 out.write(zeros,0,size % 10000); 171 } finally { 172 out.close(); 173 } 174 175 literalSource = new FileInputStream (tempFile); 176 out = new FileOutputStream (tempFile); 177 } 178 179 180 AsyncInputStream asyncStream = new AsyncInputStream(literalSource, size); 181 182 POP3DownloadThread thread = new POP3DownloadThread( asyncStream, source, out, size, mutex ); 183 184 new Thread ( thread ).start(); 185 186 return asyncStream; 187 } 188 } 189 | Popular Tags |