cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject cxf git commit: Close some streams
Date Thu, 14 Apr 2016 13:18:36 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 0abb19515 -> 1963e01b4


Close some streams


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/1963e01b
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/1963e01b
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/1963e01b

Branch: refs/heads/master
Commit: 1963e01b4364fd43de43b151338dff63314c8d8d
Parents: 0abb195
Author: Colm O hEigeartaigh <coheigea@apache.org>
Authored: Thu Apr 14 14:18:08 2016 +0100
Committer: Colm O hEigeartaigh <coheigea@apache.org>
Committed: Thu Apr 14 14:18:31 2016 +0100

----------------------------------------------------------------------
 .../java/org/apache/cxf/helpers/IOUtils.java    | 53 ++++++++++----------
 .../transport/servlet/AbstractHTTPServlet.java  |  3 +-
 ...ingConfiguredAutoRewriteSoapAddressTest.java |  5 +-
 .../provider/AbstractSourcePayloadProvider.java | 12 ++---
 .../systest/servlet/AbstractServletTest.java    |  1 +
 5 files changed, 39 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/1963e01b/core/src/main/java/org/apache/cxf/helpers/IOUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/helpers/IOUtils.java b/core/src/main/java/org/apache/cxf/helpers/IOUtils.java
index 82c5535..7c3b828 100644
--- a/core/src/main/java/org/apache/cxf/helpers/IOUtils.java
+++ b/core/src/main/java/org/apache/cxf/helpers/IOUtils.java
@@ -264,15 +264,8 @@ public final class IOUtils {
         if (Transferable.class.isAssignableFrom(inputStream.getClass())) {
             ((Transferable)inputStream).transferTo(destinationFile);
         } else {
-            FileOutputStream fout = new FileOutputStream(destinationFile);
-            try {
+            try (FileOutputStream fout = new FileOutputStream(destinationFile)) {
                 copyAndCloseInput(inputStream, fout);
-            } finally {
-                try {
-                    fout.close();
-                } catch (IOException ex) {
-                    //ignore
-                }
             }
         }
     }
@@ -308,17 +301,20 @@ public final class IOUtils {
 
         StringBuilder buf = new StringBuilder();
         final char[] buffer = new char[bufSize];
-        int n = 0;
-        n = input.read(buffer);
-        while (-1 != n) {
-            if (n == 0) {
-                throw new IOException("0 bytes read in violation of InputStream.read(byte[])");
-            }
-            buf.append(new String(buffer, 0, n));
+        try {
+            int n = 0;
             n = input.read(buffer);
+            while (-1 != n) {
+                if (n == 0) {
+                    throw new IOException("0 bytes read in violation of InputStream.read(byte[])");
+                }
+                buf.append(new String(buffer, 0, n));
+                n = input.read(buffer);
+            }
+            return buf.toString();
+        } finally {
+            input.close();
         }
-        input.close();
-        return buf.toString();
     }
 
     public static String readStringFromStream(InputStream in)
@@ -326,13 +322,14 @@ public final class IOUtils {
 
         StringBuilder sb = new StringBuilder(1024);
 
-        for (int i = in.read(); i != -1; i = in.read()) {
-            sb.append((char) i);
+        try {
+            for (int i = in.read(); i != -1; i = in.read()) {
+                sb.append((char) i);
+            }
+            return sb.toString();
+        } finally {
+            in.close();
         }
-
-        in.close();
-
-        return sb.toString();
     }
 
     /**
@@ -407,9 +404,11 @@ public final class IOUtils {
         if (i < DEFAULT_BUFFER_SIZE) {
             i = DEFAULT_BUFFER_SIZE;
         }
-        ByteArrayOutputStream bos = new ByteArrayOutputStream(i);
-        copy(in, bos);
-        in.close();
-        return bos.toByteArray();
+        try (ByteArrayOutputStream bos = new ByteArrayOutputStream(i)) {
+            copy(in, bos);
+            return bos.toByteArray();
+        } finally {
+            in.close();
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/1963e01b/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
----------------------------------------------------------------------
diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
b/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
index 2b78fe6..d013bdf 100644
--- a/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
+++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
@@ -136,6 +136,7 @@ public abstract class AbstractHTTPServlet extends HttpServlet implements
Filter
                 for (String name : props.stringPropertyNames()) {
                     staticContentTypes.put(name, props.getProperty(name));
                 }
+                is.close();
             } catch (IOException ex) {
                 String message = new org.apache.cxf.common.i18n.Message("STATIC_RESOURCES_MAP_LOAD_FAILURE",
                                                                         BUNDLE).toString();
@@ -353,7 +354,7 @@ public abstract class AbstractHTTPServlet extends HttpServlet implements
Filter
                 response.setHeader("Cache-Control", cacheControl.trim());
             }
             ServletOutputStream os = response.getOutputStream();
-            IOUtils.copy(is, os);
+            IOUtils.copyAndCloseInput(is, os);
             os.flush();
         } catch (IOException ex) {
             throw new ServletException("Static resource " + pathInfo 

http://git-wip-us.apache.org/repos/asf/cxf/blob/1963e01b/systests/jaxws/src/test/java/org/apache/cxf/systest/handlers/AbstractSpringConfiguredAutoRewriteSoapAddressTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxws/src/test/java/org/apache/cxf/systest/handlers/AbstractSpringConfiguredAutoRewriteSoapAddressTest.java
b/systests/jaxws/src/test/java/org/apache/cxf/systest/handlers/AbstractSpringConfiguredAutoRewriteSoapAddressTest.java
index 5e120ff..142d9d2 100644
--- a/systests/jaxws/src/test/java/org/apache/cxf/systest/handlers/AbstractSpringConfiguredAutoRewriteSoapAddressTest.java
+++ b/systests/jaxws/src/test/java/org/apache/cxf/systest/handlers/AbstractSpringConfiguredAutoRewriteSoapAddressTest.java
@@ -19,6 +19,7 @@
 
 package org.apache.cxf.systest.handlers;
 
+import java.io.InputStream;
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.LinkedList;
@@ -39,7 +40,9 @@ public abstract class AbstractSpringConfiguredAutoRewriteSoapAddressTest
extends
     private Document retrieveWsdlDocument(String hostname, String port) throws Exception
{
         URL wsdlUrlLocalhost = new URL("http://" + hostname + ":" + port + "/SpringEndpoint?wsdl");
         URLConnection urlConnection = wsdlUrlLocalhost.openConnection();
-        return StaxUtils.read(urlConnection.getInputStream());
+        try (InputStream input = urlConnection.getInputStream()) {
+            return StaxUtils.read(input);
+        }
     }
 
     protected List<String> findAllServiceUrlsFromWsdl(String hostname, String port)
throws Exception {

http://git-wip-us.apache.org/repos/asf/cxf/blob/1963e01b/systests/jaxws/src/test/java/org/apache/cxf/systest/provider/AbstractSourcePayloadProvider.java
----------------------------------------------------------------------
diff --git a/systests/jaxws/src/test/java/org/apache/cxf/systest/provider/AbstractSourcePayloadProvider.java
b/systests/jaxws/src/test/java/org/apache/cxf/systest/provider/AbstractSourcePayloadProvider.java
index d6842d0..3e0bd25 100644
--- a/systests/jaxws/src/test/java/org/apache/cxf/systest/provider/AbstractSourcePayloadProvider.java
+++ b/systests/jaxws/src/test/java/org/apache/cxf/systest/provider/AbstractSourcePayloadProvider.java
@@ -122,12 +122,12 @@ public abstract class AbstractSourcePayloadProvider implements SourceProvider
{
             Transformer transformer = transformerFactory.newTransformer();
             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
             transformer.setOutputProperty(OutputKeys.METHOD, "xml");
-            Writer out = new StringWriter();
-            StreamResult streamResult = new StreamResult();
-            streamResult.setWriter(out);
-            transformer.transform(s, streamResult);
-            return streamResult.getWriter().toString();
-            
+            try (Writer out = new StringWriter()) {
+                StreamResult streamResult = new StreamResult();
+                streamResult.setWriter(out);
+                transformer.transform(s, streamResult);
+                return streamResult.getWriter().toString();
+            }
         } catch (TransformerException te) {
             if ("javax.xml.transform.stax.StAXSource".equals(s.getClass().getName())) {
                 //on java6, we will get this class if "stax" is configured

http://git-wip-us.apache.org/repos/asf/cxf/blob/1963e01b/systests/transports/src/test/java/org/apache/cxf/systest/servlet/AbstractServletTest.java
----------------------------------------------------------------------
diff --git a/systests/transports/src/test/java/org/apache/cxf/systest/servlet/AbstractServletTest.java
b/systests/transports/src/test/java/org/apache/cxf/systest/servlet/AbstractServletTest.java
index 1ecde66..eaa39fa 100644
--- a/systests/transports/src/test/java/org/apache/cxf/systest/servlet/AbstractServletTest.java
+++ b/systests/transports/src/test/java/org/apache/cxf/systest/servlet/AbstractServletTest.java
@@ -52,6 +52,7 @@ public abstract class AbstractServletTest extends AbstractCXFTest {
         }   
         
         HttpUnitOptions.setExceptionsThrownOnErrorStatus(true);
+        configurationStream.close();
     } 
     @After
     public void tearDown() throws Exception {


Mime
View raw message