Index: TarArchiveInputStream.java
===================================================================
--- TarArchiveInputStream.java	(revision 1506382)
+++ TarArchiveInputStream.java	(working copy)
@@ -36,6 +36,7 @@
 import org.apache.commons.compress.archivers.zip.ZipEncodingHelper;
 import org.apache.commons.compress.utils.ArchiveUtils;
 import org.apache.commons.compress.utils.CharsetNames;
+import org.apache.commons.compress.utils.IOUtils;
 
 /**
  * The TarInputStream reads a UNIX tar archive as an InputStream.
@@ -45,18 +46,30 @@
  * @NotThreadSafe
  */
 public class TarArchiveInputStream extends ArchiveInputStream {
+
     private static final int SMALL_BUFFER_SIZE = 256;
-    private static final int BUFFER_SIZE = 8 * 1024;
 
-    private final byte[] SKIP_BUF = new byte[BUFFER_SIZE];
     private final byte[] SMALL_BUF = new byte[SMALL_BUFFER_SIZE];
 
+    /** The size the TAR header */
+    private final int recordSize;
+
+    /** True of file has hit EOF */
     private boolean hasHitEOF;
+
+    /** Size of the current entry */
     private long entrySize;
+
+    /** How far into the entry the stream is at */
     private long entryOffset;
-    private byte[] readBuf;
-    protected final TarBuffer buffer;
+
+    /** An input stream to read from */
+    private final InputStream is;
+
+    /** The meta-data about the current entry */
     private TarArchiveEntry currEntry;
+
+    /** The encoding of the file */
     private final ZipEncoding encoding;
 
     /**
@@ -64,7 +77,7 @@
      * @param is the input stream to use
      */
     public TarArchiveInputStream(InputStream is) {
-        this(is, TarBuffer.DEFAULT_BLKSIZE, TarBuffer.DEFAULT_RCDSIZE);
+        this(is, TarConstants.DEFAULT_BLKSIZE, TarConstants.DEFAULT_RCDSIZE);
     }
 
     /**
@@ -74,7 +87,8 @@
      * @since 1.4
      */
     public TarArchiveInputStream(InputStream is, String encoding) {
-        this(is, TarBuffer.DEFAULT_BLKSIZE, TarBuffer.DEFAULT_RCDSIZE, encoding);
+		this(is, TarConstants.DEFAULT_BLKSIZE, TarConstants.DEFAULT_RCDSIZE,
+				encoding);
     }
 
     /**
@@ -83,7 +97,7 @@
      * @param blockSize the block size to use
      */
     public TarArchiveInputStream(InputStream is, int blockSize) {
-        this(is, blockSize, TarBuffer.DEFAULT_RCDSIZE);
+        this(is, blockSize, TarConstants.DEFAULT_RCDSIZE);
     }
 
     /**
@@ -95,7 +109,7 @@
      */
     public TarArchiveInputStream(InputStream is, int blockSize,
                                  String encoding) {
-        this(is, blockSize, TarBuffer.DEFAULT_RCDSIZE, encoding);
+        this(is, blockSize, TarConstants.DEFAULT_RCDSIZE, encoding);
     }
 
     /**
@@ -105,7 +119,7 @@
      * @param recordSize the record size to use
      */
     public TarArchiveInputStream(InputStream is, int blockSize, int recordSize) {
-        this(is, blockSize, recordSize, null);
+        this(is, blockSize, recordSize, null);      
     }
 
     /**
@@ -118,10 +132,10 @@
      */
     public TarArchiveInputStream(InputStream is, int blockSize, int recordSize,
                                  String encoding) {
-        this.buffer = new TarBuffer(is, blockSize, recordSize);
-        this.readBuf = null;
+        this.is = is;
         this.hasHitEOF = false;
         this.encoding = ZipEncodingHelper.getZipEncoding(encoding);
+        this.recordSize = recordSize;
     }
 
     /**
@@ -130,16 +144,16 @@
      */
     @Override
     public void close() throws IOException {
-        buffer.close();
+        is.close();
     }
 
     /**
-     * Get the record size being used by this stream's TarBuffer.
+     * Get the record size being used by this stream's buffer.
      *
      * @return The TarBuffer record size.
      */
     public int getRecordSize() {
-        return buffer.getRecordSize();
+        return recordSize;
     }
 
     /**
@@ -174,21 +188,17 @@
      */
     @Override
     public long skip(long numToSkip) throws IOException {
-        // REVIEW
-        // This is horribly inefficient, but it ensures that we
-        // properly skip over bytes via the TarBuffer...
-        //
-        long skip = numToSkip;
-        while (skip > 0) {
-            int realSkip = (int) (skip > SKIP_BUF.length
-                                  ? SKIP_BUF.length : skip);
-            int numRead = read(SKIP_BUF, 0, realSkip);
-            if (numRead == -1) {
-                break;
-            }
-            skip -= numRead;
-        }
-        return (numToSkip - skip);
+
+        long available = (entrySize - entryOffset);
+        long skipped = 0;
+
+        numToSkip = Math.min(numToSkip, available);
+
+        skipped = IOUtils.skip(is, numToSkip); 
+
+        entryOffset += skipped;
+
+        return skipped;
     }
 
     /**
@@ -217,18 +227,11 @@
         }
 
         if (currEntry != null) {
-            long numToSkip = entrySize - entryOffset;
+            /* Skip will only go to the end of the current entry */
+            skip(Long.MAX_VALUE);
 
-            while (numToSkip > 0) {
-                long skipped = skip(numToSkip);
-                if (skipped <= 0) {
-                    throw new RuntimeException("failed to skip current tar"
-                                               + " entry");
-                }
-                numToSkip -= skipped;
-            }
-
-            readBuf = null;
+            /* Each archive should end on an even block */
+            skipRecordPadding();
         }
 
         byte[] headerBuf = getRecord();
