commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r911701 - /commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
Date Fri, 19 Feb 2010 05:13:32 GMT
Author: bodewig
Date: Fri Feb 19 05:13:31 2010
New Revision: 911701

URL: http://svn.apache.org/viewvc?rev=911701&view=rev
Log:
Update count properly in CpioArchiveOutputStream, get rid of CountingStream

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java?rev=911701&r1=911700&r2=911701&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
Fri Feb 19 05:13:31 2010
@@ -111,7 +111,7 @@
      */
     public CpioArchiveOutputStream(final OutputStream out, final short format,
                                    final int blockSize) {
-        this.out = new CountingStream(out);
+        this.out = out;
         switch (format) {
         case FORMAT_NEW:
         case FORMAT_NEW_CRC:
@@ -195,14 +195,17 @@
         switch (e.getFormat()) {
         case FORMAT_NEW:
             out.write(ArchiveUtils.toAsciiBytes(MAGIC_NEW));
+            count(6);
             writeNewEntry(e);
             break;
         case FORMAT_NEW_CRC:
             out.write(ArchiveUtils.toAsciiBytes(MAGIC_NEW_CRC));
+            count(6);
             writeNewEntry(e);
             break;
         case FORMAT_OLD_ASCII:
             out.write(ArchiveUtils.toAsciiBytes(MAGIC_OLD_ASCII));
+            count(6);
             writeOldAsciiEntry(e);
             break;
         case FORMAT_OLD_BINARY:
@@ -403,8 +406,7 @@
         writeHeader(this.entry);
         closeArchiveEntry();
 
-        int lengthOfLastBlock =
-            (int) (((CountingStream) out).getTotalWritten() % blockSize);
+        int lengthOfLastBlock = (int) (getBytesWritten() % blockSize);
         if (lengthOfLastBlock != 0) {
             pad(blockSize - lengthOfLastBlock);
         }
@@ -434,6 +436,7 @@
         if (count > 0){
             byte buff[] = new byte[count];
             out.write(buff);
+            count(count);
         }
     }
 
@@ -441,6 +444,7 @@
             final boolean swapHalfWord) throws IOException {
         byte tmp[] = CpioUtil.long2byteArray(number, length, swapHalfWord);
         out.write(tmp);
+        count(tmp.length);
     }
 
     private void writeAsciiLong(final long number, final int length,
@@ -464,7 +468,9 @@
         } else {
             tmpStr = tmp.substring(tmp.length() - length);
         }
-        out.write(ArchiveUtils.toAsciiBytes(tmpStr));
+        byte[] b = ArchiveUtils.toAsciiBytes(tmpStr);
+        out.write(b);
+        count(b.length);
     }
 
     /**
@@ -473,8 +479,10 @@
      * @throws IOException if the string couldn't be written
      */
     private void writeCString(final String str) throws IOException {
-        out.write(ArchiveUtils.toAsciiBytes(str)); 
+        byte[] b = ArchiveUtils.toAsciiBytes(str);
+        out.write(b);
         out.write('\0');
+        count(b.length + 1);
     }
 
     /**
@@ -490,24 +498,4 @@
         return new CpioArchiveEntry(inputFile, entryName);
     }
 
-    private static class CountingStream extends FilterOutputStream {
-        private long totalWritten = 0;
-
-        private CountingStream(final OutputStream out) {
-            super(out);
-        }
-        public void write(byte[] b) throws IOException {
-            write(b, 0, b.length);
-        }
-        public void write(int b) throws IOException {
-            out.write(b);
-            totalWritten++;
-        }
-        public void write(byte[] b, int off, int len)
-            throws IOException {
-            out.write(b, off, len);
-            totalWritten += len;
-        }
-        private long getTotalWritten() { return totalWritten; }
-    }
 }



Mime
View raw message