cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject cxf git commit: Some try with resources work for core
Date Fri, 20 Feb 2015 16:08:26 GMT
Repository: cxf
Updated Branches:
  refs/heads/master ccb82705c -> cd7095c49


Some try with resources work for core


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

Branch: refs/heads/master
Commit: cd7095c49ab2be4a5667450bf67e5e4be8ec84c1
Parents: ccb8270
Author: Colm O hEigeartaigh <coheigea@apache.org>
Authored: Fri Feb 20 16:07:59 2015 +0000
Committer: Colm O hEigeartaigh <coheigea@apache.org>
Committed: Fri Feb 20 16:07:59 2015 +0000

----------------------------------------------------------------------
 .../main/java/org/apache/cxf/BusFactory.java    |  8 +----
 .../cxf/attachment/AttachmentDeserializer.java  |  8 +----
 .../apache/cxf/common/logging/JDKBugHacks.java  |  5 +--
 .../org/apache/cxf/common/logging/LogUtils.java |  5 +--
 .../cxf/common/util/PropertiesLoaderUtils.java  |  8 +----
 .../apache/cxf/configuration/jsse/SSLUtils.java | 38 ++++----------------
 .../jsse/TLSParameterJaxBUtils.java             | 20 +++--------
 .../databinding/source/XMLStreamDataReader.java |  5 +--
 .../java/org/apache/cxf/helpers/FileUtils.java  |  5 +--
 .../org/apache/cxf/io/CachedOutputStream.java   | 33 +++--------------
 .../java/org/apache/cxf/io/CachedWriter.java    | 31 +++-------------
 .../org/apache/cxf/staxutils/StaxUtils.java     |  5 +--
 12 files changed, 28 insertions(+), 143 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/cd7095c4/core/src/main/java/org/apache/cxf/BusFactory.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/BusFactory.java b/core/src/main/java/org/apache/cxf/BusFactory.java
