001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.io;
014:
015: import java.io.IOException;
016: import java.io.OutputStream;
017: import java.util.zip.GZIPOutputStream;
018:
019: /**
020: * A Device encapsulating an OutputStream, compressing its
021: * output. You can use this, if the browser states, that it can handle
022: * compressed data; don't forget to set the appropriate header, then
023: * (Content-Encoding).
024: *
025: * Override {@link DeviceFactory} and declare the updated factory in web.xml to use other devices.
026: * <p><b>Example Draft</b><hr>
027: * <pre>String mimeType = extInfo.getMimeType();
028: * // some browsers can handle a gziped stream only for text-files.
029: * if (mimeType != null && mimeType.startsWith("text/")) {
030: * String acceptEncoding = req.getHeader("Accept-Encoding");
031: * int gzipPos;
032: * if (acceptEncoding != null
033: * && (gzipPos = acceptEncoding.indexOf("gzip")) >= 0) {
034: * // some browsers send 'x-gzip', others just 'gzip'. Our
035: * // response should be the same.
036: * boolean isXGZip = (gzipPos >= 2
037: * && acceptEncoding.charAt(gzipPos-1) == '-'
038: * && acceptEncoding.charAt(gzipPos-2) == 'x');
039: * response.addHeader("Content-Encoding",
040: * (isXGZip ? "x-gzip" : "gzip"));
041: * return new GZIPCompressingDevice(response.getOutputStream());
042: * }
043: * }
044: * return new ServletDevice(response.getOutputStream());
045: * }
046: * </pre><hr>
047: *
048: * @author <a href="mailto:H.Zeller@acm.org">Henner Zeller</a>
049: */
050: public final class GZIPCompressingDevice implements Device {
051: private final OutputStreamDevice deligee;
052:
053: public GZIPCompressingDevice(OutputStream out) throws IOException {
054: deligee = new OutputStreamDevice(new GZIPOutputStream(out));
055: }
056:
057: public GZIPCompressingDevice(Device d) throws IOException {
058: this (new DeviceOutputStream(d));
059: }
060:
061: /**
062: * this returns false, since the compressing device is <em>not</em> size
063: * preserving. Thats what is all about, right ?
064: */
065: public boolean isSizePreserving() {
066: return false;
067: }
068:
069: // rest is just delegating..
070: public void flush() throws IOException {
071: deligee.flush();
072: }
073:
074: public void close() throws IOException {
075: deligee.close();
076: }
077:
078: public Device print(char c) throws IOException {
079: deligee.print(c);
080: return this ;
081: }
082:
083: public Device print(char[] c) throws IOException {
084: deligee.print(c);
085: return this ;
086: }
087:
088: public Device print(char[] c, int start, int len)
089: throws IOException {
090: deligee.print(c, start, len);
091: return this ;
092: }
093:
094: public Device print(String s) throws IOException {
095: deligee.print(s);
096: return this ;
097: }
098:
099: public Device print(int i) throws IOException {
100: deligee.print(i);
101: return this ;
102: }
103:
104: public Device print(Object o) throws IOException {
105: deligee.print(o);
106: return this ;
107: }
108:
109: public Device write(int c) throws IOException {
110: deligee.write(c);
111: return this ;
112: }
113:
114: public Device write(byte b[]) throws IOException {
115: deligee.write(b);
116: return this ;
117: }
118:
119: public Device write(byte b[], int off, int len) throws IOException {
120: deligee.write(b, off, len);
121: return this;
122: }
123: }
|