change b64 implem step1, fix test
[nikiroo-utils.git] / src / be / nikiroo / utils / StringUtils.java
index 9acf3a1fd2a72d400168ba74ce515915a7e7fae7..baa8e26c17fcfac7ba09c21188d6c6f30b765e23 100644 (file)
@@ -21,6 +21,9 @@ import org.unbescape.html.HtmlEscape;
 import org.unbescape.html.HtmlEscapeLevel;
 import org.unbescape.html.HtmlEscapeType;
 
+import be.nikiroo.utils.streams.Base64InputStream;
+import be.nikiroo.utils.streams.Base64OutputStream;
+
 /**
  * This class offer some utilities based around {@link String}s.
  * 
@@ -604,12 +607,11 @@ public class StringUtils {
         * @throws IOException
         *             in case of I/O errors
         */
+       @Deprecated
        public static OutputStream base64(OutputStream data, boolean zip,
                        boolean breakLines) throws IOException {
-               OutputStream out = new Base64.OutputStream(data,
-                               breakLines ? Base64.DO_BREAK_LINES & Base64.ENCODE
-                                               : Base64.ENCODE);
-
+               OutputStream out = new Base64OutputStream(data, true);
+               
                if (zip) {
                        out = new java.util.zip.GZIPOutputStream(out);
                }
@@ -634,14 +636,14 @@ public class StringUtils {
         * @throws IOException
         *             in case of I/O errors
         */
+       @Deprecated
        public static InputStream base64(InputStream data, boolean zip,
                        boolean breakLines) throws IOException {
                if (zip) {
                        data = new java.util.zip.GZIPInputStream(data);
                }
 
-               return new Base64.InputStream(data, breakLines ? Base64.DO_BREAK_LINES
-                               & Base64.ENCODE : Base64.ENCODE);
+               return new Base64InputStream(data, true);
        }
 
        /**
@@ -682,9 +684,11 @@ public class StringUtils {
         * @throws IOException
         *             in case of I/O errors
         */
+       @Deprecated
        public static OutputStream unbase64(OutputStream data, boolean zip)
                        throws IOException {
-               OutputStream out = new Base64.OutputStream(data, Base64.DECODE);
+               OutputStream out = new Base64OutputStream(data, false);
+               
 
                if (zip) {
                        out = new java.util.zip.GZIPOutputStream(out);
@@ -708,13 +712,14 @@ public class StringUtils {
         * @throws IOException
         *             in case of I/O errors
         */
+       @Deprecated
        public static InputStream unbase64(InputStream data, boolean zip)
                        throws IOException {
                if (zip) {
                        data = new java.util.zip.GZIPInputStream(data);
                }
 
-               return new Base64.InputStream(data, Base64.DECODE);
+               return new Base64InputStream(data, false);
        }
 
        /**
@@ -799,11 +804,13 @@ public class StringUtils {
         * Return a display {@link String} for the given value, which can be
         * suffixed with "k" or "M" depending upon the number, if it is big enough.
         * <p>
-        * Example:
+        * <p>
+        * Examples:
         * <ul>
-        * <li><tt>8765</tt> becomes "8k"</li>
-        * <li><tt>998765</tt> becomes "998k"</li>
-        * <li><tt>12987364</tt> becomes "12M"</li>
+        * <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>
         * </ul>
         * 
         * @param value
@@ -812,39 +819,64 @@ public class StringUtils {
         * @return the display value
         */
        public static String formatNumber(long value) {
-               return formatNumber(value, true);
+               return formatNumber(value, 0);
        }
 
        /**
         * Return a display {@link String} for the given value, which can be
         * suffixed with "k" or "M" depending upon the number, if it is big enough.
         * <p>
-        * Example:
+        * Examples (assuming decimalPositions = 1):
         * <ul>
-        * <li><tt>8765</tt> becomes "8k"</li>
-        * <li><tt>998765</tt> becomes "998k"</li>
-        * <li><tt>12987364</tt> becomes "12M"</li>
+        * <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>
         * </ul>
         * 
         * @param value
         *            the value to convert
-        * @param strict
-        *            TRUE if you want any value equals or greater than 1000 to use
-        *            the "k" suffix; FALSE if you want to go a bit higher and only
-        *            use "k" for values equal or greater than 4000
+        * @param decimalPositions
+        *            the number of decimal positions to keep
         * 
         * @return the display value
         */
-       public static String formatNumber(long value, boolean strict) {
-               if (value >= 1000000l) {
-                       return Long.toString(value / 1000000l) + "M";
+       public static String formatNumber(long value, int decimalPositions) {
+               long userValue = value;
+               String suffix = "";
+               long mult = 1;
+
+               if (value >= 1000000000l) {
+                       mult = 1000000000l;
+                       userValue = value / 1000000000l;
+                       suffix = " G";
+               } else if (value >= 1000000l) {
+                       mult = 1000000l;
+                       userValue = value / 1000000l;
+                       suffix = " M";
+               } else if (value >= 1000l) {
+                       mult = 1000l;
+                       userValue = value / 1000l;
+                       suffix = " k";
                }
 
-               if ((strict && value >= 1000l) || (!strict && value >= 4000l)) {
-                       return Long.toString(value / 1000l) + "k";
+               String deci = "";
+               if (decimalPositions > 0) {
+                       deci = Long.toString(value % mult);
+                       int size = Long.toString(mult).length() - 1;
+                       while (deci.length() < size) {
+                               deci = "0" + deci;
+                       }
+
+                       deci = deci.substring(0, Math.min(decimalPositions, deci.length()));
+                       while (deci.length() < decimalPositions) {
+                               deci += "0";
+                       }
+
+                       deci = "." + deci;
                }
 
-               return Long.toString(value);
+               return Long.toString(userValue) + deci + suffix;
        }
 
        /**
@@ -853,7 +885,7 @@ public class StringUtils {
         * the full value.
         * <p>
         * Of course, the conversion to and from display form is lossy (example:
-        * <tt>6870</tt> to "6k" to <tt>6000</tt>).
+        * <tt>6870</tt> to "6.5k" to <tt>6500</tt>).
         * 
         * @param value
         *            the value in display form with possible "M" and "k" suffixes,
@@ -871,7 +903,7 @@ public class StringUtils {
         * the full value.
         * <p>
         * Of course, the conversion to and from display form is lossy (example:
-        * <tt>6870</tt> to "6k" to <tt>6000</tt>).
+        * <tt>6870</tt> to "6.5k" to <tt>6500</tt>).
         * 
         * @param value
         *            the value in display form with possible "M" and "k" suffixes,
@@ -885,19 +917,33 @@ public class StringUtils {
        public static long toNumber(String value, long def) {
                long count = def;
                if (value != null) {
+                       value = value.trim().toLowerCase();
                        try {
-                               if (value.toLowerCase().endsWith("m")) {
-                                       count = Long.parseLong(value.substring(0,
-                                                       value.length() - 1).trim());
-                                       count *= 1000000;
-                               } else if (value.toLowerCase().endsWith("k")) {
-                                       count = Long.parseLong(value.substring(0,
-                                                       value.length() - 1).trim());
-                                       count *= 1000;
-                               } else {
-                                       count = Long.parseLong(value);
+                               long mult = 1;
+                               if (value.endsWith("g")) {
+                                       value = value.substring(0, value.length() - 1).trim();
+                                       mult = 1000000000;
+                               } else if (value.endsWith("m")) {
+                                       value = value.substring(0, value.length() - 1).trim();
+                                       mult = 1000000;
+                               } else if (value.endsWith("k")) {
+                                       value = value.substring(0, value.length() - 1).trim();
+                                       mult = 1000;
+                               }
+
+                               long deci = 0;
+                               if (value.contains(".")) {
+                                       String[] tab = value.split("\\.");
+                                       if (tab.length != 2) {
+                                               throw new NumberFormatException(value);
+                                       }
+                                       double decimal = Double.parseDouble("0."
+                                                       + tab[tab.length - 1]);
+                                       deci = ((long) (mult * decimal));
+                                       value = tab[0];
                                }
-                       } catch (NumberFormatException pe) {
+                               count = mult * Long.parseLong(value) + deci;
+                       } catch (Exception e) {
                        }
                }