[java-identity-provider] 01/03: IDP-1667 Show progress when downloading

Rod Widdowson rdw at steadingsoftware.com
Wed Sep 2 09:48:54 UTC 2020


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch main
in repository java-identity-provider.

View the commit online:
http://git.shibboleth.net/view/?p=java-identity-provider.git;a=commit;h=f18cddb35b5f18f7da90e61a1092d24637230aef

commit f18cddb35b5f18f7da90e61a1092d24637230aef
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Tue Sep 1 17:04:15 2020 +0100

    IDP-1667 Show progress when downloading
    
    https://issues.shibboleth.net/jira/browse/IDP-1667
---
 .../installer/ProgressReportingOutputStream.java   | 62 ++++++++++++++++++++++
 .../idp/installer/plugin/impl/PluginInstaller.java |  9 ++--
 .../idp/installer/plugin/PluginCLITest.java        |  8 +--
 3 files changed, 70 insertions(+), 9 deletions(-)

diff --git a/idp-installer/src/main/java/net/shibboleth/idp/installer/ProgressReportingOutputStream.java b/idp-installer/src/main/java/net/shibboleth/idp/installer/ProgressReportingOutputStream.java
new file mode 100644
index 000000000..1518f3e7b
--- /dev/null
+++ b/idp-installer/src/main/java/net/shibboleth/idp/installer/ProgressReportingOutputStream.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.idp.installer;
+
+import java.io.BufferedOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * A version of {@link BufferedOutputStream} which provides some idea of progress.
+ */
+public class ProgressReportingOutputStream extends BufferedOutputStream {
+
+    /** How much to transfer before we note it.*/
+    private static final int PROGRESS_EVERY = 64 * 1024;
+
+    /** How much have we written so far? */
+    private int written;
+
+    /** Constructor.
+     * @param outStream what to bracket.
+     */
+    public ProgressReportingOutputStream(final OutputStream outStream) {
+        super(outStream);
+    }
+
+    /** Constructor.
+     * @param outStream what to bracket.
+     * @param size buffer size
+     */
+    public ProgressReportingOutputStream(final OutputStream outStream, final int size) {
+        super(outStream, size);
+    }
+
+    /** {@inheritDoc} */
+    public void write(final byte[] b, final int off, final int len) throws IOException {
+        super.write(b, off, len);
+        written += len;
+        if (written > PROGRESS_EVERY) {
+            if (System.out != null) {
+                System.out.print('.');
+                System.out.flush();
+            }
+            written -= PROGRESS_EVERY;
+        }
+    }
+}
diff --git a/idp-installer/src/main/java/net/shibboleth/idp/installer/plugin/impl/PluginInstaller.java b/idp-installer/src/main/java/net/shibboleth/idp/installer/plugin/impl/PluginInstaller.java
index 9ca2e14ca..03321f312 100644
--- a/idp-installer/src/main/java/net/shibboleth/idp/installer/plugin/impl/PluginInstaller.java
+++ b/idp-installer/src/main/java/net/shibboleth/idp/installer/plugin/impl/PluginInstaller.java
@@ -18,7 +18,6 @@
 package net.shibboleth.idp.installer.plugin.impl;
 
 import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -63,6 +62,7 @@ import com.google.common.base.Predicates;
 import net.shibboleth.ext.spring.resource.HTTPResource;
 import net.shibboleth.idp.installer.BuildWar;
 import net.shibboleth.idp.installer.InstallerSupport;
+import net.shibboleth.idp.installer.ProgressReportingOutputStream;
 import net.shibboleth.idp.installer.plugin.impl.TrustStore.Signature;
 import net.shibboleth.idp.plugin.PluginDescription;
 import net.shibboleth.utilities.java.support.annotation.constraint.NonnullAfterInit;
