commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r759287 - in /commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio: CpioArchiveInputStream.java CpioArchiveOutputStream.java
Date Fri, 27 Mar 2009 17:51:04 GMT
Author: sebb
Date: Fri Mar 27 17:51:03 2009
New Revision: 759287

URL: http://svn.apache.org/viewvc?rev=759287&view=rev
Log:
Simplify code by using getXXXPadCount() methods
Align variable names (was entry and cpioEntry)

Modified:
    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/cpio/CpioArchiveOutputStream.java

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=759287&r1=759286&r2=759287&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 Mar 27 17:51:03 2009
@@ -200,13 +200,11 @@
         return this.entry;
     }
 
-    private long skip(final long count, final int border) throws IOException {
-        long skip = count % border;
-        if (skip > 0) {
-            skip = this.in.skip(border - skip);
-            // TODO - what if not enough bytes are skipped?
+    private void skip(int bytes) throws IOException{
+        final byte[] buff = new byte[4]; // Cannot be more than 3 bytes
+        if (bytes > 0) {
+            readFully(buff, 0, bytes);
         }
-        return skip;
     }
 
     /**
@@ -251,15 +249,10 @@
         if (this.entry == null || this.entryEOF) {
             return -1;
         }
-        // N.B. These checks assume format is not 0 - otherwise condition is always true
         if (this.entryBytesRead == this.entry.getSize()) {
-            if ((this.entry.getFormat() | FORMAT_NEW_MASK) == FORMAT_NEW_MASK) {
-                skip(this.entry.getSize(), 4);
-            } else if ((this.entry.getFormat() | FORMAT_OLD_BINARY) == FORMAT_OLD_BINARY)
{
-                skip(this.entry.getSize(), 2);
-            } // No need to skip for FORMAT_OLD_ASCII
+            skip(entry.getDataPadCount());
             this.entryEOF = true;
-            if ((this.entry.getFormat() | FORMAT_NEW_CRC) == FORMAT_NEW_CRC) {
+            if (this.entry.getFormat() == FORMAT_NEW_CRC) {
                 if (this.crc != this.entry.getChksum()) {
                     throw new IOException("CRC Error");
                 }
@@ -345,7 +338,7 @@
             // TODO - change this to throw
             new IOException("Mode 0 only allowed in the trailer. Found: "+name).printStackTrace();
         }
-        skip(ret.getHeaderSize() + namesize, 4);
+        skip(ret.getHeaderPadCount());
 
         return ret;
     }
@@ -383,7 +376,7 @@
         long namesize = readBinaryLong(2, swapHalfWord);
         ret.setSize(readBinaryLong(4, swapHalfWord));
         ret.setName(readCString((int) namesize));
-        skip(ret.getHeaderSize() + namesize, 2);
+        skip(ret.getHeaderPadCount());
 
         return ret;
     }

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=759287&r1=759286&r2=759287&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 Mar 27 17:51:03 2009
@@ -60,7 +60,7 @@
 public class CpioArchiveOutputStream extends ArchiveOutputStream implements
         CpioConstants {
 
-    private CpioArchiveEntry cpioEntry;
+    private CpioArchiveEntry entry;
 
     private boolean closed = false;
 
@@ -140,7 +140,7 @@
      */
     public void putNextEntry(final CpioArchiveEntry e) throws IOException {
         ensureOpen();
-        if (this.cpioEntry != null) {
+        if (this.entry != null) {
             closeArchiveEntry(); // close previous entry
         }
         if (e.getTime() == -1) {
@@ -159,7 +159,7 @@
         }
 
         writeHeader(e);
-        this.cpioEntry = e;
+        this.entry = e;
         this.written = 0;
     }
 
@@ -200,7 +200,7 @@
         writeAsciiLong(entry.getName().length() + 1, 8, 16);
         writeAsciiLong(entry.getChksum(), 8, 16);
         writeCString(entry.getName());
-        pad(entry.getHeaderSize() + entry.getName().length() + 1, 4);
+        pad(entry.getHeaderPadCount());
     }
 
     private void writeOldAsciiEntry(final CpioArchiveEntry entry)
@@ -231,7 +231,7 @@
         writeBinaryLong(entry.getName().length() + 1, 2, swapHalfWord);
         writeBinaryLong(entry.getSize(), 4, swapHalfWord);
         writeCString(entry.getName());
-        pad(entry.getHeaderSize() + entry.getName().length() + 1, 2);
+        pad(entry.getHeaderPadCount());
     }
 
     /*(non-Javadoc)
@@ -243,23 +243,18 @@
     public void closeArchiveEntry() throws IOException {
         ensureOpen();
 
-        if (this.cpioEntry.getSize() != this.written) {
+        if (this.entry.getSize() != this.written) {
             throw new IOException("invalid entry size (expected "
-                    + this.cpioEntry.getSize() + " but got " + this.written
+                    + this.entry.getSize() + " but got " + this.written
                     + " bytes)");
         }
-        // N.B. These checks assume format is not 0
-        if ((this.cpioEntry.getFormat() | FORMAT_NEW_MASK) == FORMAT_NEW_MASK) {
-            pad(this.cpioEntry.getSize(), 4);
-        } else if ((this.cpioEntry.getFormat() | FORMAT_OLD_BINARY) == FORMAT_OLD_BINARY)
{
-            pad(this.cpioEntry.getSize(), 2);
-        } // No need to pad for FORMAT_OLD_ASCII
-        if ((this.cpioEntry.getFormat() | FORMAT_NEW_CRC) == FORMAT_NEW_CRC) {
-            if (this.crc != this.cpioEntry.getChksum()) {
+        pad(this.entry.getDataPadCount());
+        if (this.entry.getFormat() == FORMAT_NEW_CRC) {
+            if (this.crc != this.entry.getChksum()) {
                 throw new IOException("CRC Error");
             }
         }
-        this.cpioEntry = null;
+        this.entry = null;
         this.crc = 0;
         this.written = 0;
     }
@@ -287,16 +282,15 @@
             return;
         }
 
-        if (this.cpioEntry == null) {
+        if (this.entry == null) {
             throw new IOException("no current CPIO entry");
         }
-        if (this.written + len > this.cpioEntry.getSize()) {
+        if (this.written + len > this.entry.getSize()) {
             throw new IOException("attempt to write past end of STORED entry");
         }
         out.write(b, off, len);
         this.written += len;
-        // format is assumed non-zero here, otherwise the condition is always true
-        if ((this.cpioEntry.getFormat() | FORMAT_NEW_CRC) == FORMAT_NEW_CRC) {
+        if (this.entry.getFormat() == FORMAT_NEW_CRC) {
             for (int pos = 0; pos < len; pos++) {
                 this.crc += b[pos] & 0xFF;
             }
@@ -318,13 +312,13 @@
         if (this.finished) {
             return;
         }
-        if (this.cpioEntry != null) {
+        if (this.entry != null) {
             closeArchiveEntry();
         }
-        this.cpioEntry = new CpioArchiveEntry(this.entryFormat);
-        this.cpioEntry.setName(CPIO_TRAILER);
-        this.cpioEntry.setNumberOfLinks(1);
-        writeHeader(this.cpioEntry);
+        this.entry = new CpioArchiveEntry(this.entryFormat);
+        this.entry.setName(CPIO_TRAILER);
+        this.entry.setNumberOfLinks(1);
+        writeHeader(this.entry);
         closeArchiveEntry();
     }
 
@@ -343,11 +337,10 @@
         }
     }
 
-    private void pad(final long count, final int border) throws IOException {
-        long pad = count % border;
-        if (pad > 0) {
-            byte tmp[] = new byte[(int) (border - pad)];
-            out.write(tmp);
+    private void pad(int count) throws IOException{
+        if (count > 0){
+            byte buff[] = new byte[count];
+            out.write(buff);
         }
     }
 



Mime
View raw message