Source Code Cross Referenced for Bytes.java in  » Net » DrFTPD » org » drftpd » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » DrFTPD » org.drftpd 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * This file is part of DrFTPD, Distributed FTP Daemon.
003:         *
004:         * DrFTPD is free software; you can redistribute it and/or modify
005:         * it under the terms of the GNU General Public License as published by
006:         * the Free Software Foundation; either version 2 of the License, or
007:         * (at your option) any later version.
008:         *
009:         * DrFTPD is distributed in the hope that it will be useful,
010:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
011:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012:         * GNU General Public License for more details.
013:         *
014:         * You should have received a copy of the GNU General Public License
015:         * along with DrFTPD; if not, write to the Free Software
016:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
017:         */
018:        package org.drftpd;
019:
020:        import java.text.DecimalFormat;
021:        import java.text.DecimalFormatSymbols;
022:
023:        /**
024:         * See http://physics.nist.gov/cuu/Units/binary.html for an explanation of binary multiples.
025:         *
026:         * @author mog
027:         * @version $Id: Bytes.java 858 2004-12-08 05:03:47Z mog $
028:         */
029:        public class Bytes {
030:            private static final DecimalFormat FORMAT;
031:
032:            static {
033:                DecimalFormatSymbols formatsymbols = new DecimalFormatSymbols();
034:                formatsymbols.setDecimalSeparator('.');
035:                FORMAT = new DecimalFormat("0.0", formatsymbols);
036:                FORMAT.setDecimalSeparatorAlwaysShown(true);
037:            }
038:
039:            public static final long GIBI = 1073741824L;
040:            public static final long GIGA = 1000000000L;
041:            public static final long KIBI = 1024L;
042:            public static final long KILO = 1000L;
043:            public static final long MEBI = 1048576L;
044:            public static final long MEGA = 1000000L;
045:            private static final Multiple[] MULTIPLES = new Multiple[] {
046:                    new Multiple('E', 1000000000000000000L,
047:                            1152921504606846976L),
048:                    new Multiple('P', 1000000000000000L, 1125899906842624L),
049:                    new Multiple('T', 1000000000000L, 1099511627776L),
050:                    new Multiple('G', 1000000000L, 1073741824L),
051:                    new Multiple('M', 1000000L, 1048576L),
052:                    new Multiple('K', 1000L, 1024L) };
053:            public static final long PETA = 1000000000000000L;
054:            public static final long TEBI = 1099511627776L;
055:            public static final long TERRA = 1000000000000L;
056:
057:            public static String formatBytes(long bytes) {
058:                return formatBytes(bytes, System.getProperty("bytes.binary",
059:                        "false").equals("true"));
060:            }
061:
062:            public static String formatBytes(long bytes, boolean binary) {
063:                long absbytes = Math.abs(bytes);
064:
065:                for (int i = 0; i < MULTIPLES.length; i++) {
066:                    Multiple multiple = MULTIPLES[i];
067:                    long multipleVal = binary ? multiple.getBinaryMultiple()
068:                            : multiple.getMultiple();
069:
070:                    if (absbytes >= multipleVal) {
071:                        return Bytes.FORMAT.format((float) bytes / multipleVal)
072:                                + multiple.getSuffix() + (binary ? "i" : "")
073:                                + "B";
074:                    }
075:                }
076:
077:                return bytes + "B";
078:            }
079:
080:            /**
081:             * Parse a string representation of an amount of bytes. The suffix b is optional and makes no different, this method is case insensitive.
082:             * <p>
083:             * For example:
084:             * 1000 = 1000 bytes
085:             * 1000b = 1000 bytes
086:             * 1000B = 1000 bytes
087:             * 1k = 1000 bytes
088:             * 1kb = 1000 bytes
089:             * 1t = 1 terrabyte
090:             * 1tib = 1 tebibyte
091:             */
092:            public static long parseBytes(String str)
093:                    throws NumberFormatException {
094:                str = str.toUpperCase();
095:
096:                if (str.endsWith("B")) {
097:                    str = str.substring(0, str.length() - 1);
098:                }
099:
100:                boolean binary = false;
101:
102:                if (str.endsWith("I")) {
103:                    str = str.substring(0, str.length() - 1);
104:                    binary = true;
105:                }
106:
107:                char suffix = Character.toUpperCase(str
108:                        .charAt(str.length() - 1));
109:
110:                if (Character.isDigit(suffix)) {
111:                    return Long.parseLong(str);
112:                }
113:
114:                str = str.substring(0, str.length() - 1);
115:
116:                for (int i = 0; i < MULTIPLES.length; i++) {
117:                    Multiple multiple = MULTIPLES[i];
118:
119:                    //long multiple = ;
120:                    if (suffix == multiple.getSuffix()) {
121:                        return Long.parseLong(str)
122:                                * (binary ? multiple.getBinaryMultiple()
123:                                        : multiple.getMultiple());
124:                    }
125:                }
126:
127:                throw new IllegalArgumentException("Unknown suffix " + suffix);
128:            }
129:
130:            private static class Multiple {
131:                private long _binaryMultiple;
132:                private long _multiple;
133:                private char _suffix;
134:
135:                public Multiple(char suffix, long multiple, long binarymultiple) {
136:                    _suffix = suffix;
137:                    _multiple = multiple;
138:                    _binaryMultiple = binarymultiple;
139:                }
140:
141:                public long getBinaryMultiple() {
142:                    return _binaryMultiple;
143:                }
144:
145:                public long getMultiple() {
146:                    return _multiple;
147:                }
148:
149:                public char getSuffix() {
150:                    return _suffix;
151:                }
152:            }
153:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.