You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by co...@apache.org on 2016/04/15 14:54:10 UTC

cxf git commit: Close more streams

Repository: cxf
Updated Branches:
  refs/heads/master 74b0254f4 -> c94d8b2c3


Close more streams


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

Branch: refs/heads/master
Commit: c94d8b2c30e3899732d322358a0c6faa998a81e3
Parents: 74b0254
Author: Colm O hEigeartaigh <co...@apache.org>
Authored: Fri Apr 15 13:54:01 2016 +0100
Committer: Colm O hEigeartaigh <co...@apache.org>
Committed: Fri Apr 15 13:54:01 2016 +0100

----------------------------------------------------------------------
 .../org/apache/cxf/io/CachedOutputStream.java   |  5 +-
 .../attachment/AttachmentDeserializerTest.java  | 78 +++++++++++++-------
 .../cxf/resource/ClassLoaderResolverTest.java   |  1 +
 .../java/demo/jaxrs/server/BigQueryServer.java  |  9 ++-
 .../transport/https/CertConstraintsTest.java    | 20 ++---
 .../AttachmentProviderXMLClientServerTest.java  |  1 +
 .../AttachmentStreamSourceXMLProvider.java      |  3 +-
 7 files changed, 74 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/c94d8b2c/core/src/main/java/org/apache/cxf/io/CachedOutputStream.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/io/CachedOutputStream.java b/core/src/main/java/org/apache/cxf/io/CachedOutputStream.java
index e71adff..905f6f7 100644
--- a/core/src/main/java/org/apache/cxf/io/CachedOutputStream.java
+++ b/core/src/main/java/org/apache/cxf/io/CachedOutputStream.java
@@ -290,8 +290,9 @@ public class CachedOutputStream extends OutputStream {
             }
         } else {
             // read the file
-            InputStream fin = createInputStream(tempFile);
-            return IOUtils.readBytesFromStream(fin);
+            try (InputStream fin = createInputStream(tempFile)) {
+                return IOUtils.readBytesFromStream(fin);
+            }
         }
     }
 

http://git-wip-us.apache.org/repos/asf/cxf/blob/c94d8b2c/core/src/test/java/org/apache/cxf/attachment/AttachmentDeserializerTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/cxf/attachment/AttachmentDeserializerTest.java b/core/src/test/java/org/apache/cxf/attachment/AttachmentDeserializerTest.java
index 330ec9f..9a9b567 100644
--- a/core/src/test/java/org/apache/cxf/attachment/AttachmentDeserializerTest.java
+++ b/core/src/test/java/org/apache/cxf/attachment/AttachmentDeserializerTest.java
@@ -122,6 +122,7 @@ public class AttachmentDeserializerTest extends Assert {
         }
         assertEquals(0, cidlist.size());
         assertEquals(0, msg.getAttachments().size());
+        is.close();
     }
     
     @Test
@@ -154,9 +155,10 @@ public class AttachmentDeserializerTest extends Assert {
         InputStream attIs = a.getDataHandler().getInputStream();
         
         // check the cached output stream
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        IOUtils.copy(attBody, out);
-        assertTrue(out.toString().startsWith("<env:Envelope"));
+        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+            IOUtils.copy(attBody, out);
+            assertTrue(out.toString().startsWith("<env:Envelope"));
+        }
         
         // try streaming a character off the wire
         assertEquals(255, attIs.read());
@@ -166,6 +168,7 @@ public class AttachmentDeserializerTest extends Assert {
 //        assertNull(invalid.getDataHandler().getInputStream());
 //        
 //        assertTrue(attIs instanceof ByteArrayInputStream);
+        is.close();
     }
 
     @Test
@@ -198,9 +201,10 @@ public class AttachmentDeserializerTest extends Assert {
         InputStream attIs = a.getDataHandler().getInputStream();
 
         // check the cached output stream
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        IOUtils.copy(attBody, out);
-        assertTrue(out.toString().startsWith("<env:Envelope"));
+        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+            IOUtils.copy(attBody, out);
+            assertTrue(out.toString().startsWith("<env:Envelope"));
+        }
 
         // try streaming a character off the wire
         assertEquals(255, attIs.read());
