flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cd...@apache.org
Subject [07/15] git commit: [flex-utilities] [refs/heads/feature/flash-downloader] - Fixed SonarQube findings ...
Date Tue, 18 Apr 2017 12:15:13 GMT
Fixed SonarQube findings ...


Project: http://git-wip-us.apache.org/repos/asf/flex-utilities/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-utilities/commit/790b3556
Tree: http://git-wip-us.apache.org/repos/asf/flex-utilities/tree/790b3556
Diff: http://git-wip-us.apache.org/repos/asf/flex-utilities/diff/790b3556

Branch: refs/heads/feature/flash-downloader
Commit: 790b355688dc0ada8c62cb1df91faf67f52126ea
Parents: 4463329
Author: Christofer Dutz <christofer.dutz@codecentric.de>
Authored: Sun Dec 18 15:18:01 2016 +0100
Committer: Christofer Dutz <christofer.dutz@codecentric.de>
Committed: Sun Dec 18 15:18:01 2016 +0100

----------------------------------------------------------------------
 .../utilities/converter/air/AirConverter.java   |  10 +-
 .../flex/utilities/converter/BaseConverter.java |  39 ++--
 .../converter/retrievers/BaseRetriever.java     |  41 +++--
 .../retrievers/download/DownloadRetriever.java  | 176 ++++++++++++-------
 4 files changed, 174 insertions(+), 92 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-utilities/blob/790b3556/flex-maven-tools/flex-sdk-converter/converters/air/src/main/java/org/apache/flex/utilities/converter/air/AirConverter.java
----------------------------------------------------------------------
diff --git a/flex-maven-tools/flex-sdk-converter/converters/air/src/main/java/org/apache/flex/utilities/converter/air/AirConverter.java
b/flex-maven-tools/flex-sdk-converter/converters/air/src/main/java/org/apache/flex/utilities/converter/air/AirConverter.java
index 7e9fadf..46373c4 100644
--- a/flex-maven-tools/flex-sdk-converter/converters/air/src/main/java/org/apache/flex/utilities/converter/air/AirConverter.java
+++ b/flex-maven-tools/flex-sdk-converter/converters/air/src/main/java/org/apache/flex/utilities/converter/air/AirConverter.java
@@ -358,8 +358,9 @@ public class AirConverter extends BaseConverter implements Converter {
         }
 
         DataInputStream in = null;
+        FileInputStream descriptorInputStream = null;
         try {
-            final FileInputStream descriptorInputStream = new FileInputStream(sdkDescriptor);
+            descriptorInputStream = new FileInputStream(sdkDescriptor);
             in = new DataInputStream(descriptorInputStream);
             final BufferedReader br = new BufferedReader(new InputStreamReader(in));
             final String strLine = br.readLine();
@@ -374,6 +375,13 @@ public class AirConverter extends BaseConverter implements Converter
{
                     // Ignore.
                 }
             }
+            if (descriptorInputStream != null) {
+                try {
+                    descriptorInputStream.close();
+                } catch (IOException e) {
+                    // Ignore.
+                }
+            }
         }
     }
 

