平时经常会使用Bytes.toString(byte[] b)这种方法,但是这个是Hbase自带的Bytes工具类,一旦离开Hbase的依赖那么只能自己写个工具类来转换了,于是乎想到将Hbase的Bytes中部分方法提出来写成自己的工具类,毕竟大牛写的代码还是很信赖的,于是就产生了下面的工具类:
public class StrUtil {
private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
public static String toString(byte[] b) {
return b == null ? null : toString(b, 0, b.length);
}
public static String toString(byte[] b1, String sep, byte[] b2) {
return toString(b1, 0, b1.length) + sep + toString(b2, 0, b2.length);
}
public static String toString(byte[] b, int off) {
if (b == null) {
return null;
} else {
int len = b.length - off;
return len <= 0 ? "" : new String(b, off, len, UTF8_CHARSET);
}
}
public static String toString(byte[] b, int off, int len) {
return b == null ? null : (len == 0 ? "" : new String(b, off, len, UTF8_CHARSET));
}
}