KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > io > GZIPCompressingDevice


1 /*
2  * $Id: GZIPCompressingDevice.java,v 1.4 2005/03/14 13:33:24 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.io;
15
16 import java.io.IOException JavaDoc;
17 import java.io.OutputStream JavaDoc;
18 import java.util.zip.GZIPOutputStream JavaDoc;
19
20 /**
21  * A Device encapsulating an OutputStream, compressing its
22  * output. You can use this, if the browser states, that it can handle
23  * compressed data; don't forget to set the appropriate header, then
24  * (Content-Encoding). Use this in a servlet derived from SessionServlet to
25  * override the factory
26  * {@link org.wings.session.SessionServlet#createOutputDevice(HttpServletRequest,HttpServletResponse,ExternalizedResource)}, for instance.
27  * <p><b>Example</b><hr><pre>
28  * protected Device createOutputDevice(HttpServletRequest req,
29  * HttpServletResponse response,
30  * ExternalizedResource extInfo)
31  * throws IOException {
32  * String mimeType = extInfo.getMimeType();
33  * // some browsers can handle a gziped stream only for text-files.
34  * if (mimeType != null && mimeType.startsWith("text/")) {
35  * String acceptEncoding = req.getHeader("Accept-Encoding");
36  * int gzipPos;
37  * if (acceptEncoding != null
38  * && (gzipPos = acceptEncoding.indexOf("gzip")) >= 0) {
39  * // some browsers send 'x-gzip', others just 'gzip'. Our
40  * // response should be the same.
41  * boolean isXGZip = (gzipPos >= 2
42  * && acceptEncoding.charAt(gzipPos-1) == '-'
43  * && acceptEncoding.charAt(gzipPos-2) == 'x');
44  * response.addHeader("Content-Encoding",
45  * (isXGZip ? "x-gzip" : "gzip"));
46  * return new GZIPCompressingDevice(response.getOutputStream());
47  * }
48  * }
49  * return new ServletDevice(response.getOutputStream());
50  * }
51  * </pre><hr>
52  *
53  * @author <a HREF="mailto:H.Zeller@acm.org">Henner Zeller</a>
54  * @version $Revision: 1.4 $
55  */

56 public final class GZIPCompressingDevice implements Device {
57     private final OutputStreamDevice deligee;
58
59     public GZIPCompressingDevice(OutputStream JavaDoc out) throws IOException JavaDoc {
60         deligee = new OutputStreamDevice(new GZIPOutputStream JavaDoc(out));
61     }
62
63     public GZIPCompressingDevice(Device d) throws IOException JavaDoc {
64         this(new DeviceOutputStream(d));
65     }
66
67     /**
68      * this returns false, since the compressing device is <em>not</em> size
69      * preserving. Thats what is all about, right ?
70      */

71     public boolean isSizePreserving() { return false; }
72
73     // rest is just delegating..
74
public void flush() throws IOException JavaDoc {
75         deligee.flush();
76     }
77
78     public void close() throws IOException JavaDoc {
79         deligee.close();
80     }
81
82     public Device print(char c) throws IOException JavaDoc {
83         deligee.print(c);
84         return this;
85     }
86
87     public Device print(char[] c) throws IOException JavaDoc {
88         deligee.print(c);
89         return this;
90     }
91
92     public Device print(char[] c, int start, int len) throws IOException JavaDoc {
93         deligee.print(c, start, len);
94         return this;
95     }
96
97     public Device print(String JavaDoc s) throws IOException JavaDoc {
98         deligee.print(s);
99         return this;
100     }
101
102     public Device print(int i) throws IOException JavaDoc {
103         deligee.print(i);
104         return this;
105     }
106
107     public Device print(Object JavaDoc o) throws IOException JavaDoc {
108         deligee.print(o);
109         return this;
110     }
111
112     public Device write(int c) throws IOException JavaDoc {
113         deligee.write(c);
114         return this;
115     }
116
117     public Device write(byte b[]) throws IOException JavaDoc {
118         deligee.write(b);
119         return this;
120     }
121
122     public Device write(byte b[], int off, int len) throws IOException JavaDoc {
123         deligee.write(b, off, len);
124         return this;
125     }
126 }
127
128
129
Popular Tags