一、前言
基于granite源码包org.granite.util.StringUtil字符串工具类,进行字节/字符数组bytesToHexChars/hexCharsToBytes相互转换、字符串转为字节数组hexStringToBytes、对象转字符串toString等。
二、源码说明
package org.granite.util;@b@@b@import java.io.BufferedReader;@b@import java.io.StringReader;@b@import java.lang.reflect.Array;@b@import java.nio.ByteBuffer;@b@import java.nio.LongBuffer;@b@import java.util.ArrayList;@b@import java.util.Arrays;@b@import java.util.Collection;@b@import java.util.HashMap;@b@import java.util.Iterator;@b@import java.util.List;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@@b@public class StringUtil@b@{@b@ private static final char[] HEX_CHARS = "0123456789ABCDEF".toCharArray();@b@@b@ public static char[] bytesToHexChars(byte[] bytes)@b@ {@b@ return bytesToHexChars(bytes, new char[bytes.length * 2], 0);@b@ }@b@@b@ public static char[] bytesToHexChars(byte[] bytes, char[] chars, int off) {@b@ if (chars.length - off < bytes.length * 2)@b@ throw new IllegalArgumentException("Unsufficient capacity in 'chars' parameter");@b@@b@ int i = off; for (int j = 0; i < bytes.length; ) {@b@ int b = bytes[(i++)] & 0xFF;@b@ chars[(j++)] = HEX_CHARS[(b >> 4)];@b@ chars[(j++)] = HEX_CHARS[(b & 0xF)];@b@ }@b@@b@ return chars;@b@ }@b@@b@ public static byte[] hexCharsToBytes(char[] chars) {@b@ return hexCharsToBytes(chars, 0);@b@ }@b@@b@ public static byte[] hexCharsToBytes(char[] chars, int off) {@b@ int len = chars.length;@b@ if ((len - off) % 2 != 0)@b@ throw new IllegalArgumentException("(chars.length - off) must be even");@b@@b@ byte[] bytes = new byte[(len - off) / 2];@b@ int i = off; for (int j = 0; i < len; ) {@b@ int b = 0;@b@ char c = chars[(i++)];@b@ b |= c - ((c < 'A') ? '0' : '7') << 4;@b@ c = chars[(i++)];@b@ b |= c - ((c < 'A') ? '0' : '7');@b@ bytes[(j++)] = (byte)b;@b@ }@b@ return bytes;@b@ }@b@@b@ public static byte[] hexStringToBytes(String s) {@b@ return hexStringToBytes(s, 0);@b@ }@b@@b@ public static byte[] hexStringToBytes(String s, int off) {@b@ int len = s.length();@b@ if ((len - off) % 2 != 0)@b@ throw new IllegalArgumentException("(s.length() - off) must be even");@b@@b@ byte[] bytes = new byte[(len - off) / 2];@b@ int i = off; for (int j = 0; i < len; ) {@b@ int b = 0;@b@ char c = s.charAt(i++);@b@ b |= c - ((c < 'A') ? '0' : '7') << 4;@b@ c = s.charAt(i++);@b@ b |= c - ((c < 'A') ? '0' : '7');@b@ bytes[(j++)] = (byte)b;@b@ }@b@ return bytes;@b@ }@b@@b@ public static String toHexString(Number n) {@b@ if (n == null)@b@ return "null";@b@@b@ byte[] bytes = new byte[8];@b@ ByteBuffer bytesBuffer = ByteBuffer.wrap(bytes);@b@ LongBuffer longBuffer = bytesBuffer.asLongBuffer();@b@ longBuffer.put(0, n.longValue());@b@@b@ StringBuilder sb = new StringBuilder(16);@b@ for (int i = 0; i < bytes.length; ++i) {@b@ int b = bytes[i] & 0xFF;@b@ if ((b != 0) || (sb.length() > 0) || (i == bytes.length - 1))@b@ sb.append(HEX_CHARS[(b >> 4)]).append(HEX_CHARS[(b & 0xF)]);@b@ }@b@ return sb.toString();@b@ }@b@@b@ public static String removeSpaces(String s) {@b@ if (s == null)@b@ return null;@b@ String[] tokens = s.split("\\s", -1);@b@ if (tokens.length == 0)@b@ return "";@b@ if (tokens.length == 1)@b@ return tokens[0];@b@ StringBuilder sb = new StringBuilder();@b@ for (String token : tokens)@b@ sb.append(token);@b@ return sb.toString();@b@ }@b@@b@ public static String[] toStringArray(String s) {@b@ if (s == null)@b@ return new String[0];@b@@b@ List lines = new ArrayList();@b@ try {@b@ reader = new BufferedReader(new StringReader(s));@b@ String line = null;@b@ while ((line = reader.readLine()) != null)@b@ lines.add(line);@b@ }@b@ catch (java.io.IOException reader)@b@ {@b@ }@b@ return ((String[])lines.toArray(new String[lines.size()]));@b@ }@b@@b@ public static String toString(Object o) {@b@ return toString(o, -1);@b@ }@b@@b@ public static String toString(Object o, int maxItems) {@b@ if (o == null)@b@ return "null";@b@@b@ if (o instanceof String)@b@ return "\"" + o + "\"";@b@@b@ if ((o instanceof Character) || (o.getClass() == Character.TYPE))@b@ return "'" + o + "'";@b@@b@ if (o instanceof Number) {@b@ if ((o instanceof Byte) || (o instanceof Short) || (o instanceof Integer) || (o instanceof Long))@b@ return o + " <0x" + toHexString((Number)o) + ">";@b@ return String.valueOf(o);@b@ }@b@@b@ try@b@ {@b@ int max;@b@ List list;@b@ int i;@b@ if (o.getClass().isArray()) {@b@ Class type = o.getClass().getComponentType();@b@@b@ if (maxItems < 0) {@b@ if (type.isPrimitive()) {@b@ if (type == Byte.TYPE)@b@ return Arrays.toString((byte[])o);@b@ if (type == Character.TYPE)@b@ return Arrays.toString((char[])o);@b@ if (type == Integer.TYPE)@b@ return Arrays.toString((int[])o);@b@ if (type == Double.TYPE)@b@ return Arrays.toString((double[])o);@b@ if (type == Long.TYPE)@b@ return Arrays.toString((long[])o);@b@ if (type == Float.TYPE)@b@ return Arrays.toString((float[])o);@b@ if (type == Short.TYPE)@b@ return Arrays.toString((short[])o);@b@ if (type == Boolean.TYPE)@b@ return Arrays.toString((boolean[])o);@b@ return "[Array of unknown primitive type: " + type + "]";@b@ }@b@ return Arrays.toString((Object[])o);@b@ }@b@@b@ max = Math.min(maxItems, Array.getLength(o));@b@ list = new ArrayList(max);@b@@b@ for (i = 0; i < max; ++i)@b@ list.add(Array.get(o, i));@b@ if (max < Array.getLength(o))@b@ list.add("(first " + max + '/' + Array.getLength(o) + " elements only...)");@b@@b@ o = list;@b@ } else {@b@ Iterator localIterator;@b@ if ((o instanceof Collection) && (maxItems >= 0))@b@ {@b@ Collection coll = (Collection)o;@b@ max = Math.min(maxItems, coll.size());@b@ list = new ArrayList(max);@b@@b@ i = 0;@b@ for (localIterator = coll.iterator(); localIterator.hasNext(); ) { Object item = localIterator.next();@b@ if (i >= max) {@b@ list.add("(first " + max + '/' + coll.size() + " elements only...)");@b@ break;@b@ }@b@ list.add(item);@b@ ++i;@b@ }@b@@b@ o = list;@b@ }@b@ else if ((o instanceof Map) && (maxItems >= 0)) {@b@ Map map = (Map)o;@b@ max = Math.min(maxItems, map.size());@b@ Map copy = new HashMap(max);@b@@b@ i = 0;@b@ for (Map.Entry item : map.entrySet()) {@b@ if (i >= max) {@b@ copy.put("(first " + max + '/' + map.size() + " elements only...)", "...");@b@ break;@b@ }@b@ copy.put(item.getKey(), item.getValue());@b@ ++i;@b@ }@b@@b@ o = copy;@b@ }@b@ }@b@ return String.valueOf(o); } catch (Exception e) {@b@ }@b@ return o.getClass().getName() + " (exception: " + e.toString() + ")";@b@ }@b@}