1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package it.jnrpe.plugin.utils;
17
18 import java.io.IOException;
19 import java.net.URL;
20 import java.text.DecimalFormat;
21 import java.util.Properties;
22
23
24
25
26
27
28
29 public final class Utils {
30
31
32
33
34 private static final double BASE = 1024;
35
36
37
38 private static final double KB = BASE;
39
40
41
42 private static final double MB = KB * BASE;
43
44
45
46 private static final double GB = MB * BASE;
47
48
49
50
51 private Utils() {
52 }
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public static String getUrl(final URL url, final Properties requestProps,
68 final Integer timeout) throws Exception {
69 return HttpUtils.doGET(url, requestProps, timeout, false, false);
70
71 }
72
73
74
75
76
77
78
79
80
81
82 public static String getUrl(final String url) throws Exception {
83 return getUrl(new URL(url), null, null);
84 }
85
86
87
88
89
90
91
92
93 public static String formatSize(final long value) {
94 double size = value;
95 DecimalFormat df = new DecimalFormat("#.##");
96 if (size >= GB) {
97 return df.format(size / GB) + " GB";
98 }
99 if (size >= MB) {
100 return df.format(size / MB) + " MB";
101 }
102 if (size >= KB) {
103 return df.format(size / KB) + " KB";
104 }
105 return "" + (int) size + " bytes";
106 }
107
108 public static long milliToSec(long millis) {
109 long sec = millis / 1000;
110 return sec;
111 }
112
113 public static int getIntValue(boolean bool) {
114 if (bool) {
115 return 1;
116 } else {
117 return 0;
118 }
119 }
120 }