From 0988831f084e27de9927c1bb29e338e9263bfa42 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Wed, 24 Jan 2018 14:10:25 +0100 Subject: [PATCH] Fix some warnings --- src/be/nikiroo/utils/Cache.java | 27 ++++++++++--------- src/be/nikiroo/utils/Downloader.java | 16 ++++++----- src/be/nikiroo/utils/IOUtils.java | 14 ++++++---- src/be/nikiroo/utils/Image.java | 2 +- src/be/nikiroo/utils/Progress.java | 4 ++- src/be/nikiroo/utils/serial/SerialUtils.java | 2 +- .../utils/serial/server/ConnectAction.java | 2 ++ .../nikiroo/utils/serial/server/Server.java | 22 +++++++++------ src/be/nikiroo/utils/test/TestCase.java | 22 ++++++++------- src/be/nikiroo/utils/test/TestLauncher.java | 7 ++--- 10 files changed, 71 insertions(+), 47 deletions(-) diff --git a/src/be/nikiroo/utils/Cache.java b/src/be/nikiroo/utils/Cache.java index bba88bb4..13a50ea9 100644 --- a/src/be/nikiroo/utils/Cache.java +++ b/src/be/nikiroo/utils/Cache.java @@ -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()); + } } } } diff --git a/src/be/nikiroo/utils/Downloader.java b/src/be/nikiroo/utils/Downloader.java index e01ec1dc..1b827550 100644 --- a/src/be/nikiroo/utils/Downloader.java +++ b/src/be/nikiroo/utils/Downloader.java @@ -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(); + } + } } } diff --git a/src/be/nikiroo/utils/IOUtils.java b/src/be/nikiroo/utils/IOUtils.java index bf0686ba..d8331144 100644 --- a/src/be/nikiroo/utils/IOUtils.java +++ b/src/be/nikiroo/utils/IOUtils.java @@ -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 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(); diff --git a/src/be/nikiroo/utils/Image.java b/src/be/nikiroo/utils/Image.java index 3a89d29d..4eacdac8 100644 --- a/src/be/nikiroo/utils/Image.java +++ b/src/be/nikiroo/utils/Image.java @@ -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()); } /** diff --git a/src/be/nikiroo/utils/Progress.java b/src/be/nikiroo/utils/Progress.java index 6f05110e..ce88f3d8 100644 --- a/src/be/nikiroo/utils/Progress.java +++ b/src/be/nikiroo/utils/Progress.java @@ -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) { diff --git a/src/be/nikiroo/utils/serial/SerialUtils.java b/src/be/nikiroo/utils/serial/SerialUtils.java index 3308a756..fe62d283 100644 --- a/src/be/nikiroo/utils/serial/SerialUtils.java +++ b/src/be/nikiroo/utils/serial/SerialUtils.java @@ -522,6 +522,6 @@ public class SerialUtils { } } - return builder.substring(1, builder.length() - 1).toString(); + return builder.substring(1, builder.length() - 1); } } diff --git a/src/be/nikiroo/utils/serial/server/ConnectAction.java b/src/be/nikiroo/utils/serial/server/ConnectAction.java index dfdb53ed..97243d56 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectAction.java +++ b/src/be/nikiroo/utils/serial/server/ConnectAction.java @@ -133,9 +133,11 @@ abstract class ConnectAction { } } finally { out.close(); + out = null; } } finally { in.close(); + in = null; } } catch (Exception e) { onError(e); diff --git a/src/be/nikiroo/utils/serial/server/Server.java b/src/be/nikiroo/utils/serial/server/Server.java index 2022469a..afceaf95 100644 --- a/src/be/nikiroo/utils/serial/server/Server.java +++ b/src/be/nikiroo/utils/serial/server/Server.java @@ -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); } diff --git a/src/be/nikiroo/utils/test/TestCase.java b/src/be/nikiroo/utils/test/TestCase.java index 9d0caaa9..a62cb0b7 100644 --- a/src/be/nikiroo/utils/test/TestCase.java +++ b/src/be/nikiroo/utils/test/TestCase.java @@ -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); } } diff --git a/src/be/nikiroo/utils/test/TestLauncher.java b/src/be/nikiroo/utils/test/TestLauncher.java index bee3071d..3741e976 100644 --- a/src/be/nikiroo/utils/test/TestLauncher.java +++ b/src/be/nikiroo/utils/test/TestLauncher.java @@ -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()); } /** -- 2.27.0