@@ -246,6 +249,7 @@
             ioe.initCause(e);
             throw ioe;
         }
+
         entryOffset = 0;
         entrySize = currEntry.getSize();
 
@@ -284,10 +288,23 @@
         // information, we update entrySize here so that it contains
         // the correct value.
         entrySize = currEntry.getSize();
+
         return currEntry;
     }
+    
+    /**
+     * The last record block should be written at the full size, so skip any
+     * additional space used to fill a record after an entry
+     */
+    private void skipRecordPadding() throws IOException {
+        if (this.entrySize > 0) {
+            long numRecords = (this.entrySize / this.recordSize) + 1;
+            long padding = (numRecords * this.recordSize) - this.entrySize;
+            IOUtils.skip(is, padding);
+        }
+    }
 
-    /**
+	/**
      * Get the next entry in this tar archive as longname data.
      *
      * @return The next entry in the archive as longname data, or null.
@@ -335,19 +352,43 @@
      * @throws IOException on error
      */
     private byte[] getRecord() throws IOException {
-        byte[] headerBuf = null;
-        if (!hasHitEOF) {
-            headerBuf = buffer.readRecord();
-            hasHitEOF = buffer.isEOFRecord(headerBuf);
-            if (hasHitEOF && headerBuf != null) {
-                buffer.tryToConsumeSecondEOFRecord();
-                headerBuf = null;
-            }
+        byte[] headerBuf = readRecord();
+        hasHitEOF = isEOFRecord(headerBuf);
+        if (hasHitEOF && headerBuf != null) {
+            tryToConsumeSecondEOFRecord();
+            headerBuf = null;
         }
-
         return headerBuf;
     }
 
+    /**
+     * Determine if an archive record indicate End of Archive. End of
+     * archive is indicated by a record that consists entirely of null bytes.
+     *
+     * @param record The record data to check.
+     * @return true if the record data is an End of Archive
+     */
+    protected boolean isEOFRecord(byte[] record) {
+        return record == null || ArchiveUtils.isArrayZero(record, recordSize);
+    }
+    
+    /**
+     * Read a record from the input stream and return the data.
+     *
+     * @return The record data or null if EOF has been hit.
+     * @throws IOException on error
+     */
+    protected byte[] readRecord() throws IOException {
+
+        byte[] record = new byte[recordSize];
+
+        if (is.read(record) != recordSize) {
+            return null;
+        }
+
+        return record;
+    }
+
     private void paxHeaders() throws IOException{
         Map<String, String> headers = parsePaxHeaders(this);
         getNextEntry(); // Get the actual file entry
@@ -468,10 +509,42 @@
         }
     }
 
