move canRead/canWrite up. Remove isSupportedCompressionMethod. COMPRESS-93
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/compress/trunk@911795 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index e69cedc..4a56873 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -24,11 +24,11 @@
<body>
<release version="1.1" date="as in SVN" description="Release 1.1">
<action type="add" date="2010-02-19">
- The Zip*Stream and ZipFile classes now have canRead/Write
+ The Achive*Stream and ZipFile classes now have canRead/Write
methods that can be used to check whether a given entry can be
read/written.
- The method currently returns false if an entry uses an
- unsupported compression method or encryption.
+ The method currently returns false for ZIP archives if an
+ entry uses an unsupported compression method or encryption.
</action>
<action type="add" date="2010-02-19" issue="COMPRESS-89">
The ZIP classes now detect encrypted entries.
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 5f0f805..41f708a 100644
--- a/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java
@@ -96,6 +96,7 @@
* Doesn't increment if the EOF has been hit (read == -1)
*
* @param read the number of bytes read
+ * @since Apache Commons Compress 1.1
*/
protected void count(long read) {
if (read != -1) {
@@ -107,6 +108,7 @@
* Decrements the counter of already read bytes.
*
* @param read the number of bytes pushed back.
+ * @since Apache Commons Compress 1.1
*/
protected void pushedBackBytes(long pushedBack) {
bytesRead -= pushedBack;
@@ -125,8 +127,24 @@
/**
* Returns the current number of bytes read from this stream.
* @return the number of read bytes
+ * @since Apache Commons Compress 1.1
*/
public long getBytesRead() {
return bytesRead;
}
+
+ /**
+ * Whether this stream is able to read the given entry.
+ *
+ * <p>Some archive formats support variants or details that are
+ * not supported (yet).</p>
+ *
+ * <p>This implementation always returns true.
+ *
+ * @since Apache Commons Compress 1.1
+ */
+ public boolean canRead(ArchiveEntry ae) {
+ return true;
+ }
+
}
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 7ba2fed..9e0da6f 100644
--- a/src/main/java/org/apache/commons/compress/archivers/ArchiveOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/ArchiveOutputStream.java
@@ -127,6 +127,7 @@
* Doesn't increment if the EOF has been hit (read == -1)
*
* @param written the number of bytes written
+ * @since Apache Commons Compress 1.1
*/
protected void count(long written) {
if (written != -1) {
@@ -147,8 +148,22 @@
/**
* Returns the current number of bytes written to this stream.
* @return the number of written bytes
+ * @since Apache Commons Compress 1.1
*/
public long getBytesWritten() {
return bytesWritten;
}
+
+ /**
+ * Whether this stream is able to write the given entry.
+ *
+ * <p>Some archive formats support variants or details that are
+ * not supported (yet).</p>
+ *
+ * <p>This implementation always returns true.
+ * @since Apache Commons Compress 1.1
+ */
+ public boolean canWrite(ArchiveEntry ae) {
+ return true;
+ }
}
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
index 85d38d4..a5550f0 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
@@ -148,20 +148,6 @@
}
/**
- * Checks whether the compression method of this entry is supported,
- * i.e. whether the content of this entry can be accessed.
- *
- * @since Commons Compress 1.1
- * @see <a href="https://issues.apache.org/jira/browse/COMPRESS-93"
- * >COMPRESS-93</a>
- * @return <code>true</code> if the compression method is known
- * and supported, <code>false</code> otherwise
- */
- public boolean isSupportedCompressionMethod() {
- return method == STORED || method == DEFLATED;
- }
-
- /**
* Returns the compression method of this entry, or -1 if the
* compression method has not been specified.
*
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
index a8e4318..71580ef 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
@@ -196,10 +196,14 @@
* compression method that hasn't been implemented yet.</p>
* @since Apache Commons Compress 1.1
*/
- public boolean canRead(ZipArchiveEntry ze) {
- return !ze.isEncrypted() &&
- (ze.getMethod() == ZipArchiveEntry.STORED
- || ze.getMethod() == ZipArchiveEntry.DEFLATED);
+ public boolean canRead(ArchiveEntry ae) {
+ if (ae instanceof ZipArchiveEntry) {
+ ZipArchiveEntry ze = (ZipArchiveEntry) ae;
+ return !ze.isEncrypted() &&
+ (ze.getMethod() == ZipArchiveEntry.STORED
+ || ze.getMethod() == ZipArchiveEntry.DEFLATED);
+ }
+ return super.canRead(ae);
}
public int read(byte[] buffer, int start, int length) throws IOException {
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 97062c8..eb74346 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
@@ -511,9 +511,13 @@
* compression method that hasn't been implemented yet.</p>
* @since Apache Commons Compress 1.1
*/
- public boolean canWrite(ZipArchiveEntry ze) {
- return !ze.isEncrypted() &&
- (ze.getMethod() == STORED || ze.getMethod() == DEFLATED);
+ public boolean canWrite(ArchiveEntry ae) {
+ if (ae instanceof ZipArchiveEntry) {
+ ZipArchiveEntry ze = (ZipArchiveEntry) ae;
+ return !ze.isEncrypted() &&
+ (ze.getMethod() == STORED || ze.getMethod() == DEFLATED);
+ }
+ return super.canWrite(ae);
}
/**
diff --git a/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java b/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
index 0e2547e..8fc7ae2 100644
--- a/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
+++ b/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
@@ -122,11 +122,11 @@
*/
public void testSupportedCompressionMethod() throws IOException {
ZipFile bla = new ZipFile(getFile("bla.zip"));
- assertTrue(bla.getEntry("test1.xml").isSupportedCompressionMethod());
+ assertTrue(bla.canRead(bla.getEntry("test1.xml")));
bla.close();
ZipFile moby = new ZipFile(getFile("moby.zip"));
- assertFalse(moby.getEntry("README").isSupportedCompressionMethod());
+ assertFalse(moby.canRead(moby.getEntry("README")));
moby.close();
}
@@ -145,7 +145,7 @@
try {
ZipArchiveEntry entry = zip.getNextZipEntry();
assertEquals("README", entry.getName());
- assertFalse(entry.isSupportedCompressionMethod());
+ assertFalse(zip.canRead(entry));
try {
assertNull(zip.getNextZipEntry());
} catch (IOException e) {
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntryTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntryTest.java
index 1ce7822..43bb72b 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntryTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntryTest.java
@@ -201,22 +201,24 @@
* >COMPRESS-93</a>.
*/
public void testCompressionMethod() {
+ ZipArchiveOutputStream zos =
+ new ZipArchiveOutputStream((java.io.OutputStream) null);
ZipArchiveEntry entry = new ZipArchiveEntry("foo");
assertEquals(-1, entry.getMethod());
- assertFalse(entry.isSupportedCompressionMethod());
+ assertFalse(zos.canWrite(entry));
entry.setMethod(ZipArchiveEntry.STORED);
assertEquals(ZipArchiveEntry.STORED, entry.getMethod());
- assertTrue(entry.isSupportedCompressionMethod());
+ assertTrue(zos.canWrite(entry));
entry.setMethod(ZipArchiveEntry.DEFLATED);
assertEquals(ZipArchiveEntry.DEFLATED, entry.getMethod());
- assertTrue(entry.isSupportedCompressionMethod());
+ assertTrue(zos.canWrite(entry));
// Test the unsupported "imploded" compression method (6)
entry.setMethod(6);
assertEquals(6, entry.getMethod());
- assertFalse(entry.isSupportedCompressionMethod());
+ assertFalse(zos.canWrite(entry));
}
/**