@@ -210,6 +214,7 @@ public class AttachmentDeserializerTest extends Assert {
 //        assertNull(invalid.getDataHandler().getInputStream());
 //
 //        assertTrue(attIs instanceof ByteArrayInputStream);
+        is.close();
     }
     
     @Test
@@ -242,9 +247,10 @@ public class AttachmentDeserializerTest extends Assert {
         InputStream attIs = a.getDataHandler().getInputStream();
 
         // check the cached output stream
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        IOUtils.copy(attBody, out);
-        assertTrue(out.toString().startsWith("<?xml"));
+        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+            IOUtils.copy(attBody, out);
+            assertTrue(out.toString().startsWith("<?xml"));
+        }
         
         // try streaming a character off the wire
         assertTrue(attIs.read() == 'f');
@@ -254,6 +260,8 @@ public class AttachmentDeserializerTest extends Assert {
         assertTrue(attIs.read() == 'a');
         assertTrue(attIs.read() == 'r');
         assertTrue(attIs.read() == -1);
+
+        is.close();
     }
     
     @Test
@@ -284,9 +292,10 @@ public class AttachmentDeserializerTest extends Assert {
         InputStream attIs = a.getDataHandler().getInputStream();
         
         // check the cached output stream
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        IOUtils.copy(attBody, out);
-        assertTrue(out.toString().startsWith("<?xml"));
+        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+            IOUtils.copy(attBody, out);
+            assertTrue(out.toString().startsWith("<?xml"));
+        }
         
         // try streaming a character off the wire
         assertTrue(attIs.read() == 'f');
@@ -298,6 +307,7 @@ public class AttachmentDeserializerTest extends Assert {
         assertTrue(attIs.read() == -1);
         
         assertFalse(itr.hasNext());
+        is.close();
     }
     
     @Test
@@ -332,10 +342,11 @@ public class AttachmentDeserializerTest extends Assert {
         
         assertFalse(itr.hasNext());
         
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        IOUtils.copy(attIs, out);
-        assertTrue(out.size() > 1000);
-
+        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+            IOUtils.copy(attIs, out);
+            assertTrue(out.size() > 1000);
+        }
+        is.close();
     }
     
     
@@ -384,6 +395,9 @@ public class AttachmentDeserializerTest extends Assert {
         InputStream inputStreamWithoutAttachments = message.getContent(InputStream.class);
         SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
         parser.parse(inputStreamWithoutAttachments, new DefaultHandler());
+
+        inputStreamWithoutAttachments.close();
+        rawInputStream.close();
     }
     
     @Test
@@ -412,6 +426,7 @@ public class AttachmentDeserializerTest extends Assert {
         ad.initializeAttachments();
         message.getAttachments().size();
 
+        inputStream.close();
     }
     @Test
     public void testDoesntReturnZero() throws Exception {
@@ -457,20 +472,23 @@ public class AttachmentDeserializerTest extends Assert {
             s = getString(a.getDataHandler().getInputStream());
             assertEquals("ABCD" + count++, s);
         }
+
+        in.close();
     }
     
     private String getString(InputStream ins) throws Exception {
-        ByteArrayOutputStream bout = new ByteArrayOutputStream(100);
-        byte b[] = new byte[100];
-        int i = ins.read(b);
-        while (i > 0) {
-            bout.write(b, 0, i);
-            i = ins.read(b);
-        }
-        if (i == 0) {
-            throw new IOException("Should not be 0");
+        try (ByteArrayOutputStream bout = new ByteArrayOutputStream(100)) {
+            byte b[] = new byte[100];
+            int i = ins.read(b);
+            while (i > 0) {
+                bout.write(b, 0, i);
+                i = ins.read(b);
+            }
+            if (i == 0) {
+                throw new IOException("Should not be 0");
+            }
+            return bout.toString();
         }
-        return bout.toString();
     }
     
     @Test
@@ -510,6 +528,7 @@ public class AttachmentDeserializerTest extends Assert {
                 sz = ins.read(bts, count, bts.length - count);
             }
             assertEquals(x + 1, count);
+            ins.close();
         }
     }
 
@@ -552,6 +571,8 @@ public class AttachmentDeserializerTest extends Assert {
         assertEquals(1024, count);
         assertEquals(225, ins.read(new byte[1000], 500, 500));
         assertEquals(-1, ins.read(new byte[1000], 500, 500));
+
+        ins.close();
     }
 
     @Test
