Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.github.javaclub.configcenter.client.util.Utils Maven / Gradle / Ivy
/*
* @(#)Utils.java 2018年2月23日
*
* Copyright (c) 2018. All Rights Reserved.
*
*/
package com.github.javaclub.configcenter.client.util;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.net.URL;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.github.javaclub.configcenter.client.DefaultConfigCenterClient;
/**
* Utils 通用工具类
*
* @author Gerald Chen
* @version $Id: Utils.java 2018年2月23日 22:01:51 Exp $
*/
public class Utils {
public static final int BUFFER_SIZE = 4096;
public static final String EMPTY_STRING = "";
public static String CONFIG_CLIENT_VER;
private Utils() {
// forbidden build
}
public static String getConfigClientVer() {
if(isNotBlank(CONFIG_CLIENT_VER)) {
return CONFIG_CLIENT_VER;
}
try {
CONFIG_CLIENT_VER = getJarVersion(DefaultConfigCenterClient.JAR_GROUP_ID, DefaultConfigCenterClient.JAR_ARTIFACT_ID);
} catch (Exception e) {
CONFIG_CLIENT_VER = "2.1.1"; // start version
}
System.out.println(DefaultConfigCenterClient.JAR_GROUP_ID + ":"
+ DefaultConfigCenterClient.JAR_ARTIFACT_ID + ":" + CONFIG_CLIENT_VER);
return CONFIG_CLIENT_VER;
}
// ===== Strings start =====
public static String noneNull(String str) {
return (null == str ? EMPTY_STRING : str.trim());
}
public static String noneNull(String str, String defaults) {
return (null == str ? defaults : str.trim());
}
public static String anyNoneNull(String ... arrays) {
if(null == arrays || 0 >= arrays.length) {
return EMPTY_STRING;
}
for (String string : arrays) {
if(isNotBlank(string)) {
return string.trim();
}
}
return EMPTY_STRING;
}
public static String noneBlank(String str, String defaults) {
return (isBlank(str) ? defaults : str.trim());
}
public static String anyToString(Object o, boolean noneNull) {
if(null == o && !noneNull) {
return null;
}
return (null == o) ? EMPTY_STRING : o.toString();
}
public static boolean isEmpty(String input) {
return null == input || 0 == input.length();
}
public static boolean isNotEmpty(String input) {
return null != input && input.length() > 0;
}
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
}
public static boolean isNotBlank(String str) {
return !isBlank(str);
}
public static boolean isNotBlank(String ... arrays) {
for (String string : arrays) {
if(isBlank(string)) {
return false;
}
}
return true;
}
public static boolean hasLength(CharSequence str) {
return (str != null && str.length() > 0);
}
public static boolean hasLength(String str) {
return hasLength((CharSequence) str);
}
public static boolean hasText(CharSequence str) {
if (!hasLength(str)) {
return false;
}
int strLen = str.length();
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return true;
}
}
return false;
}
public static boolean hasText(String str) {
return hasText((CharSequence) str);
}
public static String capitalize(String str) {
return changeFirstCharacterCase(str, true);
}
public static String uncapitalize(String str) {
return changeFirstCharacterCase(str, false);
}
private static String changeFirstCharacterCase(String str, boolean capitalize) {
if (str == null || str.length() == 0) {
return str;
}
StringBuffer buf = new StringBuffer(str.length());
if (capitalize) {
buf.append(Character.toUpperCase(str.charAt(0)));
} else {
buf.append(Character.toLowerCase(str.charAt(0)));
}
buf.append(str.substring(1));
return buf.toString();
}
/**
*
* Utils.split(null) = null
* Utils.split("") = []
* Utils.split("abc def") = ["abc", "def"]
* Utils.split("abc def") = ["abc", "def"]
* Utils.split(" abc ") = ["abc"]
*
*
* @param str the String to parse, may be null
* @return an array of parsed Strings, null
if null String input
*/
public static String[] split(String str) {
return split(str, null, -1);
}
/**
*
* StringUtils.split(null, *) = null
* StringUtils.split("", *) = []
* StringUtils.split("a.b.c", '.') = ["a", "b", "c"]
* StringUtils.split("a..b.c", '.') = ["a", "b", "c"]
* StringUtils.split("a:b:c", '.') = ["a:b:c"]
* StringUtils.split("a b c", ' ') = ["a", "b", "c"]
*
*/
public static String[] split(String str, char separatorChar) {
return splitWorker(str, separatorChar, false);
}
/**
*
* StringUtils.split(null, *) = null
* StringUtils.split("", *) = []
* StringUtils.split("abc def", null) = ["abc", "def"]
* StringUtils.split("abc def", " ") = ["abc", "def"]
* StringUtils.split("abc def", " ") = ["abc", "def"]
* StringUtils.split("ab:cd:ef", ":") = ["ab", "cd", "ef"]
*
*/
public static String[] split(String str, String separatorChars) {
return splitWorker(str, separatorChars, -1, false);
}
public static String[] split(String str, String separatorChars, int max) {
return splitWorker(str, separatorChars, max, false);
}
/**
* 将目标字符串以指定字符分隔后,并每个分隔的元素去除首尾空格
*
* @param str 被处理字符串
* @param separatorChars 分隔字符
* @return
*/
public static String[] splitAndTrim(String str, String separatorChars) {
List list = new ArrayList();
String[] strArray = splitWorker(str, separatorChars, -1, false);
if(null != strArray && strArray.length > 0) {
for (String e : strArray) {
if(null == e) continue;
list.add(e.trim());
}
}
return list.toArray(new String[0]);
}
private static String[] splitWorker(String str, char separatorChar, boolean preserveAllTokens) {
// Performance tuned for 2.0 (JDK1.4)
if (str == null) {
return null;
}
int len = str.length();
if (len == 0) {
return new String[0];
}
List list = new ArrayList();
int i = 0, start = 0;
boolean match = false;
boolean lastMatch = false;
while (i < len) {
if (str.charAt(i) == separatorChar) {
if (match || preserveAllTokens) {
list.add(str.substring(start, i));
match = false;
lastMatch = true;
}
start = ++i;
continue;
}
lastMatch = false;
match = true;
i++;
}
if (match || (preserveAllTokens && lastMatch)) {
list.add(str.substring(start, i));
}
return (String[]) list.toArray(new String[list.size()]);
}
private static String[] splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens) {
// Performance tuned for 2.0 (JDK1.4)
// Direct code is quicker than StringTokenizer.
// Also, StringTokenizer uses isSpace() not isWhitespace()
if (str == null) {
return null;
}
int len = str.length();
if (len == 0) {
return new String[0];
}
List list = new ArrayList();
int sizePlus1 = 1;
int i = 0, start = 0;
boolean match = false;
boolean lastMatch = false;
if (separatorChars == null) {
// Null separator means use whitespace
while (i < len) {
if (Character.isWhitespace(str.charAt(i))) {
if (match || preserveAllTokens) {
lastMatch = true;
if (sizePlus1++ == max) {
i = len;
lastMatch = false;
}
list.add(str.substring(start, i));
match = false;
}
start = ++i;
continue;
}
lastMatch = false;
match = true;
i++;
}
} else if (separatorChars.length() == 1) {
// Optimise 1 character case
char sep = separatorChars.charAt(0);
while (i < len) {
if (str.charAt(i) == sep) {
if (match || preserveAllTokens) {
lastMatch = true;
if (sizePlus1++ == max) {
i = len;
lastMatch = false;
}
list.add(str.substring(start, i));
match = false;
}
start = ++i;
continue;
}
lastMatch = false;
match = true;
i++;
}
} else {
// standard case
while (i < len) {
if (separatorChars.indexOf(str.charAt(i)) >= 0) {
if (match || preserveAllTokens) {
lastMatch = true;
if (sizePlus1++ == max) {
i = len;
lastMatch = false;
}
list.add(str.substring(start, i));
match = false;
}
start = ++i;
continue;
}
lastMatch = false;
match = true;
i++;
}
}
if (match || (preserveAllTokens && lastMatch)) {
list.add(str.substring(start, i));
}
return (String[]) list.toArray(new String[list.size()]);
}
// ===== Strings end =======
public static ClassLoader getDefaultClassLoader() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
}
catch (Throwable ex) {
// Cannot access thread context ClassLoader - falling back to system class loader...
}
if (cl == null) {
// No thread context class loader -> use class loader of this class.
cl = Utils.class.getClassLoader();
}
return cl;
}
// ======== IO start =========
public static File getClasspathFile(String classpath) {
URL url = getDefaultClassLoader().getResource(classpath);
if (url == null) {
throw new IllegalStateException(
"Could not find classpath properties resource: "
+ classpath);
}
if (url.getProtocol().equals("file") == false) {
throw new IllegalArgumentException(
"URL could not be converted to a File: " + url);
}
return toFile(url);
}
/**
* 读取当前jar中内容
*
* @param resource classpath资源路径,如:META-INF/MANIFEST.MF
* @return
*/
public static InputStream getClasspathStream(String resourcePath) {
return Utils.class.getClassLoader().getResourceAsStream(resourcePath);
}
public static File toFile(URL url) {
if (url == null || !url.getProtocol().equals("file")) {
return null;
} else {
String filename = url.getFile().replace('/', File.separatorChar);
int pos = 0;
while ((pos = filename.indexOf('%', pos)) >= 0) {
if (pos + 2 < filename.length()) {
String hexStr = filename.substring(pos + 1, pos + 3);
char ch = (char) Integer.parseInt(hexStr, 16);
filename = filename.substring(0, pos) + ch
+ filename.substring(pos + 3);
}
}
return new File(filename);
}
}
public static String getJarVersion(String groupId, String artifactId) {
Properties pom = null;
try {
pom = getJarProperties(groupId, artifactId);
} catch (Exception e) {
}
return null == pom ? null : pom.getProperty("version");
}
public static Properties getJarProperties(String groupId, String artifactId) throws IOException {
Properties properties = new Properties();
String mainfestFile = "META-INF/MANIFEST.MF";
String mavenPomFile = String.format("META-INF/maven/%s/%s/pom.properties", groupId, artifactId);
InputStream pomResource = getClasspathStream(mavenPomFile);
if (null != pomResource) {
// #Generated by Maven
// #Wed Jul 22 13:09:50 CST 2015
// version=1.0.5-SNAPSHOT
// groupId=com.shandiangou
// artifactId=shangou-commons
byte[] pomData = copyToByteArray(pomResource);
properties.load(new ByteArrayInputStream(pomData));
BufferedReader br = new BufferedReader(new StringReader(new String(pomData, "utf-8")));
List lines = new ArrayList();
for (String line = br.readLine(); line != null; line = br.readLine()) {
lines.add(line);
}
// try to get package time
if (lines.size() >= 2 && lines.get(1).startsWith("#")) {
String time = lines.get(1).substring(1);
properties.put("packageTime", time);
}
}
InputStream mainfestResource = getClasspathStream(mainfestFile);
if (null != mainfestResource) {
// Manifest-Version: 1.0
// Archiver-Version: Plexus Archiver
// Built-By: hengyunabc
// Created-By: Apache Maven 3.3.1
// Build-Jdk: 1.8.0_45
byte[] mainfestData = copyToByteArray(mainfestResource);
BufferedReader br = new BufferedReader(new StringReader(new String(mainfestData)));
for (String line = br.readLine(); line != null; line = br.readLine()) {
String[] split = split(line, ':');
if (split != null && split.length == 2) {
properties.put(split[0].trim(), split[1].trim());
}
}
}
return properties;
}
public static byte[] copyToByteArray(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE);
copy(in, out);
return out.toByteArray();
}
public static int copy(InputStream in, OutputStream out) throws IOException {
try {
int byteCount = 0;
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
byteCount += bytesRead;
}
out.flush();
return byteCount;
} finally {
closeQuietly(in);
closeQuietly(out);
}
}
public static void closeQuietly(InputStream in) {
try {
if(null != in) {
in.close();
}
} catch (Exception e) {
}
}
public static void closeQuietly(OutputStream out) {
try {
if(null != out) {
out.close();
}
} catch (Exception e) {
}
}
public static void closeQuietly(Connection con) {
try {
if(null != con) {
con.close();
}
} catch (Exception e) {
}
}
// ======== IO end ===========
public static void main(String[] args) {
String mavenPomFile = String.format("META-INF/maven/%s/%s/pom.properties", "groupId", "artifactId");
System.out.println(mavenPomFile);
}
}