From 530d4062471346d6ececf76d74a0358c91323998 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sun, 26 Nov 2017 20:38:04 +0100 Subject: [PATCH] Version 2.2.3: change the trace handler system --- VERSION | 2 +- changelog.md | 5 ++ src/be/nikiroo/utils/Cache.java | 46 +++++++------- src/be/nikiroo/utils/Downloader.java | 44 +++++++------ src/be/nikiroo/utils/IOUtils.java | 20 +++--- src/be/nikiroo/utils/TraceHandler.java | 88 ++++++++++++++++++++++++++ 6 files changed, 153 insertions(+), 52 deletions(-) create mode 100644 src/be/nikiroo/utils/TraceHandler.java diff --git a/VERSION b/VERSION index b1b25a5..5859406 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.2.2 +2.2.3 diff --git a/changelog.md b/changelog.md index fb17b43..ab1be9e 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,10 @@ # nikiroo-utils +## Version 2.2.3 + +- Fix in readSmallStream +- Change traces handling + ## Version 2.2.2 - New method in Cache: manually delete items diff --git a/src/be/nikiroo/utils/Cache.java b/src/be/nikiroo/utils/Cache.java index 1e59054..dcbde74 100644 --- a/src/be/nikiroo/utils/Cache.java +++ b/src/be/nikiroo/utils/Cache.java @@ -19,6 +19,7 @@ public class Cache { private File dir; private long tooOldChanging; private long tooOldStable; + private TraceHandler tracer = new TraceHandler(); /** * Create a new {@link Cache} object. @@ -53,6 +54,25 @@ public class Cache { } } + /** + * The traces handler for this {@link Cache}. + * + * @return the traces handler + */ + public TraceHandler getTraceHandler() { + return tracer; + } + + /** + * The traces handler for this {@link Cache}. + * + * @param tracer + * the new traces handler + */ + public void setTraceHandler(TraceHandler tracer) { + this.tracer = tracer; + } + /** * Check the resource to see if it is in the cache. * @@ -91,24 +111,6 @@ public class Cache { return clean(onlyOld, dir); } - /** - * Trace information (info/error) generated by this class. - *

- * You can override it if you don't want the default sysout/syserr. - * - * @param message - * the message - * @param error - * TRUE for error messages, FALSE for information messages - */ - protected void trace(String message, boolean error) { - if (error) { - System.err.println(message); - } else { - System.out.println(message); - } - } - /** * Clean the cache (delete the cached items) in the given cache directory. * @@ -130,8 +132,8 @@ public class Cache { if (file.delete()) { num++; } else { - trace("Cannot delete temporary file: " - + file.getAbsolutePath(), true); + tracer.error("Cannot delete temporary file: " + + file.getAbsolutePath()); } } } @@ -304,8 +306,8 @@ public class Cache { long time = new Date().getTime() - file.lastModified(); if (time < 0) { - trace("Timestamp in the future for file: " + file.getAbsolutePath(), - true); + tracer.error("Timestamp in the future for file: " + + file.getAbsolutePath()); } return time < 0 || time > max; diff --git a/src/be/nikiroo/utils/Downloader.java b/src/be/nikiroo/utils/Downloader.java index 67fd652..a8a591a 100644 --- a/src/be/nikiroo/utils/Downloader.java +++ b/src/be/nikiroo/utils/Downloader.java @@ -28,6 +28,7 @@ import java.util.zip.GZIPInputStream; public class Downloader { private String UA; private CookieManager cookies; + private TraceHandler tracer = new TraceHandler(); /** * Create a new {@link Downloader}. @@ -46,6 +47,25 @@ public class Downloader { CookieHandler.setDefault(cookies); } + /** + * The traces handler for this {@link Cache}. + * + * @return the traces handler + */ + public TraceHandler getTraceHandler() { + return tracer; + } + + /** + * The traces handler for this {@link Cache}. + * + * @param tracer + * the new traces handler + */ + public void setTraceHandler(TraceHandler tracer) { + this.tracer = tracer; + } + /** * Clear all the cookies currently in the jar. *

