1 30 31 34 package net.sf.jasperreports.engine.fill; 35 36 import java.io.ByteArrayInputStream ; 37 import java.io.ByteArrayOutputStream ; 38 import java.io.IOException ; 39 import java.util.Collections ; 40 import java.util.HashMap ; 41 import java.util.Map ; 42 import java.util.zip.GZIPInputStream ; 43 import java.util.zip.GZIPOutputStream ; 44 45 import net.sf.jasperreports.engine.JRVirtualizable; 46 47 53 public class JRGzipVirtualizer extends JRAbstractLRUVirtualizer 54 { 55 private final Map zippedData; 56 57 62 public JRGzipVirtualizer(int maxSize) { 63 super(maxSize); 64 this.zippedData = Collections.synchronizedMap(new HashMap ()); 65 } 66 67 protected void dispose(String virtualId) { 68 zippedData.remove(virtualId); 69 } 70 71 protected void pageOut(JRVirtualizable o) throws IOException { 72 if (!zippedData.containsKey(o.getUID())) { 73 GZIPOutputStream gos = null; 74 try { 75 ByteArrayOutputStream baos = new ByteArrayOutputStream (3000); 76 gos = new GZIPOutputStream (baos); 77 writeData(o, gos); 78 gos.finish(); 79 gos.flush(); 80 81 byte[] data = baos.toByteArray(); 82 zippedData.put(o.getUID(), data); 83 } 84 finally { 85 if (gos != null) { 86 gos.close(); 87 } 88 } 89 } 90 else { 91 if (!isReadOnly(o)) { 92 throw new IllegalStateException ( 93 "Cannot virtualize data because the data for object UID \"" + o.getUID() 94 + "\" already exists."); 95 } 96 } 97 } 98 99 protected void pageIn(JRVirtualizable o) throws IOException { 100 GZIPInputStream gis = null; 101 try { 102 byte[] data = (byte[]) zippedData.get(o.getUID()); 103 if (data == null) { 104 throw new NullPointerException ("No data found for object with UID " + o.getUID()); 105 } 106 ByteArrayInputStream bais = new ByteArrayInputStream (data); 107 gis = new GZIPInputStream (bais); 108 readData(o, gis); 109 } 110 finally { 111 if (gis != null) { 112 gis.close(); 113 } 114 } 115 116 if (!isReadOnly(o)) { 117 zippedData.remove(o.getUID()); 119 } 120 } 121 122 public void cleanup() 123 { 124 zippedData.clear(); 125 reset(); 126 } 127 } 128 | Popular Tags |