+    /**
+     * Returns the next Archive Entry in this Stream.
+     *
+     * @return the next entry,
+     *         or {@code null} if there are no more entries
+     * @throws IOException if the next entry could not be read
+     */
     @Override
     public ArchiveEntry getNextEntry() throws IOException {
         return getNextTarEntry();
     }
+    
+    /**
+     * Tries to read the next record rewinding the stream if it is not a EOF record.
+     *
+     * <p>This is meant to protect against cases where a tar
+     * implementation has written only one EOF record when two are
+     * expected.  Actually this won't help since a non-conforming
+     * implementation likely won't fill full blocks consisting of - by
+     * default - ten records either so we probably have already read
+     * beyond the archive anyway.</p>
+     */
+    void tryToConsumeSecondEOFRecord() throws IOException {
+        boolean shouldReset = true;
+        boolean marked = is.markSupported();
+        if (marked) {
+        	is.mark(recordSize);
+        }
+        try {
+            shouldReset = !isEOFRecord(readRecord());
+        } finally {
+            if (shouldReset && marked) {
+            	is.reset();
+            }
+        }
+    }
 
     /**
      * Reads bytes from the current tar archive entry.
@@ -487,70 +560,28 @@
      * @throws IOException on error
      */
     @Override
-    public int read(byte[] buf, int offset, int numToRead) throws IOException {
-        int totalRead = 0;
+    public int read(byte[] buf, int offset, int numToRead) throws IOException
+    {
+    	int totalRead = 0;
 
-        if (entryOffset >= entrySize) {
+        if (hasHitEOF || entryOffset >= entrySize)
+        {
             return -1;
         }
 
-        if ((numToRead + entryOffset) > entrySize) {
-            numToRead = (int) (entrySize - entryOffset);
+        numToRead = Math.min(numToRead, available());
+        
+        totalRead = is.read(buf, offset, numToRead);
+        
+        if (totalRead == -1)
+        {
+        	hasHitEOF = true;
         }
-
-        if (readBuf != null) {
-            int sz = (numToRead > readBuf.length) ? readBuf.length
-                : numToRead;
-
-            System.arraycopy(readBuf, 0, buf, offset, sz);
-
-            if (sz >= readBuf.length) {
-                readBuf = null;
-            } else {
-                int newLen = readBuf.length - sz;
-                byte[] newBuf = new byte[newLen];
-
-                System.arraycopy(readBuf, sz, newBuf, 0, newLen);
-
-                readBuf = newBuf;
-            }
-
-            totalRead += sz;
-            numToRead -= sz;
-            offset += sz;
+        else
+        {
+        	entryOffset += (long) totalRead;
         }
 
-        while (numToRead > 0) {
-            byte[] rec = buffer.readRecord();
-
-            if (rec == null) {
-                // Unexpected EOF!
-                throw new IOException("unexpected EOF with " + numToRead
-                                      + " bytes unread. Occured at byte: " + getBytesRead());
-            }
-            count(rec.length);
-            int sz = numToRead;
-            int recLen = rec.length;
-
-            if (recLen > sz) {
-                System.arraycopy(rec, 0, buf, offset, sz);
-
-                readBuf = new byte[recLen - sz];
-
-                System.arraycopy(rec, sz, readBuf, 0, recLen - sz);
-            } else {
-                sz = recLen;
-
-                System.arraycopy(rec, 0, buf, offset, recLen);
-            }
-
-            totalRead += sz;
-            numToRead -= sz;
-            offset += sz;
-        }
-
-        entryOffset += totalRead;
-
         return totalRead;
     }
 
@@ -568,22 +599,15 @@
         return false;
     }
 
-    protected final TarArchiveEntry getCurrentEntry() {
+    /**
+     * Get the current TAR Archive Entry that this input stream is processing
+     * 
+     * @return The current Archive Entry
+     */
+    public ArchiveEntry getCurrentEntry() {
         return currEntry;
     }
 
-    protected final void setCurrentEntry(TarArchiveEntry e) {
-        currEntry = e;
-    }
-
-    protected final boolean isAtEOF() {
-        return hasHitEOF;
-    }
-
-    protected final void setAtEOF(boolean b) {
-        hasHitEOF = b;
-    }
-
     /**
      * Checks if the signature matches what is expected for a tar file.
      *
