KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > iofilter > AbstractZipFilter


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.iofilter;
28
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.util.zip.ZipEntry JavaDoc;
33 import java.util.zip.ZipInputStream JavaDoc;
34 import java.util.zip.ZipOutputStream JavaDoc;
35
36 import org.nightlabs.io.AbstractIOFilter;
37 import org.nightlabs.io.ReadException;
38 import org.nightlabs.io.WriteException;
39
40 public abstract class AbstractZipFilter
41 extends AbstractIOFilter
42 {
43
44     public AbstractZipFilter() {
45         super();
46     }
47
48     /**
49      * determines if the Stream should be zipped or not
50      */

51     protected boolean useZip = true;
52     public boolean isUseZip() {
53         return useZip;
54     }
55     public void setUseZip(boolean useZip) {
56         this.useZip = useZip;
57     }
58     
59     /**
60      * the Compression Level of the ZipOutputStream
61      * valid values are 0-9 (0=no compression, 9=strongest compression)
62      */

63     protected int compressLevel = 9;
64     public int getCompressLevel() {
65         return compressLevel;
66     }
67     public void setCompressLevel(int compressLevel)
68     {
69         if (compressLevel > 9)
70             compressLevel = 9;
71         else if (compressLevel < 0)
72             compressLevel = 0;
73         
74         this.compressLevel = compressLevel;
75     }
76
77     /**
78      * @see org.nightlabs.io.IOFilter
79      */

80     public Object JavaDoc read(InputStream JavaDoc in)
81     throws ReadException
82     {
83         if (isUseZip())
84             return readZip(in);
85         else
86             return readStream(in);
87     }
88
89     /**
90      * This Methods wraps the readStream(InputStream in) - Method into a ZipInpuStream
91      *
92      * @param in The InpuStream to read from
93      * @return the Object returned from readStream(InputStream in)
94      */

95     protected Object JavaDoc readZip(InputStream JavaDoc in)
96     {
97         try {
98             ZipInputStream JavaDoc zipStream = new ZipInputStream JavaDoc(in);
99             ZipEntry JavaDoc entry = zipStream.getNextEntry();
100             Object JavaDoc o = readStream(zipStream);
101             zipStream.close();
102             closeReadStream();
103             return o;
104         }
105         catch (IOException JavaDoc e) {
106             throw new RuntimeException JavaDoc(e);
107         }
108     }
109     
110     /**
111      * Implement in this Method what would normally be implemented in IOFilter.read(InputStream in)
112      *
113      * @param in The InpuStream to read from
114      * @return The Object to read
115      */

116     protected abstract Object JavaDoc readStream(InputStream JavaDoc in);
117     
118     /**
119      * @see org.nightlabs.io.IOFilter
120      */

121     public void write(Object JavaDoc o, OutputStream JavaDoc out)
122     throws WriteException
123     {
124         if (isUseZip())
125             writeZip(o, out, getEntryName());
126         else
127             writeStream(o, out);
128     }
129
130     /**
131      * This Method wraps the content of the writeStream(Object o, OutputStream out)-Method into
132      * a ZipOutputStream with only one entry
133      *
134      * @param o The Object to write
135      * @param out The OutputStream to which will be written
136      */

137     protected void writeZip(Object JavaDoc o, OutputStream JavaDoc out, String JavaDoc entryName)
138     {
139         try {
140             ZipOutputStream JavaDoc zipStream = new ZipOutputStream JavaDoc(out);
141             zipStream.setLevel(compressLevel);
142             ZipEntry JavaDoc entry = new ZipEntry JavaDoc(entryName);
143             zipStream.putNextEntry(entry);
144             
145             writeStream(o, zipStream);
146             
147             zipStream.closeEntry();
148             zipStream.finish();
149             zipStream.close();
150             
151             closeWriteStream();
152         } catch (IOException JavaDoc e) {
153             throw new RuntimeException JavaDoc(e);
154         }
155     }
156     
157     protected abstract void closeReadStream();
158     protected abstract void closeWriteStream();
159     
160     /**
161      * Implement in this Method what would normally be implemented in IOFilter.write(Object o, OutputStream out)
162      *
163      * @param o the Object to write
164      * @param out the OutputStream to write to
165      */

166     protected abstract void writeStream(Object JavaDoc o, OutputStream JavaDoc out);
167     
168     /**
169      * determines the name of the Entry in the ZipStream
170      */

171     public abstract String JavaDoc getEntryName();
172 }
173
Popular Tags