/** * 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"); }
/** * 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, "..."); }
/** * Suspends encoding of the stream. May be helpful if you need to embed a piece of base64-encoded data in a * stream. * * @throws java.io.IOException * if there's an error flushing * @since 1.5.1 */ public void suspendEncoding() throws java.io.IOException { flushBase64(); this.suspendEncoding = true; } // end suspendEncoding
@Test public void testMaxSize() { ObjectCache<String, String> cache = createCacheWith4Entries(0); cache.put("5", "e"); assertEquals(4, cache.size()); assertNull(cache.get("1")); assertEquals(cache.get("5"), "e"); assertEquals(1, cache.getCountEvicted()); }
@Test public void testEvictToTargetSize() { ObjectCache<String, String> cache = createCacheWith4Entries(0); cache.evictToTargetSize(2); assertEquals(2, cache.size()); assertEquals(cache.get("3"), "c"); assertEquals(cache.get("4"), "d"); cache.evictToTargetSize(0); assertEquals(0, cache.size()); }
private ObjectCache<String, String> createCacheWith4Entries(int expirationMillis) { ObjectCache<String, String> cache = new ObjectCache(ObjectCache.ReferenceType.STRONG, 4, expirationMillis); cache.put("1", "a"); cache.put("2", "b"); cache.put("3", "c"); cache.put("4", "d"); assertEquals(4, cache.size()); return cache; } }
@Test public void testSplit() throws Exception { assertArrayEquals(ss("John", "Doe"), StringUtils.split("John Doe", ' ')); assertArrayEquals(ss("John", "", "Doe", ""), StringUtils.split("John Doe ", ' ')); assertArrayEquals(ss("", "John", "Doe", ""), StringUtils.split(" John Doe ", ' ')); assertArrayEquals(ss("John", "Christoph", "Doe"), StringUtils.split("John Christoph Doe", ' ')); assertArrayEquals(ss("John", "", "", "Doe"), StringUtils.split("John,,,Doe", ',')); assertArrayEquals(ss("John", "Doe", ""), StringUtils.split("John Doe ", ' ')); assertArrayEquals(ss("John", "", "", ""), StringUtils.split("John,,,", ',')); }
public boolean containsKeyWithValue(KEY key) { return get(key) != null; }
@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"); } }
/** * Decodes data from Base64 notation, automatically detecting gzip-compressed data and decompressing it. * * @param s * the string to decode * @return the decoded data * @throws java.io.IOException * If there is a problem * @since 1.4 */ public static byte[] decode(String s) throws java.io.IOException { return decode(s, NO_OPTIONS); }
void checkCleanUpObsoleteEntries() { if (!isStrongReference || isExpiring) { if ((isExpiring && nextCleanUpTimestamp != 0 && System.currentTimeMillis() > nextCleanUpTimestamp) || countPutCountSinceEviction > maxSize / 2) { cleanUpObsoleteEntries(); } } }
/** * 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"); }
/** * 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, "..."); }
public boolean containsKeyWithValue(KEY key) { return get(key) != null; }
private static String generateLongString(int wordsCount) { StringBuilder builder = new StringBuilder(); String[] words = StringUtils.split(SHORT_STRING, ' '); for (int i = 0; i < wordsCount; i++) { builder.append(words[i % words.length]).append(' '); } return builder.toString(); }
/** * Suspends encoding of the stream. May be helpful if you need to embed a piece of base64-encoded data in a * stream. * * @throws java.io.IOException * if there's an error flushing * @since 1.5.1 */ public void suspendEncoding() throws java.io.IOException { flushBase64(); this.suspendEncoding = true; } // end suspendEncoding
/** * Decodes data from Base64 notation, automatically detecting gzip-compressed data and decompressing it. * * @param s * the string to decode * @return the decoded data * @throws java.io.IOException * If there is a problem * @since 1.4 */ public static byte[] decode(String s) throws java.io.IOException { return decode(s, NO_OPTIONS); }
void checkCleanUpObsoleteEntries() { if (!isStrongReference || isExpiring) { if ((isExpiring && nextCleanUpTimestamp != 0 && System.currentTimeMillis() > nextCleanUpTimestamp) || countPutCountSinceEviction > maxSize / 2) { cleanUpObsoleteEntries(); } } }
/** * 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"); }
/** * 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"); }