1 28 package net.sf.jasperreports.engine.fill; 29 30 import java.io.ByteArrayInputStream ; 31 import java.io.ByteArrayOutputStream ; 32 import java.io.IOException ; 33 import java.util.Collections ; 34 import java.util.HashMap ; 35 import java.util.Map ; 36 37 import net.sf.jasperreports.engine.JRVirtualizable; 38 import net.sf.jasperreports.engine.fill.JRAbstractLRUVirtualizer; 39 import net.sf.jasperreports.engine.util.JRSwapFile; 40 41 42 48 public class JRSwapFileVirtualizer extends JRAbstractLRUVirtualizer 49 { 50 private final JRSwapFile swap; 51 private final boolean swapOwner; 52 private final Map handles; 53 54 55 63 public JRSwapFileVirtualizer(int maxSize, JRSwapFile swap) 64 { 65 this(maxSize, swap, true); 66 } 67 68 69 78 public JRSwapFileVirtualizer(int maxSize, JRSwapFile swap, boolean swapOwner) 79 { 80 super(maxSize); 81 82 this.swap = swap; 83 this.swapOwner = swapOwner; 84 handles = Collections.synchronizedMap(new HashMap ()); 85 } 86 87 protected void pageOut(JRVirtualizable o) throws IOException 88 { 89 if (!handles.containsKey(o.getUID())) 90 { 91 ByteArrayOutputStream bout = new ByteArrayOutputStream (3000); 92 writeData(o, bout); 93 byte[] data = bout.toByteArray(); 94 95 JRSwapFile.SwapHandle handle = swap.write(data); 96 handles.put(o.getUID(), handle); 97 } 98 else 99 { 100 if (!isReadOnly(o)) 101 { 102 throw new IllegalStateException ("Cannot virtualize data because the data for object UID \"" + o.getUID() + "\" already exists."); 103 } 104 } 105 } 106 107 protected void pageIn(JRVirtualizable o) throws IOException 108 { 109 JRSwapFile.SwapHandle handle = (JRSwapFile.SwapHandle) handles.get(o.getUID()); 110 byte[] data = swap.read(handle, !isReadOnly(o)); 111 112 readData(o, new ByteArrayInputStream (data)); 113 114 if (!isReadOnly(o)) 115 { 116 handles.remove(o.getUID()); 117 } 118 } 119 120 protected void dispose(String id) 121 { 122 JRSwapFile.SwapHandle handle = (JRSwapFile.SwapHandle) handles.remove(id); 123 if (handle != null) 124 { 125 swap.free(handle); 126 } 127 } 128 129 130 134 public void cleanup() 135 { 136 handles.clear(); 137 if (swapOwner) 138 { 139 swap.dispose(); 140 } 141 } 142 } 143 | Popular Tags |