1 package org.columba.mail.folder.zippedmh; 2 3 import java.io.File ; 4 import java.io.FileFilter ; 5 import java.io.FileOutputStream ; 6 import java.io.InputStream ; 7 import java.util.regex.Matcher ; 8 import java.util.regex.Pattern ; 9 import java.util.zip.ZipEntry ; 10 import java.util.zip.ZipFile ; 11 import java.util.zip.ZipOutputStream ; 12 13 import org.columba.core.io.StreamUtils; 14 import org.columba.mail.folder.IDataStorage; 15 import org.columba.ristretto.io.Source; 16 import org.columba.ristretto.io.SourceInputStream; 17 import org.columba.ristretto.io.TempSourceFactory; 18 19 public class ZippedMHDataStorage implements IDataStorage { 20 21 private File directory; 22 23 private Source actSource; 24 private Object actUid; 25 26 27 public ZippedMHDataStorage(File directory) { 28 this.directory = directory; 29 } 30 31 public void removeMessage(Object uid) throws Exception { 32 File messageFile = getMessageFile(uid); 33 34 if( !messageFile.delete() ) { 35 messageFile.deleteOnExit(); 36 } 37 38 if( actUid != null && actUid.equals(uid)) { 39 actUid = null; 40 } 41 } 42 43 public Source getMessageSource(Object uid) throws Exception { 44 return inflateToTempSource(uid); 45 } 46 47 public InputStream getMessageStream(Object uid) throws Exception { 48 return new SourceInputStream(inflateToTempSource(uid)); 49 } 50 51 private Source inflateToTempSource(Object uid) throws Exception { 52 if( actUid == null || !actUid.equals(uid)) { 53 ZipFile messageFile = new ZipFile (getMessageFile(uid)); 54 ZipEntry entry = messageFile.getEntry(uid.toString()); 55 56 actUid = uid; 57 actSource =TempSourceFactory.createTempSource(messageFile.getInputStream(entry), (int)entry.getSize()); 58 } 59 60 return actSource; 61 } 62 63 public void saveMessage(Object uid, InputStream source) throws Exception { 64 File messageFile = getMessageFile(uid); 65 ZipOutputStream out = new ZipOutputStream (new FileOutputStream (messageFile)); 66 out.setMethod(ZipOutputStream.DEFLATED); 67 68 ZipEntry zipentry = new ZipEntry (uid.toString()); 69 zipentry.setSize(source.available()); 70 out.putNextEntry(zipentry); 71 72 StreamUtils.streamCopy(source, out); 73 74 source.close(); 75 out.close(); 76 } 77 78 private File getMessageFile(Object uid) { 79 File messageFile = new File (directory, uid + ".zip"); 80 return messageFile; 81 } 82 83 public int getMessageCount() { 84 return directory.listFiles(new FileFilter () { 85 86 public boolean accept(File pathname) { 87 return pathname.getName().matches("\\d*\\.zip"); 88 } 89 90 }).length; 91 } 92 93 public boolean exists(Object uid) throws Exception { 94 return getMessageFile(uid).exists(); 95 } 96 97 public Object [] getMessageUids() { 98 File [] messageFiles = directory.listFiles(new FileFilter () { 99 100 public boolean accept(File pathname) { 101 return pathname.getName().matches("\\d*\\.zip"); 102 } 103 104 }); 105 106 Matcher matcher = Pattern.compile("(\\d+)").matcher(""); 107 108 Object [] uids = new Object [messageFiles.length]; 109 for( int i=0; i<uids.length; i++) { 110 matcher.reset(messageFiles[i].getName()); 111 matcher.find(); 112 uids[i] = new Integer (matcher.group(1)); 113 } 114 115 return uids; 116 } 117 118 } 119 | Popular Tags |