@Override public synchronized void write(byte[] bytes, int offset, int length) throws IOException { if (length > 0) { flushLastByte(); if (length > 1) { reenableCompression(); super.write(bytes, offset, length - 1); } rememberLastByte(bytes[offset + length - 1]); } }
@Override public void write(byte[] bytes) throws IOException { write(bytes, 0, bytes.length); }
@Override public synchronized void write(int i) throws IOException { flushLastByte(); rememberLastByte((byte) i); }
/** * End the current request. It is acceptable to write extra bytes using * buffer.doWrite during the execution of this method. */ @Override public long end() throws IOException { if (compressionStream == null) { compressionStream = new FlushableGZIPOutputStream(fakeOutputStream); } compressionStream.finish(); compressionStream.close(); return ((OutputFilter) buffer).end(); }
@Override public synchronized void finish() throws IOException { try { flushLastByte(); } catch (IOException ignore) { // If our write failed, then trailer write in finish() will fail // with IOException as well, but it will leave Deflater in more // consistent state. } super.finish(); }
@Override public synchronized void flush() throws IOException { if (!hasData) { return; // do not allow the gzip header to be flushed on its own } // trick the deflater to flush /** * Now this is tricky: We force the Deflater to flush its data by * switching compression level. As yet, a perplexingly simple workaround * for * http://developer.java.sun.com/developer/bugParade/bugs/4255743.html */ if (!def.finished()) { def.setInput(EMPTYBYTEARRAY, 0, 0); def.setLevel(Deflater.NO_COMPRESSION); deflate(); def.setLevel(Deflater.DEFAULT_COMPRESSION); deflate(); out.flush(); } hasData = false; // no more data to flush }
private void flushLastByte() throws IOException { if (hasLastByte) { reenableCompression(); // Clear the flag first, because write() may fail hasLastByte = false; super.write(lastByte, 0, 1); } }
/** * End the current request. It is acceptable to write extra bytes using * buffer.doWrite during the execution of this method. */ public long end() throws IOException { if (compressionStream == null) { compressionStream = new FlushableGZIPOutputStream(fakeOutputStream); } compressionStream.finish(); compressionStream.close(); return ((OutputFilter) buffer).end(); }
@Override public synchronized void write(int i) throws IOException { flushLastByte(); rememberLastByte((byte) i); }
@Override public synchronized void finish() throws IOException { try { flushLastByte(); } catch (IOException ignore) { // If our write failed, then trailer write in finish() will fail // with IOException as well, but it will leave Deflater in more // consistent state. } super.finish(); }
@Override public synchronized void flush() throws IOException { if (!hasData) { return; // do not allow the gzip header to be flushed on its own } // trick the deflater to flush /** * Now this is tricky: We force the Deflater to flush its data by * switching compression level. As yet, a perplexingly simple workaround * for * http://developer.java.sun.com/developer/bugParade/bugs/4255743.html */ if (!def.finished()) { def.setInput(EMPTYBYTEARRAY, 0, 0); def.setLevel(Deflater.NO_COMPRESSION); deflate(); def.setLevel(Deflater.DEFAULT_COMPRESSION); deflate(); out.flush(); } hasData = false; // no more data to flush }
/** * End the current request. It is acceptable to write extra bytes using * buffer.doWrite during the execution of this method. */ @Override public long end() throws IOException { if (compressionStream == null) { compressionStream = new FlushableGZIPOutputStream(fakeOutputStream); } compressionStream.finish(); compressionStream.close(); return ((OutputFilter) buffer).end(); }
@Override public synchronized void write(byte[] bytes, int offset, int length) throws IOException { if (length > 0) { flushLastByte(); if (length > 1) { super.write(bytes, offset, length - 1); } rememberLastByte(bytes[offset + length - 1]); } }
@Override public synchronized void close() throws IOException { try { flushLastByte(); } catch (IOException ignored) { // Ignore. As OutputStream#close() says, the contract of close() // is to close the stream. It does not matter much if the // stream is not writable any more. } super.close(); }
@Override public void write(byte[] bytes) throws IOException { write(bytes, 0, bytes.length); }
/** * End the current request. It is acceptable to write extra bytes using * buffer.doWrite during the execution of this method. */ @Override public long end() throws IOException { if (compressionStream == null) { compressionStream = new FlushableGZIPOutputStream(fakeOutputStream); } compressionStream.finish(); compressionStream.close(); return ((OutputFilter) buffer).end(); }
@Override public synchronized void close() throws IOException { try { flushLastByte(); } catch (IOException ignored) { // Ignore. As OutputStream#close() says, the contract of close() // is to close the stream. It does not matter much if the // stream is not writable any more. } super.close(); }
/** * Write some bytes. * * @return number of bytes written by the filter */ @Override public int doWrite(ByteChunk chunk, Response res) throws IOException { if (compressionStream == null) { compressionStream = new FlushableGZIPOutputStream(fakeOutputStream); } compressionStream.write(chunk.getBytes(), chunk.getStart(), chunk.getLength()); return chunk.getLength(); }
@Override public synchronized void flush() throws IOException { if (hasLastByte) { // - do not allow the gzip header to be flushed on its own // - do not do anything if there is no data to send // trick the deflater to flush /** * Now this is tricky: We force the Deflater to flush its data by * switching compression level. As yet, a perplexingly simple workaround * for * http://developer.java.sun.com/developer/bugParade/bugs/4255743.html */ if (!def.finished()) { def.setLevel(Deflater.NO_COMPRESSION); flushLastByte(); flagReenableCompression = true; } } out.flush(); }
/** * Write some bytes. * * @return number of bytes written by the filter */ public int doWrite(ByteChunk chunk, Response res) throws IOException { if (compressionStream == null) { compressionStream = new FlushableGZIPOutputStream(fakeOutputStream); } compressionStream.write(chunk.getBytes(), chunk.getStart(), chunk.getLength()); return chunk.getLength(); }