# nikiroo-utils
+## Version 2.2.3
+
+- Fix in readSmallStream
+- Change traces handling
+
## Version 2.2.2
- New method in Cache: manually delete items
private File dir;
private long tooOldChanging;
private long tooOldStable;
+ private TraceHandler tracer = new TraceHandler();
/**
* Create a new {@link Cache} object.
}
}
+ /**
+ * 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.
*
return clean(onlyOld, dir);
}
- /**
- * Trace information (info/error) generated by this class.
- * <p>
- * 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.
*
if (file.delete()) {
num++;
} else {
- trace("Cannot delete temporary file: "
- + file.getAbsolutePath(), true);
+ tracer.error("Cannot delete temporary file: "
+ + file.getAbsolutePath());
}
}
}
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;
public class Downloader {
private String UA;
private CookieManager cookies;
+ private TraceHandler tracer = new TraceHandler();
/**
* Create a new {@link 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.
* <p>
*
* @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
getParams, oauth);
}
- /**
- * Trace information (info/error) generated by this class.
- * <p>
- * 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.
*
Map<String, String> postParams, Map<String, String> getParams,
String oauth) throws IOException {
- trace("Download: " + url, false);
+ tracer.trace("Download: " + url);
URLConnection conn = openConnectionWithCookies(url, currentReferer,
cookiesValues);
* 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();
}
/**
--- /dev/null
+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.
+ * <p>
+ * 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);
+ }
+ }
+}