[Schmitzm-commits] r1968 - in trunk/schmitzm-core/src/main/java/de/schmitzm: io lang

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Wed Apr 25 18:36:22 CEST 2012


Author: mojays
Date: 2012-04-25 18:36:22 +0200 (Wed, 25 Apr 2012)
New Revision: 1968

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
Log:
LangUtil:
- methods to determine elapsed seconds (e.g. during debugging)
IOUtil:
- included copyInputStreamToFile(.) from Apache Commons IO (2.0)
- downloadUrlToFile(.) now uses Apache Commons IO methods (unfortunately no gain of speed and reliability because internally Apache Commons IO uses nearly the same stream copy!)


Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java	2012-04-25 15:45:57 UTC (rev 1967)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java	2012-04-25 16:36:22 UTC (rev 1968)
@@ -581,32 +581,83 @@
         file = createTemporaryFile("java-schmitzm-",null,true);
       if ( proxy == null )
         proxy = Proxy.NO_PROXY;
+
+      LOGGER.debug("Download: "+url+" to "+file);
       
-      OutputStream output = null;
-      InputStream  input = null;
+      // do like FileUtils.copyURLToFile(URL,File,int,int)
+      // from Apache Commons IO 2.0
+      // JUST HANDLE USING PROXY!!
+      // Note: Problem with read timeout exception remains! :-(
+      URLConnection connection = url.openConnection(proxy);
+      connection.setConnectTimeout(timeout);
+      connection.setReadTimeout(timeout);
+      InputStream input = connection.getInputStream();
+      copyInputStreamToFile(input, file);
+
+      
+//      OutputStream output = null;
+//      InputStream  input = null;
+//      long debugStartTime = System.currentTimeMillis();
+//      try {
+//        // Prepare output
+//        output = new FileOutputStream(file);
+//        // Prepare input from URL
+//        URLConnection urlconn = url.openConnection(proxy);
+//        LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After URLConnection.opernConnection(.)");
+//        if ( timeout >= 0 ) {
+//          urlconn.setConnectTimeout(timeout);
+//          urlconn.setReadTimeout(timeout);
+//        }
+//        urlconn.setDoOutput(true);
+//        LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After URLConnection.setDoOutput(true)");
+//        urlconn.connect();
+//        LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After URLConnection.connect()");
+//        input = urlconn.getInputStream();
+//        LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After URLConnection.getInputStream()");
+//        // Read/write line-wise
+//        byte[] buffer = new byte[204800];
+//        int    bufferSize = 0;
+//        while ( (bufferSize = input.read(buffer)) > 0 ) {
+//          LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After InputStream.read(.). BufferSize "+bufferSize);
+//          output.write(buffer, 0, bufferSize);
+//        }
+////        int i;
+////        while ( (i = input.read()) != -1 ) {
+//////          LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After InputStream.read()");
+////          output.write(i);
+////        }
+//        LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After read/write loop");
+//        output.flush();
+//        LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After OutputStream.flush()");
+////        urlconn.dis
+//      } finally {
+//        closeInputStream(input);
+//        closeOutputStream(output);
+//      }
+      
+      return file;
+    }
+
+    /**
+     * Copies the content of an {@link InputStream} to local {@link File}.<br>
+     * Copied 1:1 from FileUtils (Apache Commons IO 2.0).
+     * @param source source stream
+     * @param destination destination file
+     */
+    public static void copyInputStreamToFile(InputStream source, File destination) throws IOException {
       try {
-        // Prepare output
-        output = new FileOutputStream(file);
-        // Prepare input from URL
-        URLConnection urlconn = url.openConnection(proxy);
-        if ( timeout >= 0 )
-          urlconn.setConnectTimeout(timeout);
-        urlconn.setDoOutput(true);
-        urlconn.connect();
-        input = urlconn.getInputStream();
-        // Read/write line-wise
-        byte[] buffer = new byte[2048];
-        int    bufferSize = 0;
-        while ( (bufferSize = input.read(buffer)) > 0 )
-          output.write(buffer, 0, bufferSize);
-        output.flush();
+        FileOutputStream output = FileUtils.openOutputStream(destination);
+        try {
+          IOUtils.copy(source, output);
+          output.close(); // don't swallow close Exception if copy completes normally
+        } finally {
+          IOUtils.closeQuietly(output);
+        }
       } finally {
-        closeInputStream(input);
-        closeOutputStream(output);
+        IOUtils.closeQuietly(source);
       }
-      
-      return file;
     }
+    
 
     /**
 	 * A replacement for File.toURI().toURL().
@@ -1972,6 +2023,8 @@
         try {
           final URL url = new URL("http",addrStr,"");
           final URLConnection conn = url.openConnection(proxy);
+          conn.setConnectTimeout(timeoutMillis);
+          conn.setReadTimeout(timeoutMillis);
           final InputStream input = conn.getInputStream();
           input.close();
           return true;

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2012-04-25 15:45:57 UTC (rev 1967)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2012-04-25 16:36:22 UTC (rev 1968)
@@ -85,6 +85,8 @@
  * @version 1.0
  */
 public class LangUtil {
+    private static NumberFormat DEFAULT_DELAY_FORMAT = new DecimalFormat("0.00");
+  
 	/** Eine Sekunde in Millisekunden. */
 	public static final long SEC_MILLIS = 1000;
 	/** Eine Minute in Millisekunden. */
@@ -494,6 +496,29 @@
 	}
 
 	/**
+	 * Returns the delay (since a start time) in seconds.
+	 * @param startMillis start offset
+	 */
+    public static double getDelaySeconds(long startMillis) {
+      long delayMillis = System.currentTimeMillis() - startMillis;
+      double delaySecs = delayMillis*1.0 / SEC_MILLIS;
+      return delaySecs;
+    }
+
+    /**
+     * Returns the delay (since a start time) in seconds.
+     * @param startMillis start offset
+     * @param format formatter to format the seconds value (if {@code null} a format with
+     *               2 fraction digits is used)
+     */
+    public static String getDelaySeconds(long startMillis, NumberFormat format) {
+      if ( format == null )
+        format = DEFAULT_DELAY_FORMAT;
+      double delaySecs = getDelaySeconds(startMillis);
+      return format.format(delaySecs);
+    }
+
+    /**
 	 * Returns an exception stack as string.
 	 */
 	public static String getStackTraceFromException(Throwable err) {



More information about the Schmitzm-commits mailing list