mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 12:19:54 +01:00
Added utility methods for converting string to UTF-8 and vice versa.
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package org.briarproject.util;
|
package org.briarproject.util;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
public class StringUtils {
|
public class StringUtils {
|
||||||
|
|
||||||
private static final char[] HEX = new char[] {
|
private static final char[] HEX = new char[] {
|
||||||
@@ -11,6 +13,14 @@ public class StringUtils {
|
|||||||
return s == null || s.length() == 0;
|
return s == null || s.length() == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte[] toUtf8(String s) {
|
||||||
|
return s.getBytes(Charset.forName("UTF-8"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String fromUtf8(byte[] bytes) {
|
||||||
|
return new String(bytes, Charset.forName("UTF-8"));
|
||||||
|
}
|
||||||
|
|
||||||
/** Converts the given byte array to a hex character array. */
|
/** Converts the given byte array to a hex character array. */
|
||||||
public static char[] toHexChars(byte[] bytes) {
|
public static char[] toHexChars(byte[] bytes) {
|
||||||
char[] hex = new char[bytes.length * 2];
|
char[] hex = new char[bytes.length * 2];
|
||||||
|
|||||||
@@ -7,18 +7,6 @@ import org.junit.Test;
|
|||||||
|
|
||||||
public class StringUtilsTest extends BriarTestCase {
|
public class StringUtilsTest extends BriarTestCase {
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testHead() {
|
|
||||||
String head = StringUtils.head("123456789", 5);
|
|
||||||
assertEquals("12345...", head);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testTail() {
|
|
||||||
String tail = StringUtils.tail("987654321", 5);
|
|
||||||
assertEquals("...54321", tail);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToHexString() {
|
public void testToHexString() {
|
||||||
byte[] b = new byte[] {1, 2, 3, 127, -128};
|
byte[] b = new byte[] {1, 2, 3, 127, -128};
|
||||||
|
|||||||
Reference in New Issue
Block a user