Author: bodewig
Date: Sat Feb 22 19:19:30 2014
New Revision: 1570885
URL: http://svn.apache.org/r1570885
Log:
COMPRESS-267 throw a specific exception in ArchiveStreamFactory if asked to
read/write 7z - or any other future format we don't support streaming for
Added:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/StreamingNotSupportedException.java
(with props)
Modified:
commons/proper/compress/trunk/src/changes/changes.xml
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java
Modified: commons/proper/compress/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/changes/changes.xml?rev=1570885&r1=1570884&r2=1570885&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/changes/changes.xml (original)
+++ commons/proper/compress/trunk/src/changes/changes.xml Sat Feb 22 19:19:30
2014
@@ -77,6 +77,13 @@ The <action> type attribute can be add,u
backslashes or certain non-ASCII characters in their name
TarArchiveOutputStream could fail.
</action>
+ <action issue="COMPRESS-267" type="fix" date="2014-02-22">
+ ArchiveStreamFactory now throws a StreamingNotSupported - a
+ new subclass of ArchiveException - if it is asked to read from
+ or write to a stream and Commons Compress doesn't support
+ streaming for the format. This currently only applies to the
+ 7z format.
+ </action>
</release>
<release version="1.7" date="2014-01-20"
description="Release 1.7">
Modified:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java?rev=1570885&r1=1570884&r2=1570885&view=diff
==============================================================================
---
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
(original)
+++
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
Sat Feb 22 19:19:30 2014
@@ -31,6 +31,7 @@ import org.apache.commons.compress.archi
import org.apache.commons.compress.archivers.dump.DumpArchiveInputStream;
import org.apache.commons.compress.archivers.jar.JarArchiveInputStream;
import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream;
+import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
@@ -110,6 +111,11 @@ public class ArchiveStreamFactory {
* @since 1.1
*/
public static final String ZIP = "zip";
+ /**
+ * Constant used to identify the 7z archive format.
+ * @since 1.8
+ */
+ public static final String SEVEN_Z = "7z";
/**
* Entry encoding, null for the default.
@@ -144,6 +150,8 @@ public class ArchiveStreamFactory {
* @param in the input stream
* @return the archive input stream
* @throws ArchiveException if the archiver name is not known
+ * @throws StreamingNotSupportedException if the format cannot be
+ * read from a stream
* @throws IllegalArgumentException if the archiver name or stream is null
*/
public ArchiveInputStream createArchiveInputStream(
@@ -199,6 +207,9 @@ public class ArchiveStreamFactory {
return new DumpArchiveInputStream(in);
}
}
+ if (SEVEN_Z.equalsIgnoreCase(archiverName)) {
+ throw new StreamingNotSupportedException(SEVEN_Z);
+ }
throw new ArchiveException("Archiver: " + archiverName + " not
found.");
}
@@ -210,6 +221,8 @@ public class ArchiveStreamFactory {
* @param out the output stream
* @return the archive output stream
* @throws ArchiveException if the archiver name is not known
+ * @throws StreamingNotSupportedException if the format cannot be
+ * written to a stream
* @throws IllegalArgumentException if the archiver name or stream is null
*/
public ArchiveOutputStream createArchiveOutputStream(
@@ -249,6 +262,9 @@ public class ArchiveStreamFactory {
return new CpioArchiveOutputStream(out);
}
}
+ if (SEVEN_Z.equalsIgnoreCase(archiverName)) {
+ throw new StreamingNotSupportedException(SEVEN_Z);
+ }
throw new ArchiveException("Archiver: " + archiverName + " not
found.");
}
@@ -260,6 +276,8 @@ public class ArchiveStreamFactory {
* @param in the input stream
* @return the archive input stream
* @throws ArchiveException if the archiver name is not known
+ * @throws StreamingNotSupportedException if the format cannot be
+ * read from a stream
* @throws IllegalArgumentException if the stream is null or does not
support mark
*/
public ArchiveInputStream createArchiveInputStream(final InputStream in)
@@ -291,6 +309,8 @@ public class ArchiveStreamFactory {
return new CpioArchiveInputStream(in);
} else if (ArjArchiveInputStream.matches(signature,
signatureLength)) {
return new ArjArchiveInputStream(in);
+ } else if (SevenZFile.matches(signature, signatureLength)) {
+ throw new StreamingNotSupportedException(SEVEN_Z);
}
// Dump needs a bigger buffer to check the signature;
Added:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/StreamingNotSupportedException.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/StreamingNotSupportedException.java?rev=1570885&view=auto
==============================================================================
---
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/StreamingNotSupportedException.java
(added)
+++
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/StreamingNotSupportedException.java
Sat Feb 22 19:19:30 2014
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.compress.archivers;
+
+/**
+ * Exception thrown by ArchiveStreamFactory if a format is
+ * requested/detected that doesn't support streaming.
+ * @since 1.8
+ */
+public class StreamingNotSupportedException extends ArchiveException {
+ private final String format;
+
+ /**
+ * Creates a new StreamingNotSupportedException.
+ * @param format the format that has been requested/detected.
+ */
+ public StreamingNotSupportedException(String format) {
+ super("The " + format + " doesn't support streaming.");
+ this.format = format;
+ }
+
+ /**
+ * Returns the format that has been requested/detected.
+ * @return the format that has been requested/detected.
+ */
+ public String getFormat() {
+ return format;
+ }
+
+}
Propchange:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/StreamingNotSupportedException.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java?rev=1570885&r1=1570884&r2=1570885&view=diff
==============================================================================
---
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java
(original)
+++
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java
Sat Feb 22 19:19:30 2014
@@ -18,11 +18,13 @@
*/
package org.apache.commons.compress.archivers;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
@@ -84,6 +86,43 @@ public class ArchiveStreamFactoryTest {
}
}
+ @Test(expected = StreamingNotSupportedException.class)
+ public void cantRead7zFromStream() throws Exception {
+ new ArchiveStreamFactory()
+ .createArchiveInputStream(ArchiveStreamFactory.SEVEN_Z,
+ new ByteArrayInputStream(new byte[0]));
+ }
+
+ @Test(expected = StreamingNotSupportedException.class)
+ public void cantWrite7zToStream() throws Exception {
+ new ArchiveStreamFactory()
+ .createArchiveOutputStream(ArchiveStreamFactory.SEVEN_Z,
+ new ByteArrayOutputStream());
+ }
+
+ /**
+ * Test case for
+ * <a href="https://issues.apache.org/jira/browse/COMPRESS-267"
+ * >COMPRESS-267</a>.
+ */
+ @Test
+ public void detectsAndThrowsFor7z() throws Exception {
+ FileInputStream fis = new FileInputStream("src/test/resources/bla.7z");
+ try {
+ InputStream bis = new BufferedInputStream(fis);
+ try {
+ new ArchiveStreamFactory().createArchiveInputStream(bis);
+ fail("Expected a StreamingNotSupportedException");
+ } catch (StreamingNotSupportedException ex) {
+ assertEquals(ArchiveStreamFactory.SEVEN_Z, ex.getFormat());
+ } finally {
+ bis.close();
+ }
+ } finally {
+ fis.close();
+ }
+ }
+
/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/COMPRESS-208"