@Override public void run() { final String hex = StringUtils.hex(bytes); // avoid optimization by HotSpot if (hex.length() != bytes.length * 2) { throw new RuntimeException(); } }
/** * Cuts the string at the end if it's longer than maxLength and appends "..." to it. The length of the resulting * string including "..." is always less or equal to the given maxLength. It's valid to pass a null text; in this * case null is returned. */ public static String ellipsize(String text, int maxLength) { return ellipsize(text, maxLength, "..."); }
@Override public void run() { int count = 0; for (int i = 0; i < SHORT_REPEAT_COUNT; i++) { final String[] strings = StringUtils.split(SHORT_STRING, ' '); count += strings.length; } if (count != 9 * SHORT_REPEAT_COUNT) { throw new RuntimeException("Check test condition"); } }
@Test public void testConcatLines() { String[] lines = StringUtils.splitLines(LINES, false); ArrayList<String> list = new ArrayList<String>(); for (String line : lines) { list.add(line); } String concated = StringUtils.join(list, "\n"); assertEquals("Line 1\nLine 2\n\nLine 4\nLine 5\n\nLine 7", concated); }
public static List<String> findLinesContaining(String text, String searchText) { String[] splitLinesSkipEmpty = splitLines(text, true); List<String> matching = new ArrayList<>(); for (String line : splitLinesSkipEmpty) { if (line.contains(searchText)) { matching.add(line); } } return matching; }
/** * Generates the SHA-1 digest (40 hex characters) for a given String based on UTF-8. * The SHA-1 algorithm produces less collisions than MD5. * * @return SHA-1 digest . */ public static String sha1(String stringToEncode) { return digest(stringToEncode, "SHA-1", "UTF-8"); }
@Test public void testHexBig() { for (int i = 0; i < 256 * 256; i++) { byte[] bytes = {(byte) (i >> 8), (byte) i}; String hexExpected = DatatypeConverter.printHexBinary(bytes); String hex = StringUtils.hex(bytes); assertEquals(hexExpected, hex); byte[] bytes2 = StringUtils.parseHex(hex); assertEquals(bytes[0], bytes2[0]); assertEquals(bytes[1], bytes2[1]); } } }
/** * @throws IllegalArgumentException if the given string is invalid hex */ public static byte[] parseHex(String hex) { int length = hex.length(); if (length % 2 != 0) { throw new IllegalArgumentException("Illegal string length: " + length); } int bytesLength = length / 2; byte[] bytes = new byte[bytesLength]; int idxChar = 0; for (int i = 0; i < bytesLength; i++) { int value = parseHexDigit(hex.charAt(idxChar++)) << 4; value |= parseHexDigit(hex.charAt(idxChar++)); bytes[i] = (byte) value; } return bytes; }
public static List<String> findLinesContaining(String text, String searchText) { String[] splitLinesSkipEmpty = splitLines(text, true); List<String> matching = new ArrayList<>(); for (String line : splitLinesSkipEmpty) { if (line.contains(searchText)) { matching.add(line); } } return matching; }
/** * Generates the MD5 digest (32 hex characters) for a given String based on UTF-8. */ public static String md5(String stringToEncode) { return digest(stringToEncode, "MD5", "UTF-8"); }
/** * @throws IllegalArgumentException if the given string is invalid hex */ public static byte[] parseHex(String hex) { int length = hex.length(); if (length % 2 != 0) { throw new IllegalArgumentException("Illegal string length: " + length); } int bytesLength = length / 2; byte[] bytes = new byte[bytesLength]; int idxChar = 0; for (int i = 0; i < bytesLength; i++) { int value = parseHexDigit(hex.charAt(idxChar++)) << 4; value |= parseHexDigit(hex.charAt(idxChar++)); bytes[i] = (byte) value; } return bytes; }
/** * Generates a digest (hex string) for the given string */ public static String digest(String string, String digestAlgo, String encoding) { try { MessageDigest digester = MessageDigest.getInstance(digestAlgo); digester.update(string.getBytes(encoding)); return hex(digester.digest()); } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { throw new RuntimeException(e); } }
@Override public void run() { final String[] strings = StringUtils.split(LONG_STRING, ' '); if (strings.length != LONG_WORDS_COUNT + 1) { // "+ 1" for the last closing space throw new RuntimeException("Check test condition"); } }
@Test public void testSplitLinesSkipEmptyLines() { String[] lines = StringUtils.splitLines(LINES, true); assertEquals(5, lines.length); assertEquals("Line 1", lines[0]); assertEquals("Line 2", lines[1]); assertEquals("Line 4", lines[2]); assertEquals("Line 5", lines[3]); assertEquals("Line 7", lines[4]); }
/** * Cuts the string at the end if it's longer than maxLength and appends "..." to it. The length of the resulting * string including "..." is always less or equal to the given maxLength. It's valid to pass a null text; in this * case null is returned. */ public static String ellipsize(String text, int maxLength) { return ellipsize(text, maxLength, "..."); }
/** * Generates the MD5 digest (32 hex characters) for a given String based on UTF-8. */ public static String md5(String stringToEncode) { return digest(stringToEncode, "MD5", "UTF-8"); }
/** * @throws IllegalArgumentException if the given string is invalid hex */ public static byte[] parseHex(String hex) { int length = hex.length(); if (length % 2 != 0) { throw new IllegalArgumentException("Illegal string length: " + length); } int bytesLength = length / 2; byte[] bytes = new byte[bytesLength]; int idxChar = 0; for (int i = 0; i < bytesLength; i++) { int value = parseHexDigit(hex.charAt(idxChar++)) << 4; value |= parseHexDigit(hex.charAt(idxChar++)); bytes[i] = (byte) value; } return bytes; }