1 package org.columba.mail.folder.mh; 17 18 import java.io.File ; 19 import java.io.FileInputStream ; 20 import java.io.FileOutputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.List ; 27 import java.util.logging.Logger ; 28 29 30 import org.columba.core.io.StreamUtils; 31 import org.columba.mail.folder.AbstractLocalFolder; 32 import org.columba.mail.folder.FolderInconsistentException; 33 import org.columba.mail.folder.IDataStorage; 34 import org.columba.ristretto.io.FileSource; 35 import org.columba.ristretto.io.Source; 36 37 38 54 public class MHDataStorage implements IDataStorage { 55 56 57 private static final Logger LOG = Logger.getLogger("org.columba.mail.folder.mh"); 58 59 protected AbstractLocalFolder folder; 60 61 public MHDataStorage(AbstractLocalFolder folder) { 62 this.folder = folder; 63 } 64 65 public boolean exists(Object uid) throws Exception { 66 File file = new File (folder.getDirectoryFile() + File.separator 67 + ((Integer ) uid).toString()); 68 69 return file.exists(); 70 } 71 72 public void removeMessage(Object uid) throws Exception { 73 File file = new File (folder.getDirectoryFile() + File.separator 74 + ((Integer ) uid).toString()); 75 76 if (!file.delete()) { 78 LOG.warning("Could not delete " + file.getAbsolutePath() 80 + ". Will try to delete it on exit"); 81 82 file.deleteOnExit(); 84 } else { 85 LOG.info(file.getAbsolutePath() + " deleted successfully"); 86 } 87 } 88 89 public int getMessageCount() { 90 File [] list = folder.getDirectoryFile().listFiles(MHMessageFileFilter.getInstance()); 91 92 return list.length; 93 } 94 95 98 public Object [] getMessageUids() { 99 File [] list = folder.getDirectoryFile().listFiles(MHMessageFileFilter.getInstance()); 100 101 List result = new ArrayList (list.length); 104 for (int i = 0; i < list.length; i++) { 105 result.add(i, new Integer (list[i].getName())); 106 } 107 108 Collections.sort(result); 109 110 return result.toArray(); 111 } 112 113 116 public Source getMessageSource(Object uid) throws Exception { 117 File file = new File (folder.getDirectoryFile() + File.separator + ((Integer ) uid).toString()); 118 119 try { 120 return new FileSource(file); 121 } catch (IOException e) { 122 throw new FolderInconsistentException(); 123 } 124 } 125 126 129 public void saveMessage(Object uid, InputStream source) 130 throws IOException { 131 File file = new File (folder.getDirectoryFile() + File.separator + (Integer ) uid); 132 133 OutputStream out = new FileOutputStream (file); 134 135 StreamUtils.streamCopy(source, out); 136 137 source.close(); 138 out.close(); 139 } 140 141 144 public InputStream getMessageStream(Object uid) throws Exception { 145 146 return new FileInputStream ( new File (folder.getDirectoryFile() + File.separator + ((Integer ) uid).toString())); 147 } 148 } 149 | Popular Tags |