io.fabric8.docker.client.utils.Utils Maven / Gradle / Ivy
/*
* Copyright (C) 2016 Original Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.fabric8.docker.client.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Utils {
public static boolean isNullOrEmpty(String str) {
return str == null || str.isEmpty();
}
public static boolean isNotNullOrEmpty(String str) {
return !isNullOrEmpty(str);
}
public static String getSystemPropertyOrEnvVar(String systemPropertyName, String envVarName, String defaultValue) {
String answer = System.getProperty(systemPropertyName);
if (isNotNullOrEmpty(answer)) {
return answer;
}
answer = System.getenv(envVarName);
if (isNotNullOrEmpty(answer)) {
return answer;
}
return defaultValue;
}
public static String convertSystemPropertyNameToEnvVar(String systemPropertyName) {
return systemPropertyName.toUpperCase().replaceAll("[.-]", "_");
}
public static String getEnvVar(String envVarName, String defaultValue) {
String answer = System.getenv(envVarName);
return isNotNullOrEmpty(answer) ? answer : defaultValue;
}
public static String getSystemPropertyOrEnvVar(String systemPropertyName, String defaultValue) {
return getSystemPropertyOrEnvVar(systemPropertyName, convertSystemPropertyNameToEnvVar(systemPropertyName), defaultValue);
}
public static String getSystemPropertyOrEnvVar(String systemPropertyName) {
return getSystemPropertyOrEnvVar(systemPropertyName, (String) null);
}
public static Boolean getSystemPropertyOrEnvVar(String systemPropertyName, Boolean defaultValue) {
String result = getSystemPropertyOrEnvVar(systemPropertyName, defaultValue.toString());
return result.equals("1") || Boolean.parseBoolean(result);
}
public static int getSystemPropertyOrEnvVar(String systemPropertyName, int defaultValue) {
String result = getSystemPropertyOrEnvVar(systemPropertyName, new Integer(defaultValue).toString());
return Integer.parseInt(result);
}
public static String join(final Object[] array) {
return join(array, ',');
}
public static String join(final Object[] array, final char separator) {
if (array == null) {
return null;
}
if (array.length == 0) {
return "";
}
final StringBuilder buf = new StringBuilder();
for (int i = 0; i < array.length; i++) {
if (i > 0) {
buf.append(separator);
}
if (array[i] != null) {
buf.append(array[i]);
}
}
return buf.toString();
}
public static String readFully(InputStream is) throws IOException {
StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String read;
while ((read = br.readLine()) != null) {
sb.append(read);
}
}
return sb.toString();
}
}