use long to count bytes or we get trouble with archives > 2GB

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/compress/trunk@911465 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java
index 71f5891..5f0f805 100644
--- a/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java
@@ -43,7 +43,7 @@
     private static final int BYTE_MASK = 0xFF;
     
     /** holds the number of bytes read in this stream */
-    private int bytesRead = 0;
+    private long bytesRead = 0;
 
     /**
      * Returns the next Archive Entry in this Stream.
@@ -74,8 +74,7 @@
      * 
      * @return the byte read, or -1 if end of input is reached
      * @throws IOException
-     *             if an I/O error has occurred or if a CPIO file error has
-     *             occurred
+     *             if an I/O error has occurred
      */
     public int read() throws IOException {
         int num = read(SINGLE, 0, 1);
@@ -89,16 +88,45 @@
      * @param read the number of bytes read
      */
     protected void count(int read) {
-        if(read != -1) {
+        count((long) read);
+    }
+
+    /**
+     * Increments the counter of already read bytes.
+     * Doesn't increment if the EOF has been hit (read == -1)
+     * 
+     * @param read the number of bytes read
+     */
+    protected void count(long read) {
+        if (read != -1) {
             bytesRead = bytesRead + read;
         }
     }
     
     /**
+     * Decrements the counter of already read bytes.
+     * 
+     * @param read the number of bytes pushed back.
+     */
+    protected void pushedBackBytes(long pushedBack) {
+        bytesRead -= pushedBack;
+    }
+    
+    /**
+     * Returns the current number of bytes read from this stream.
+     * @return the number of read bytes
+     * @deprecated this method may yield wrong results for large
+     * archives, use #getBytesRead instead
+     */
+    public int getCount() {
+        return (int) bytesRead;
+    }
+
+    /**
      * Returns the current number of bytes read from this stream.
      * @return the number of read bytes
      */
-    public int getCount() {
+    public long getBytesRead() {
         return bytesRead;
     }
 }
diff --git a/src/main/java/org/apache/commons/compress/archivers/ArchiveOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/ArchiveOutputStream.java
index d1875cb..af3fa10 100644
--- a/src/main/java/org/apache/commons/compress/archivers/ArchiveOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/ArchiveOutputStream.java
@@ -52,8 +52,8 @@
     private final byte[] oneByte = new byte[1];
     static final int BYTE_MASK = 0xFF;
 
-    /** holds the number of bytes read in this stream */
-    private int bytesRead = 0;
+    /** holds the number of bytes written to this stream */
+    private long bytesWritten = 0;
     // Methods specific to ArchiveOutputStream
     
     /**
@@ -113,22 +113,42 @@
     }
 
     /**
-     * Increments the counter of already read bytes.
+     * Increments the counter of already written bytes.
      * Doesn't increment if the EOF has been hit (read == -1)
      * 
      * @param read the number of bytes read
      */
     protected void count(int read) {
-        if(read != -1) {
-            bytesRead = bytesRead + read;
+        count((long) read);
+    }
+
+    /**
+     * Increments the counter of already written bytes.
+     * Doesn't increment if the EOF has been hit (read == -1)
+     * 
+     * @param written the number of bytes written
+     */
+    protected void count(long written) {
+        if (written != -1) {
+            bytesWritten = bytesWritten + written;
         }
     }
     
     /**
-     * Returns the current number of bytes read from this stream.
-     * @return the number of read bytes
+     * Returns the current number of bytes written to this stream.
+     * @return the number of written bytes
+     * @deprecated this method may yield wrong results for large
+     * archives, use #getBytesWritten instead
      */
     public int getCount() {
-        return bytesRead;
+        return (int) bytesWritten;
+    }
+
+    /**
+     * Returns the current number of bytes written to this stream.
+     * @return the number of written bytes
+     */
+    public long getBytesWritten() {
+        return bytesWritten;
     }
 }
diff --git a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java
index 04ef8d2..de92c96 100644
--- a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java
@@ -86,7 +86,7 @@
             final byte[] realized = new byte[expected.length];
             final int read = read(realized);
             if (read != expected.length) {
-                throw new IOException("failed to read header. Occured at byte: " + getCount());
+                throw new IOException("failed to read header. Occured at byte: " + getBytesRead());
             }
             for (int i = 0; i < expected.length; i++) {
                 if (expected[i] != realized[i]) {
@@ -125,11 +125,11 @@
             final byte[] realized = new byte[expected.length];
             final int read = read(realized);
             if (read != expected.length) {
-                throw new IOException("failed to read entry header. Occured at byte: " + getCount());
+                throw new IOException("failed to read entry header. Occured at byte: " + getBytesRead());
             }
             for (int i = 0; i < expected.length; i++) {
                 if (expected[i] != realized[i]) {
-                    throw new IOException("invalid entry header. not read the content? Occured at byte: " + getCount());
+                    throw new IOException("invalid entry header. not read the content? Occured at byte: " + getBytesRead());
                 }
             }
         }
diff --git a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java
index 6cd17db..799c650 100644
--- a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java
@@ -184,7 +184,7 @@
             } else if (magicString.equals(MAGIC_OLD_ASCII)) {
                 this.entry = readOldAsciiEntry();
             } else {
-                throw new IOException("Unknown magic [" + magicString + "]. Occured at byte: " + getCount());
+                throw new IOException("Unknown magic [" + magicString + "]. Occured at byte: " + getBytesRead());
             }
         }
 
@@ -239,7 +239,7 @@
             this.entryEOF = true;
             if (this.entry.getFormat() == FORMAT_NEW_CRC) {
                 if (this.crc != this.entry.getChksum()) {
-                    throw new IOException("CRC Error. Occured at byte: " + getCount());
+                    throw new IOException("CRC Error. Occured at byte: " + getBytesRead());
                 }
             }
             return -1; // EOF for this entry
@@ -320,7 +320,7 @@
         String name = readCString((int) namesize);
         ret.setName(name);
         if (mode == 0 && !name.equals(CPIO_TRAILER)){
-            throw new IOException("Mode 0 only allowed in the trailer. Found entry name: "+name + " Occured at byte: " + getCount());
+            throw new IOException("Mode 0 only allowed in the trailer. Found entry name: "+name + " Occured at byte: " + getBytesRead());
         }
         skip(ret.getHeaderPadCount());
 
@@ -346,7 +346,7 @@
         final String name = readCString((int) namesize);
         ret.setName(name);
         if (mode == 0 && !name.equals(CPIO_TRAILER)){
-            throw new IOException("Mode 0 only allowed in the trailer. Found entry: "+ name + " Occured at byte: " + getCount());
+            throw new IOException("Mode 0 only allowed in the trailer. Found entry: "+ name + " Occured at byte: " + getBytesRead());
         }
 
         return ret;
@@ -372,7 +372,7 @@
         final String name = readCString((int) namesize);
         ret.setName(name);
         if (mode == 0 && !name.equals(CPIO_TRAILER)){
-            throw new IOException("Mode 0 only allowed in the trailer. Found entry: "+name + "Occured at byte: " + getCount());
+            throw new IOException("Mode 0 only allowed in the trailer. Found entry: "+name + "Occured at byte: " + getBytesRead());
         }
         skip(ret.getHeaderPadCount());
 
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
index 9c077aa..cbe9dd3 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
@@ -275,7 +275,7 @@
             if (rec == null) {
                 // Unexpected EOF!
                 throw new IOException("unexpected EOF with " + numToRead
-                                      + " bytes unread. Occured at byte: " + getCount());
+                                      + " bytes unread. Occured at byte: " + getBytesRead());
             }
             count(rec.length);
             int sz = numToRead;