@@ -231,7 +231,7 @@ public final class PluginInstaller extends AbstractInitializableComponent implem
                 LOG.debug("Copying from {} to {}", pair.getFirst(), to);
                 final Resource from  = new HTTPResource(httpClient, pair.getFirst());
                 try (final InputStream in = new BufferedInputStream(from.getInputStream());
-                     final OutputStream out =  new BufferedOutputStream(new FileOutputStream(to.toFile()))) {
+                     final OutputStream out =  new ProgressReportingOutputStream(new FileOutputStream(to.toFile()))) {
 
                     in.transferTo(out);
 
@@ -302,7 +302,7 @@ public final class PluginInstaller extends AbstractInitializableComponent implem
                 createParent(to);
                 LOG.debug("Copying from {} to {}", from, to);
                 try (final InputStream in = new BufferedInputStream(new FileInputStream(from.toFile()));
-                     final OutputStream out =  new BufferedOutputStream(new FileOutputStream(to.toFile()))) {
+                     final OutputStream out =  new ProgressReportingOutputStream(new FileOutputStream(to.toFile()))) {
                     in.transferTo(out);
                 }
             } catch (final IOException e) {
@@ -383,8 +383,7 @@ public final class PluginInstaller extends AbstractInitializableComponent implem
         final Path filePath = downloadDirectory.resolve(fileName);
         LOG.info("Downloading from {}", fileResource.getDescription());
         LOG.debug("Downloading to {}", filePath);
-        try (final OutputStream fileOut = new BufferedOutputStream(
-                new FileOutputStream(filePath.toFile()))) {
+        try (final OutputStream fileOut = new ProgressReportingOutputStream(new FileOutputStream(filePath.toFile()))) {
             fileResource.getInputStream().transferTo(fileOut);
         }
     }
diff --git a/idp-installer/src/test/java/net/shibboleth/idp/installer/plugin/PluginCLITest.java b/idp-installer/src/test/java/net/shibboleth/idp/installer/plugin/PluginCLITest.java
index 75990142b..0011b81fb 100644
--- a/idp-installer/src/test/java/net/shibboleth/idp/installer/plugin/PluginCLITest.java
+++ b/idp-installer/src/test/java/net/shibboleth/idp/installer/plugin/PluginCLITest.java
@@ -19,7 +19,6 @@ package net.shibboleth.idp.installer.plugin;
 
 import static org.testng.Assert.assertEquals;
 
-import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -36,6 +35,7 @@ import org.testng.annotations.Test;
 
 import net.shibboleth.ext.spring.cli.AbstractCommandLine;
 import net.shibboleth.ext.spring.resource.HTTPResource;
+import net.shibboleth.idp.installer.ProgressReportingOutputStream;
 import net.shibboleth.idp.installer.plugin.impl.PluginInstaller;
 import net.shibboleth.utilities.java.support.httpclient.HttpClientBuilder;
 
@@ -89,12 +89,12 @@ public class PluginCLITest extends BasePluginTest {
             final HttpClient client = new HttpClientBuilder().buildClient();
             Resource from = new HTTPResource(client, RHINO_DISTRO);
             try (final InputStream in = from.getInputStream(); 
-                 final OutputStream out = new BufferedOutputStream(new FileOutputStream(unpack.resolve("rhino.tar.gz").toFile()))) {
+                 final OutputStream out = new ProgressReportingOutputStream(new FileOutputStream(unpack.resolve("rhino.tar.gz").toFile()))) {
                     in.transferTo(out);
             }
             from = new HTTPResource(client, RHINO_DISTRO + ".asc");
             try (final InputStream in = from.getInputStream(); 
-                    final OutputStream out = new BufferedOutputStream(new FileOutputStream(unpack.resolve("rhino.tar.gz.asc").toFile()))) {
+                    final OutputStream out = new ProgressReportingOutputStream(new FileOutputStream(unpack.resolve("rhino.tar.gz.asc").toFile()))) {
                        in.transferTo(out);
                }
 
@@ -106,7 +106,7 @@ public class PluginCLITest extends BasePluginTest {
             final Path trustStorePath = credentials.resolve("truststore.asc");
             from = new ClassPathResource("credentials/truststore.asc");
             try (final InputStream in = from.getInputStream(); 
-                    final OutputStream out = new BufferedOutputStream(new FileOutputStream(trustStorePath.toFile(), true))) {
+                    final OutputStream out = new ProgressReportingOutputStream(new FileOutputStream(trustStorePath.toFile(), true))) {
                 in.transferTo(out);
             }
             //

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list