index 9588c54..1019fcf 100644
--- a/core/src/main/java/org/apache/cxf/BusFactory.java
+++ b/core/src/main/java/org/apache/cxf/BusFactory.java
@@ -378,15 +378,9 @@ public abstract class BusFactory {
             }
 
             if (is != null) {
-                BufferedReader rd = null;
-                try {
-                    rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
+                try (BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF-8")))
{
                     busFactoryClass = rd.readLine();
                     busFactoryCondition = rd.readLine();
-                } finally {
-                    if (rd != null) {
-                        rd.close();
-                    }
                 }
             }
             if (isValidBusFactoryClass(busFactoryClass) 

http://git-wip-us.apache.org/repos/asf/cxf/blob/cd7095c4/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java b/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java
index 38e01ce..c4f99cf 100644
--- a/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java
+++ b/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java
@@ -208,18 +208,12 @@ public class AttachmentDeserializer {
             return;
         }
         loaded.add(input);
-        CachedOutputStream out = null;
         InputStream origIn = input.getInputStream();
-        try {
-            out = new CachedOutputStream();
+        try (CachedOutputStream out = new CachedOutputStream()) {
             AttachmentUtil.setStreamedAttachmentProperties(message, out);
             IOUtils.copy(input, out);
             input.setInputStream(out.getInputStream());
             origIn.close();
-        } finally {
-            if (out != null) {
-                out.close();
-            }
         }
     }
 

http://git-wip-us.apache.org/repos/asf/cxf/blob/cd7095c4/core/src/main/java/org/apache/cxf/common/logging/JDKBugHacks.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/common/logging/JDKBugHacks.java b/core/src/main/java/org/apache/cxf/common/logging/JDKBugHacks.java
index ae1d63c..1ab35c2 100644
--- a/core/src/main/java/org/apache/cxf/common/logging/JDKBugHacks.java
+++ b/core/src/main/java/org/apache/cxf/common/logging/JDKBugHacks.java
@@ -71,14 +71,11 @@ final class JDKBugHacks {
                     ins = ClassLoader.getSystemResourceAsStream("META-INF/cxf/" + key);
                 }
                 if (ins != null) {
-                    BufferedReader din = new BufferedReader(new InputStreamReader(ins));
-                    try {
+                    try (BufferedReader din = new BufferedReader(new InputStreamReader(ins)))
{
                         cname = din.readLine();
                         if (cname != null) {
                             cname = cname.trim();
                         }
-                    } finally {
-                        din.close();
                     }
                 }
             }

http://git-wip-us.apache.org/repos/asf/cxf/blob/cd7095c4/core/src/main/java/org/apache/cxf/common/logging/LogUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/common/logging/LogUtils.java b/core/src/main/java/org/apache/cxf/common/logging/LogUtils.java
index 4248ee1..83faf0f 100644
--- a/core/src/main/java/org/apache/cxf/common/logging/LogUtils.java
+++ b/core/src/main/java/org/apache/cxf/common/logging/LogUtils.java
@@ -87,11 +87,8 @@ public final class LogUtils {
                     ins = ClassLoader.getSystemResourceAsStream("META-INF/cxf/" + KEY);
                 }
                 if (ins != null) {
-                    BufferedReader din = new BufferedReader(new InputStreamReader(ins));
-                    try {
+                    try (BufferedReader din = new BufferedReader(new InputStreamReader(ins)))
{
                         cname = din.readLine();
-                    } finally {
-                        din.close();
                     }
                 }
             }

http://git-wip-us.apache.org/repos/asf/cxf/blob/cd7095c4/core/src/main/java/org/apache/cxf/common/util/PropertiesLoaderUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/common/util/PropertiesLoaderUtils.java b/core/src/main/java/org/apache/cxf/common/util/PropertiesLoaderUtils.java
index e7c91d3..5afb1fd 100644
--- a/core/src/main/java/org/apache/cxf/common/util/PropertiesLoaderUtils.java
+++ b/core/src/main/java/org/apache/cxf/common/util/PropertiesLoaderUtils.java
@@ -75,14 +75,8 @@ public final class PropertiesLoaderUtils {
                 logger.log(level, msg, url.toString());
             }
             
-            InputStream is = null;
-            try {
-                is = url.openStream();
+            try (InputStream is = url.openStream()) {
                 properties.loadFromXML(new BufferedInputStream(is));
-            } finally {
-                if (is != null) {
-                    is.close();
-                }
             }
         }
         return properties;

http://git-wip-us.apache.org/repos/asf/cxf/blob/cd7095c4/core/src/main/java/org/apache/cxf/configuration/jsse/SSLUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/configuration/jsse/SSLUtils.java b/core/src/main/java/org/apache/cxf/configuration/jsse/SSLUtils.java
index 534c256..b656820 100644
--- a/core/src/main/java/org/apache/cxf/configuration/jsse/SSLUtils.java
+++ b/core/src/main/java/org/apache/cxf/configuration/jsse/SSLUtils.java
@@ -108,17 +108,11 @@ public final class SSLUtils {
         KeyStore ks = KeyStore.getInstance(keyStoreType);
         
         if (keyStoreType.equalsIgnoreCase(PKCS12_TYPE)) {
-            DataInputStream dis = null;
             byte[] bytes = null;
-            try {
-                FileInputStream fis = new FileInputStream(keyStoreLocation);
-                dis = new DataInputStream(fis);
+            try (FileInputStream fis = new FileInputStream(keyStoreLocation);
+                DataInputStream dis = new DataInputStream(fis)) {
                 bytes = new byte[dis.available()];
                 dis.readFully(bytes);
-            } finally {
-                if (dis != null) {
-                    dis.close();
-                }
             }
             ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
             
@@ -252,14 +246,8 @@ public final class SSLUtils {
                              new Object[]{trustStoreLocation, e.getMessage()});
             } 
         } else {
-            FileInputStream trustStoreInputStream = null;
-            try {
-                trustStoreInputStream = new FileInputStream(trustStoreLocation);
+            try (FileInputStream trustStoreInputStream = new FileInputStream(trustStoreLocation))
{
                 trustedCertStore.load(trustStoreInputStream, null);
-            } finally {
-                if (trustStoreInputStream != null) {
-                    trustStoreInputStream.close();
-                }
             }
         }
         
@@ -274,10 +262,8 @@ public final class SSLUtils {
         if (fileName == null) {
             return null;
         }
-        FileInputStream in = null;
-        try {
-            in = new FileInputStream(fileName);
-            ByteArrayOutputStream out = new ByteArrayOutputStream();
+        try (FileInputStream in = new FileInputStream(fileName);
+            ByteArrayOutputStream out = new ByteArrayOutputStream()) {
             byte[] buf = new byte[512];
             int i = in.read(buf);
             while (i  > 0) {
@@ -285,10 +271,6 @@ public final class SSLUtils {
                 i = in.read(buf);
             }
             return out.toByteArray();
-        } finally {
-            if (in != null) {
-                in.close();
-            }
         }
     }
 
@@ -296,10 +278,8 @@ public final class SSLUtils {
         if (fileName == null) {
             return null;
         }
-        FileInputStream in = null;
-        try {
-            in = new FileInputStream(fileName);
-            ByteArrayOutputStream out = new ByteArrayOutputStream();
+        try (FileInputStream in = new FileInputStream(fileName);
+            ByteArrayOutputStream out = new ByteArrayOutputStream()) {
             byte[] buf = new byte[512];
             int i = in.read(buf);
         
@@ -308,10 +288,6 @@ public final class SSLUtils {
                 i = in.read(buf);
             }
             return out.toByteArray();
-        } finally {
-            if (in != null) {
-                in.close();
-            }
         }
     }
 

http://git-wip-us.apache.org/repos/asf/cxf/blob/cd7095c4/core/src/main/java/org/apache/cxf/configuration/jsse/TLSParameterJaxBUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/configuration/jsse/TLSParameterJaxBUtils.java
b/core/src/main/java/org/apache/cxf/configuration/jsse/TLSParameterJaxBUtils.java
index f515627..22fc564 100644
--- a/core/src/main/java/org/apache/cxf/configuration/jsse/TLSParameterJaxBUtils.java
+++ b/core/src/main/java/org/apache/cxf/configuration/jsse/TLSParameterJaxBUtils.java
@@ -125,14 +125,8 @@ public final class TLSParameterJaxBUtils {
                     : KeyStore.getInstance(type, provider);
 
         if (kst.isSetFile()) {
-            FileInputStream kstInputStream = null;
-            try {
-                kstInputStream = new FileInputStream(kst.getFile());
+            try (FileInputStream kstInputStream = new FileInputStream(kst.getFile())) {
                 keyStore.load(kstInputStream, password);
-            } finally {
-                if (kstInputStream != null) {
-                    kstInputStream.close();
-                }
             }
         } else if (kst.isSetResource()) {
             final java.io.InputStream is = getResourceAsStream(kst.getResource());
@@ -147,15 +141,9 @@ public final class TLSParameterJaxBUtils {
             keyStore.load(new URL(kst.getUrl()).openStream(), password);
         } else {
             String loc = SSLUtils.getKeystore(null, LOG);
-            InputStream ins = null;
-            try {
-                if (loc != null) {
-                    ins = new FileInputStream(loc);
-                }
-                keyStore.load(ins, password);
-            } finally {
-                if (ins != null) {
-                    ins.close();
+            if (loc != null) {
+                try (InputStream ins = new FileInputStream(loc)) {
+                    keyStore.load(ins, password);
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/cxf/blob/cd7095c4/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataReader.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataReader.java
b/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataReader.java
index 8233ec1..092a841 100644
--- a/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataReader.java
+++ b/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataReader.java
@@ -271,12 +271,9 @@ public class XMLStreamDataReader implements DataReader<XMLStreamReader>
{
     private InputStream getInputStream(XMLStreamReader input) 
         throws XMLStreamException, IOException {
         
-        CachedOutputStream out = new CachedOutputStream();
-        try {
+        try (CachedOutputStream out = new CachedOutputStream()) {
             StaxUtils.copy(input, out);
             return out.getInputStream();
-        } finally {
-            out.close();
         }
     }
     public DOMSource read(XMLStreamReader reader) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/cd7095c4/core/src/main/java/org/apache/cxf/helpers/FileUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/helpers/FileUtils.java b/core/src/main/java/org/apache/cxf/helpers/FileUtils.java
index 82444d1..773b821 100644
--- a/core/src/main/java/org/apache/cxf/helpers/FileUtils.java
+++ b/core/src/main/java/org/apache/cxf/helpers/FileUtils.java
@@ -344,16 +344,13 @@ public final class FileUtils {
         if (!file.exists()) {
             return new ArrayList<String>();
         }
-        BufferedReader reader = new BufferedReader(new FileReader(file));
         List<String> results = new ArrayList<String>();
-        try {
+        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
             String line = reader.readLine();
             while (line != null) {
                 results.add(line);
                 line = reader.readLine();
             }
-        } finally {
-            reader.close();
         }
         return results;
     }

http://git-wip-us.apache.org/repos/asf/cxf/blob/cd7095c4/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 6bb597a..9464b51 100644
--- a/core/src/main/java/org/apache/cxf/io/CachedOutputStream.java
+++ b/core/src/main/java/org/apache/cxf/io/CachedOutputStream.java
@@ -334,11 +334,8 @@ public class CachedOutputStream extends OutputStream {
             }
         } else {
             // read the file
-            InputStream fin = null;
-            Reader reader = null;
-            try {
-                fin = createInputStream(tempFile);
-                reader = new InputStreamReader(fin, charsetName);
+            try (InputStream fin = createInputStream(tempFile);
+                Reader reader = new InputStreamReader(fin, charsetName)) {
                 char bytes[] = new char[1024];
                 long x = reader.read(bytes);
                 while (x != -1) {
@@ -354,13 +351,6 @@ public class CachedOutputStream extends OutputStream {
                         x = reader.read(bytes);
                     }
                 }
-            } finally {
-                if (reader != null) {
-                    reader.close();
-                }
-                if (fin != null) {
-                    fin.close();
-                }
             }
         }
     }
@@ -383,24 +373,14 @@ public class CachedOutputStream extends OutputStream {
             }
         } else {
             // read the file
-            InputStream fin = null;
-            Reader reader = null;
-            try {
-                fin = createInputStream(tempFile);
-                reader = new InputStreamReader(fin, charsetName);
+            try (InputStream fin = createInputStream(tempFile);
+                Reader reader = new InputStreamReader(fin, charsetName)) {
                 char bytes[] = new char[1024];
                 int x = reader.read(bytes);
                 while (x != -1) {
                     out.append(bytes, 0, x);
                     x = reader.read(bytes);
                 }
-            } finally {
-                if (reader != null) {
-                    reader.close();
-                }
-                if (fin != null) {
-                    fin.close();
-                }
             }
         }
     }
@@ -671,11 +651,8 @@ public class CachedOutputStream extends OutputStream {
             if (!transfered) {
                 // Data is in memory, or we failed to rename the file, try copying
                 // the stream instead.
-                FileOutputStream fout = new FileOutputStream(destinationFile);
-                try {
+                try (FileOutputStream fout = new FileOutputStream(destinationFile)) {
                     IOUtils.copyAndCloseInput(this, fout);
-                } finally {
-                    fout.close();
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/cxf/blob/cd7095c4/core/src/main/java/org/apache/cxf/io/CachedWriter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/io/CachedWriter.java b/core/src/main/java/org/apache/cxf/io/CachedWriter.java
index b7693c3..77b2266 100644
--- a/core/src/main/java/org/apache/cxf/io/CachedWriter.java
+++ b/core/src/main/java/org/apache/cxf/io/CachedWriter.java
@@ -304,9 +304,7 @@ public class CachedWriter extends Writer {
             }
         } else {
             // read the file
-            Reader fin = null;
-            try {
-                fin = createInputStreamReader(tempFile);
+            try (Reader fin = createInputStreamReader(tempFile)) {
                 CharArrayWriter out = new CharArrayWriter((int)tempFile.length());
                 char bytes[] = new char[1024];
                 int x = fin.read(bytes);
@@ -315,10 +313,6 @@ public class CachedWriter extends Writer {
                     x = fin.read(bytes);
                 }
                 return out.toCharArray();
-            } finally {
-                if (fin != null) {
-                    fin.close();
-                }
             }
         }
     }
@@ -333,19 +327,13 @@ public class CachedWriter extends Writer {
             }
         } else {
             // read the file
-            Reader fin = null;
-            try {
-                fin = createInputStreamReader(tempFile);
+            try (Reader fin = createInputStreamReader(tempFile)) {
                 char bytes[] = new char[1024];
                 int x = fin.read(bytes);
                 while (x != -1) {
                     out.write(bytes, 0, x);
                     x = fin.read(bytes);
                 }
-            } finally {
-                if (fin != null) {
-                    fin.close();
-                }
             }
         }
     }
@@ -368,9 +356,7 @@ public class CachedWriter extends Writer {
             }
         } else {
             // read the file
-            Reader fin = null;
-            try {
-                fin = createInputStreamReader(tempFile);
+            try (Reader fin = createInputStreamReader(tempFile)) {
                 char bytes[] = new char[1024];
                 long x = fin.read(bytes);
                 while (x != -1) {
@@ -386,10 +372,6 @@ public class CachedWriter extends Writer {
                         x = fin.read(bytes);
                     }
                 }
-            } finally {
-                if (fin != null) {
-                    fin.close();
-                }
             }
         }
     }
@@ -405,18 +387,13 @@ public class CachedWriter extends Writer {
             }
         } else {
             // read the file
-            Reader r = createInputStreamReader(tempFile);
-            try {
+            try (Reader r = createInputStreamReader(tempFile)) {
                 char chars[] = new char[1024];
                 int x = r.read(chars);
                 while (x != -1) {
                     out.append(chars, 0, x);
                     x = r.read(chars);
                 }
-            } finally {
-                if (r != null) {
-                    r.close();
-                }
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/cxf/blob/cd7095c4/core/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/staxutils/StaxUtils.java b/core/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
index 52b2246..78b0e11 100644
--- a/core/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
+++ b/core/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
@@ -1140,11 +1140,8 @@ public final class StaxUtils {
         }
     }
     public static Document read(File is) throws XMLStreamException, IOException {
-        InputStream fin = new FileInputStream(is);
-        try {
+        try (InputStream fin = new FileInputStream(is)) {
             return read(fin);
-        } finally {
-            fin.close();
         }
     }
     public static Document read(InputSource s) throws XMLStreamException {


Mime
View raw message