001    package railo.commons.lang;
002    
003    import railo.runtime.op.Caster;
004    
005    public final class ByteSizeParser {
006    
007            private static final long B=1;
008            private static final long KB=1024;
009            private static final long MB=KB*1024;
010            private static final long GB=MB*1024;
011            private static final long TB=GB*1024;
012            
013            public static long parseByteSizeDefinition(String value, long defaultValue) {
014            value=value.trim().toLowerCase();
015            
016            long factor=B;
017            String num=value;
018            if(value.endsWith("kb")) {
019                    factor=KB;
020                    num=value.substring(0,value.length()-2).trim();
021            }
022            else if(value.endsWith("k")) {
023                    factor=KB;
024                    num=value.substring(0,value.length()-1).trim();
025            }
026            else if(value.endsWith("mb")) {
027                    factor=MB;
028                    num=value.substring(0,value.length()-2).trim();
029            }
030            else if(value.endsWith("m")) {
031                    factor=MB;
032                    num=value.substring(0,value.length()-1).trim();
033            }
034            else if(value.endsWith("gb")) {
035                    factor=GB;
036                    num=value.substring(0,value.length()-2).trim();
037            }
038            else if(value.endsWith("g")) {
039                    factor=GB;
040                    num=value.substring(0,value.length()-1).trim();
041            }
042            else if(value.endsWith("tb")) {
043                    factor=TB;
044                    num=value.substring(0,value.length()-2).trim();
045            }
046            else if(value.endsWith("t")) {
047                    factor=TB;
048                    num=value.substring(0,value.length()-1).trim();
049            }
050            else if(value.endsWith("b")) {
051                    factor=B;
052                    num=value.substring(0,value.length()-1).trim();
053            }
054            
055            long tmp = Caster.toLongValue(num,Long.MIN_VALUE);
056            if(tmp==Long.MIN_VALUE) return defaultValue;
057            return tmp*factor;
058            }
059    }