X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FStringUtils.java;h=baa8e26c17fcfac7ba09c21188d6c6f30b765e23;hb=f28a134e4d06ee40d62c0c62123fc4799d49d8eb;hp=ef3d84b921aea488f5ce17e5b8b52334cf24a375;hpb=a43e4f72629d04cd3122bde830b6e4925fd3aa91;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/StringUtils.java b/src/be/nikiroo/utils/StringUtils.java index ef3d84b..baa8e26 100644 --- a/src/be/nikiroo/utils/StringUtils.java +++ b/src/be/nikiroo/utils/StringUtils.java @@ -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); } /** @@ -795,6 +800,156 @@ public class StringUtils { return new String(unbase64(data, offset, count, zip), "UTF-8"); } + /** + * 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. + *

+ *

+ * Examples: + *

+ * + * @param value + * the value to convert + * + * @return the display value + */ + public static String formatNumber(long value) { + 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. + *

+ * Examples (assuming decimalPositions = 1): + *

+ * + * @param value + * the value to convert + * @param decimalPositions + * the number of decimal positions to keep + * + * @return the display value + */ + 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"; + } + + 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(userValue) + deci + suffix; + } + + /** + * The reverse operation to {@link StringUtils#formatNumber(long)}: it will + * read a "display" number that can contain a "M" or "k" suffix and return + * the full value. + *

+ * Of course, the conversion to and from display form is lossy (example: + * 6870 to "6.5k" to 6500). + * + * @param value + * the value in display form with possible "M" and "k" suffixes, + * can be NULL + * + * @return the value as a number, or 0 if not possible to convert + */ + public static long toNumber(String value) { + return toNumber(value, 0l); + } + + /** + * The reverse operation to {@link StringUtils#formatNumber(long)}: it will + * read a "display" number that can contain a "M" or "k" suffix and return + * the full value. + *

+ * Of course, the conversion to and from display form is lossy (example: + * 6870 to "6.5k" to 6500). + * + * @param value + * the value in display form with possible "M" and "k" suffixes, + * can be NULL + * @param def + * the default value if it is not possible to convert the given + * value to a number + * + * @return the value as a number, or 0 if not possible to convert + */ + public static long toNumber(String value, long def) { + long count = def; + if (value != null) { + value = value.trim().toLowerCase(); + try { + 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]; + } + count = mult * Long.parseLong(value) + deci; + } catch (Exception e) { + } + } + + return count; + } + /** * The "remove accents" pattern. *