001: package org.columba.mail.folder.zippedmh;
002:
003: import java.io.File;
004: import java.io.FileFilter;
005: import java.io.FileOutputStream;
006: import java.io.InputStream;
007: import java.util.regex.Matcher;
008: import java.util.regex.Pattern;
009: import java.util.zip.ZipEntry;
010: import java.util.zip.ZipFile;
011: import java.util.zip.ZipOutputStream;
012:
013: import org.columba.core.io.StreamUtils;
014: import org.columba.mail.folder.IDataStorage;
015: import org.columba.ristretto.io.Source;
016: import org.columba.ristretto.io.SourceInputStream;
017: import org.columba.ristretto.io.TempSourceFactory;
018:
019: public class ZippedMHDataStorage implements IDataStorage {
020:
021: private File directory;
022:
023: private Source actSource;
024: private Object actUid;
025:
026: public ZippedMHDataStorage(File directory) {
027: this .directory = directory;
028: }
029:
030: public void removeMessage(Object uid) throws Exception {
031: File messageFile = getMessageFile(uid);
032:
033: if (!messageFile.delete()) {
034: messageFile.deleteOnExit();
035: }
036:
037: if (actUid != null && actUid.equals(uid)) {
038: actUid = null;
039: }
040: }
041:
042: public Source getMessageSource(Object uid) throws Exception {
043: return inflateToTempSource(uid);
044: }
045:
046: public InputStream getMessageStream(Object uid) throws Exception {
047: return new SourceInputStream(inflateToTempSource(uid));
048: }
049:
050: private Source inflateToTempSource(Object uid) throws Exception {
051: if (actUid == null || !actUid.equals(uid)) {
052: ZipFile messageFile = new ZipFile(getMessageFile(uid));
053: ZipEntry entry = messageFile.getEntry(uid.toString());
054:
055: actUid = uid;
056: actSource = TempSourceFactory.createTempSource(messageFile
057: .getInputStream(entry), (int) entry.getSize());
058: }
059:
060: return actSource;
061: }
062:
063: public void saveMessage(Object uid, InputStream source)
064: throws Exception {
065: File messageFile = getMessageFile(uid);
066: ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
067: messageFile));
068: out.setMethod(ZipOutputStream.DEFLATED);
069:
070: ZipEntry zipentry = new ZipEntry(uid.toString());
071: zipentry.setSize(source.available());
072: out.putNextEntry(zipentry);
073:
074: StreamUtils.streamCopy(source, out);
075:
076: source.close();
077: out.close();
078: }
079:
080: private File getMessageFile(Object uid) {
081: File messageFile = new File(directory, uid + ".zip");
082: return messageFile;
083: }
084:
085: public int getMessageCount() {
086: return directory.listFiles(new FileFilter() {
087:
088: public boolean accept(File pathname) {
089: return pathname.getName().matches("\\d*\\.zip");
090: }
091:
092: }).length;
093: }
094:
095: public boolean exists(Object uid) throws Exception {
096: return getMessageFile(uid).exists();
097: }
098:
099: public Object[] getMessageUids() {
100: File[] messageFiles = directory.listFiles(new FileFilter() {
101:
102: public boolean accept(File pathname) {
103: return pathname.getName().matches("\\d*\\.zip");
104: }
105:
106: });
107:
108: Matcher matcher = Pattern.compile("(\\d+)").matcher("");
109:
110: Object[] uids = new Object[messageFiles.length];
111: for (int i = 0; i < uids.length; i++) {
112: matcher.reset(messageFiles[i].getName());
113: matcher.find();
114: uids[i] = new Integer(matcher.group(1));
115: }
116:
117: return uids;
118: }
119:
120: }
|