@@ -589,6 +610,8 @@ public class AttachmentDeserializerTest extends Assert {
         assertEquals(500, count);
         assertEquals(-1, ins.read(new byte[1000], 500, 500));
 
+        ins.close();
+
         cid = "1a66bb35-67fc-4e89-9f33-48af417bf9fe-2@apache.org";
         ds = AttachmentUtil.getAttachmentDataSource(cid, message.getAttachments());
         bts = new byte[1024];
@@ -601,6 +624,7 @@ public class AttachmentDeserializerTest extends Assert {
         }
         assertEquals(1249, count);
         assertEquals(-1, ins.read(new byte[1000], 500, 500));
+        ins.close();
     }
     @Test
     public void testCXF3582c() throws Exception {
@@ -636,6 +660,7 @@ public class AttachmentDeserializerTest extends Assert {
         }
         assertEquals(500, count);
         assertEquals(-1, ins.read(new byte[1000], 100, 600));
+        ins.close();
 
         cid = "1a66bb35-67fc-4e89-9f33-48af417bf9fe-2@apache.org";
         ds = AttachmentUtil.getAttachmentDataSource(cid, message.getAttachments());
@@ -649,6 +674,7 @@ public class AttachmentDeserializerTest extends Assert {
         }
         assertEquals(1249, count);
         assertEquals(-1, ins.read(new byte[1000], 100, 600));
+        ins.close();
     }
 }
 

