1 19 package org.netbeans.modules.localhistory.store; 20 21 import java.io.BufferedInputStream ; 22 import java.io.BufferedOutputStream ; 23 import java.io.File ; 24 import java.io.FileNotFoundException ; 25 import java.io.FileOutputStream ; 26 import java.io.FileInputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.OutputStream ; 30 import java.sql.Date ; 31 import java.util.zip.ZipEntry ; 32 import java.util.zip.ZipInputStream ; 33 import java.util.zip.ZipOutputStream ; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileUtil; 36 37 45 public abstract class StoreEntry { 46 47 private final File file; 48 private final File storeFile; 49 private final long ts; 50 private final String label; 51 private final Date date; 52 private String mimeType = null; 53 54 public static StoreEntry createStoreEntry(File file, File storeFile, long ts, String label) { 55 return new DefaultStoreEntry(file, storeFile, ts, label); 56 } 57 58 public static StoreEntry createDeletedStoreEntry(File file, long ts) { 59 return new DeletedStoreEntry(file, ts); 60 } 61 62 public static StoreEntry createFakeStoreEntry(File file, long ts) { 63 return new FakeStoreEntry(file, ts); 64 } 65 66 private StoreEntry(File file, File storeFile, long ts, String label) { 67 this.file = file; 68 this.storeFile = storeFile; 69 this.ts = ts; 70 this.label = label; 71 this.date = new Date (ts); 72 } 73 74 public File getStoreFile() { 75 return storeFile; 76 } 77 78 public File getFile() { 79 return file; 80 } 81 82 public long getTimestamp() { 83 return ts; 84 } 85 86 public String getLabel() { 87 return label != null ? label : ""; 88 } 89 90 public Date getDate() { 91 return date; 92 } 93 94 public boolean representsFile() { 95 return storeFile.isFile(); 96 } 97 98 public String getMIMEType() { 99 if(mimeType == null) { 100 FileObject fo = FileUtil.toFileObject(getFile()); 101 if(fo != null) { 102 mimeType = fo.getMIMEType(); 103 } else { 104 mimeType = "content/unknown"; 105 } 106 } 107 return mimeType; 108 } 109 110 static OutputStream createStoreFileOutputSteam(File storeFile) throws FileNotFoundException , IOException { 112 ZipOutputStream zos = new ZipOutputStream (new BufferedOutputStream (new FileOutputStream (storeFile))); 113 ZipEntry entry = new ZipEntry (storeFile.getName()); 114 zos.putNextEntry(entry); 115 return zos; 116 } 117 118 abstract OutputStream getStoreFileOutputStream() throws FileNotFoundException , IOException ; 119 public abstract InputStream getStoreFileInputStream() throws FileNotFoundException , IOException ; 120 121 private static class DefaultStoreEntry extends StoreEntry { 122 123 private DefaultStoreEntry(File file, File storeFile, long ts, String label) { 124 super(file, storeFile, ts, label); 125 } 126 127 OutputStream getStoreFileOutputStream() throws FileNotFoundException , IOException { 128 return createStoreFileOutputSteam(getStoreFile()); 129 } 130 131 public InputStream getStoreFileInputStream() throws FileNotFoundException , IOException { 132 ZipInputStream zis = new ZipInputStream (new BufferedInputStream (new FileInputStream (getStoreFile()))); 133 ZipEntry entry; 134 while ( (entry = zis.getNextEntry()) != null ) { 135 if( entry.getName().equals(getStoreFile().getName()) ) { 136 return zis; 137 } 138 } 139 throw new FileNotFoundException (); 140 } 141 } 142 143 private static class DeletedStoreEntry extends StoreEntry { 144 public DeletedStoreEntry(File file, long ts) { 145 super(file, null, ts, ""); 146 } 147 148 OutputStream getStoreFileOutputStream() throws FileNotFoundException , IOException { 149 throwNoStoreEntry(); 150 return null; 151 } 152 153 public InputStream getStoreFileInputStream() throws FileNotFoundException , IOException { 154 throwNoStoreEntry(); 155 return null; 156 } 157 158 private void throwNoStoreEntry() throws FileNotFoundException { 159 throw new FileNotFoundException ("There is no store entry for file " + getFile() + " and timestamp " + getTimestamp()); 160 } 161 162 } 163 164 private static class FakeStoreEntry extends StoreEntry { 165 166 public FakeStoreEntry(File file, long ts) { 167 super(file, file, ts, ""); 168 } 169 170 OutputStream getStoreFileOutputStream() throws FileNotFoundException , IOException { 171 throw new FileNotFoundException ("There is no OutputStream for this for file " + getFile()); 172 } 173 174 public InputStream getStoreFileInputStream() throws FileNotFoundException , IOException { 175 return new FileInputStream (getFile()); 176 } 177 } 178 } 179 | Popular Tags |