You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by ke...@apache.org on 2006/12/12 14:57:37 UTC

svn commit: r486146 - /ant/core/trunk/src/main/org/apache/tools/ant/util/StringUtils.java

Author: kevj
Date: Tue Dec 12 05:57:36 2006
New Revision: 486146

URL: http://svn.apache.org/viewvc?view=rev&rev=486146
Log:
- add Human Readable file size 'parsing' method

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/util/StringUtils.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/util/StringUtils.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/util/StringUtils.java?view=diff&rev=486146&r1=486145&r2=486146
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/util/StringUtils.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/util/StringUtils.java Tue Dec 12 05:57:36 2006
@@ -186,4 +186,43 @@
         }
         return b.toString();
     }
+    
+    /**
+     * Takes a human readable size representation eg 10K
+     * a long value. Doesn't support 1.1K or other rational values.
+     * @param humanSize
+     * @return a long value representation
+     * @throws Exception
+     * @since Ant 1.7
+     */
+    public static long parseHumanSizes(String humanSize) throws Exception {
+    	final long KILOBYTE = 1024;
+    	final long MEGABYTE = KILOBYTE * 1024;
+    	final long GIGABYTE = MEGABYTE * 1024;
+    	final long TERABYTE = GIGABYTE * 1024;
+    	final long PETABYTE = TERABYTE * 1024;
+    	String regex = "\\d+[K|M|G|T|P]";
+    	if(humanSize.matches(regex)) {
+    		char c = humanSize.charAt(humanSize.length()-1);
+    		long value = Long.valueOf(humanSize.substring(0, humanSize.length()-1)).longValue();
+    		switch (c) {
+    			case 'K':
+    				return value * KILOBYTE;
+    			case 'M':
+    				return value * MEGABYTE;
+    			case 'G':
+    				return value * GIGABYTE;
+    			case 'T':
+    				return value * TERABYTE;
+    			case 'P':
+    				return value * PETABYTE;
+    			default:
+    				return value;
+    		}
+    	} else if(humanSize.matches("\\d+")) {
+    		return Long.parseLong(humanSize);
+    	} else {
+    		throw new Exception("Couldn't parse string: "+humanSize);
+    	}
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org