1 17 18 package org.apache.james.core; 19 20 import java.io.*; 21 import javax.mail.MessagingException ; 22 23 import org.apache.avalon.framework.activity.Disposable; 24 25 35 public class MimeMessageInputStreamSource 36 extends MimeMessageSource 37 implements Disposable { 38 39 42 File file = null; 43 44 47 String sourceId = null; 48 49 60 public MimeMessageInputStreamSource(String key, InputStream in) 61 throws MessagingException { 62 OutputStream fout = null; 65 try { 66 file = File.createTempFile(key, ".m64"); 67 fout = new BufferedOutputStream(new FileOutputStream(file)); 68 int b = -1; 69 while ((b = in.read()) != -1) { 70 fout.write(b); 71 } 72 fout.flush(); 73 74 sourceId = file.getCanonicalPath(); 75 } catch (IOException ioe) { 76 throw new MessagingException ("Unable to retrieve the data: " + ioe.getMessage(), ioe); 77 } finally { 78 try { 79 if (fout != null) { 80 fout.close(); 81 } 82 } catch (IOException ioe) { 83 } 85 86 try { 87 if (in != null) { 88 in.close(); 89 } 90 } catch (IOException ioe) { 91 } 93 } 94 } 95 96 101 public String getSourceId() { 102 return sourceId; 103 } 104 105 110 public synchronized InputStream getInputStream() throws IOException { 111 return new BufferedInputStream(new FileInputStream(file)); 112 } 113 114 121 public long getMessageSize() throws IOException { 122 return file.length(); 123 } 124 125 128 public void dispose() { 129 try { 130 if (file != null && file.exists()) { 131 file.delete(); 132 } 133 } catch (Exception e) { 134 } 136 file = null; 137 } 138 139 145 public void finalize() { 146 dispose(); 147 } 148 } 149 | Popular Tags |