Hi,
I have been writing a filter that will compress the response using gzip and then send it back to the client. This has been working for my jsp's and anything else using getWriter(), but the outputstream part isn't working.
Basically I have a filter that checks if the client accepts gzip, and then sets the response header to tell the client that the encoding will be gzip.
Then I pass a HttpServletResponseWrapper to the chain.
I have tested it with a file that I call 'test.bin' that contains the characters 'Hello World'. If I do not flush the outputstream after the chain has completed, the client just blocks, as if it is still waiting for the bytes to be written, but if I flush it, it only writes 'Hel'.
Any help would be appreciated.
Here is the wrapper source :
public class CompressionResponse extends HttpServletResponseWrapper {
private CompressionStream stream;
private PrintWriter writer;
public CompressionResponse(HttpServletResponse response) {
super(response);
response.setHeader("Content-Encoding", "gzip");
}
public ServletOutputStream getOutputStream() throws IOException {
if (writer != null) {
throw new IOException("getWriter() has already been called on this response.");
}
if (stream == null) {
stream = new CompressionStream(
new GZIPOutputStream(getResponse().getOutputStream()));
}
return stream;
}
public PrintWriter getWriter() throws IOException {
if (stream != null) {
throw new IOException("getOutputStream() has already been called on this response.");
}
if (writer == null) {
writer = new PrintWriter(
new GZIPOutputStream(getResponse().getOutputStream()));
}
return writer;
}
public void flushBuffer() throws IOException {
if (stream != null) {
stream.flush();
stream.close();
} else if (writer != null) {
writer.flush();
writer.close();
}
}
private class CompressionStream extends ServletOutputStream {
private OutputStream out;
public CompressionStream(OutputStream out) {
this.out = out;
}
public void close() throws IOException {
out.close();
}
public void flush() throws IOException {
out.flush();
}
public void write(int b) throws IOException {
out.write(b);
}
}
}