diff --git a/src/main/java/org/apache/commons/compress/compressors/CompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/CompressorInputStream.java
index 18053c1..d215a95 100644
--- a/src/main/java/org/apache/commons/compress/compressors/CompressorInputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/CompressorInputStream.java
@@ -21,7 +21,7 @@
 import java.io.InputStream;
 
 public abstract class CompressorInputStream extends InputStream {
-    private int bytesRead = 0;
+    private long bytesRead = 0;
     
     /**
      * Increments the counter of already read bytes.
@@ -30,16 +30,36 @@
      * @param read the number of bytes read
      */
     protected void count(int read) {
+        count((long) read);
+    }
+    
+    /**
+     * Increments the counter of already read bytes.
+     * Doesn't increment if the EOF has been hit (read == -1)
+     * 
+     * @param read the number of bytes read
+     */
+    protected void count(long read) {
         if(read != -1) {
             bytesRead = bytesRead + read;
         }
     }
-    
+
+    /**
+     * Returns the current number of bytes read from this stream.
+     * @return the number of read bytes
+     * @deprecated this method may yield wrong results for large
+     * archives, use #getBytesRead instead
+     */
+    public int getCount() {
+        return (int) bytesRead;
+    }
+
     /**
      * Returns the current number of bytes read from this stream.
      * @return the number of read bytes
      */
-    public int getCount() {
+    public long getBytesRead() {
         return bytesRead;
     }
 }