The finish() method should finish writing compressed data to the output stream without closing the underlying stream. With this method it is possible to apply multiple filters in succession to the same output stream.
Created attachment 20388 [details] Verifies/specifies the semantics of finish and close
Proposed patch: - finish added - close and finalize changed ------------------------------------------------------------------------------ /** * Finishes compressing to the underlying stream without closing it, * so that multiple compressors can write subsequently to the same * output stream. * * @throws IOException */ public void finish() throws IOException { OutputStream outShadow = this.out; if ( outShadow != null && this.data != null ) { try { if ( this.runLength > 0 ) { writeRun(); } this.currentChar = -1; endBlock(); endCompression(); // outShadow.close(); } finally { // this.out = null; this.data = null; } } } /** * Overriden to close the stream. */ protected void finalize() throws Throwable { if ( this.data != null ) { close(); super.finalize(); } } public void close() throws IOException { finish(); OutputStream outShadow = this.out; if ( outShadow != null ) { try { outShadow.close(); } finally { this.out = null; } } // OutputStream outShadow = this.out; // if ( outShadow != null ) { // try { // if ( this.runLength > 0 ) { // writeRun(); // } // this.currentChar = -1; // endBlock(); // endCompression(); // outShadow.close(); // } finally { // this.out = null; // this.data = null; // } // } }
Created attachment 21001 [details] Verifies/specifies the semantics of finish and close Now contains Apache License 2.0
This is request that would allow the Eclipse provisioning to make direct use of the Apache Ant support without having to patch the CBZip2OutputStream. Please see https://bugs.eclipse.org/bugs/show_bug.cgi?id=208996 Can some please review and consider this patch?
The patch is against a ant 1.7.0 version of cbzip2outputstream. This have been replaced with a version from ant 1.6.5 as the ant 1.7.0 outstream produces corrupt bz2 files for some inputs (ouch!). The intent of the patch is fine, - although I would like ZipOutputStream and CBZip2OutputStream to have the same semantics for finish, close and finalize(!).
Having the same semantics as ZipOutputStream means that finalize() would not be overridden? That would be ok for me. Important for us is the semantics of finish() which ´finishes´ the compression but does not close the underlying output stream.
Is there a bug report for this faultily behavior of the 1.7.0 version? I modified the head version of bzip2 such that is supports the finish() semantics. But one of my unit tests (already attached here) does fail. This failure seems to be known (http://issues.apache.org/bugzilla/show_bug.cgi?id=32200) and fixed (?).
The buggy behavior is described in http://issues.apache.org/bugzilla/show_bug.cgi?id=41596 The best place to look for these is in WHATSNEW. I do not know about http://issues.apache.org/bugzilla/show_bug.cgi?id=32200 but I assume that the rolled back code retriggered it. Peter
I tried to manually apply the patch from http://issues.apache.org/bugzilla/show_bug.cgi?id=32200 to the head version of bzip2. This causes BZip2OutputStream to no longer fail when nothing is written to it, but the resulting compressed date fails to decompress with BZip2InputStream. The failure in Bzip2OS (no writes, just close) is caused by the field last = -1. This leads to division by zero after adding 1 in method mainSort(): for (i = 0; i < NUM_OVERSHOOT_BYTES; i++) { block[last + i + 2] = block[(i % (last + 1)) + 1]; } The patch now catches this case (last < 0) and sets than last = 0. But this case (last == 0) is also the case when you write just exactly one byte to the Bzip2OS. I guess thats why Bzip2IS fails with reading the result of an ´empty compressed´ Bzip2OS.
https://issues.apache.org/jira/browse/SANDBOX-194
merged commons-compress finish method in svn revision 741608
oops, svn revision 741618 it is.