COMPRESS-276 verify there is a current archive entry before reading from or writing to the stream

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/compress/trunk@1586935 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 844617c..31e74d6 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -59,6 +59,10 @@
         IOUtils#skip might skip fewer bytes than requested even though
         more could be read from the stream.
       </action>
+      <action type="add" date="2014-04-13" issue="COMPRESS-276">
+        ArchiveStreams now validate there is a current entry before
+        rreading or writing entry data.
+      </action>
     </release>
     <release version="1.8" date="2014-03-12"
              description="Release 1.8">
diff --git a/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStream.java
index f99aa0d..a508fed 100644
--- a/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStream.java
@@ -341,6 +341,9 @@
     
     @Override
     public int read(final byte[] b, final int off, final int len) throws IOException {
+        if (currentLocalFileHeader == null) {
+            throw new IllegalStateException("No current arj entry");
+        }
         if (currentLocalFileHeader.method != LocalFileHeader.Methods.STORED) {
             throw new IOException("Unsupported compression method " + currentLocalFileHeader.method);
         }
diff --git a/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java
index 0f80454..bf411e3 100644
--- a/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java
@@ -465,6 +465,10 @@
             return -1;
         }
 
+        if (active == null) {
+            throw new IllegalStateException("No current dump entry");
+        }
+
         if (len + entryOffset > entrySize) {
             len = (int) (entrySize - entryOffset);
         }
diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
index e1502e2..ae350b6 100644
--- a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
@@ -871,6 +871,9 @@
      *             if an I/O error has occurred
      */
     public int read() throws IOException {
+        if (currentEntryInputStream == null) {
+            throw new IllegalStateException("No current 7z entry");
+        }
         return currentEntryInputStream.read();
     }
     
@@ -897,6 +900,9 @@
      *             if an I/O error has occurred
      */
     public int read(byte[] b, int off, int len) throws IOException {
+        if (currentEntryInputStream == null) {
+            throw new IllegalStateException("No current 7z entry");
+        }
         return currentEntryInputStream.read(b, off, len);
     }
     
diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
index b99cf4e..84d1431 100644
--- a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
@@ -181,7 +181,7 @@
         compressedCrc32.reset();
         fileBytesWritten = 0;
     }
-    
+
     /**
      * Writes a byte to the current archive entry.
      * @param b The byte to be written.
@@ -272,6 +272,10 @@
     }
 
     private CountingOutputStream setupFileOutputStream() throws IOException {
+        if (files.isEmpty()) {
+            throw new IllegalStateException("No current 7z entry");
+        }
+
         OutputStream out = new OutputStreamWrapper();
         ArrayList<CountingOutputStream> moreStreams = new ArrayList<CountingOutputStream>();
         boolean first = true;
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 d79eb8a..fe2c113 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
@@ -573,6 +573,10 @@
             return -1;
         }
 
+        if (currEntry == null) {
+            throw new IllegalStateException("No current tar entry");
+        }
+
         numToRead = Math.min(numToRead, available());
         
         totalRead = is.read(buf, offset, numToRead);
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
index cd7368a..2a5d591 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
@@ -265,7 +265,7 @@
      */
     @Override
     public void putArchiveEntry(ArchiveEntry archiveEntry) throws IOException {
-        if(finished) {
+        if (finished) {
             throw new IOException("Stream has already been finished");
         }
         TarArchiveEntry entry = (TarArchiveEntry) archiveEntry;
@@ -369,6 +369,9 @@
      */
     @Override
     public void write(byte[] wBuf, int wOffset, int numToWrite) throws IOException {
+        if (!haveUnclosedEntry) {
+            throw new IllegalStateException("No current tar entry");
+        }
         if (currBytes + numToWrite > currSize) {
             throw new IOException("request to write '" + numToWrite
                                   + "' bytes exceeds size in header of '"
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
index 12a1c66..20985fa 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
@@ -753,6 +753,9 @@
      */
     @Override
     public void write(byte[] b, int offset, int length) throws IOException {
+        if (entry == null) {
+            throw new IllegalStateException("No current entry");
+        }
         ZipUtil.checkRequestedFeatures(entry.entry);
         entry.hasWritten = true;
         if (entry.entry.getMethod() == DEFLATED) {