@@ -74,6 +94,10 @@ public class Downloader { * * @param url * the {@link URL} to open + * @param currentReferer + * the current referer, for websites that needs this info + * @param cookiesValues + * the cookies * @param postParams * the POST parameters * @param getParams @@ -93,24 +117,6 @@ public class Downloader { getParams, oauth); } - /** - * Trace information (info/error) generated by this class. - *

- * You can override it if you don't want the default sysout/syserr. - * - * @param message - * the message - * @param error - * TRUE for error messages, FALSE for information messages - */ - protected void trace(String message, boolean error) { - if (error) { - System.err.println(message); - } else { - System.out.println(message); - } - } - /** * Open the given {@link URL} and update the cookies. * @@ -134,7 +140,7 @@ public class Downloader { Map postParams, Map getParams, String oauth) throws IOException { - trace("Download: " + url, false); + tracer.trace("Download: " + url); URLConnection conn = openConnectionWithCookies(url, currentReferer, cookiesValues); diff --git a/src/be/nikiroo/utils/IOUtils.java b/src/be/nikiroo/utils/IOUtils.java index 35481b2..973a61b 100644 --- a/src/be/nikiroo/utils/IOUtils.java +++ b/src/be/nikiroo/utils/IOUtils.java @@ -193,19 +193,19 @@ public class IOUtils { * in case of I/O error */ public static String readSmallStream(InputStream stream) throws IOException { + // do NOT close the reader, or the related stream will be closed, too + // reader.close(); BufferedReader reader = new BufferedReader( new InputStreamReader(stream)); - try { - StringBuilder builder = new StringBuilder(); - for (String line = reader.readLine(); line != null; line = reader - .readLine()) { - builder.append(line); - builder.append("\n"); - } - return builder.toString(); - } finally { - reader.close(); + + StringBuilder builder = new StringBuilder(); + for (String line = reader.readLine(); line != null; line = reader + .readLine()) { + builder.append(line); + builder.append("\n"); } + + return builder.toString(); } /** diff --git a/src/be/nikiroo/utils/TraceHandler.java b/src/be/nikiroo/utils/TraceHandler.java new file mode 100644 index 0000000..5fa0843 --- /dev/null +++ b/src/be/nikiroo/utils/TraceHandler.java @@ -0,0 +1,88 @@ +package be.nikiroo.utils; + +/** + * A handler when a trace message is sent or when a recoverable exception was + * caught by the program. + * + * @author niki + */ +public class TraceHandler { + private boolean showErrorDetails; + private boolean showTraces; + + /** + * Show more details (usually equivalent to the value of DEBUG). + * + * @return TRUE or FALSE + */ + public boolean isShowErrorDetails() { + return showErrorDetails; + } + + /** + * Show more details (usually equivalent to the value of DEBUG). + * + * @param showErrorDetails + * TRUE or FALSE + */ + public void setShowErrorDetails(boolean showErrorDetails) { + this.showErrorDetails = showErrorDetails; + } + + /** + * Show DEBUG traces. + * + * @return TRUE or FALSE + */ + public boolean isShowTraces() { + return showTraces; + } + + /** + * Show DEBUG traces. + * + * @param showTraces + * TRUE or FALSE + */ + public void setShowTraces(boolean showTraces) { + this.showTraces = showTraces; + } + + /** + * An exception happened, log it. + * + * @param e + * the exception + */ + public void error(Exception e) { + if (isShowErrorDetails()) { + e.printStackTrace(); + } else { + error(e.getMessage()); + } + } + + /** + * An error happened, log it. + * + * @param message + * the error message + */ + public void error(String message) { + System.err.println(message); + } + + /** + * A trace happened, show it. + *

+ * Will only be effective if {@link TraceHandler#isShowTraces()} is true. + * + * @param message + * the trace message + */ + public void trace(String message) { + if (isShowTraces()) { + System.out.println(message); + } + } +} -- 2.27.0