Fix some warnings
authorNiki Roo <niki@nikiroo.be>
Wed, 24 Jan 2018 13:10:25 +0000 (14:10 +0100)
committerNiki Roo <niki@nikiroo.be>
Wed, 24 Jan 2018 13:10:25 +0000 (14:10 +0100)
src/be/nikiroo/utils/Cache.java
src/be/nikiroo/utils/Downloader.java
src/be/nikiroo/utils/IOUtils.java
src/be/nikiroo/utils/Image.java
src/be/nikiroo/utils/Progress.java
src/be/nikiroo/utils/serial/SerialUtils.java
src/be/nikiroo/utils/serial/server/ConnectAction.java
src/be/nikiroo/utils/serial/server/Server.java
src/be/nikiroo/utils/test/TestCase.java
src/be/nikiroo/utils/test/TestLauncher.java

index bba88bb4b48f7e970039002393a72a47d50081cf..13a50ea92219663a379eb58d7956237217a3f49a 100644 (file)
@@ -47,8 +47,8 @@ public class Cache {
        public Cache(File dir, int hoursChanging, int hoursStable)
                        throws IOException {
                this.dir = dir;
-               this.tooOldChanging = 1000 * 60 * 60 * hoursChanging;
-               this.tooOldStable = 1000 * 60 * 60 * hoursStable;
+               this.tooOldChanging = 1000L * 60 * 60 * hoursChanging;
+               this.tooOldStable = 1000L * 60 * 60 * hoursStable;
 
                if (dir != null && !dir.exists()) {
                        dir.mkdirs();
@@ -174,16 +174,19 @@ public class Cache {
         */
        private int clean(boolean onlyOld, File cacheDir) {
                int num = 0;
-               for (File file : cacheDir.listFiles()) {
-                       if (file.isDirectory()) {
-                               num += clean(onlyOld, file);
-                       } else {
-                               if (!onlyOld || isOld(file, true)) {
-                                       if (file.delete()) {
-                                               num++;
-                                       } else {
-                                               tracer.error("Cannot delete temporary file: "
-                                                               + file.getAbsolutePath());
+               File[] files = cacheDir.listFiles();
+               if (files != null) {
+                       for (File file : files) {
+                               if (file.isDirectory()) {
+                                       num += clean(onlyOld, file);
+                               } else {
+                                       if (!onlyOld || isOld(file, true)) {
+                                               if (file.delete()) {
+                                                       num++;
+                                               } else {
+                                                       tracer.error("Cannot delete temporary file: "
+                                                                       + file.getAbsolutePath());
+                                               }
                                        }
                                }
                        }
index e01ec1dcc24cb3abccc28bb3618b62edf0ad7a54..1b82755067986b20d584c821b0d6c73fca3cf2ad 100644 (file)
@@ -186,12 +186,16 @@ public class Downloader {
                        }
 
                        if (requestData != null) {
-                               OutputStreamWriter writer = new OutputStreamWriter(
-                                               conn.getOutputStream());
-
-                               writer.write(requestData.toString());
-                               writer.flush();
-                               writer.close();
+                               OutputStreamWriter writer = null;
+                               try {
+                                       writer = new OutputStreamWriter(conn.getOutputStream());
+                                       writer.write(requestData.toString());
+                                       writer.flush();
+                               } finally {
+                                       if (writer != null) {
+                                               writer.close();
+                                       }
+                               }
                        }
                }
 
index bf0686babca9e7addfbbd810a7a8e4d5fd66756d..d83311449c3bb5016ae35b396284f56822786ee0 100644 (file)
@@ -89,8 +89,12 @@ public class IOUtils {
                                }
                                zip.putNextEntry(new ZipEntry(base + "/"));
                        }
-                       for (File file : target.listFiles()) {
-                               zip(zip, base, file, false);
+
+                       File[] files = target.listFiles();
+                       if (files != null) {
+                               for (File file : files) {
+                                       zip(zip, base, file, false);
+                               }
                        }
                } else {
                        if (base == null || base.isEmpty()) {
@@ -234,13 +238,13 @@ public class IOUtils {
                        throws IOException {
                List<File> list = deltree(target, null);
                if (exception && !list.isEmpty()) {
-                       String slist = "";
+                       StringBuilder slist = new StringBuilder();
                        for (File file : list) {
-                               slist += "\n" + file.getPath();
+                               slist.append("\n").append(file.getPath());
                        }
 
                        throw new IOException("Cannot delete all the files from: <" //
-                                       + target + ">:" + slist);
+                                       + target + ">:" + slist.toString());
                }
 
                return list.isEmpty();
index 3a89d29d4887e0121107871af39d660c4ca041c3..4eacdac8f0b15ae08bb09767a0145032080d9965 100644 (file)
@@ -100,7 +100,7 @@ public class Image implements Closeable {
         * @return the Base64 representation
         */
        public String toBase64() {
-               return new String(Base64.encodeBytes(getData()));
+               return Base64.encodeBytes(getData());
        }
 
        /**
index 6f05110e8715305b438258b45cf1674b992a4451..ce88f3d8f8c55941bccc094d58b2fd42954be59c 100644 (file)
@@ -397,7 +397,7 @@ public class Progress {
                if (weight < min || weight > max) {
                        throw new RuntimeException(String.format(
                                        "Progress object %s cannot have a weight of %f, "
-                                                       + "it is outside of its parent (%s) range (%f)",
+                                                       + "it is outside of its parent (%s) range (%d)",
                                        progress.name, weight, name, max));
                }
 
@@ -408,6 +408,8 @@ public class Progress {
                                        name, progress.parent.name));
                }
 
+               progress.parent = this;
+
                progress.addProgressListener(new ProgressListener() {
                        @Override
                        public void progress(Progress pg, String name) {
index 3308a756c578bd14c3f5bc758cd31e30164afcaa..fe62d283cdceb48afc530ce239dde40ac774f06e 100644 (file)
@@ -522,6 +522,6 @@ public class SerialUtils {
                        }
                }
 
-               return builder.substring(1, builder.length() - 1).toString();
+               return builder.substring(1, builder.length() - 1);
        }
 }
index dfdb53ed017da95df65d80f5b8111fc5e0fdc153..97243d567a2ed9f39dfcaddbf8deeaee51bac575 100644 (file)
@@ -133,9 +133,11 @@ abstract class ConnectAction {
                                        }
                                } finally {
                                        out.close();
+                                       out = null;
                                }
                        } finally {
                                in.close();
+                               in = null;
                        }
                } catch (Exception e) {
                        onError(e);
index 2022469a0296387f90bc5aca40005d4b2981849c..afceaf95323ec349825372c858b65fc95f185761 100644 (file)
@@ -293,13 +293,13 @@ abstract class Server implements Runnable {
                                        }
                                }
                        }
+               }
 
-                       // only return when stopped
-                       while (started || exiting) {
-                               try {
-                                       Thread.sleep(10);
-                               } catch (InterruptedException e) {
-                               }
+               // return only when stopped
+               while (started || exiting) {
+                       try {
+                               Thread.sleep(10);
+                       } catch (InterruptedException e) {
                        }
                }
        }
@@ -357,7 +357,10 @@ abstract class Server implements Runnable {
                Socket s;
                if (ssl) {
                        s = SSLSocketFactory.getDefault().createSocket(host, port);
-                       ((SSLSocket) s).setEnabledCipherSuites(ANON_CIPHERS);
+                       if (s instanceof SSLSocket) {
+                               // Should always be the case
+                               ((SSLSocket) s).setEnabledCipherSuites(ANON_CIPHERS);
+                       }
                } else {
                        s = new Socket(host, port);
                }
@@ -388,7 +391,10 @@ abstract class Server implements Runnable {
                ServerSocket ss;
                if (ssl) {
                        ss = SSLServerSocketFactory.getDefault().createServerSocket(port);
-                       ((SSLServerSocket) ss).setEnabledCipherSuites(ANON_CIPHERS);
+                       if (ss instanceof SSLServerSocket) {
+                               // Should always be the case
+                               ((SSLServerSocket) ss).setEnabledCipherSuites(ANON_CIPHERS);
+                       }
                } else {
                        ss = new ServerSocket(port);
                }
index 9d0caaa9026ea16530360f57966ccaab584f6b45..a62cb0b79646752fb9b37ffca7c4173dc795cac7 100644 (file)
@@ -151,7 +151,7 @@ abstract public class TestCase {
         *             in case they differ
         */
        public void assertEquals(long expected, long actual) throws AssertException {
-               assertEquals(new Long(expected), new Long(actual));
+               assertEquals(Long.valueOf(expected), Long.valueOf(actual));
        }
 
        /**
@@ -169,7 +169,7 @@ abstract public class TestCase {
         */
        public void assertEquals(String errorMessage, long expected, long actual)
                        throws AssertException {
-               assertEquals(errorMessage, new Long(expected), new Long(actual));
+               assertEquals(errorMessage, Long.valueOf(expected), Long.valueOf(actual));
        }
 
        /**
@@ -185,7 +185,7 @@ abstract public class TestCase {
         */
        public void assertEquals(boolean expected, boolean actual)
                        throws AssertException {
-               assertEquals(new Boolean(expected), new Boolean(actual));
+               assertEquals(Boolean.valueOf(expected), Boolean.valueOf(actual));
        }
 
        /**
@@ -203,7 +203,8 @@ abstract public class TestCase {
         */
        public void assertEquals(String errorMessage, boolean expected,
                        boolean actual) throws AssertException {
-               assertEquals(errorMessage, new Boolean(expected), new Boolean(actual));
+               assertEquals(errorMessage, Boolean.valueOf(expected),
+                               Boolean.valueOf(actual));
        }
 
        /**
@@ -219,7 +220,7 @@ abstract public class TestCase {
         */
        public void assertEquals(double expected, double actual)
                        throws AssertException {
-               assertEquals(new Double(expected), new Double(actual));
+               assertEquals(Double.valueOf(expected), Double.valueOf(actual));
        }
 
        /**
@@ -237,7 +238,8 @@ abstract public class TestCase {
         */
        public void assertEquals(String errorMessage, double expected, double actual)
                        throws AssertException {
-               assertEquals(errorMessage, new Double(expected), new Double(actual));
+               assertEquals(errorMessage, Double.valueOf(expected),
+                               Double.valueOf(actual));
        }
 
        /**
@@ -255,8 +257,8 @@ abstract public class TestCase {
                        throws AssertException {
                if (actual == null) {
                        String defaultReason = String.format("" //
-                                       + "Assertion failed!\n" //
-                                       + "Object should have been NULL: [%s]", actual);
+                                       + "Assertion failed!%n" //
+                                       + "Object should not have been NULL");
 
                        if (errorMessage == null) {
                                throw new AssertException(defaultReason);
@@ -280,8 +282,8 @@ abstract public class TestCase {
         */
        public static String generateAssertMessage(Object expected, Object actual) {
                return String.format("" //
-                               + "Assertion failed!\n" //
-                               + "Expected value: [%s]\n" //
+                               + "Assertion failed!%n" //
+                               + "Expected value: [%s]%n" //
                                + "Actual value: [%s]", expected, actual);
        }
 }
index bee3071d7fce7a8689a1be50492b2ca28f729e68..3741e976a54dada71bb9fa37f3fa7a07f5e5cafa 100644 (file)
@@ -287,11 +287,12 @@ public class TestLauncher {
                name = prefix(depth, false)
                                + (name == null ? "" : name).replace("\t", "    ");
 
-               while (name.length() < columns - 11) {
-                       name += ".";
+               StringBuilder dots = new StringBuilder();
+               while ((name.length() + dots.length()) < columns - 11) {
+                       dots.append('.');
                }
 
-               System.out.print(name);
+               System.out.print(name + dots.toString());
        }
 
        /**