More stream stuff
Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/78e71151 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/78e71151 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/78e71151 Branch: refs/heads/master-jaxrs-2.1 Commit: 78e71151ab8e834582ce554b35b136c40e8ddefd Parents: a00cdee Author: Colm O hEigeartaigh <[email protected]> Authored: Mon Apr 25 14:48:17 2016 +0100 Committer: Colm O hEigeartaigh <[email protected]> Committed: Mon Apr 25 16:15:09 2016 +0100 ---------------------------------------------------------------------- .../cxf/databinding/source/NodeDataReader.java | 5 ++-- .../handler/logical/LogicalMessageImpl.java | 8 ++--- .../apache/cxf/javascript/fortest/MtoMImpl.java | 3 +- .../netty/server/NettyHttpServerEngineTest.java | 7 +++-- .../MultiplexHttpAddressClientServerTest.java | 14 ++++----- .../apache/cxf/systest/mtom/MtomPolicyTest.java | 8 ++--- .../apache/cxf/systest/mtom/MtomServerTest.java | 31 ++++++++++---------- 7 files changed, 36 insertions(+), 40 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/78e71151/core/src/main/java/org/apache/cxf/databinding/source/NodeDataReader.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/databinding/source/NodeDataReader.java b/core/src/main/java/org/apache/cxf/databinding/source/NodeDataReader.java index aaf36ce..bd5969d 100644 --- a/core/src/main/java/org/apache/cxf/databinding/source/NodeDataReader.java +++ b/core/src/main/java/org/apache/cxf/databinding/source/NodeDataReader.java @@ -55,12 +55,11 @@ public class NodeDataReader implements DataReader<Node> { XMLStreamReader reader = StaxUtils.createXMLStreamReader((Element)input); return new StaxSource(reader); } else if (StreamSource.class.isAssignableFrom(type)) { - try { - CachedOutputStream out = new CachedOutputStream(); + try (CachedOutputStream out = new CachedOutputStream()) { StaxUtils.writeTo(input, out); InputStream is = out.getInputStream(); out.close(); - + return new StreamSource(is); } catch (IOException e) { throw new Fault("COULD_NOT_READ_XML_STREAM", LOG, e); http://git-wip-us.apache.org/repos/asf/cxf/blob/78e71151/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageImpl.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageImpl.java b/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageImpl.java index 835289a..d0c7b1e 100644 --- a/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageImpl.java +++ b/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageImpl.java @@ -144,13 +144,11 @@ public class LogicalMessageImpl implements LogicalMessage { if (message instanceof SoapMessage) { // StreamSource may only be used once, need to make a copy if (obj instanceof StreamSource) { - try { - CachedOutputStream cos = new CachedOutputStream(); + try (CachedOutputStream cos = new CachedOutputStream()) { StaxUtils.copy(obj, cos); obj = new StreamSource(cos.getInputStream()); message.setContent(Source.class, new StreamSource(cos.getInputStream())); - cos.close(); } catch (Exception e) { throw new Fault(e); } @@ -159,14 +157,12 @@ public class LogicalMessageImpl implements LogicalMessage { if (mode == Service.Mode.PAYLOAD) { source = obj; } else { - try { - CachedOutputStream cos = new CachedOutputStream(); + try (CachedOutputStream cos = new CachedOutputStream()) { StaxUtils.copy(obj, cos); InputStream in = cos.getInputStream(); SOAPMessage msg = initSOAPMessage(in); source = new DOMSource(SAAJUtils.getBody(msg).getFirstChild()); in.close(); - cos.close(); } catch (Exception e) { throw new Fault(e); } http://git-wip-us.apache.org/repos/asf/cxf/blob/78e71151/rt/javascript/javascript-tests/src/test/java/org/apache/cxf/javascript/fortest/MtoMImpl.java ---------------------------------------------------------------------- diff --git a/rt/javascript/javascript-tests/src/test/java/org/apache/cxf/javascript/fortest/MtoMImpl.java b/rt/javascript/javascript-tests/src/test/java/org/apache/cxf/javascript/fortest/MtoMImpl.java index cbd1429..4149ff6 100644 --- a/rt/javascript/javascript-tests/src/test/java/org/apache/cxf/javascript/fortest/MtoMImpl.java +++ b/rt/javascript/javascript-tests/src/test/java/org/apache/cxf/javascript/fortest/MtoMImpl.java @@ -46,8 +46,7 @@ public class MtoMImpl implements MtoM { public MtoMImpl() { InputStream someData = getClass().getClassLoader().getResourceAsStream("org/apache/cxf/javascript/cxf-utils.js"); - StringWriter sw = new StringWriter(); - try { + try (StringWriter sw = new StringWriter()) { InputStreamReader isr = new InputStreamReader(someData, "utf-8"); IOUtils.copy(isr, sw, 4096); returnData = sw.toString(); http://git-wip-us.apache.org/repos/asf/cxf/blob/78e71151/rt/transports/http-netty/netty-server/src/test/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngineTest.java ---------------------------------------------------------------------- diff --git a/rt/transports/http-netty/netty-server/src/test/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngineTest.java b/rt/transports/http-netty/netty-server/src/test/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngineTest.java index 1c82680..f03466d 100644 --- a/rt/transports/http-netty/netty-server/src/test/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngineTest.java +++ b/rt/transports/http-netty/netty-server/src/test/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngineTest.java @@ -148,8 +148,9 @@ public class NettyHttpServerEngineTest extends Assert { assertTrue(connection instanceof HttpURLConnection); connection.connect(); InputStream in = connection.getInputStream(); - ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - IOUtils.copy(in, buffer); - return buffer.toString(); + try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) { + IOUtils.copy(in, buffer); + return buffer.toString(); + } } } http://git-wip-us.apache.org/repos/asf/cxf/blob/78e71151/systests/uncategorized/src/test/java/org/apache/cxf/systest/factory_pattern/MultiplexHttpAddressClientServerTest.java ---------------------------------------------------------------------- diff --git a/systests/uncategorized/src/test/java/org/apache/cxf/systest/factory_pattern/MultiplexHttpAddressClientServerTest.java b/systests/uncategorized/src/test/java/org/apache/cxf/systest/factory_pattern/MultiplexHttpAddressClientServerTest.java index d358eb0..62b1e0a 100644 --- a/systests/uncategorized/src/test/java/org/apache/cxf/systest/factory_pattern/MultiplexHttpAddressClientServerTest.java +++ b/systests/uncategorized/src/test/java/org/apache/cxf/systest/factory_pattern/MultiplexHttpAddressClientServerTest.java @@ -167,13 +167,13 @@ public class MultiplexHttpAddressClientServerTest extends AbstractBusClientServe URL url = new URL(address + "?wsdl"); URLConnection urlConn = url.openConnection(); - BufferedReader br = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); - - while (br.ready()) { - String str = br.readLine(); - if (str.contains("soap:address") - && str.contains("location=" + "\"" + address + "\"")) { - return true; + try (BufferedReader br = new BufferedReader(new InputStreamReader(urlConn.getInputStream()))) { + while (br.ready()) { + String str = br.readLine(); + if (str.contains("soap:address") + && str.contains("location=" + "\"" + address + "\"")) { + return true; + } } } return false; http://git-wip-us.apache.org/repos/asf/cxf/blob/78e71151/systests/uncategorized/src/test/java/org/apache/cxf/systest/mtom/MtomPolicyTest.java ---------------------------------------------------------------------- diff --git a/systests/uncategorized/src/test/java/org/apache/cxf/systest/mtom/MtomPolicyTest.java b/systests/uncategorized/src/test/java/org/apache/cxf/systest/mtom/MtomPolicyTest.java index 3d44399..3963b93 100644 --- a/systests/uncategorized/src/test/java/org/apache/cxf/systest/mtom/MtomPolicyTest.java +++ b/systests/uncategorized/src/test/java/org/apache/cxf/systest/mtom/MtomPolicyTest.java @@ -164,10 +164,10 @@ public class MtomPolicyTest extends AbstractBusClientServerTestBase { assertEquals(1, attachments.size()); Attachment inAtt = attachments.iterator().next(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - IOUtils.copy(inAtt.getDataHandler().getInputStream(), out); - out.close(); - assertEquals(27364, out.size()); + try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { + IOUtils.copy(inAtt.getDataHandler().getInputStream(), out); + assertEquals(27364, out.size()); + } } http://git-wip-us.apache.org/repos/asf/cxf/blob/78e71151/systests/uncategorized/src/test/java/org/apache/cxf/systest/mtom/MtomServerTest.java ---------------------------------------------------------------------- diff --git a/systests/uncategorized/src/test/java/org/apache/cxf/systest/mtom/MtomServerTest.java b/systests/uncategorized/src/test/java/org/apache/cxf/systest/mtom/MtomServerTest.java index 4285378..22d0f86 100644 --- a/systests/uncategorized/src/test/java/org/apache/cxf/systest/mtom/MtomServerTest.java +++ b/systests/uncategorized/src/test/java/org/apache/cxf/systest/mtom/MtomServerTest.java @@ -122,10 +122,10 @@ public class MtomServerTest extends AbstractBusClientServerTestBase { assertEquals(1, attachments.size()); Attachment inAtt = attachments.iterator().next(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - IOUtils.copy(inAtt.getDataHandler().getInputStream(), out); - out.close(); - assertEquals(27364, out.size()); + try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { + IOUtils.copy(inAtt.getDataHandler().getInputStream(), out); + assertEquals(27364, out.size()); + } } @Test @@ -168,12 +168,13 @@ public class MtomServerTest extends AbstractBusClientServerTestBase { if (is == null) { throw new RuntimeException("Could not find resource " + "request"); } - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - IOUtils.copy(is, bout); - String s = bout.toString(StandardCharsets.UTF_8.name()); - s = s.replaceAll(":9036/", ":" + PORT2 + "/"); + try (ByteArrayOutputStream bout = new ByteArrayOutputStream()) { + IOUtils.copy(is, bout); + String s = bout.toString(StandardCharsets.UTF_8.name()); + s = s.replaceAll(":9036/", ":" + PORT2 + "/"); - os.write(s.getBytes(StandardCharsets.UTF_8)); + os.write(s.getBytes(StandardCharsets.UTF_8)); + } os.flush(); is.close(); os.close(); @@ -191,12 +192,12 @@ public class MtomServerTest extends AbstractBusClientServerTestBase { assertEquals(1, attachments.size()); Attachment inAtt = attachments.iterator().next(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - IOUtils.copy(inAtt.getDataHandler().getInputStream(), out); - out.close(); - assertTrue("Wrong size: " + out.size() - + "\n" + out.toString(), - out.size() > 970 && out.size() < 1020); + try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { + IOUtils.copy(inAtt.getDataHandler().getInputStream(), out); + assertTrue("Wrong size: " + out.size() + + "\n" + out.toString(), + out.size() > 970 && out.size() < 1020); + } unregisterServStatic("http://localhost:" + PORT2 + "/policy.xsd"); }
