Author: [email protected]
Date: Mon Feb  6 18:31:37 2012
New Revision: 2059

Log:
AMDATU-521: some minor last things fixed before closing this issue.

Modified:
   
trunk/amdatu-web/resource/src/main/java/org/amdatu/web/resource/service/ResourceServlet.java

Modified: 
trunk/amdatu-web/resource/src/main/java/org/amdatu/web/resource/service/ResourceServlet.java
==============================================================================
--- 
trunk/amdatu-web/resource/src/main/java/org/amdatu/web/resource/service/ResourceServlet.java
        (original)
+++ 
trunk/amdatu-web/resource/src/main/java/org/amdatu/web/resource/service/ResourceServlet.java
        Mon Feb  6 18:31:37 2012
@@ -15,6 +15,7 @@
  */
 package org.amdatu.web.resource.service;
 
+import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -48,7 +49,7 @@
             try {
                 conn = url.openConnection();
 
-                // convert millisecond precision of getLastModified() to 
second precision used 
+                // convert millisecond precision of getLastModified() to 
second precision used
                 // in HTTP headers before comparing
                 long lastModified = (conn.getLastModified() / 1000) * 1000;
                 if (lastModified != 0) {
@@ -65,12 +66,8 @@
                         copy(is, os);
                     }
                     finally {
-                        if (is != null) {
-                            is.close();
-                        }
-                        if (os != null) {
-                            os.close();
-                        }
+                        safeClose(is);
+                        safeClose(os);
                     }
                 }
                 else {
@@ -82,7 +79,7 @@
                     // AMDATU-432 if the connection is open (conn != null), 
but nothing
                     // has been read (is == null), and the URL points to a 
local file,
                     // it stays open, so explicitly close it.
-                    conn.getInputStream().close();
+                    safeClose(conn.getInputStream());
                 }
             }
         }
@@ -109,16 +106,32 @@
     }
 
     /**
+     * Closes a given {@link Closeable} implementation in "safe" manner, 
meaning that it can
+     * handle null-values and ignores any I/O exceptions that occur during the 
close.
+     * 
+     * @param closeable the {@link Closeable} implementation to close, can be 
<code>null</code>
+     *        in which case this method does nothing.
+     */
+    private static void safeClose(Closeable closeable) {
+        if (closeable != null) {
+            try {
+                closeable.close();
+            }
+            catch (IOException ignore) {
+                // Not relevant; we're trashing the implementation anyway...
+            }
+        }
+    }
+
+    /**
      * Copy data from InputStream to OutputStream.
      */
-    private void copy(InputStream is, OutputStream os) throws IOException {
-        int len = 0;
+    private static void copy(InputStream is, OutputStream os) throws 
IOException {
         byte[] buf = new byte[INTERNAL_BUFFER_SIZE];
         int n;
 
         while ((n = is.read(buf, 0, buf.length)) >= 0) {
             os.write(buf, 0, n);
-            len += n;
         }
     }
 }
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to