org.nervousync.utils.ConvertUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils-jdk11 Show documentation
Show all versions of utils-jdk11 Show documentation
Java utility collections, development by Nervousync Studio (NSYC)
/*
* Licensed to the Nervousync Studio (NSYC) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.nervousync.utils;
import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.*;
import org.nervousync.commons.Globals;
/**
* Data convert utilities
*
* Current utilities implements features:
* Convert data bytes to hex string
* Convert data bytes to string
* Convert data bytes to Object
* Convert any to data bytes
* Convert properties to data map
*
* 数据转换工具集
*
* 此工具集实现以下功能:
* 转换字节数组为十六进制字符串
* 转换字节数组为字符串
* 转换字节数组为实例对象
* 转换任意实例对象为字节数组
* 转换属性信息为数据映射表
*
*
* @author Steven Wee [email protected]
* @version $Revision: 1.2.0 $ $Date: Jan 12, 2010 15:12:05 $
*/
public final class ConvertUtils {
/**
* Logger instance
* 日志实例
*/
private final static LoggerUtils.Logger LOGGER = LoggerUtils.getLogger(ConvertUtils.class);
/**
* Private constructor for ConvertUtils
* 数据转换工具集的私有构造方法
*/
private ConvertUtils() {
}
/**
* Convert data byte array to hex string
* 转换二进制数组为十六进制字符串
*
* @param dataBytes the original data byte array
* 原始二进制数组
*
* @return Converted hex string
* 转换后的十六进制字符串
*/
public static String toHex(final byte[] dataBytes) {
if (dataBytes == null) {
return Globals.DEFAULT_VALUE_STRING;
}
StringBuilder stringBuilder = new StringBuilder();
for (byte b : dataBytes) {
String tmp = Integer.toHexString(b & 0xFF);
if (tmp.length() == 1) {
stringBuilder.append("0");
}
stringBuilder.append(tmp);
}
return stringBuilder.toString();
}
/**
* Convert data byte array to string using default encoding: UTF-8
* 转换二进制数组为字符串,使用默认编码集:UTF-8
*
* @param dataBytes the original data byte array
* 原始二进制数组
*
* @return Converted string
* 转换后的字符串
*/
public static String toString(final byte[] dataBytes) {
return toString(dataBytes, Globals.DEFAULT_ENCODING);
}
/**
* Convert data byte array to string using given encoding
* 转换二进制数组为字符串,使用给定的编码集
*
* @param dataBytes the original data byte array
* 原始二进制数组
* @param encoding the charset encoding
* 字符集
*
* @return Converted string
* 转换后的字符串
*/
public static String toString(final byte[] dataBytes, final String encoding) {
try {
return new String(dataBytes, encoding);
} catch (UnsupportedEncodingException ex) {
return new String(dataBytes, Charset.defaultCharset());
}
}
/**
* Convert given string to data byte array using default encoding: UTF-8
* 转换字符串为二进制数组,使用默认编码集:UTF-8
*
* @param content the original string
* 原始字符串
*
* @return Converted data byte array
* 转换后的二进制数组
*/
public static byte[] toByteArray(final String content) {
return toByteArray(content, Globals.DEFAULT_ENCODING);
}
/**
* Convert string to data byte array using given encoding
* 转换字符串为二进制数组,使用给定的编码集
*
* @param content the original string
* 原始字符串
* @param encoding the charset encoding
* 字符集
*
* @return Converted data byte array
* 转换后的二进制数组
*/
public static byte[] toByteArray(final String content, final String encoding) {
try {
return content.getBytes(encoding);
} catch (UnsupportedEncodingException ex) {
return content.getBytes(Charset.defaultCharset());
}
}
/**
* Convert given object instance to data byte array
* 转换实例对象为二进制数组
*
* @param object the object instance
* 换实例对象
*
* @return Converted data byte array
* 转换后的二进制数组
*/
public static byte[] toByteArray(final Object object) {
if (object == null) {
return new byte[0];
}
if (object instanceof String) {
return toByteArray((String)object);
}
if (object instanceof byte[] || object instanceof Byte[]) {
assert object instanceof byte[];
return (byte[])object;
}
ByteArrayOutputStream outputStream = null;
ObjectOutputStream objectOutputStream = null;
try {
outputStream = new ByteArrayOutputStream();
objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(object);
return outputStream.toByteArray();
} catch (Exception e) {
LOGGER.error("Convert_Object_To_Array_Error");
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Stack_Message_Error", e);
}
} finally {
IOUtils.closeStream(objectOutputStream);
IOUtils.closeStream(outputStream);
}
return new byte[0];
}
/**
* Convert given data byte array instance to object instance
* 转换二进制数组为实例对象
*
* @param dataBytes the original data byte array
* 原始二进制数组
*
* @return Converted object instance
* 转换后的实例对象
*/
public static Object toObject(final byte[] dataBytes) {
if (dataBytes.length == 0) {
return null;
}
ByteArrayInputStream byteInputStream = null;
ObjectInputStream objectInputStream = null;
try {
byteInputStream = new ByteArrayInputStream(dataBytes);
objectInputStream = new ObjectInputStream(byteInputStream);
return objectInputStream.readObject();
} catch (Exception e) {
return dataBytes;
} finally {
IOUtils.closeStream(objectInputStream);
IOUtils.closeStream(byteInputStream);
}
}
/**
* Read properties file and convert data to hash map
* 读取属性文件并转换数据为哈希表
*
* @param propertiesFilePath The properties file path
* 属性文件地址
*
* @return Data hash map
* 数据哈希表
*/
public static Map toMap(final String propertiesFilePath) {
return toMap(propertiesFilePath, null);
}
/**
* Read properties file and merge data with given hash map
* 读取属性文件并将数据与给定的哈希表合并
*
* @param propertiesFilePath The properties file path
* 属性文件地址
* @param messageMap The merged data hash map
* 要合并的数据哈希表
*
* @return Merged data hash map
* 合并后的数据哈希表
*/
public static Map toMap(final String propertiesFilePath, final Map messageMap) {
return toMap(PropertiesUtils.loadProperties(propertiesFilePath), messageMap);
}
/**
* Read properties file from URL instance and convert data to hash map
* 从URL实例对象读取属性文件并转换数据为哈希表
*
* @param url The url instance of properties file
* 属性文件的URL实例对象
*
* @return Data hash map
* 数据哈希表
*/
public static Map toMap(final URL url) {
return toMap(url, null);
}
/**
* Read properties file from URL instance and merge data with given hash map
* 从URL实例对象读取属性文件并将数据与给定的哈希表合并
*
* @param url The url instance of properties file
* 属性文件的URL实例对象
* @param messageMap The merged data hash map
* 要合并的数据哈希表
*
* @return Merged data hash map
* 合并后的数据哈希表
*/
public static Map toMap(final URL url, Map messageMap) {
return toMap(PropertiesUtils.loadProperties(url), messageMap);
}
/**
* Convert properties instance to hash map and merge data with given hash map
* 转换属性实例对象为哈希表并将数据与给定的哈希表合并
*
* @param properties The properties instance
* 属性实例对象
*
* @return Merged data hash map
* 合并后的数据哈希表
*/
public static Map toMap(final Properties properties) {
return toMap(properties, null);
}
/**
* Convert properties instance to hash map and merge data with given hash map
* 转换属性实例对象为哈希表并将数据与给定的哈希表合并
*
* @param properties The properties instance
* 属性实例对象
* @param messageMap The merged data hash map
* 要合并的数据哈希表
*
* @return Merged data hash map
* 合并后的数据哈希表
*/
public static Map toMap(final Properties properties, final Map messageMap) {
final Map dataMap = (messageMap == null) ? new HashMap<>() : messageMap;
if (properties != null) {
properties.forEach((key, value) -> dataMap.put((String) key, (String) value));
}
return dataMap;
}
}