2121import java .text .SimpleDateFormat ;
2222import java .util .Date ;
2323import java .util .Formatter ;
24+ import java .util .Locale ;
2425
2526import com .cloud .utils .exception .CloudRuntimeException ;
2627
@@ -49,17 +50,16 @@ public static int parseInt(String s, int defaultValue) {
4950 }
5051
5152 public static float parseFloat (String s , float defaultValue ) {
52- if (s == null ) {
53- return defaultValue ;
54- }
55- try {
56- return Float .parseFloat (s );
57- } catch (NumberFormatException e ) {
58- return defaultValue ;
59- }
53+ if (s == null ) {
54+ return defaultValue ;
55+ }
56+ try {
57+ return Float .parseFloat (s );
58+ } catch (NumberFormatException e ) {
59+ return defaultValue ;
60+ }
6061 }
6162
62-
6363 /**
6464 * Converts bytes to short on input.
6565 */
@@ -91,26 +91,26 @@ public static int bytesToInt(byte b[], int pos) {
9191 */
9292 public static byte [] shortToBytes (int n ) {
9393 byte b [] = new byte [2 ];
94- b [1 ] = (byte ) (n & 0xff );
95- b [0 ] = (byte ) ((n >> 8 ) & 0xff );
94+ b [1 ] = (byte )(n & 0xff );
95+ b [0 ] = (byte )((n >> 8 ) & 0xff );
9696 return b ;
9797 }
9898
9999 public static char encodeByte (int b ) {
100100 if (b < 10 ) {
101- return (char ) (b + '0' );
101+ return (char )(b + '0' );
102102 } else if (b < 36 ) {
103- return (char ) (b - 10 + 'A' );
103+ return (char )(b - 10 + 'A' );
104104 } else if (b < 62 ) {
105- return (char ) (b - 36 + 'a' );
105+ return (char )(b - 36 + 'a' );
106106 } else if (b == 62 ) {
107107 return '(' ;
108108 } else if (b == 63 ) {
109109 return ')' ;
110110 }
111- return (char ) 255 ;
111+ return (char )255 ;
112112 }
113-
113+
114114 public static int decodeByte (char b ) {
115115 if (b >= 'A' && b <= 'Z' ) {
116116 return b + 10 - 'A' ;
@@ -131,10 +131,10 @@ public static int decodeByte(char b) {
131131 */
132132 public static byte [] intToBytes (int n ) {
133133 byte b [] = new byte [4 ];
134- b [3 ] = (byte ) (n & 0xff );
135- b [2 ] = (byte ) ((n >> 8 ) & 0xff );
136- b [1 ] = (byte ) ((n >> 16 ) & 0xff );
137- b [0 ] = (byte ) ((n >> 24 ) & 0xff );
134+ b [3 ] = (byte )(n & 0xff );
135+ b [2 ] = (byte )((n >> 8 ) & 0xff );
136+ b [1 ] = (byte )((n >> 16 ) & 0xff );
137+ b [0 ] = (byte )((n >> 24 ) & 0xff );
138138 return b ;
139139 }
140140
@@ -143,14 +143,14 @@ public static byte[] intToBytes(int n) {
143143 **/
144144 public static byte [] longToBytes (long n ) {
145145 byte b [] = new byte [8 ];
146- b [7 ] = (byte ) (n & 0xff );
147- b [6 ] = (byte ) ((n >> 8 ) & 0xff );
148- b [5 ] = (byte ) ((n >> 16 ) & 0xff );
149- b [4 ] = (byte ) ((n >> 24 ) & 0xff );
150- b [3 ] = (byte ) ((n >> 32 ) & 0xff );
151- b [2 ] = (byte ) ((n >> 40 ) & 0xff );
152- b [1 ] = (byte ) ((n >> 48 ) & 0xff );
153- b [0 ] = (byte ) ((n >> 56 ) & 0xff );
146+ b [7 ] = (byte )(n & 0xff );
147+ b [6 ] = (byte )((n >> 8 ) & 0xff );
148+ b [5 ] = (byte )((n >> 16 ) & 0xff );
149+ b [4 ] = (byte )((n >> 24 ) & 0xff );
150+ b [3 ] = (byte )((n >> 32 ) & 0xff );
151+ b [2 ] = (byte )((n >> 40 ) & 0xff );
152+ b [1 ] = (byte )((n >> 48 ) & 0xff );
153+ b [0 ] = (byte )((n >> 56 ) & 0xff );
154154 return b ;
155155 }
156156
@@ -191,41 +191,37 @@ public static String bytesToString(byte[] data, int start, int end) {
191191 }
192192 return buf .toString ();
193193 }
194-
194+
195195 protected static final long KB = 1024 ;
196196 protected static final long MB = 1024 * KB ;
197197 protected static final long GB = 1024 * MB ;
198198 protected static final long TB = 1024 * GB ;
199+
199200 public static String toReadableSize (long bytes ) {
200201 if (bytes <= KB && bytes >= 0 ) {
201202 return Long .toString (bytes ) + " bytes" ;
202- } else if (bytes < MB ) {
203- StringBuilder builder = new StringBuilder ();
204- Formatter format = new Formatter (builder );
203+ }
204+ StringBuilder builder = new StringBuilder ();
205+ Formatter format = new Formatter (builder , Locale .getDefault ());
206+ if (bytes < MB ) {
205207 format .format ("%.2f KB" , (float )bytes / (float )KB );
206208 format .close ();
207209 return builder .toString ();
208210 } else if (bytes < GB ) {
209- StringBuilder builder = new StringBuilder ();
210- Formatter format = new Formatter (builder );
211211 format .format ("%.2f MB" , (float )bytes / (float )MB );
212212 format .close ();
213213 return builder .toString ();
214214 } else if (bytes < TB ) {
215- StringBuilder builder = new StringBuilder ();
216- Formatter format = new Formatter (builder );
217215 format .format ("%.2f GB" , (float )bytes / (float )GB );
218216 format .close ();
219217 return builder .toString ();
220218 } else {
221- StringBuilder builder = new StringBuilder ();
222- Formatter format = new Formatter (builder );
223219 format .format ("%.4f TB" , (float )bytes / (float )TB );
224220 format .close ();
225221 return builder .toString ();
226222 }
227223 }
228-
224+
229225 /**
230226 * Converts a string of the format 'yy-MM-dd'T'HH:mm:ss.SSS" into ms.
231227 *
@@ -244,7 +240,7 @@ public static long parseInterval(String str, long defaultValue) {
244240 } else if (str .contains ("S" )) {
245241 sdf = new SimpleDateFormat ("ss'S'SSS'ms'" );
246242 } else if (str .contains ("ms" )) {
247- sdf = new SimpleDateFormat ("SSS'ms'" );
243+ sdf = new SimpleDateFormat ("SSS'ms'" );
248244 }
249245 Date date ;
250246 try {
@@ -261,11 +257,11 @@ public static long parseInterval(String str, long defaultValue) {
261257 }
262258 }
263259 }
264-
260+
265261 public static int hash (long value ) {
266- return (int )(value ^ (value >>>32 ));
262+ return (int )(value ^ (value >>> 32 ));
267263 }
268-
264+
269265 public static void main (String [] args ) {
270266 long interval = parseInterval (args [0 ], -1 );
271267 System .out .println (args [0 ] + " is " + interval );
0 commit comments