Parent Directory | Revision Log | Patch
--- lucene/nutch/trunk/src/java/org/apache/nutch/util/StringUtil.java 2005/12/29 15:25:20 359821 +++ lucene/nutch/trunk/src/java/org/apache/nutch/util/StringUtil.java 2005/12/29 15:28:30 359822 @@ -49,6 +49,79 @@ public class StringUtil { return sb.toString(); } + + private static final char[] HEX_DIGITS = + {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; + + /** + * Convenience call for {@link #toHexString(byte[], String, int)}, where + * <code>sep = null; lineLen = Integer.MAX_VALUE</code>. + * @param buf + * @return + */ + public static String toHexString(byte[] buf) { + return toHexString(buf, null, Integer.MAX_VALUE); + } + + /** + * Get a text representation of a byte[] as hexadecimal String, where each + * pair of hexadecimal digits corresponds to consecutive bytes in the array. + * @param buf input data + * @param sep separate every pair of hexadecimal digits with this separator, or + * null if no separation is needed. + * @param lineLen break the output String into lines containing output for lineLen + * bytes. + */ + public static String toHexString(byte[] buf, String sep, int lineLen) { + if (buf == null) return null; + if (lineLen <= 0) lineLen = Integer.MAX_VALUE; + StringBuffer res = new StringBuffer(buf.length * 2); + for (int i = 0; i < buf.length; i++) { + int b = buf[i]; + res.append(HEX_DIGITS[(b >> 4) & 0xf]); + res.append(HEX_DIGITS[b & 0xf]); + if (i > 0 && (i % lineLen) == 0) res.append('\n'); + else if (sep != null && i < lineLen - 1) res.append(sep); + } + return res.toString(); + } + + /** + * Convert a String containing consecutive (no inside whitespace) hexadecimal + * digits into a corresponding byte array. If the number of digits is not even, + * a '0' will be appended in the front of the String prior to conversion. + * Leading and trailing whitespace is ignored. + * @param text input text + * @return converted byte array, or null if unable to convert + */ + public static byte[] fromHexString(String text) { + text = text.trim(); + if (text.length() % 2 != 0) text = "0" + text; + int resLen = text.length() / 2; + int loNibble, hiNibble; + byte[] res = new byte[resLen]; + for (int i = 0; i < resLen; i++) { + int j = i << 1; + hiNibble = charToNibble(text.charAt(j)); + loNibble = charToNibble(text.charAt(j + 1)); + if (loNibble == -1 || hiNibble == -1) return null; + res[i] = (byte)(hiNibble << 4 | loNibble); + } + return res; + } + + private static final int charToNibble(char c) { + if (c >= '0' && c <= '9') { + return c - '0'; + } else if (c >= 'a' && c <= 'f') { + return 0xa + (c - 'a'); + } else if (c >= 'A' && c <= 'F') { + return 0xA + (c - 'A'); + } else { + return -1; + } + } + /** * Parse the character encoding from the specified content type header. * If the content type is null, or there is no explicit character encoding,
infrastructure at apache.org | ViewVC Help |
Powered by ViewVC 1.1.26 |