1 30 31 34 package net.sf.jasperreports.engine.fill; 35 36 import java.io.BufferedInputStream ; 37 import java.io.BufferedOutputStream ; 38 import java.io.File ; 39 import java.io.FileInputStream ; 40 import java.io.FileNotFoundException ; 41 import java.io.FileOutputStream ; 42 import java.io.IOException ; 43 44 import net.sf.jasperreports.engine.JRRuntimeException; 45 import net.sf.jasperreports.engine.JRVirtualizable; 46 import net.sf.jasperreports.engine.util.JRProperties; 47 48 import org.apache.commons.logging.Log; 49 import org.apache.commons.logging.LogFactory; 50 51 60 public class JRFileVirtualizer extends JRAbstractLRUVirtualizer { 61 62 private static final Log log = LogFactory.getLog(JRFileVirtualizer.class); 63 64 65 76 public static final String PROPERTY_TEMP_FILES_SET_DELETE_ON_EXIT = JRProperties.PROPERTY_PREFIX + "virtualizer.files.delete.on.exit"; 77 78 private final String directory; 79 80 87 public JRFileVirtualizer(int maxSize) { 88 this(maxSize, null); 89 } 90 91 99 public JRFileVirtualizer(int maxSize, String directory) { 100 super(maxSize); 101 102 this.directory = directory; 103 } 104 105 private String makeFilename(JRVirtualizable o) { 106 String uid = o.getUID(); 107 return "virt" + uid; 108 } 109 110 private String makeFilename(String virtualId) { 111 return "virt" + virtualId; 112 } 113 114 protected void pageOut(JRVirtualizable o) throws IOException { 115 String filename = makeFilename(o); 117 File file = new File (directory, filename); 118 119 if (file.createNewFile()) { 120 if (JRProperties.getBooleanProperty(PROPERTY_TEMP_FILES_SET_DELETE_ON_EXIT)) { 121 file.deleteOnExit(); 122 } 123 124 FileOutputStream fos = null; 125 try { 126 fos = new FileOutputStream (file); 127 BufferedOutputStream bufferedOut = new BufferedOutputStream (fos); 128 writeData(o, bufferedOut); 129 } 130 catch (FileNotFoundException e) { 131 log.error("Error virtualizing object", e); 132 throw new JRRuntimeException(e); 133 } 134 finally { 135 if (fos != null) { 136 fos.close(); 137 } 138 } 139 } else { 140 if (!isReadOnly(o)) { 141 throw new IllegalStateException ( 142 "Cannot virtualize data because the file \"" + filename 143 + "\" already exists."); 144 } 145 } 146 } 147 148 protected void pageIn(JRVirtualizable o) throws IOException { 149 String filename = makeFilename(o); 151 File file = new File (directory, filename); 152 153 FileInputStream fis = null; 154 try { 155 fis = new FileInputStream (file); 156 BufferedInputStream bufferedIn = new BufferedInputStream (fis); 157 readData(o, bufferedIn); 158 } 159 catch (FileNotFoundException e) { 160 log.error("Error devirtualizing object", e); 161 throw new JRRuntimeException(e); 162 } 163 finally { 164 if (fis != null) { 165 fis.close(); 166 } 167 } 168 169 if (!isReadOnly(o)) { 170 file.delete(); 172 } 173 } 174 175 protected void dispose(String virtualId) { 176 String filename = makeFilename(virtualId); 177 File file = new File (directory, filename); 178 file.delete(); 179 } 180 181 182 186 public synchronized void cleanup() 187 { 188 disposeAll(); 189 reset(); 190 } 191 } 192 | Popular Tags |