http://git-wip-us.apache.org/repos/asf/flex-utilities/blob/790b3556/flex-maven-tools/flex-sdk-converter/converters/base/src/main/java/org/apache/flex/utilities/converter/BaseConverter.java
----------------------------------------------------------------------
diff --git a/flex-maven-tools/flex-sdk-converter/converters/base/src/main/java/org/apache/flex/utilities/converter/BaseConverter.java
b/flex-maven-tools/flex-sdk-converter/converters/base/src/main/java/org/apache/flex/utilities/converter/BaseConverter.java
index 1d92181..d3171fb 100644
--- a/flex-maven-tools/flex-sdk-converter/converters/base/src/main/java/org/apache/flex/utilities/converter/BaseConverter.java
+++ b/flex-maven-tools/flex-sdk-converter/converters/base/src/main/java/org/apache/flex/utilities/converter/BaseConverter.java
@@ -95,10 +95,11 @@ public abstract class BaseConverter {
     protected String calculateChecksum(File jarFile) throws ConverterException {
         // Implement the calculation of checksums for a given jar.
         final MessageDigest digest;
+        InputStream is = null;
         try {
             digest = MessageDigest.getInstance("SHA-1");
 
-            final InputStream is = new FileInputStream(jarFile);
+            is = new FileInputStream(jarFile);
             final byte[] buffer = new byte[8192];
             int read;
             try {
@@ -110,21 +111,22 @@ public abstract class BaseConverter {
                 return bigInt.toString(16);
             }
             catch(IOException e) {
-                throw new RuntimeException("Unable to process file for MD5", e);
-            }
-            finally {
-                try {
-                    is.close();
-                }
-                catch(IOException e) {
-                    //noinspection ThrowFromFinallyBlock
-                    throw new RuntimeException("Unable to close input stream for MD5 calculation",
e);
-                }
+                throw new ConverterException("Unable to process file for MD5", e);
             }
         } catch (NoSuchAlgorithmException e) {
             throw new ConverterException("Error calculating checksum of file '" + jarFile.getPath()
+ "'", e);
         } catch (FileNotFoundException e) {
             throw new ConverterException("Error calculating checksum of file '" + jarFile.getPath()
+ "'", e);
+        } finally {
+            try {
+                if(is != null) {
+                    is.close();
+                }
+            }
+            catch(IOException e) {
+                //noinspection ThrowFromFinallyBlock
+                throw new ConverterException("Unable to close input stream for MD5 calculation",
e);
+            }
         }
     }
 
@@ -142,10 +144,17 @@ public abstract class BaseConverter {
             } else {
                 connection = queryUrl.openConnection();
             }
-            final ReadableByteChannel rbc = Channels.newChannel(connection.getInputStream());
-            final ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
-            if(rbc.read(byteBuffer) > 0) {
-                output = new String (byteBuffer.array(), "UTF-8");
+            ReadableByteChannel rbc = null;
+            try {
+                rbc = Channels.newChannel(connection.getInputStream());
+                final ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
+                if (rbc.read(byteBuffer) > 0) {
+                    output = new String(byteBuffer.array(), "UTF-8");
+                }
+            } finally {
+                if(rbc != null) {
+                    rbc.close();
+                }
             }
         } catch (MalformedURLException e) {
             throw new ConverterException("Error querying maven central.", e);

http://git-wip-us.apache.org/repos/asf/flex-utilities/blob/790b3556/flex-maven-tools/flex-sdk-converter/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/BaseRetriever.java
----------------------------------------------------------------------
diff --git a/flex-maven-tools/flex-sdk-converter/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/BaseRetriever.java
b/flex-maven-tools/flex-sdk-converter/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/BaseRetriever.java
index 514ed2e..b9e3bcd 100644
--- a/flex-maven-tools/flex-sdk-converter/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/BaseRetriever.java
+++ b/flex-maven-tools/flex-sdk-converter/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/BaseRetriever.java
@@ -45,9 +45,11 @@ public abstract class BaseRetriever implements Retriever {
 
         ArchiveInputStream archiveInputStream = null;
         ArchiveEntry entry;
+        FileInputStream fileInputStream = null;
         try {
 
-            final CountingInputStream inputStream = new CountingInputStream(new FileInputStream(inputArchive));
+            fileInputStream = new FileInputStream(inputArchive);
+            final CountingInputStream inputStream = new CountingInputStream(fileInputStream);
 
             final long inputFileSize = inputArchive.length();
 
@@ -76,20 +78,28 @@ public abstract class BaseRetriever implements Retriever {
                 // Entry is a file.
                 else {
                     final byte[] data = new byte[BUFFER_MAX];
-                    final FileOutputStream fos = new FileOutputStream(outputFile);
-                    BufferedOutputStream dest = null;
+                    FileOutputStream fos = null;
                     try {
-                        dest = new BufferedOutputStream(fos, BUFFER_MAX);
+                        fos = new FileOutputStream(outputFile);
 
-                        int count;
-                        while ((count = archiveInputStream.read(data, 0, BUFFER_MAX)) !=
-1) {
-                            dest.write(data, 0, count);
-                            progressBar.updateProgress(inputStream.getBytesRead());
+                        BufferedOutputStream dest = null;
+                        try {
+                            dest = new BufferedOutputStream(fos, BUFFER_MAX);
+
+                            int count;
+                            while ((count = archiveInputStream.read(data, 0, BUFFER_MAX))
!= -1) {
+                                dest.write(data, 0, count);
+                                progressBar.updateProgress(inputStream.getBytesRead());
+                            }
+                        } finally {
+                            if (dest != null) {
+                                dest.flush();
+                                dest.close();
+                            }
                         }
                     } finally {
-                        if(dest != null) {
-                            dest.flush();
-                            dest.close();
+                        if(fos != null) {
+                            fos.close();
                         }
                     }
                 }
@@ -106,7 +116,14 @@ public abstract class BaseRetriever implements Retriever {
             if(archiveInputStream != null) {
                 try {
                     archiveInputStream.close();
-                } catch(Exception e) {
+                } catch(IOException e) {
+                    // Ignore...
+                }
+            }
+            if(fileInputStream != null) {
+                try {
+                    fileInputStream.close();
+                } catch(IOException e) {
                     // Ignore...
                 }
             }

http://git-wip-us.apache.org/repos/asf/flex-utilities/blob/790b3556/flex-maven-tools/flex-sdk-converter/retrievers/download/src/main/java/org/apache/flex/utilities/converter/retrievers/download/DownloadRetriever.java
----------------------------------------------------------------------
diff --git a/flex-maven-tools/flex-sdk-converter/retrievers/download/src/main/java/org/apache/flex/utilities/converter/retrievers/download/DownloadRetriever.java
b/flex-maven-tools/flex-sdk-converter/retrievers/download/src/main/java/org/apache/flex/utilities/converter/retrievers/download/DownloadRetriever.java
index 6843d17..f80c7f2 100644
--- a/flex-maven-tools/flex-sdk-converter/retrievers/download/src/main/java/org/apache/flex/utilities/converter/retrievers/download/DownloadRetriever.java
+++ b/flex-maven-tools/flex-sdk-converter/retrievers/download/src/main/java/org/apache/flex/utilities/converter/retrievers/download/DownloadRetriever.java
@@ -27,9 +27,9 @@ import org.apache.flex.utilities.converter.retrievers.utils.ProgressBar;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpResponse;
-import org.apache.http.client.HttpClient;
 import org.apache.http.client.config.RequestConfig;
 import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
 import org.w3c.dom.Document;
@@ -189,29 +189,47 @@ public class DownloadRetriever extends BaseRetriever {
         } else {
             connection = sourceUrl.openConnection();
         }
-        final ReadableByteChannel rbc = Channels.newChannel(connection.getInputStream());
-        final FileOutputStream fos = new FileOutputStream(targetFile);
+        ReadableByteChannel rbc = null;
+        FileOutputStream fos = null;
+        try {
+            rbc = Channels.newChannel(connection.getInputStream());
+            fos = new FileOutputStream(targetFile);
 
-        ////////////////////////////////////////////////////////////////////////////////
-        // Do the downloading.
-        ////////////////////////////////////////////////////////////////////////////////
+                ////////////////////////////////////////////////////////////////////////////////
+                // Do the downloading.
+                ////////////////////////////////////////////////////////////////////////////////
 
-        final long expectedSize = connection.getContentLength();
-        long transferedSize = 0L;
+            final long expectedSize = connection.getContentLength();
+            long transferedSize = 0L;
 
-        System.out.println("===========================================================");
-        System.out.println("Downloading " + sourceUrl.toString());
-        if(expectedSize > 1014 * 1024) {
-            System.out.println("Expected size: " + (expectedSize / 1024 / 1024) + "MB");
-        } else {
-            System.out.println("Expected size: " + (expectedSize / 1024 ) + "KB");
-        }
-        final ProgressBar progressBar = new ProgressBar(expectedSize);
-        while (transferedSize < expectedSize) {
-            transferedSize += fos.getChannel().transferFrom(rbc, transferedSize, 1 <<
20);
-            progressBar.updateProgress(transferedSize);
+            System.out.println("===========================================================");
+            System.out.println("Downloading " + sourceUrl.toString());
+            if (expectedSize > 1014 * 1024) {
+                System.out.println("Expected size: " + (expectedSize / 1024 / 1024) + "MB");
+            } else {
+                System.out.println("Expected size: " + (expectedSize / 1024) + "KB");
+            }
+            final ProgressBar progressBar = new ProgressBar(expectedSize);
+            while (transferedSize < expectedSize) {
+                transferedSize += fos.getChannel().transferFrom(rbc, transferedSize, 1 <<
20);
+                progressBar.updateProgress(transferedSize);
+            }
+        } finally {
+            if(rbc != null) {
+                try {
+                    rbc.close();
+                } catch (IOException e) {
+                    // Ignore ...
+                }
+            }
+            if(fos != null) {
+                try {
+                    fos.close();
+                } catch (IOException e) {
+                    // Ignore ...
+                }
+            }
         }
-        fos.close();
         System.out.println();
         System.out.println("Finished downloading.");
         System.out.println("===========================================================");
@@ -227,59 +245,89 @@ public class DownloadRetriever extends BaseRetriever {
             config = RequestConfig.DEFAULT;
         }
 
-        HttpGet httpget = new HttpGet(sourceUri);
-        httpget.setConfig(config);
-        HttpClient httpclient = HttpClients.createDefault();
-        HttpResponse response = httpclient.execute(httpget);
-
-        String reasonPhrase = response.getStatusLine().getReasonPhrase();
-        int statusCode = response.getStatusLine().getStatusCode();
-        System.out.println(String.format("statusCode: %d", statusCode));
-        System.out.println(String.format("reasonPhrase: %s", reasonPhrase));
-
-        HttpEntity entity = response.getEntity();
-        InputStream content = entity.getContent();
+        CloseableHttpClient httpclient = null;
+        try {
+            HttpGet httpget = new HttpGet(sourceUri);
+            httpget.setConfig(config);
+            httpclient = HttpClients.createDefault();
+            HttpResponse response = httpclient.execute(httpget);
 
-        final ReadableByteChannel rbc = Channels.newChannel(content);
-        final FileOutputStream fos = new FileOutputStream(targetFile);
+            String reasonPhrase = response.getStatusLine().getReasonPhrase();
+            int statusCode = response.getStatusLine().getStatusCode();
+            System.out.println(String.format("statusCode: %d", statusCode));
+            System.out.println(String.format("reasonPhrase: %s", reasonPhrase));
 
-        ////////////////////////////////////////////////////////////////////////////////
-        // Do the downloading.
-        ////////////////////////////////////////////////////////////////////////////////
+            HttpEntity entity = response.getEntity();
+            InputStream content = entity.getContent();
 
-        final long expectedSize = entity.getContentLength();
-        System.out.println("===========================================================");
-        System.out.println("Downloading " + sourceUri.toString());
-        if(expectedSize <= 0) {
+            ReadableByteChannel rbc = null;
+            FileOutputStream fos = null;
             try {
-                System.out.println("Unknown size.");
-                IOUtils.copy(content, fos);
+                rbc = Channels.newChannel(content);
+                fos = new FileOutputStream(targetFile);
+
+            ////////////////////////////////////////////////////////////////////////////////
+            // Do the downloading.
+            ////////////////////////////////////////////////////////////////////////////////
+
+                final long expectedSize = entity.getContentLength();
+                System.out.println("===========================================================");
+                System.out.println("Downloading " + sourceUri.toString());
+                if (expectedSize <= 0) {
+                    try {
+                        System.out.println("Unknown size.");
+                        IOUtils.copy(content, fos);
+                    } finally {
+                        // close http network connection
+                        content.close();
+                    }
+                } else {
+                    if (expectedSize > 1014 * 1024) {
+                        System.out.println("Expected size: " + (expectedSize / 1024 / 1024)
+ "MB");
+                    } else {
+                        System.out.println("Expected size: " + (expectedSize / 1024) + "KB");
+                    }
+                    final ProgressBar progressBar = new ProgressBar(expectedSize);
+                    long transferredSize = 0L;
+                    while ((expectedSize == 0) || (transferredSize < expectedSize)) {
+                        // Transfer about 1MB in each iteration.
+                        long currentSize = fos.getChannel().transferFrom(rbc, transferredSize,
MEGABYTE);
+                        if (currentSize < MEGABYTE) {
+                            break;
+                        }
+                        transferredSize += currentSize;
+                        progressBar.updateProgress(transferredSize);
+                    }
+                    fos.close();
+                    System.out.println();
+                }
+                System.out.println("Finished downloading.");
+                System.out.println("===========================================================");
             } finally {
-                // close http network connection
-                content.close();
-            }
-        } else {
-            if (expectedSize > 1014 * 1024) {
-                System.out.println("Expected size: " + (expectedSize / 1024 / 1024) + "MB");
-            } else {
-                System.out.println("Expected size: " + (expectedSize / 1024) + "KB");
+                if(rbc != null) {
+                    try {
+                        rbc.close();
+                    } catch (IOException e) {
+                        // Ignore ...
+                    }
+                }
+                if(fos != null) {
+                    try {
+                        fos.close();
+                    } catch (IOException e) {
+                        // Ignore ...
+                    }
+                }
             }
-            final ProgressBar progressBar = new ProgressBar(expectedSize);
-            long transferredSize = 0L;
-            while ((expectedSize == 0) || (transferredSize < expectedSize)) {
-                // Transfer about 1MB in each iteration.
-                long currentSize = fos.getChannel().transferFrom(rbc, transferredSize, MEGABYTE);
-                if(currentSize < MEGABYTE) {
-                    break;
+        } finally {
+            if(httpclient != null) {
+                try {
+                    httpclient.close();
+                } catch(IOException e) {
+                    // Ignore ...
                 }
-                transferredSize += currentSize;
-                progressBar.updateProgress(transferredSize);
             }
-            fos.close();
-            System.out.println();
         }
-        System.out.println("Finished downloading.");
-        System.out.println("===========================================================");
     }
 
     protected String getBinaryUrl(SdkType sdkType, String version, PlatformType platformType)


Mime
View raw message