KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > fill > JRGzipVirtualizer


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2005 Works, Inc. http://www.works.com/
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * Works, Inc.
24  * 6034 West Courtyard Drive
25  * Suite 210
26  * Austin, TX 78730-5032
27  * USA
28  * http://www.works.com/
29  */

30
31 /*
32  * Licensed to JasperSoft Corporation under a Contributer Agreement
33  */

34 package net.sf.jasperreports.engine.fill;
35
36 import java.io.ByteArrayInputStream JavaDoc;
37 import java.io.ByteArrayOutputStream JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.util.Collections JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.Map JavaDoc;
42 import java.util.zip.GZIPInputStream JavaDoc;
43 import java.util.zip.GZIPOutputStream JavaDoc;
44
45 import net.sf.jasperreports.engine.JRVirtualizable;
46
47 /**
48  * GZips the pages that it doesn't need, but keeps them in memory.
49  *
50  * @author John Bindel
51  * @version $Id: JRGzipVirtualizer.java 1413 2006-09-28 13:47:40 +0300 (Thu, 28 Sep 2006) teodord $
52  */

53 public class JRGzipVirtualizer extends JRAbstractLRUVirtualizer
54 {
55     private final Map JavaDoc zippedData;
56
57     /**
58      * @param maxSize
59      * the maximum size (in JRVirtualizable objects) of the paged in
60      * cache.
61      */

62     public JRGzipVirtualizer(int maxSize) {
63         super(maxSize);
64         this.zippedData = Collections.synchronizedMap(new HashMap JavaDoc());
65     }
66
67     protected void dispose(String JavaDoc virtualId) {
68         zippedData.remove(virtualId);
69     }
70
71     protected void pageOut(JRVirtualizable o) throws IOException JavaDoc {
72         if (!zippedData.containsKey(o.getUID())) {
73             GZIPOutputStream JavaDoc gos = null;
74             try {
75                 ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(3000);
76                 gos = new GZIPOutputStream JavaDoc(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 JavaDoc(
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 JavaDoc {
100         GZIPInputStream JavaDoc gis = null;
101         try {
102             byte[] data = (byte[]) zippedData.get(o.getUID());
103             if (data == null) {
104                 throw new NullPointerException JavaDoc("No data found for object with UID " + o.getUID());
105             }
106             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(data);
107             gis = new GZIPInputStream JavaDoc(bais);
108             readData(o, gis);
109         }
110         finally {
111             if (gis != null) {
112                 gis.close();
113             }
114         }
115
116         if (!isReadOnly(o)) {
117             // Wait until we know it worked before tossing the data.
118
zippedData.remove(o.getUID());
119         }
120     }
121
122     public void cleanup()
123     {
124         zippedData.clear();
125         reset();
126     }
127 }
128
Popular Tags