下面列出了 org.apache.commons.codec.binary.StringUtils # getBytesUtf8 ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void givenSomeUnencodedString_whenApacheCommonsCodecEncode_thenCompareEquals() {
String rawString = "Entwickeln Sie mit Vergnügen";
byte[] bytes = StringUtils.getBytesUtf8(rawString);
String utf8EncodedString = StringUtils.newStringUtf8(bytes);
assertEquals(rawString, utf8EncodedString);
}
public static byte[] getBytesUtf8(String str) {
return StringUtils.getBytesUtf8(str);
}
private static void appendPredicate(final Statement statement, final Text keyText) {
final Value statementValue = new Value(StringUtils.getBytesUtf8(StatementSerializer.writePredicate(statement)));
final byte[] hashOfValue = uniqueFromValueForKey(statementValue);
appendBytes(HASH_PREFIX, keyText); // prefix the hash with a zero byte.
appendBytes(hashOfValue, keyText);
}
private static void appendSubjectPredicate(final Statement statement, final Text keyText) {
final Value statementValue = new Value(StringUtils.getBytesUtf8(StatementSerializer.writeSubjectPredicate(statement)));
final byte[] hashOfValue = uniqueFromValueForKey(statementValue);
appendBytes(HASH_PREFIX, keyText); // prefix the hash with a zero byte.
appendBytes(hashOfValue, keyText);
}
@Override
public byte[] getAsKeyBytes() {
return StringUtils.getBytesUtf8(getAsKeyString());
}
@Test
public void testBase64Encoder() {
final byte[] value = StringUtils.getBytesUtf8("123");
final IFunctions.Base64Encoder f = IFunctions.Base64Encoder.INSTANCE;
Assert.assertEquals(Base64.encodeBase64String(value), f.func(value));
}
/**
* Get a collision unlikely hash string and append to the key,
* so that if two keys have the same value, then they will be the same,
* if two different values that occur at the same time there keys are different.
* If the application uses a very large number of statements at the exact same time,
* the md5 value might be upgraded to for example sha-1 to avoid collisions.
* @param statement
* @param keyText
*/
public static void appendUniqueness(final Statement statement, final Text keyText) {
keyText.append(HASH_PREFIX, 0, 1); // delimiter
final Value statementValue = new Value(StringUtils.getBytesUtf8(StatementSerializer.writeStatement(statement)));
final byte[] hashOfValue = Md5Hash.md5Binary(statementValue);
keyText.append(hashOfValue, 0, hashOfValue.length);
}
/**
* Creates an instance using the provided algorithm type.
*
* @param algorithm to use
* @param key the key to use
* @throws IllegalArgumentException
* when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
* @since 1.11
*/
public HmacUtils(final String algorithm, final String key) {
this(algorithm, StringUtils.getBytesUtf8(key));
}
/**
* Creates an instance using the provided algorithm type.
*
* @param algorithm to use
* @param key the key to use
* @throws IllegalArgumentException
* when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
* @since 1.11
*/
public HmacUtils(final HmacAlgorithms algorithm, final String key) {
this(algorithm.getName(), StringUtils.getBytesUtf8(key));
}
/**
* Creates an instance using the provided algorithm type.
*
* @param algorithm to use
* @param key the key to use
* @throws IllegalArgumentException
* when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
* @since 1.11
*/
public HmacUtils(final String algorithm, final String key) {
this(algorithm, StringUtils.getBytesUtf8(key));
}
/**
* Creates an instance using the provided algorithm type.
*
* @param algorithm to use
* @param key the key to use
* @throws IllegalArgumentException
* when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
* @since 1.11
*/
public HmacUtils(final HmacAlgorithms algorithm, final String key) {
this(algorithm.getName(), StringUtils.getBytesUtf8(key));
}
/**
* Serializes 'data' into a UTF8 byte array.
* @param data
* @return
*/
public static byte[] valueAsBytes(Object data) {
return StringUtils.getBytesUtf8(valueAsString(data));
}