com.blade.kit.ConvertKit Maven / Gradle / Ivy
/**
* Copyright (c) 2017, biezhi 王爵 ([email protected])
*
* 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 com.blade.kit;
import lombok.experimental.UtilityClass;
import java.io.*;
@UtilityClass
public class ConvertKit {
/**
* Hex Digits
*/
private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
/**
* byteArr convert hexString
*
e.g:
* bytes2HexString(new byte[] { 0, (byte) 0xa8 }) returns 00A8
*
* @param bytes byte array
* @return hex decimal uppercase string
*/
public static String bytes2HexString(final byte[] bytes) {
if (bytes == null) return null;
int len = bytes.length;
if (len <= 0) return null;
char[] ret = new char[len << 1];
for (int i = 0, j = 0; i < len; i++) {
ret[j++] = HEX_DIGITS[bytes[i] >>> 4 & 0x0f];
ret[j++] = HEX_DIGITS[bytes[i] & 0x0f];
}
return new String(ret);
}
/**
* hexString转byteArr
* 例如:
* hexString2Bytes("00A8") returns { 0, (byte) 0xA8 }
*
* @param hexString 十六进制字符串
* @return 字节数组
*/
public static byte[] hexString2Bytes(String hexString) {
if (isSpace(hexString)) return null;
int len = hexString.length();
if (len % 2 != 0) {
hexString = "0" + hexString;
len = len + 1;
}
char[] hexBytes = hexString.toUpperCase().toCharArray();
byte[] ret = new byte[len >> 1];
for (int i = 0; i < len; i += 2) {
ret[i >> 1] = (byte) (hex2Dec(hexBytes[i]) << 4 | hex2Dec(hexBytes[i + 1]));
}
return ret;
}
/**
* hexChar转int
*
* @param hexChar hex单个字节
* @return 0..15
*/
public static int hex2Dec(final char hexChar) {
if (hexChar >= '0' && hexChar <= '9') {
return hexChar - '0';
} else if (hexChar >= 'A' && hexChar <= 'F') {
return hexChar - 'A' + 10;
} else {
throw new IllegalArgumentException();
}
}
/**
* charArr转byteArr
*
* @param chars 字符数组
* @return 字节数组
*/
public static byte[] chars2Bytes(final char[] chars) {
if (chars == null || chars.length <= 0) return null;
int len = chars.length;
byte[] bytes = new byte[len];
for (int i = 0; i < len; i++) {
bytes[i] = (byte) (chars[i]);
}
return bytes;
}
/**
* byteArr转charArr
*
* @param bytes 字节数组
* @return 字符数组
*/
public static char[] bytes2Chars(final byte[] bytes) {
if (bytes == null) return null;
int len = bytes.length;
if (len <= 0) return null;
char[] chars = new char[len];
for (int i = 0; i < len; i++) {
chars[i] = (char) (bytes[i] & 0xff);
}
return chars;
}
/**
* 以unit为单位的内存大小转字节数
*
* @param memorySize 大小
* @param unit 单位类型
*
* - {@link MemoryConst#BYTE}: 字节
* - {@link MemoryConst#KB} : 千字节
* - {@link MemoryConst#MB} : 兆
* - {@link MemoryConst#GB} : GB
*
* @return 字节数
*/
public static long memorySize2Byte(final long memorySize, @MemoryConst.Unit final int unit) {
if (memorySize < 0) return -1;
return memorySize * unit;
}
/**
* 字节数转以unit为单位的内存大小
*
* @param byteNum 字节数
* @param unit 单位类型
*
* - {@link MemoryConst#BYTE}: 字节
* - {@link MemoryConst#KB} : 千字节
* - {@link MemoryConst#MB} : 兆
* - {@link MemoryConst#GB} : GB
*
* @return 以unit为单位的size
*/
public static double byte2MemorySize(final long byteNum, @MemoryConst.Unit final int unit) {
if (byteNum < 0) return -1;
return (double) byteNum / unit;
}
/**
* 字节数转合适内存大小
* 保留3位小数
*
* @param byteNum 字节数
* @return 合适内存大小
*/
public static String byte2FitMemorySize(final long byteNum) {
if (byteNum < 0) {
return "shouldn't be less than zero!";
} else if (byteNum < MemoryConst.KB) {
return String.format("%.3fB", (double) byteNum);
} else if (byteNum < MemoryConst.MB) {
return String.format("%.3fKB", (double) byteNum / MemoryConst.KB);
} else if (byteNum < MemoryConst.GB) {
return String.format("%.3fMB", (double) byteNum / MemoryConst.MB);
} else {
return String.format("%.3fGB", (double) byteNum / MemoryConst.GB);
}
}
/**
* 字节数转合适内存大小
* 保留3位小数
*
* @param byteNum 字节数
* @return 合适内存大小
*/
public static String byte2FitMemoryString(final long byteNum) {
if (byteNum < 0) {
return "shouldn't be less than zero!";
} else if (byteNum < MemoryConst.KB) {
return String.format("%d B", byteNum);
} else if (byteNum < MemoryConst.MB) {
return String.format("%d KB", byteNum / MemoryConst.KB);
} else if (byteNum < MemoryConst.GB) {
return String.format("%d MB", byteNum / MemoryConst.MB);
} else {
return String.format("%d GB", byteNum / MemoryConst.GB);
}
}
/**
* bytes转bits
*
* @param bytes 字节数组
* @return bits
*/
public static String bytes2Bits(final byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte aByte : bytes) {
for (int j = 7; j >= 0; --j) {
sb.append(((aByte >> j) & 0x01) == 0 ? '0' : '1');
}
}
return sb.toString();
}
/**
* bits转bytes
*
* @param bits 二进制
* @return bytes
*/
public static byte[] bits2Bytes(String bits) {
int lenMod = bits.length() % 8;
int byteLen = bits.length() / 8;
// 不是8的倍数前面补0
if (lenMod != 0) {
for (int i = lenMod; i < 8; i++) {
bits = "0" + bits;
}
byteLen++;
}
byte[] bytes = new byte[byteLen];
for (int i = 0; i < byteLen; ++i) {
for (int j = 0; j < 8; ++j) {
bytes[i] <<= 1;
bytes[i] |= bits.charAt(i * 8 + j) - '0';
}
}
return bytes;
}
/**
* inputStream转outputStream
*
* @param is 输入流
* @return outputStream子类
*/
public static ByteArrayOutputStream input2OutputStream(final InputStream is) {
if (is == null) return null;
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] b = new byte[MemoryConst.KB];
int len;
while ((len = is.read(b, 0, MemoryConst.KB)) != -1) {
os.write(b, 0, len);
}
return os;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
IOKit.closeQuietly(is);
}
}
/**
* outputStream转inputStream
*
* @param out 输出流
* @return inputStream子类
*/
public ByteArrayInputStream output2InputStream(final OutputStream out) {
if (out == null) return null;
return new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray());
}
/**
* inputStream转byteArr
*
* @param is 输入流
* @return 字节数组
*/
public static byte[] inputStream2Bytes(final InputStream is) {
if (is == null) return null;
return input2OutputStream(is).toByteArray();
}
/**
* byteArr转inputStream
*
* @param bytes 字节数组
* @return 输入流
*/
public static InputStream bytes2InputStream(final byte[] bytes) {
if (bytes == null || bytes.length <= 0) return null;
return new ByteArrayInputStream(bytes);
}
/**
* outputStream转byteArr
*
* @param out 输出流
* @return 字节数组
*/
public static byte[] outputStream2Bytes(final OutputStream out) {
if (out == null) return null;
return ((ByteArrayOutputStream) out).toByteArray();
}
/**
* outputStream转byteArr
*
* @param bytes 字节数组
* @return 字节数组
*/
public static OutputStream bytes2OutputStream(final byte[] bytes) {
if (bytes == null || bytes.length <= 0) return null;
ByteArrayOutputStream os = null;
try {
os = new ByteArrayOutputStream();
os.write(bytes);
return os;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
IOKit.closeQuietly(os);
}
}
/**
* inputStream转string按编码
*
* @param is 输入流
* @param charsetName 编码格式
* @return 字符串
*/
public static String inputStream2String(final InputStream is, final String charsetName) {
if (is == null || isSpace(charsetName)) return null;
try {
return new String(inputStream2Bytes(is), charsetName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
/**
* string转inputStream按编码
*
* @param string 字符串
* @param charsetName 编码格式
* @return 输入流
*/
public static InputStream string2InputStream(final String string, final String charsetName) {
if (string == null || isSpace(charsetName)) return null;
try {
return new ByteArrayInputStream(string.getBytes(charsetName));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
/**
* outputStream转string按编码
*
* @param out 输出流
* @param charsetName 编码格式
* @return 字符串
*/
public static String outputStream2String(final OutputStream out, final String charsetName) {
if (out == null || isSpace(charsetName)) return null;
try {
return new String(outputStream2Bytes(out), charsetName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
/**
* string转outputStream按编码
*
* @param string 字符串
* @param charsetName 编码格式
* @return 输入流
*/
public static OutputStream string2OutputStream(final String string, final String charsetName) {
if (string == null || isSpace(charsetName)) return null;
try {
return bytes2OutputStream(string.getBytes(charsetName));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
/**
* 判断字符串是否为null或全为空白字符
*
* @param s 待校验字符串
* @return {@code true}: null或全空白字符
{@code false}: 不为null且不全空白字符
*/
private static boolean isSpace(final String s) {
if (s == null) return true;
for (int i = 0, len = s.length(); i < len; ++i) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}
return true;
}
}