Improve cache + jdoc, traces
authorNiki Roo <niki@nikiroo.be>
Sun, 26 May 2019 17:54:21 +0000 (19:54 +0200)
committerNiki Roo <niki@nikiroo.be>
Sun, 26 May 2019 17:54:21 +0000 (19:54 +0200)
src/be/nikiroo/utils/Cache.java
src/be/nikiroo/utils/Downloader.java
src/be/nikiroo/utils/StringUtils.java
src/be/nikiroo/utils/android/ImageUtilsAndroid.java
src/be/nikiroo/utils/ui/ImageUtilsAwt.java

index ff2859eb44403062a5186abd78aa0233316a4926..779b194f5cc66f0e706865e360c6a7b992344f9d 100644 (file)
@@ -281,13 +281,15 @@ public class Cache {
         * @param uniqueID
         *            a unique ID used to locate the cached resource
         * 
+        * @return the number of bytes written
+        * 
         * @throws IOException
         *             in case of I/O error
         */
-       public void save(InputStream in, String uniqueID) throws IOException {
+       public long save(InputStream in, String uniqueID) throws IOException {
                File cached = getCached(uniqueID);
                cached.getParentFile().mkdirs();
-               save(in, cached);
+               return save(in, cached);
        }
 
        /**
@@ -298,12 +300,14 @@ public class Cache {
         * @param url
         *            the {@link URL} used to locate the cached resource
         * 
+        * @return the number of bytes written
+        * 
         * @throws IOException
         *             in case of I/O error
         */
-       public void save(InputStream in, URL url) throws IOException {
+       public long save(InputStream in, URL url) throws IOException {
                File cached = getCached(url);
-               save(in, cached);
+               return save(in, cached);
        }
 
        /**
@@ -316,13 +320,16 @@ public class Cache {
         * @param cached
         *            the cached {@link File} to save to
         * 
+        * @return the number of bytes written
+        * 
         * @throws IOException
         *             in case of I/O error
         */
-       private void save(InputStream in, File cached) throws IOException {
+       private long save(InputStream in, File cached) throws IOException {
                // We delete AFTER so not to remove the subdir we will use...
-               IOUtils.write(in, cached);
+               long bytes = IOUtils.write(in, cached);
                clean(true, dir, 10);
+               return bytes;
        }
 
        /**
index f7a5f57f0a7d5cd9020d5b5bdc47389fe2ca0222..68e4dd73e285a941e3193ac6155d846fea2e8316 100644 (file)
@@ -1,5 +1,6 @@
 package be.nikiroo.utils;
 
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStreamWriter;
@@ -288,7 +289,6 @@ public class Downloader {
                                                "application/x-www-form-urlencoded");
                                conn.setRequestProperty("Content-Length",
                                                Integer.toString(requestData.length()));
-                               conn.setRequestProperty("charset", "utf-8");
                        }
 
                        if (oauth != null) {
@@ -316,7 +316,8 @@ public class Downloader {
                conn.connect();
 
                // Check if redirect
-               // BEWARE! POST data cannot be redirected, so it is ignored here
+               // BEWARE! POST data cannot be redirected (some webservers complain) for
+               // HTTP codes 302 and 303
                if (conn instanceof HttpURLConnection) {
                        int repCode = 0;
                        try {
@@ -328,32 +329,48 @@ public class Downloader {
                        if (repCode / 100 == 3) {
                                String newUrl = conn.getHeaderField("Location");
                                return open(new URL(newUrl), originalUrl, currentReferer,
-                                               cookiesValues, null, getParams, oauth, stable);
+                                               cookiesValues, //
+                                               (repCode == 302 || repCode == 303) ? null : postParams, //
+                                               getParams, oauth, stable);
                        }
                }
 
-               InputStream in = conn.getInputStream();
-               if ("gzip".equals(conn.getContentEncoding())) {
-                       in = new GZIPInputStream(in);
-               }
+               try {
+                       InputStream in = conn.getInputStream();
+                       if ("gzip".equals(conn.getContentEncoding())) {
+                               in = new GZIPInputStream(in);
+                       }
 
-               if (in != null && cache != null) {
-                       tracer.trace("Save to cache: " + originalUrl);
-                       try {
+                       if (in == null) {
+                               throw new IOException("No InputStream!");
+                       }
+
+                       if (cache != null) {
+                               String size = conn.getContentLengthLong() < 0 ? "unknown size"
+                                               : StringUtils.formatNumber(conn.getContentLengthLong())
+                                                               + "bytes";
+                               tracer.trace("Save to cache (" + size + "): " + originalUrl);
                                try {
-                                       cache.save(in, originalUrl);
-                               } finally {
-                                       in.close();
+                                       try {
+                                               long bytes = cache.save(in, originalUrl);
+                                               tracer.trace("Saved to cache: "
+                                                               + StringUtils.formatNumber(bytes) + "bytes");
+                                       } finally {
+                                               in.close();
+                                       }
+                                       in = cache.load(originalUrl, true, true);
+                               } catch (IOException e) {
+                                       tracer.error(new IOException(
+                                                       "Cannot save URL to cache, will ignore cache: "
+                                                                       + url, e));
                                }
-                               in = cache.load(originalUrl, true, false);
-                       } catch (IOException e) {
-                               tracer.error(new IOException(
-                                               "Cannot save URL to cache, will ignore cache: " + url,
-                                               e));
                        }
-               }
 
-               return in;
+                       return in;
+               } catch (IOException e) {
+                       throw new IOException(String.format(
+                                       "Cannot find %s (current URL: %s)", originalUrl, url), e);
+               }
        }
 
        /**
@@ -381,6 +398,7 @@ public class Downloader {
                conn.setRequestProperty("User-Agent", UA);
                conn.setRequestProperty("Accept-Encoding", "gzip");
                conn.setRequestProperty("Accept", "*/*");
+               conn.setRequestProperty("Charset", "utf-8");
 
                if (currentReferer != null) {
                        conn.setRequestProperty("Referer", currentReferer.toString());
index fa72a4e83e14ee56f51e702056e7a5972345f72d..b3c1071908ed359c23205fe27902024101665b85 100644 (file)
@@ -713,10 +713,10 @@ public class StringUtils {
         * <p>
         * Examples:
         * <ul>
-        * <li><tt>8 765</tt> becomes "8k"</li>
-        * <li><tt>998 765</tt> becomes "998k"</li>
-        * <li><tt>12 987 364</tt> becomes "12M"</li>
-        * <li><tt>5 534 333 221</tt> becomes "5G"</li>
+        * <li><tt>8 765</tt> becomes "8 k"</li>
+        * <li><tt>998 765</tt> becomes "998 k"</li>
+        * <li><tt>12 987 364</tt> becomes "12 M"</li>
+        * <li><tt>5 534 333 221</tt> becomes "5 G"</li>
         * </ul>
         * 
         * @param value
@@ -734,10 +734,10 @@ public class StringUtils {
         * <p>
         * Examples (assuming decimalPositions = 1):
         * <ul>
-        * <li><tt>8 765</tt> becomes "8.7k"</li>
-        * <li><tt>998 765</tt> becomes "998.7k"</li>
-        * <li><tt>12 987 364</tt> becomes "12.9M"</li>
-        * <li><tt>5 534 333 221</tt> becomes "5.5G"</li>
+        * <li><tt>8 765</tt> becomes "8.7 k"</li>
+        * <li><tt>998 765</tt> becomes "998.7 k"</li>
+        * <li><tt>12 987 364</tt> becomes "12.9 M"</li>
+        * <li><tt>5 534 333 221</tt> becomes "5.5 G"</li>
         * </ul>
         * 
         * @param value
@@ -749,7 +749,7 @@ public class StringUtils {
         */
        public static String formatNumber(long value, int decimalPositions) {
                long userValue = value;
-               String suffix = "";
+               String suffix = " ";
                long mult = 1;
 
                if (value >= 1000000000l) {
index b717989505a9b173065b7d7be7cd238f89de2b31..c2e269cc58291cfbcb32d2e105c7d63938c6e136 100644 (file)
@@ -77,7 +77,7 @@ public class ImageUtilsAndroid extends ImageUtils {
                        Bitmap image = BitmapFactory.decodeStream(stream);
                        if (image == null) {
                                String extra = "";
-                               if (img.getSize() <= 1024) {
+                               if (img.getSize() <= 2048) {
                                        try {
                                                extra = ", content: "
                                                                + new String(img.getData(), "UTF-8");
index 90d780ae501afbfc436fd4fde803e7cf18674435..4cf12c04cd0573629d90ed6e376be7c5ca6e7155 100644 (file)
@@ -100,7 +100,7 @@ public class ImageUtilsAwt extends ImageUtils {
 
                        if (image == null) {
                                String extra = "";
-                               if (img.getSize() <= 1024) {
+                               if (img.getSize() <= 2048) {
                                        try {
                                                extra = ", content: "
                                                                + new String(img.getData(), "UTF-8");