http://git-wip-us.apache.org/repos/asf/cxf/blob/c94d8b2c/core/src/test/java/org/apache/cxf/resource/ClassLoaderResolverTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/cxf/resource/ClassLoaderResolverTest.java b/core/src/test/java/org/apache/cxf/resource/ClassLoaderResolverTest.java
index 642d96f7..0d953de 100644
--- a/core/src/test/java/org/apache/cxf/resource/ClassLoaderResolverTest.java
+++ b/core/src/test/java/org/apache/cxf/resource/ClassLoaderResolverTest.java
@@ -79,6 +79,7 @@ public class ClassLoaderResolverTest extends Assert {
         String content = reader.readLine(); 
 
         assertEquals("resource content incorrect", RESOURCE_DATA, content);
+        reader.close();
     } 
 
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/c94d8b2c/distribution/src/main/release/samples/jax_rs/big_query/src/main/java/demo/jaxrs/server/BigQueryServer.java
----------------------------------------------------------------------
diff --git a/distribution/src/main/release/samples/jax_rs/big_query/src/main/java/demo/jaxrs/server/BigQueryServer.java b/distribution/src/main/release/samples/jax_rs/big_query/src/main/java/demo/jaxrs/server/BigQueryServer.java
index 33b8d46..aba941e 100644
--- a/distribution/src/main/release/samples/jax_rs/big_query/src/main/java/demo/jaxrs/server/BigQueryServer.java
+++ b/distribution/src/main/release/samples/jax_rs/big_query/src/main/java/demo/jaxrs/server/BigQueryServer.java
@@ -100,10 +100,11 @@ public final class BigQueryServer {
     }
 
     private static PrivateKey loadPrivateKey(String p12File, String password) throws Exception {
-        InputStream is = new FileInputStream(p12File);
-        KeyStore store = KeyStore.getInstance("PKCS12");
-        store.load(is, password.toCharArray());
-        return (PrivateKey)store.getKey("privateKey", password.toCharArray());
+        try (InputStream is = new FileInputStream(p12File)) {
+            KeyStore store = KeyStore.getInstance("PKCS12");
+            store.load(is, password.toCharArray());
+            return (PrivateKey)store.getKey("privateKey", password.toCharArray());
+        }
     }
 
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/c94d8b2c/rt/transports/http/src/test/java/org/apache/cxf/transport/https/CertConstraintsTest.java
----------------------------------------------------------------------
diff --git a/rt/transports/http/src/test/java/org/apache/cxf/transport/https/CertConstraintsTest.java b/rt/transports/http/src/test/java/org/apache/cxf/transport/https/CertConstraintsTest.java
index 964e556..6d81eae 100644
--- a/rt/transports/http/src/test/java/org/apache/cxf/transport/https/CertConstraintsTest.java
+++ b/rt/transports/http/src/test/java/org/apache/cxf/transport/https/CertConstraintsTest.java
@@ -121,12 +121,13 @@ public class CertConstraintsTest extends org.junit.Assert {
             FileSystems.getDefault().getPath("src/test/java/org/apache/cxf/transport/https/resources/", 
                                              keystoreFilename);
         byte[] bytes = Files.readAllBytes(path);
-        ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
-        store.load(bin, keystorePassword.toCharArray());
-        for (java.util.Enumeration<String> aliases = store.aliases(); aliases.hasMoreElements();) {
-            final String alias = aliases.nextElement();
-            if (id.equals(alias)) {
-                return (X509Certificate) store.getCertificate(alias);
+        try (ByteArrayInputStream bin = new ByteArrayInputStream(bytes)) {
+            store.load(bin, keystorePassword.toCharArray());
+            for (java.util.Enumeration<String> aliases = store.aliases(); aliases.hasMoreElements();) {
+                final String alias = aliases.nextElement();
+                if (id.equals(alias)) {
+                    return (X509Certificate) store.getCertificate(alias);
+                }
             }
         }
         assert false;
@@ -159,9 +160,10 @@ public class CertConstraintsTest extends org.junit.Assert {
     private static org.w3c.dom.Document loadDocument(
         final String name
     ) throws Exception {
-        final java.io.InputStream inStream = 
-            CertConstraintsTest.class.getResourceAsStream(name);
-        return StaxUtils.read(inStream);
+        try (java.io.InputStream inStream = 
+            CertConstraintsTest.class.getResourceAsStream(name)) {
+            return StaxUtils.read(inStream);
+        }
     }
 
     private static <T> T unmarshal(

http://git-wip-us.apache.org/repos/asf/cxf/blob/c94d8b2c/systests/jaxws/src/test/java/org/apache/cxf/systest/provider/AttachmentProviderXMLClientServerTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxws/src/test/java/org/apache/cxf/systest/provider/AttachmentProviderXMLClientServerTest.java b/systests/jaxws/src/test/java/org/apache/cxf/systest/provider/AttachmentProviderXMLClientServerTest.java
index e5d4e71..7b6e851 100644
--- a/systests/jaxws/src/test/java/org/apache/cxf/systest/provider/AttachmentProviderXMLClientServerTest.java
+++ b/systests/jaxws/src/test/java/org/apache/cxf/systest/provider/AttachmentProviderXMLClientServerTest.java
@@ -67,6 +67,7 @@ public class AttachmentProviderXMLClientServerTest extends AbstractBusClientServ
         assertTrue("wrong content type: " + connection.getContentType(),
                    connection.getContentType().contains("multipart/related"));
         String input = IOUtils.toString(connection.getInputStream());
+        connection.getInputStream().close();
         
         int idx = input.indexOf("--uuid");
         int idx2 = input.indexOf("--uuid", idx + 5);

http://git-wip-us.apache.org/repos/asf/cxf/blob/c94d8b2c/systests/jaxws/src/test/java/org/apache/cxf/systest/provider/AttachmentStreamSourceXMLProvider.java
----------------------------------------------------------------------
diff --git a/systests/jaxws/src/test/java/org/apache/cxf/systest/provider/AttachmentStreamSourceXMLProvider.java b/systests/jaxws/src/test/java/org/apache/cxf/systest/provider/AttachmentStreamSourceXMLProvider.java
index c9dd69d..9e99252 100644
--- a/systests/jaxws/src/test/java/org/apache/cxf/systest/provider/AttachmentStreamSourceXMLProvider.java
+++ b/systests/jaxws/src/test/java/org/apache/cxf/systest/provider/AttachmentStreamSourceXMLProvider.java
@@ -81,9 +81,8 @@ public class AttachmentStreamSourceXMLProvider implements Provider<StreamSource>
                 if (i++ > count) {
                     break;
                 }
-                try {
+                try (ByteArrayOutputStream bous = new ByteArrayOutputStream()) {
                     InputStream is = entry.getValue().getInputStream();
-                    ByteArrayOutputStream bous = new ByteArrayOutputStream();
                     IOUtils.copy(is, bous);
             
                     buf.append("<att contentId=\"" + entry.getKey() + "\">");