1 package com.atlassian.core.util;
2
3 import java.text.DecimalFormat;
4 import java.text.NumberFormat;
5
6
7
8
9
10 public class FileSize
11 {
12 public FileSize()
13 {
14
15 }
16
17 private static final float KB_SIZE = 1024;
18 private static final float MB_SIZE = KB_SIZE * KB_SIZE;
19
20 private static final String KB = " kB";
21 private static final String MB = " MB";
22
23
24
25
26
27
28
29 public static String format(Long filesize)
30 {
31 return format(filesize.longValue());
32 }
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public static String format(long filesize)
55 {
56
57
58 if (filesize > MB_SIZE)
59 {
60 return formatMB(filesize);
61 }
62 else if (filesize > KB_SIZE)
63 {
64 return formatKB(filesize);
65 }
66 else
67 {
68 return formatBytes(filesize);
69 }
70
71 }
72
73 private static String formatMB(long filesize)
74 {
75 NumberFormat mbFormat = new DecimalFormat();
76 mbFormat.setMinimumIntegerDigits(1);
77 mbFormat.setMaximumFractionDigits(2);
78 mbFormat.setMinimumFractionDigits(2);
79 float mbsize = (float) filesize / MB_SIZE;
80 return mbFormat.format(mbsize) + MB;
81 }
82
83 private static String formatKB(long filesize)
84 {
85 long kbsize = Math.round((float) filesize / KB_SIZE);
86 return String.valueOf(kbsize) + KB;
87 }
88
89 private static String formatBytes(long filesize)
90 {
91 NumberFormat bFormat = new DecimalFormat();
92 bFormat.setMinimumIntegerDigits(1);
93 bFormat.setMaximumFractionDigits(1);
94 bFormat.setMinimumFractionDigits(1);
95 float mbsize = (float) filesize / KB_SIZE;
96 return bFormat.format(mbsize) + KB;
97 }
98 }