KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * <p> Project: NightLabsEditor2D </p>
3  * <p> Copyright: Copyright (c) 2005 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 26.08.2005 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.editor2d.iofilter;
9
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.OutputStream JavaDoc;
13 import java.util.zip.ZipEntry JavaDoc;
14 import java.util.zip.ZipInputStream JavaDoc;
15 import java.util.zip.ZipOutputStream JavaDoc;
16
17 import com.nightlabs.io.IOFilter;
18 import com.nightlabs.io.ReadException;
19 import com.nightlabs.io.WriteException;
20
21 public abstract class AbstractZipFilter
22 implements IOFilter
23 {
24
25     public AbstractZipFilter() {
26         super();
27     }
28
29     /**
30      * determines if the Stream should be zipped or not
31      */

32     protected boolean useZip = true;
33     public boolean isUseZip() {
34         return useZip;
35     }
36     public void setUseZip(boolean useZip) {
37         this.useZip = useZip;
38     }
39     
40     /**
41      * the Compression Level of the ZipOutputStream
42      * valid values are 0-9 (0=no compression, 9=strongest compression)
43      */

44     protected int compressLevel = 9;
45     public int getCompressLevel() {
46         return compressLevel;
47     }
48     public void setCompressLevel(int compressLevel)
49     {
50         if (compressLevel > 9)
51             compressLevel = 9;
52         else if (compressLevel < 0)
53             compressLevel = 0;
54         
55         this.compressLevel = compressLevel;
56     }
57
58     /**
59      * @see com.nightlabs.io.IOFilter
60      */

61     public Object JavaDoc read(InputStream JavaDoc in)
62     throws ReadException
63     {
64         if (isUseZip())
65             return readZip(in);
66         else
67             return readStream(in);
68     }
69
70     /**
71      * This Methods wraps the readStream(InputStream in) - Method into a ZipInpuStream
72      *
73      * @param in The InpuStream to read from
74      * @return the Object returned from readStream(InputStream in)
75      */

76     protected Object JavaDoc readZip(InputStream JavaDoc in)
77     {
78         try {
79             ZipInputStream JavaDoc zipStream = new ZipInputStream JavaDoc(in);
80             ZipEntry JavaDoc entry = zipStream.getNextEntry();
81             Object JavaDoc o = readStream(zipStream);
82             zipStream.close();
83             closeReadStream();
84             return o;
85         }
86         catch (IOException JavaDoc e) {
87             throw new RuntimeException JavaDoc(e);
88         }
89     }
90     
91     /**
92      * Implement in this Method what would normally be implemented in IOFilter.read(InputStream in)
93      *
94      * @param in The InpuStream to read from
95      * @return The Object to read
96      */

97     protected abstract Object JavaDoc readStream(InputStream JavaDoc in);
98     
99     /**
100      * @see com.nightlabs.io.IOFilter
101      */

102     public void write(Object JavaDoc o, OutputStream JavaDoc out)
103     throws WriteException
104     {
105         if (isUseZip())
106             writeZip(o, out, getEntryName());
107         else
108             writeStream(o, out);
109     }
110
111     /**
112      * This Method wraps the content of the writeStream(Object o, OutputStream out)-Method into
113      * a ZipOutputStream with only one entry
114      *
115      * @param o The Object to write
116      * @param out The OutputStream to which will be written
117      */

118     protected void writeZip(Object JavaDoc o, OutputStream JavaDoc out, String JavaDoc entryName)
119     {
120         try {
121             ZipOutputStream JavaDoc zipStream = new ZipOutputStream JavaDoc(out);
122             zipStream.setLevel(compressLevel);
123             ZipEntry JavaDoc entry = new ZipEntry JavaDoc(entryName);
124             zipStream.putNextEntry(entry);
125             
126             writeStream(o, zipStream);
127             
128             zipStream.closeEntry();
129             zipStream.finish();
130             zipStream.close();
131             
132             closeWriteStream();
133         } catch (IOException JavaDoc e) {
134             throw new RuntimeException JavaDoc(e);
135         }
136     }
137     
138     protected abstract void closeReadStream();
139     protected abstract void closeWriteStream();
140     
141     /**
142      * Implement in this Method what would normally be implemented in IOFilter.write(Object o, OutputStream out)
143      *
144      * @param o the Object to write
145      * @param out the OutputStream to write to
146      */

147     protected abstract void writeStream(Object JavaDoc o, OutputStream JavaDoc out);
148     
149     /**
150      * determines the name of the Entry in the ZipStream
151      */

152     public abstract String JavaDoc getEntryName();
153     
154     /**
155      * @see com.nightlabs.io.IOFilter
156      */

157     public abstract String JavaDoc getFileExtension();
158     
159     /**
160      * @see com.nightlabs.io.IOFilter
161      */

162     public abstract String JavaDoc getDescription();
163 }
164
Popular Tags