1 /*******************************************************************************
2 * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * Utility class for various operations.
25 *
26 * @author Frederico
27 *
28 */
29 public final class Utils {
30
31 /**
32 * Base multiplier.
33 */
34 private static final double BASE = 1024;
35 /**
36 * KB value.
37 */
38 private static final double KB = BASE;
39 /**
40 * MB value.
41 */
42 private static final double MB = KB * BASE;
43 /**
44 * GB value.
45 */
46 private static final double GB = MB * BASE;
47
48 /**
49 *
50 */
51 private Utils() {
52 }
53
54 /**
55 * Get contents from an URL.
56 *
57 * @param url
58 * The url
59 * @param requestProps
60 * The request props
61 * @param timeout
62 * The timeout
63 * @return Requested value
64 * @throws IOException
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 * Get contents from an URL.
75 *
76 * @param url
77 * The url
78 * @return -
79 * @throws IOException
80 * -
81 */
82 public static String getUrl(final String url) throws Exception {
83 return getUrl(new URL(url), null, null);
84 }
85
86 /**
87 * Returns formatted size of a file size.
88 *
89 * @param value
90 * The value
91 * @return String
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 }