commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1648317 - in /commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip: FileBasedScatterGatherBackingStore.java ScatterGatherBackingStore.java ScatterZipOutputStream.java StreamCompressor.java
Date Mon, 29 Dec 2014 12:24:02 GMT
Author: bodewig
Date: Mon Dec 29 12:24:02 2014
New Revision: 1648317

URL: http://svn.apache.org/r1648317
Log:
extract ZIP-scatter backing store responsibility into an interface

Added:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/FileBasedScatterGatherBackingStore.java
  (with props)
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ScatterGatherBackingStore.java
  (with props)
Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ScatterZipOutputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/StreamCompressor.java

Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/FileBasedScatterGatherBackingStore.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/FileBasedScatterGatherBackingStore.java?rev=1648317&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/FileBasedScatterGatherBackingStore.java
(added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/FileBasedScatterGatherBackingStore.java
Mon Dec 29 12:24:02 2014
@@ -0,0 +1,53 @@
+/*
+ *  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.zip;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * ScatterGatherBackingStore that is backed by a file.
+ *
+ * @since 1.10
+ */
+class FileBasedScatterGatherBackingStore implements ScatterGatherBackingStore {
+    private final File target;
+    private final FileOutputStream os;
+
+    FileBasedScatterGatherBackingStore(File target) throws FileNotFoundException {
+        this.target = target;
+        os = new FileOutputStream(target);
+    }
+
+    public InputStream getInputStream() throws IOException {
+        return new FileInputStream(target);
+    }
+
+    @SuppressWarnings("ResultOfMethodCallIgnored")
+    public void close() throws IOException {
+        os.close();
+    }
+
+    public void writeOut(byte[] data, int offset, int length) throws IOException {
+        os.write(data, offset, length);
+    }
+}

Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/FileBasedScatterGatherBackingStore.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ScatterGatherBackingStore.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ScatterGatherBackingStore.java?rev=1648317&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ScatterGatherBackingStore.java
(added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ScatterGatherBackingStore.java
Mon Dec 29 12:24:02 2014
@@ -0,0 +1,49 @@
+/*
+ *  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.zip;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Abstraction over a scatter-output zip archives can be written to
+ * with a method to gather all content from an InputStream later on.
+ *
+ * @since 1.10
+ */
+interface ScatterGatherBackingStore extends Closeable {
+
+    /**
+     * An input stream that contains the scattered payload
+     *
+     * @return An InputStream, should be closed by the caller of this method.
+     * @throws IOException when something fails
+     */
+    InputStream getInputStream() throws IOException;
+
+    /**
+     * Writes a piece of payload.
+     *
+     * @param data the data to write
+     * @param offset offset inside data to start writing from
+     * @param length the amount of data to write
+     * @throws IOException when something fails
+     */
+    void writeOut(byte[] data, int offset, int length) throws IOException ;
+}

Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ScatterGatherBackingStore.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ScatterZipOutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ScatterZipOutputStream.java?rev=1648317&r1=1648316&r2=1648317&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ScatterZipOutputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ScatterZipOutputStream.java
Mon Dec 29 12:24:02 2014
@@ -44,8 +44,10 @@ import java.util.zip.Deflater;
  *
  * @since 1.10
  */
-public abstract class ScatterZipOutputStream  {
+public class ScatterZipOutputStream  {
     private final Queue<CompressedEntry> items = new ConcurrentLinkedQueue<CompressedEntry>();
+    private final ScatterGatherBackingStore backingStore;
+    private final StreamCompressor streamCompressor;
 
     private static class CompressedEntry {
         final ZipArchiveEntry entry;
@@ -71,6 +73,12 @@ public abstract class ScatterZipOutputSt
         }
     }
 
+    public ScatterZipOutputStream(ScatterGatherBackingStore backingStore,
+                                  StreamCompressor streamCompressor) {
+        this.backingStore = backingStore;
+        this.streamCompressor = streamCompressor;
+    }
+
     /**
      * Add an archive entry to this scatter stream.
      *
@@ -80,10 +88,11 @@ public abstract class ScatterZipOutputSt
      * @throws IOException    If writing fails
      */
     public void addArchiveEntry(ZipArchiveEntry zipArchiveEntry, InputStream payload, int
method) throws IOException {
-        StreamCompressor sc = getStreamCompressor();
-        sc.deflate(payload, method);
+        streamCompressor.deflate(payload, method);
         payload.close();
-        items.add(new CompressedEntry(zipArchiveEntry, sc.getCrc32(), sc.getBytesWritten(),
method, sc.getBytesRead()));
+        items.add(new CompressedEntry(zipArchiveEntry, streamCompressor.getCrc32(),
+                                      streamCompressor.getBytesWritten(), method,
+                                      streamCompressor.getBytesRead()));
     }
 
     /**
@@ -93,8 +102,8 @@ public abstract class ScatterZipOutputSt
      * @throws IOException If writing fails
      */
     public void writeTo(ZipArchiveOutputStream target) throws IOException {
-        closeBackingStorage();
-        InputStream data = getInputStream();
+        backingStore.close();
+        InputStream data = backingStore.getInputStream();
         for (CompressedEntry compressedEntry : items) {
             final BoundedInputStream rawStream = new BoundedInputStream(data, compressedEntry.compressedSize);
             target.addRawArchiveEntry(compressedEntry.transferToArchiveEntry(), rawStream);
@@ -104,30 +113,6 @@ public abstract class ScatterZipOutputSt
     }
 
     /**
-     * Returns a stream compressor that can be used to compress the data.
-     * <p/>
-     * This method is expected to return the same instance every time.
-     *
-     * @return The stream compressor
-     * @throws FileNotFoundException
-     */
-    protected abstract StreamCompressor getStreamCompressor() throws FileNotFoundException;
-
-    /**
-     * An input stream that contains the scattered payload
-     *
-     * @return An InputStream, should be closed by the caller of this method.
-     * @throws IOException when something fails
-     */
-    protected abstract InputStream getInputStream() throws IOException;
-
-
-    /**
-     * Closes whatever storage is backing this scatter stream
-     */
-    protected abstract void closeBackingStorage() throws IOException;
-
-    /**
      * Create a ScatterZipOutputStream with default compression level that is backed by a
file
      *
      * @param file The file to offload compressed data into.
@@ -147,33 +132,8 @@ public abstract class ScatterZipOutputSt
      * @throws FileNotFoundException
      */
     public static ScatterZipOutputStream fileBased(File file, int compressionLevel) throws
FileNotFoundException {
-        return new FileScatterOutputStream(file, compressionLevel);
-    }
-
-    private static class FileScatterOutputStream extends ScatterZipOutputStream {
-        final File target;
-        private StreamCompressor streamDeflater;
-        final FileOutputStream os;
-
-        FileScatterOutputStream(File target, int compressionLevel) throws FileNotFoundException
{
-            this.target = target;
-            os = new FileOutputStream(target);
-            streamDeflater = StreamCompressor.create(compressionLevel, os);
-        }
-
-        @Override
-        protected StreamCompressor getStreamCompressor() throws FileNotFoundException {
-            return streamDeflater;
-        }
-
-        @Override
-        protected InputStream getInputStream() throws IOException {
-            return new FileInputStream(target);
-        }
-
-        @SuppressWarnings("ResultOfMethodCallIgnored")
-        public void closeBackingStorage() throws IOException {
-            os.close();
-        }
+        ScatterGatherBackingStore bs = new FileBasedScatterGatherBackingStore(file);
+        StreamCompressor sc = StreamCompressor.create(compressionLevel, bs);
+        return new ScatterZipOutputStream(bs, sc);
     }
 }

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/StreamCompressor.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/StreamCompressor.java?rev=1648317&r1=1648316&r2=1648317&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/StreamCompressor.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/StreamCompressor.java
Mon Dec 29 12:24:02 2014
@@ -92,6 +92,28 @@ public abstract class StreamCompressor {
     }
 
     /**
+     * Create a stream compressor with the given compression level.
+     *
+     * @param compressionLevel The #Deflater compression level
+     * @param bs The #ScatterGatherBackingStore to receive output
+     * @return A stream compressor
+     */
+    public static StreamCompressor create(int compressionLevel, ScatterGatherBackingStore
bs) {
+        final Deflater deflater = new Deflater(compressionLevel, true);
+        return new ScatterGatherBackingStoreCompressor(deflater, bs);
+    }
+
+    /**
+     * Create a stream compressor with the default compression level.
+     *
+     * @param bs The #ScatterGatherBackingStore to receive output
+     * @return A stream compressor
+     */
+    public static StreamCompressor create( ScatterGatherBackingStore bs) {
+        return create(Deflater.DEFAULT_COMPRESSION, bs);
+    }
+
+    /**
      * The crc32 of the last deflated file
      * @return the crc32
      */
@@ -197,6 +219,20 @@ public abstract class StreamCompressor {
 
     protected abstract void writeOut(byte[] data, int offset, int length) throws IOException
;
 
+    private static final class ScatterGatherBackingStoreCompressor extends StreamCompressor
{
+        private final ScatterGatherBackingStore bs;
+
+        public ScatterGatherBackingStoreCompressor(Deflater deflater, ScatterGatherBackingStore
bs) {
+            super(deflater);
+            this.bs = bs;
+        }
+
+        protected final void writeOut(byte[] data, int offset, int length)
+                throws IOException {
+                bs.writeOut(data, offset, length);
+        }
+    }
+
     private static final class OutputStreamCompressor extends StreamCompressor {
         private final OutputStream os;
 



Mime
View raw message