org.wings.io.GZIPCompressingDevice Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2000,2005 wingS development team.
*
* This file is part of wingS (http://wingsframework.org).
*
* wingS is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* Please see COPYING for the complete licence.
*/
package org.wings.io;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;
/**
* A Device encapsulating an OutputStream, compressing its
* output. You can use this, if the browser states, that it can handle
* compressed data; don't forget to set the appropriate header, then
* (Content-Encoding).
*
* Override {@link DeviceFactory} and declare the updated factory in web.xml to use other devices.
* Example Draft
* String mimeType = extInfo.getMimeType();
* // some browsers can handle a gziped stream only for text-files.
* if (mimeType != null && mimeType.startsWith("text/")) {
* String acceptEncoding = req.getHeader("Accept-Encoding");
* int gzipPos;
* if (acceptEncoding != null
* && (gzipPos = acceptEncoding.indexOf("gzip")) >= 0) {
* // some browsers send 'x-gzip', others just 'gzip'. Our
* // response should be the same.
* boolean isXGZip = (gzipPos >= 2
* && acceptEncoding.charAt(gzipPos-1) == '-'
* && acceptEncoding.charAt(gzipPos-2) == 'x');
* response.addHeader("Content-Encoding",
* (isXGZip ? "x-gzip" : "gzip"));
* return new GZIPCompressingDevice(response.getOutputStream());
* }
* }
* return new ServletDevice(response.getOutputStream());
* }
*
*
* @author Henner Zeller
*/
public final class GZIPCompressingDevice implements Device {
private final OutputStreamDevice deligee;
public GZIPCompressingDevice(OutputStream out) throws IOException {
deligee = new OutputStreamDevice(new GZIPOutputStream(out));
}
public GZIPCompressingDevice(Device d) throws IOException {
this(new DeviceOutputStream(d));
}
/**
* this returns false, since the compressing device is not size
* preserving. Thats what is all about, right ?
*/
@Override
public boolean isSizePreserving() { return false; }
// rest is just delegating..
@Override
public void flush() throws IOException {
deligee.flush();
}
@Override
public void close() throws IOException {
deligee.close();
}
@Override
public Device print(char c) throws IOException {
deligee.print(c);
return this;
}
@Override
public Device print(char... c) throws IOException {
deligee.print(c);
return this;
}
@Override
public Device print(char[] c, int start, int len) throws IOException {
deligee.print(c, start, len);
return this;
}
@Override
public Device print(String s) throws IOException {
deligee.print(s);
return this;
}
@Override
public Device print(int i) throws IOException {
deligee.print(i);
return this;
}
@Override
public Device print(Object o) throws IOException {
deligee.print(o);
return this;
}
@Override
public Device write(int c) throws IOException {
deligee.write(c);
return this;
}
@Override
public Device write(byte... b) throws IOException {
deligee.write(b);
return this;
}
@Override
public Device write(byte b[], int off, int len) throws IOException {
deligee.write(b, off, len);
return this;
}
}