You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gr...@apache.org on 2009/04/10 17:36:16 UTC

svn commit: r763964 - in /commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers: ./ ar/ cpio/ tar/ zip/

Author: grobmeier
Date: Fri Apr 10 15:36:15 2009
New Revision: 763964

URL: http://svn.apache.org/viewvc?rev=763964&view=rev
Log:
COMPRESS-56: enabled counting read bytes in InputStreams and added this information to exceptions, where it seems to fit.

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java?rev=763964&r1=763963&r2=763964&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java Fri Apr 10 15:36:15 2009
@@ -41,6 +41,7 @@
 
     private byte[] SINGLE = new byte[1];
     private static final int BYTE_MASK = 0xFF;
+    private int bytesRead = 0;
 
     /**
      * Returns the next Archive Entry in this Stream.
@@ -60,7 +61,6 @@
      */
     // public abstract XXXArchiveEntry getNextXXXEntry() throws IOException;
 
-
     /**
      * Reads a byte of data. This method will block until enough input is
      * available.
@@ -79,4 +79,24 @@
         int num = read(SINGLE, 0, 1);
         return num == -1 ? -1 : SINGLE[0] & BYTE_MASK;
     }
+    
+    /**
+     * 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(int read) {
+        if(read != -1) {
+            bytesRead = bytesRead + read;
+        }
+    }
+    
+    /**
+     * Returns the current number of bytes read from this stream.
+     * @return the number of read bytes
+     */
+    public int getCount() {
+        return bytesRead;
+    }
 }

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java?rev=763964&r1=763963&r2=763964&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java Fri Apr 10 15:36:15 2009
@@ -77,7 +77,7 @@
             final byte[] realized = new byte[expected.length]; 
             final int read = read(realized);
             if (read != expected.length) {
-                throw new IOException("failed to read header");
+                throw new IOException("failed to read header. Occured at byte: " + getCount());
             }
             for (int i = 0; i < expected.length; i++) {
                 if (expected[i] != realized[i]) {
@@ -116,11 +116,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");
+                throw new IOException("failed to read entry header. Occured at byte: " + getCount());
             }
             for (int i = 0; i < expected.length; i++) {
                 if (expected[i] != realized[i]) {
-                    throw new IOException("invalid entry header. not read the content?");
+                    throw new IOException("invalid entry header. not read the content? Occured at byte: " + getCount());
                 }
             }
         }
@@ -162,6 +162,7 @@
             }
         }
         final int ret = this.input.read(b, off, toRead);
+        count(ret);
         offset += (ret > 0 ? ret : 0);
         return ret;
     }

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java?rev=763964&r1=763963&r2=763964&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java Fri Apr 10 15:36:15 2009
@@ -183,7 +183,7 @@
             } else if (magicString.equals(MAGIC_OLD_ASCII)) {
                 this.entry = readOldAsciiEntry();
             } else {
-                throw new IOException("Unknown magic [" + magicString + "]");
+                throw new IOException("Unknown magic [" + magicString + "]. Occured at byte: " + getCount());
             }
         }
 
@@ -238,7 +238,7 @@
             this.entryEOF = true;
             if (this.entry.getFormat() == FORMAT_NEW_CRC) {
                 if (this.crc != this.entry.getChksum()) {
-                    throw new IOException("CRC Error");
+                    throw new IOException("CRC Error. Occured at byte: " + getCount());
                 }
             }
             return -1; // EOF for this entry
@@ -268,6 +268,7 @@
         int n = 0;
         while (n < len) {
             int count = this.in.read(b, off + n, len - n);
+            count(count);
             if (count < 0) {
                 throw new EOFException();
             }
@@ -318,7 +319,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);
+            throw new IOException("Mode 0 only allowed in the trailer. Found entry name: "+name + " Occured at byte: " + getCount());
         }
         skip(ret.getHeaderPadCount());
 
@@ -344,7 +345,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);
+            throw new IOException("Mode 0 only allowed in the trailer. Found entry: "+ name + " Occured at byte: " + getCount());
         }
 
         return ret;
@@ -370,7 +371,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);
+            throw new IOException("Mode 0 only allowed in the trailer. Found entry: "+name + "Occured at byte: " + getCount());
         }
         skip(ret.getHeaderPadCount());
 

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java?rev=763964&r1=763963&r2=763964&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java Fri Apr 10 15:36:15 2009
@@ -172,8 +172,7 @@
             while (numToSkip > 0) {
                 long skipped = skip(numToSkip);
                 if (skipped <= 0) {
-                    throw new RuntimeException("failed to skip current tar"
-                                               + " entry");
+                    throw new RuntimeException("failed to skip current tar entry");
                 }
                 numToSkip -= skipped;
             }
@@ -278,9 +277,9 @@
             if (rec == null) {
                 // Unexpected EOF!
                 throw new IOException("unexpected EOF with " + numToRead
-                                      + " bytes unread");
+                                      + " bytes unread. Occured at byte: " + getCount());
             }
-
+            count(rec.length);
             int sz = numToRead;
             int recLen = rec.length;
 

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java?rev=763964&r1=763963&r2=763964&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java Fri Apr 10 15:36:15 2009
@@ -208,6 +208,7 @@
                     if ((lengthOfLastRead = in.read(buf)) == -1) {
                         return -1;
                     }
+                    count(lengthOfLastRead);
                     bytesReadFromStream += lengthOfLastRead;
                 }
                 int toRead = length > lengthOfLastRead