Author: nextgens
Date: 2008-08-12 19:25:54 +0000 (Tue, 12 Aug 2008)
New Revision: 21772

Modified:
   trunk/freenet/src/freenet/clients/http/FirstTimeWizardToadlet.java
   trunk/freenet/src/freenet/config/IntOption.java
   trunk/freenet/src/freenet/config/LongOption.java
   trunk/freenet/src/freenet/config/ShortOption.java
   trunk/freenet/src/freenet/node/fcp/ClientRequest.java
   trunk/freenet/src/freenet/support/Fields.java
   trunk/freenet/src/freenet/support/SizeUtil.java
Log:
Fix the parsing issue properly

Modified: trunk/freenet/src/freenet/clients/http/FirstTimeWizardToadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/FirstTimeWizardToadlet.java  
2008-08-12 17:43:55 UTC (rev 21771)
+++ trunk/freenet/src/freenet/clients/http/FirstTimeWizardToadlet.java  
2008-08-12 19:25:54 UTC (rev 21772)
@@ -168,14 +168,12 @@
                        } else {
                                if(freeSpace / 10 > 1024*1024*1024) {
                                        // If 10GB+ free, default to 10% of 
available disk space.
-                                       String size = 
SizeUtil.formatSize(freeSpace/10);
-                                       String shortSize = 
SizeUtil.stripBytesEtc(size);
-                                       result.addChild("option", new String[] 
{ "value", "selected" }, new String[] { shortSize, "selected" }, size+" 
"+l10n("tenPercentDisk"));
+                                       String shortSize = 
SizeUtil.formatSize(freeSpace/10);
+                                       result.addChild("option", new String[] 
{ "value", "selected" }, new String[] { shortSize, "selected" }, shortSize+" 
"+l10n("tenPercentDisk"));
                                        if(freeSpace / 20 > 1024*1024*1024) {
                                                // If 20GB+ free, also offer 5% 
of available disk space.
-                                               size = 
SizeUtil.formatSize(freeSpace/20);
-                                               shortSize = 
SizeUtil.stripBytesEtc(size);
-                                               result.addChild("option", 
"value", shortSize, size+" "+l10n("fivePercentDisk"));
+                                               shortSize = 
SizeUtil.formatSize(freeSpace/20);
+                                               result.addChild("option", 
"value", shortSize, shortSize+" "+l10n("fivePercentDisk"));
                                        }
                                        result.addChild("option", "value", 
"1G", "1GiB");
                                } else if(freeSpace < 1024*1024*1024) {
@@ -322,7 +320,7 @@
                                int downstreamBWLimit = 
bwIndicator.getDownstreamMaxBitRate();
                                if(downstreamBWLimit > 0) {
                                        bytes = (downstreamBWLimit / 8) - 1;
-                                       String downstreamBWLimitString = 
SizeUtil.stripBytesEtc(SizeUtil.formatSize(bytes * 2/3));
+                                       String downstreamBWLimitString = 
SizeUtil.formatSize(bytes * 2/3);
                                        
_setDownstreamBandwidthLimit(downstreamBWLimitString);
                                        Logger.normal(this, "The node has a 
bandwidthIndicator: it has reported downstream="+downstreamBWLimit+ 
"bits/sec... we will use "+ downstreamBWLimitString +" and skip the bandwidth 
selection step of the wizard.");
                                }

Modified: trunk/freenet/src/freenet/config/IntOption.java
===================================================================
--- trunk/freenet/src/freenet/config/IntOption.java     2008-08-12 17:43:55 UTC 
(rev 21771)
+++ trunk/freenet/src/freenet/config/IntOption.java     2008-08-12 19:25:54 UTC 
(rev 21772)
@@ -5,7 +5,6 @@

 import freenet.l10n.L10n;
 import freenet.support.Fields;
-import freenet.support.SizeUtil;
 import freenet.support.api.IntCallback;

 /** Integer config variable */
@@ -29,7 +28,7 @@
        public IntOption(SubConfig conf, String optionName, String 
defaultValueString,
                        int sortOrder, boolean expert, boolean forceWrite, 
String shortDesc, String longDesc, IntCallback cb) {
                super(conf, optionName, cb, sortOrder, expert, forceWrite, 
shortDesc, longDesc, Option.DATA_TYPE_NUMBER);
-               this.defaultValue = Fields.parseSIInt(defaultValueString);
+               this.defaultValue = Fields.parseInt(defaultValueString);
                this.cb = cb;
                this.currentValue = defaultValue;
                this.cachedStringValue = defaultValueString;
@@ -51,8 +50,7 @@
        public void setValue(String val) throws InvalidConfigValueException {
                int x;
                try{
-                       // FIXME: don't strip, parse properly!
-                       x = Fields.parseSIInt(SizeUtil.stripBytesEtc(val));
+                       x = Fields.parseInt(val);
                } catch (NumberFormatException e) {
                        throw new 
InvalidConfigValueException(l10n("parseError", "val", val));
                }
@@ -64,7 +62,7 @@
        public void setInitialValue(String val) throws 
InvalidConfigValueException {
                int x;
                try{
-                       x = Fields.parseSIInt(val);
+                       x = Fields.parseInt(val);
                } catch (NumberFormatException e) {
                        throw new 
InvalidConfigValueException(l10n("parseError", "val", val));
                }

Modified: trunk/freenet/src/freenet/config/LongOption.java
===================================================================
--- trunk/freenet/src/freenet/config/LongOption.java    2008-08-12 17:43:55 UTC 
(rev 21771)
+++ trunk/freenet/src/freenet/config/LongOption.java    2008-08-12 19:25:54 UTC 
(rev 21772)
@@ -29,7 +29,7 @@
        public LongOption(SubConfig conf, String optionName, String 
defaultValueString, 
                        int sortOrder, boolean expert, boolean forceWrite, 
String shortDesc, String longDesc, LongCallback cb) {
                super(conf, optionName, cb, sortOrder, expert, forceWrite, 
shortDesc, longDesc, Option.DATA_TYPE_NUMBER);
-               this.defaultValue = Fields.parseSILong(defaultValueString);
+               this.defaultValue = Fields.parseLong(defaultValueString);
                this.cb = cb;
                this.currentValue = defaultValue;
                this.cachedStringValue = defaultValueString;
@@ -51,8 +51,7 @@
        public void setValue(String val) throws InvalidConfigValueException {
                long x;
                try{
-                       // FIXME: don't strip, parse properly!
-                       x = Fields.parseSILong(SizeUtil.stripBytesEtc(val));
+                       x = Fields.parseLong(val);
                }catch (NumberFormatException e) {
                        throw new 
InvalidConfigValueException(l10n("parseError", "val", val));
                }
@@ -72,7 +71,7 @@
        public void setInitialValue(String val) throws 
InvalidConfigValueException {
                long x;
                try{
-                       x = Fields.parseSILong(val);
+                       x = Fields.parseLong(val);
                }catch (NumberFormatException e) {
                        throw new 
InvalidConfigValueException(l10n("parseError", "val", val));
                }

Modified: trunk/freenet/src/freenet/config/ShortOption.java
===================================================================
--- trunk/freenet/src/freenet/config/ShortOption.java   2008-08-12 17:43:55 UTC 
(rev 21771)
+++ trunk/freenet/src/freenet/config/ShortOption.java   2008-08-12 19:25:54 UTC 
(rev 21772)
@@ -29,7 +29,7 @@
        public void setValue(String val) throws InvalidConfigValueException {
                short x;
                try{
-                       x= Fields.parseSIShort(val);
+                       x= Fields.parseShort(val);
                } catch (NumberFormatException e) {
                        throw new 
InvalidConfigValueException(l10n("unrecognisedShort", "val", val));
                }
@@ -44,7 +44,7 @@
        public void setInitialValue(String val) throws 
InvalidConfigValueException {
                short x;
                try{
-                       x = Fields.parseSIShort(val);
+                       x = Fields.parseShort(val);
                } catch (NumberFormatException e) {
                        throw new 
InvalidConfigValueException(l10n("unrecognisedShort", "val", val));
                }

Modified: trunk/freenet/src/freenet/node/fcp/ClientRequest.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/ClientRequest.java       2008-08-12 
17:43:55 UTC (rev 21771)
+++ trunk/freenet/src/freenet/node/fcp/ClientRequest.java       2008-08-12 
19:25:54 UTC (rev 21772)
@@ -109,7 +109,7 @@
                finished = Fields.stringToBool(fs.get("Finished"), false);
                global = Fields.stringToBool(fs.get("Global"), false);
                final String stime = fs.get("StartupTime");
-               this.startupTime = stime == null ? System.currentTimeMillis() : 
Fields.parseSILong(stime);
+               this.startupTime = stime == null ? System.currentTimeMillis() : 
Fields.parseLong(stime);
                completionTime = fs.getLong("CompletionTime", 0);
                if (finished)
                        started=true;

Modified: trunk/freenet/src/freenet/support/Fields.java
===================================================================
--- trunk/freenet/src/freenet/support/Fields.java       2008-08-12 17:43:55 UTC 
(rev 21771)
+++ trunk/freenet/src/freenet/support/Fields.java       2008-08-12 19:25:54 UTC 
(rev 21772)
@@ -60,6 +60,15 @@
                        'y',
                        'z' };

+       private static final long[] MULTIPLES = {
+               1000,                                           1l << 10,
+               1000 * 1000,                                    1l << 20,
+               1000l * 1000l * 1000l,                          1l << 30,
+               1000l * 1000l * 1000l * 1000l,                  1l << 40,
+               1000l * 1000l * 1000l * 1000l * 1000,           1l << 50,
+               1000l * 1000l * 1000l * 1000l * 1000l * 1000l,  1l << 60
+       };
+       
        /**
         * Converts a hex string into a long. Long.parseLong(hex, 16) assumes 
the
         * input is nonnegative unless there is a preceding minus sign. This 
method
@@ -597,23 +606,20 @@
        }

     /**
-     * Parse a human-readable string possibly including SI units into a short.
+     * Parse a human-readable string possibly including SI and ICE units into 
a short.
         * @throws NumberFormatException
         *             if the string is not parseable
         */
-       public static short parseSIShort(String s) throws NumberFormatException 
{
+       public static short parseShort(String s) throws NumberFormatException {
+               s = s.replaceFirst("(i)*B$", "");
                short res = 1;
                int x = s.length() - 1;
                int idx;
                try {
-                       long[] l =
-                               {
-                                       1000,
-                                       1 << 10 };
                        while ((x >= 0)
                                && ((idx = "kK".indexOf(s.charAt(x))) != -1)) {
                                x--;
-                               res *= l[idx];
+                               res *= MULTIPLES[idx];
                        }
                        res *= Double.parseDouble(s.substring(0, x + 1));
                } catch (ArithmeticException e) {
@@ -624,27 +630,20 @@
        }

        /**
-        * Parse a human-readable string possibly including SI units into an 
integer.
+        * Parse a human-readable string possibly including SI and ICE units 
into an integer.
         * @throws NumberFormatException
         *             if the string is not parseable
         */
-       public static int parseSIInt(String s) throws NumberFormatException {
+       public static int parseInt(String s) throws NumberFormatException {
+               s = s.replaceFirst("(i)*B$", "");
                int res = 1;
                int x = s.length() - 1;
                int idx;
                try {
-                       long[] l =
-                               {
-                                       1000,
-                                       1 << 10,
-                                       1000 * 1000,
-                                       1 << 20,
-                                       1000 * 1000 * 1000,
-                                       1 << 30 };
                        while ((x >= 0)
                                && ((idx = "kKmMgG".indexOf(s.charAt(x))) != 
-1)) {
                                x--;
-                               res *= l[idx];
+                               res *= MULTIPLES[idx];
                        }
                        res *= Double.parseDouble(s.substring(0, x + 1));
                } catch (ArithmeticException e) {
@@ -655,33 +654,20 @@
        }

        /**
-        * Parse a human-readable string possibly including SI units into a 
long.
+        * Parse a human-readable string possibly including SI and ICE units 
into a long.
         * @throws NumberFormatException
         *             if the string is not parseable
         */
-       public static long parseSILong(String s) throws NumberFormatException {
+       public static long parseLong(String s) throws NumberFormatException {
+               s = s.replaceFirst("(i)*B$", "");
                long res = 1;
                int x = s.length() - 1;
                int idx;
                try {
-                       long[] l =
-                               {
-                                       1000,
-                                       1 << 10,
-                                       1000 * 1000,
-                                       1 << 20,
-                                       1000l * 1000l * 1000l,
-                                       1l << 30,
-                                       1000l * 1000l * 1000l * 1000l,
-                                       1l << 40,
-                                       1000l * 1000l * 1000l * 1000l * 1000,
-                                       1l << 50,
-                                       1000l * 1000l * 1000l * 1000l * 1000l * 
1000l,
-                                       1l << 60 };
                        while ((x >= 0)
                                && ((idx = "kKmMgGtTpPeE".indexOf(s.charAt(x))) 
!= -1)) {
                                x--;
-                               res *= l[idx];
+                               res *= MULTIPLES[idx];
                        }
                        String multiplier = s.substring(0, x + 1).trim();
                        if(multiplier.indexOf('.') > -1 || 
multiplier.indexOf('E') > -1) {

Modified: trunk/freenet/src/freenet/support/SizeUtil.java
===================================================================
--- trunk/freenet/src/freenet/support/SizeUtil.java     2008-08-12 17:43:55 UTC 
(rev 21771)
+++ trunk/freenet/src/freenet/support/SizeUtil.java     2008-08-12 19:25:54 UTC 
(rev 21772)
@@ -43,20 +43,4 @@
                        return o;
                }
        }
-
-       public static String stripBytesEtc(String size) {
-               if(size.length() > 0 && size.charAt(size.length()-1) == 'B')
-                       size = size.substring(0, size.length()-1);
-               if(size.length() > 0 && size.charAt(size.length()-1) == 'i')
-                       size = size.substring(0, size.length()-1);
-               if(size.indexOf(' ') != -1) {
-                       StringBuffer sb = new StringBuffer(size.length()-1);
-                       for(int i=0;i<size.length();i++) {
-                               char c = size.charAt(i);
-                               if(c != ' ') sb.append(c);
-                       }
-                       size = sb.toString();
-               }
-               return size;
-       }
 }


Reply via email to