Repository: commons-compress
Updated Branches:
  refs/heads/master 12d9048c6 -> 422160005


COMPRESS-327 convenience constructors and more javadocs

patch by Maciej Nowakowski


Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/42216000
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/42216000
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/42216000

Branch: refs/heads/master
Commit: 42216000561c9b19bc258e337738ac125e5e6a06
Parents: 12d9048
Author: Stefan Bodewig <[email protected]>
Authored: Fri Nov 4 16:30:34 2016 +0100
Committer: Stefan Bodewig <[email protected]>
Committed: Fri Nov 4 16:30:34 2016 +0100

----------------------------------------------------------------------
 .../compress/archivers/sevenz/SevenZFile.java   | 15 +++++++++
 .../commons/compress/archivers/zip/ZipFile.java | 17 ++++++++++
 .../utils/SeekableInMemoryByteChannel.java      | 33 ++++++++++++++++++++
 src/site/xdoc/examples.xml                      | 22 ++++++++++++-
 .../archivers/sevenz/SevenZFileTest.java        |  3 +-
 5 files changed, 87 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/42216000/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
----------------------------------------------------------------------
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 c2f7999..e0ee6cd 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
@@ -109,6 +109,21 @@ public class SevenZFile implements Closeable {
      * allows you to read from an in-memory archive.</p>
      *
      * @param channel the channel to read
+     * @throws IOException if reading the archive fails
+     * @since 1.13
+     */
+    public SevenZFile(final SeekableByteChannel channel) throws IOException {
+        this(channel, "unknown archive", null);
+    }
+
+    /**
+     * Reads a SeekableByteChannel as 7z archive
+     *
+     * <p>{@link
+     * org.apache.commons.compress.utils.SeekableInMemoryByteChannel}
+     * allows you to read from an in-memory archive.</p>
+     *
+     * @param channel the channel to read
      * @param password optional password if the archive is encrypted -
      * the byte array is supposed to be the UTF16-LE encoded
      * representation of the password.

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/42216000/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java 
b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
index cff0f4b..5bc46e7 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
@@ -220,6 +220,23 @@ public class ZipFile implements Closeable {
     }
 
     /**
+     * Opens the given channel for reading, assuming "UTF8" for file names.
+     *
+     * <p>{@link
+     * org.apache.commons.compress.utils.SeekableInMemoryByteChannel}
+     * allows you to read from an in-memory archive.</p>
+     *
+     * @param channel the archive.
+     *
+     * @throws IOException if an error occurs while reading the file.
+     * @since 1.13
+     */
+    public ZipFile(final SeekableByteChannel channel)
+            throws IOException {
+        this(channel, "unknown archive", ZipEncodingHelper.UTF8, true);
+    }
+
+    /**
      * Opens the given channel for reading, assuming the specified
      * encoding for file names.
      *

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/42216000/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java
 
b/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java
index 4bf2d7f..2057f17 100644
--- 
a/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java
+++ 
b/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java
@@ -27,6 +27,11 @@ import java.util.concurrent.atomic.AtomicBoolean;
 
 /**
  * A {@link SeekableByteChannel} implementation that wraps a byte[].
+ *
+ * <p>When this channel is used for writing an internal buffer grows to 
accommodate
+ * incoming data. A natural size limit is the value of {@link 
Integer#MAX_VALUE}.
+ * Internal buffer can be accessed via {@link 
SeekableInMemoryByteChannel#array()}.</p>
+ *
  * @since 1.13
  * @NotThreadSafe
  */
@@ -36,15 +41,37 @@ public class SeekableInMemoryByteChannel implements 
SeekableByteChannel {
     private final AtomicBoolean closed = new AtomicBoolean();
     private int position, size;
 
+    /**
+     * Constructor taking a byte array.
+     *
+     * <p>This constructor is intended to be used with pre-allocated buffer or 
when
+     * reading from a given byte array.</p>
+     *
+     * @param data input data or pre-allocated array.
+     */
     public SeekableInMemoryByteChannel(byte[] data) {
         this.data = data;
         size = data.length;
     }
 
+    /**
+     * Parameterless constructor - allocates internal buffer by itself.
+     */
     public SeekableInMemoryByteChannel() {
         this(new byte[0]);
     }
 
+    /**
+     * Constructor taking a size of storage to be allocated.
+     *
+     * <p>Creates a channel and allocates internal storage of a given size.</p>
+     *
+     * @param size size of internal buffer to allocate, in bytes.
+     */
+    public SeekableInMemoryByteChannel(int size) {
+        this(new byte[size]);
+    }
+
     @Override
     public long position() {
         return position;
@@ -116,6 +143,12 @@ public class SeekableInMemoryByteChannel implements 
SeekableByteChannel {
 
     /**
      * Obtains the array backing this channel.
+     *
+     * <p>NOTE:
+     * The returned buffer is not aligned with containing data, use
+     * {@link #size()} to obtain the size of data stored in the buffer.</p>
+     *
+     * @return internal byte array.
      */
     public byte[] array() {
         return data;

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/42216000/src/site/xdoc/examples.xml
----------------------------------------------------------------------
diff --git a/src/site/xdoc/examples.xml b/src/site/xdoc/examples.xml
index 6b22a1a..38ebe28 100644
--- a/src/site/xdoc/examples.xml
+++ b/src/site/xdoc/examples.xml
@@ -291,6 +291,17 @@ try {
 }
 ]]></source>
 
+          <p>Reading entries from an in-memory zip archive using
+              <code>SeekableInMemoryByteChannel</code> and 
<code>ZipFile</code> class:</p>
+<source><![CDATA[
+byte[] inputData; // zip archive contents
+SeekableInMemoryByteChannel inMemoryByteChannel = new 
SeekableInMemoryByteChannel(inputData);
+ZipFile zipFile = new ZipFile(inMemoryByteChannel);
+ZipArchiveEntry archiveEntry = zipFile.getEntry("entryName");
+InputStream inputStream = zipFile.getInputStream(archiveEntry);
+inputStream.read() // read data from the input stream
+]]></source>
+
           <p>Creating a zip file with multiple threads:</p>
 
           A simple implementation to create a zip file might look like this:
@@ -580,7 +591,7 @@ defIn.close();
         Commons Compress compared to the native 7z executable.</p>
 
         <p>Reading or writing requires a
-        <code>SeekableByteChannel</code> that will be obtain
+        <code>SeekableByteChannel</code> that will be obtained
         transparently when reading from or writing to a file. The
         class
         
<code>org.apache.commons.compress.utils.SeekableInMemoryByteChannel</code>
@@ -606,6 +617,15 @@ LOOP UNTIL entry.getSize() HAS BEEN READ {
     sevenZFile.read(content, offset, content.length - offset);
 }
 ]]></source>
+
+          <p>Uncompressing a given in-memory 7z archive:</p>
+          <source><![CDATA[
+byte[] inputData; // 7z archive contents
+SeekableInMemoryByteChannel inMemoryByteChannel = new 
SeekableInMemoryByteChannel(inputData);
+SevenZFile sevenZFile = new SevenZFile(inMemoryByteChannel);
+SevenZArchiveEntry entry = sevenZFile.getNextEntry();
+sevenZFile.read();  // read current entry's data
+]]></source>
       </subsection>
 
       <subsection name="arj">

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/42216000/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
----------------------------------------------------------------------
diff --git 
a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
 
b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
index 01efce7..c9c596f 100644
--- 
a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
+++ 
b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
@@ -248,8 +248,7 @@ public class SevenZFileTest extends AbstractTestCase {
         try (FileInputStream fis = new FileInputStream(getFile("bla.7z"))) {
             data = IOUtils.toByteArray(fis);
         }
-        try (SevenZFile sevenZFile = new SevenZFile(new 
SeekableInMemoryByteChannel(data),
-                                                    null)) {
+        try (SevenZFile sevenZFile = new SevenZFile(new 
SeekableInMemoryByteChannel(data))) {
             final Iterable<SevenZArchiveEntry> entries = 
sevenZFile.getEntries();
             final Iterator<SevenZArchiveEntry> iter = entries.iterator();
             SevenZArchiveEntry entry = iter.next();

Reply via email to