001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2005 Works, Inc. http://www.works.com/
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * Works, Inc.
024: * 6034 West Courtyard Drive
025: * Suite 210
026: * Austin, TX 78730-5032
027: * USA
028: * http://www.works.com/
029: */
030:
031: /*
032: * Licensed to JasperSoft Corporation under a Contributer Agreement
033: */
034: package net.sf.jasperreports.engine.fill;
035:
036: import java.io.ByteArrayInputStream;
037: import java.io.ByteArrayOutputStream;
038: import java.io.IOException;
039: import java.util.Collections;
040: import java.util.HashMap;
041: import java.util.Map;
042: import java.util.zip.GZIPInputStream;
043: import java.util.zip.GZIPOutputStream;
044:
045: import net.sf.jasperreports.engine.JRVirtualizable;
046:
047: /**
048: * GZips the pages that it doesn't need, but keeps them in memory.
049: *
050: * @author John Bindel
051: * @version $Id: JRGzipVirtualizer.java 1413 2006-09-28 10:47:40Z teodord $
052: */
053: public class JRGzipVirtualizer extends JRAbstractLRUVirtualizer {
054: private final Map zippedData;
055:
056: /**
057: * @param maxSize
058: * the maximum size (in JRVirtualizable objects) of the paged in
059: * cache.
060: */
061: public JRGzipVirtualizer(int maxSize) {
062: super (maxSize);
063: this .zippedData = Collections.synchronizedMap(new HashMap());
064: }
065:
066: protected void dispose(String virtualId) {
067: zippedData.remove(virtualId);
068: }
069:
070: protected void pageOut(JRVirtualizable o) throws IOException {
071: if (!zippedData.containsKey(o.getUID())) {
072: GZIPOutputStream gos = null;
073: try {
074: ByteArrayOutputStream baos = new ByteArrayOutputStream(
075: 3000);
076: gos = new GZIPOutputStream(baos);
077: writeData(o, gos);
078: gos.finish();
079: gos.flush();
080:
081: byte[] data = baos.toByteArray();
082: zippedData.put(o.getUID(), data);
083: } finally {
084: if (gos != null) {
085: gos.close();
086: }
087: }
088: } else {
089: if (!isReadOnly(o)) {
090: throw new IllegalStateException(
091: "Cannot virtualize data because the data for object UID \""
092: + o.getUID() + "\" already exists.");
093: }
094: }
095: }
096:
097: protected void pageIn(JRVirtualizable o) throws IOException {
098: GZIPInputStream gis = null;
099: try {
100: byte[] data = (byte[]) zippedData.get(o.getUID());
101: if (data == null) {
102: throw new NullPointerException(
103: "No data found for object with UID "
104: + o.getUID());
105: }
106: ByteArrayInputStream bais = new ByteArrayInputStream(data);
107: gis = new GZIPInputStream(bais);
108: readData(o, gis);
109: } finally {
110: if (gis != null) {
111: gis.close();
112: }
113: }
114:
115: if (!isReadOnly(o)) {
116: // Wait until we know it worked before tossing the data.
117: zippedData.remove(o.getUID());
118: }
119: }
120:
121: public void cleanup() {
122: zippedData.clear();
123: reset();
124: }
125: }
|