commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Robert Simac (Created) (JIRA)" <>
Subject [jira] [Created] (COMPRESS-160) TarArchiveOutputStream.getBytesWritten() returns invalid value
Date Fri, 21 Oct 2011 13:24:32 GMT
TarArchiveOutputStream.getBytesWritten() returns invalid value

                 Key: COMPRESS-160
             Project: Commons Compress
          Issue Type: Bug
          Components: Archivers
    Affects Versions: 1.2
         Environment: SE Runtime Environment
os.arch=x86 7
This issue may be unrelated to environment
            Reporter: Robert Simac

It appears the TarArchiveOutputStream.getBytesWritten()returns zero or invalid value when
In the code sample below, it returns zero, even after an sizeable file was processed.
I've printed it twice, once before closing the output stream, and once after, just for the
It is also demonstrable on multiple processed files.

Within the TarArchiveOutputStream.getBytesWritten() implementation, it appears the call for
count(numToWrite) is made after the numToWrite is depleted in the process of actual byte writing.
When call for count(numToWrite); is moved up, the returned values for TarArchiveOutputStream.getBytesWritten()
are getting equal to the sum of the sizes of processed files. This is much closer to expected
value ("Returns the current number of bytes written to this stream.") but still not correct,
for that number should include the tar header sizes as well.

At any rate, please find the proposed patch below, merely moving count(numToWrite); up a few
lines. This makes TarArchiveOutputStream.getBytesWritten() closer to true value.

Test code:
	public void tartest() throws Exception {
		FileOutputStream myOutputStream = new FileOutputStream("C:/temp/tartest.tar");
		ArchiveOutputStream sTarOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.TAR,
		File sSource = new File("C:/share/od_l.txt");
		TarArchiveEntry sEntry = new TarArchiveEntry(sSource);
		FileInputStream sInput = new FileInputStream(sSource);
		byte[] cpRead = new byte[8192];
		int iRead = 0;
		while ((iRead = > 0) {
			sTarOut.write(cpRead, 0, iRead);
		}"Processed: "+sTarOut.getBytesWritten()+" bytes. File Len: "+sSource.length());
		sTarOut.close();"Processed: "+sTarOut.getBytesWritten()+" bytes. File Len: "+sSource.length());


Test Output:
Oct 21, 2011 9:09:28 AM com.cronsult.jndmpd.test.Backup tartest
INFO: Processed: 0 bytes. File Len: 186974208
Oct 21, 2011 9:09:28 AM com.cronsult.jndmpd.test.Backup tartest
INFO: Processed: 0 bytes. File Len: 186974208

Proposed Patch:
Index: src/main/java/org/apache/commons/compress/archivers/tar/
--- src/main/java/org/apache/commons/compress/archivers/tar/	(revision
+++ src/main/java/org/apache/commons/compress/archivers/tar/	(working
@@ -276,6 +276,8 @@
             // eliminate some of the buffer copying.
+        count(numToWrite);
         if (assemLen > 0) {
             if ((assemLen + numToWrite) >= recordBuf.length) {
@@ -325,7 +327,7 @@
             wOffset += num;
-        count(numToWrite);


This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators:!default.jspa
For more information on JIRA, see:


View raw message