You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2018/04/29 18:37:59 UTC

commons-compress git commit: COMPRESS-118 extract factory methods from FileToArchiveSink

Repository: commons-compress
Updated Branches:
  refs/heads/master 9a84b4eec -> b15221d6f


COMPRESS-118 extract factory methods from FileToArchiveSink


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

Branch: refs/heads/master
Commit: b15221d6f8478c936542c2c1a4cc1b6475c8a903
Parents: 9a84b4e
Author: Stefan Bodewig <bo...@apache.org>
Authored: Sun Apr 29 20:37:33 2018 +0200
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Sun Apr 29 20:37:33 2018 +0200

----------------------------------------------------------------------
 .../compress/archivers/examples/ArchiveCli.java |  2 +-
 .../archivers/examples/ArchiveSinks.java        | 87 ++++++++++++++++++++
 .../archivers/examples/FileToArchiveSink.java   | 57 -------------
 3 files changed, 88 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/b15221d6/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveCli.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveCli.java b/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveCli.java
index 7df4a82..cb20460 100644
--- a/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveCli.java
+++ b/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveCli.java
@@ -35,7 +35,7 @@ public class ArchiveCli {
             System.err.println("Usage: ArchiveCli dir format target");
             System.exit(1);
         }
-        try (Sink<File> sink = FileToArchiveSink.forFile(args[1], new File(args[2]))) {
+        try (Sink<File> sink = ArchiveSinks.forFile(args[1], new File(args[2]))) {
             Archive.directory(new File(args[0]))
                 .to(sink);
         }

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/b15221d6/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveSinks.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveSinks.java b/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveSinks.java
new file mode 100644
index 0000000..56c940d
--- /dev/null
+++ b/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveSinks.java
@@ -0,0 +1,87 @@
+/*
+ * 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.examples;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.FileOutputStream;
+import java.nio.channels.Channels;
+import java.nio.channels.FileChannel;
+import java.nio.channels.SeekableByteChannel;
+import java.nio.file.StandardOpenOption;
+import org.apache.commons.compress.archivers.ArchiveException;
+import org.apache.commons.compress.archivers.ArchiveOutputStream;
+import org.apache.commons.compress.archivers.ArchiveStreamFactory;
+import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
+
+/**
+ * Supplies factory methods for file sinks that write to archives,
+ * @since 1.17
+ */
+public class ArchiveSinks {
+    /**
+     * Wraps an ArchiveOutputStream.
+     */
+    public static Sink<File> forStream(ArchiveOutputStream os) {
+        return new FileToArchiveSink(os);
+    }
+
+    /**
+     * Uses {@link ArchiveFactory#createArchiveOutputStream}.
+     *
+     * <p>Will not support 7z.</p>
+     */
+    public static Sink<File> forStream(String format, OutputStream os) throws IOException, ArchiveException {
+        return new FileToArchiveSink(new ArchiveStreamFactory().createArchiveOutputStream(format, os));
+    }
+
+    /**
+     * Uses {@link ArchiveFactory#createArchiveOutputStream} unless
+     * special handling for ZIP or 7z is required.
+     */
+    public static Sink<File> forFile(String format, File target) throws IOException, ArchiveException {
+        if (prefersSeekableByteChannel(format)) {
+            return forChannel(format, FileChannel.open(target.toPath(), StandardOpenOption.WRITE,
+                StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING));
+        }
+        return new FileToArchiveSink(new ArchiveStreamFactory()
+            .createArchiveOutputStream(format, new FileOutputStream(target)));
+    }
+
+    /**
+     * Uses {@link ArchiveFactory#createArchiveOutputStream} unless
+     * special handling for ZIP or 7z is required.
+     */
+    public static Sink<File> forChannel(String format, SeekableByteChannel c) throws IOException, ArchiveException {
+        if (!prefersSeekableByteChannel(format)) {
+            return forStream(format, Channels.newOutputStream(c));
+        } else if (ArchiveStreamFactory.ZIP.equalsIgnoreCase(format)) {
+            return forStream(new ZipArchiveOutputStream(c));
+        } else if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) {
+            return new SevenZOutputFileSink(c);
+        } else {
+            throw new ArchiveException("don't know how to handle format " + format);
+        }
+    }
+
+    private static boolean prefersSeekableByteChannel(String format) {
+        return ArchiveStreamFactory.ZIP.equalsIgnoreCase(format) || ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format);
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/b15221d6/src/main/java/org/apache/commons/compress/archivers/examples/FileToArchiveSink.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/FileToArchiveSink.java b/src/main/java/org/apache/commons/compress/archivers/examples/FileToArchiveSink.java
index 5afdb75..c2c1f2b 100644
--- a/src/main/java/org/apache/commons/compress/archivers/examples/FileToArchiveSink.java
+++ b/src/main/java/org/apache/commons/compress/archivers/examples/FileToArchiveSink.java
@@ -22,17 +22,9 @@ import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.FileOutputStream;
-import java.nio.channels.Channels;
-import java.nio.channels.FileChannel;
-import java.nio.channels.SeekableByteChannel;
-import java.nio.file.StandardOpenOption;
 import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.archivers.ArchiveException;
 import org.apache.commons.compress.archivers.ArchiveOutputStream;
-import org.apache.commons.compress.archivers.ArchiveStreamFactory;
-import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
 import org.apache.commons.compress.utils.IOUtils;
 
 /**
@@ -71,53 +63,4 @@ public class FileToArchiveSink extends Sink<File> {
         os.close();
     }
 
-    /**
-     * Wraps an ArchiveOutputStream.
-     */
-    public static Sink<File> forStream(ArchiveOutputStream os) {
-        return new FileToArchiveSink(os);
-    }
-
-    /**
-     * Uses {@link ArchiveFactory#createArchiveOutputStream}.
-     *
-     * <p>Will not support 7z.</p>
-     */
-    public static Sink<File> forStream(String format, OutputStream os) throws IOException, ArchiveException {
-        return new FileToArchiveSink(new ArchiveStreamFactory().createArchiveOutputStream(format, os));
-    }
-
-    /**
-     * Uses {@link ArchiveFactory#createArchiveOutputStream} unless
-     * special handling for ZIP or 7z is required.
-     */
-    public static Sink<File> forFile(String format, File target) throws IOException, ArchiveException {
-        if (prefersSeekableByteChannel(format)) {
-            return forChannel(format, FileChannel.open(target.toPath(), StandardOpenOption.WRITE,
-                StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING));
-        }
-        return new FileToArchiveSink(new ArchiveStreamFactory()
-            .createArchiveOutputStream(format, new FileOutputStream(target)));
-    }
-
-    /**
-     * Uses {@link ArchiveFactory#createArchiveOutputStream} unless
-     * special handling for ZIP or 7z is required.
-     */
-    public static Sink<File> forChannel(String format, SeekableByteChannel c) throws IOException, ArchiveException {
-        if (!prefersSeekableByteChannel(format)) {
-            return forStream(format, Channels.newOutputStream(c));
-        } else if (ArchiveStreamFactory.ZIP.equalsIgnoreCase(format)) {
-            return forStream(new ZipArchiveOutputStream(c));
-        } else if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) {
-            return new SevenZOutputFileSink(c);
-        } else {
-            throw new ArchiveException("don't know how to handle format " + format);
-        }
-    }
-
-    private static boolean prefersSeekableByteChannel(String format) {
-        return ArchiveStreamFactory.ZIP.equalsIgnoreCase(format) || ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format);
-    }
-
 }