2525import java .io .UnsupportedEncodingException ;
2626import java .nio .ByteBuffer ;
2727import java .nio .charset .Charset ;
28+ import java .nio .charset .StandardCharsets ;
2829import java .nio .charset .UnsupportedCharsetException ;
2930import java .util .Arrays ;
3031import java .util .Random ;
@@ -45,10 +46,36 @@ public class HexTest {
4546
4647 private final static boolean LOG = false ;
4748
49+ /**
50+ * Allocate a ByteBuffer.
51+ *
52+ * <p>The default implementation uses {@link ByteBuffer#allocate(int)}.
53+ * The method is overridden in AllocateDirectHexTest to use
54+ * {@link ByteBuffer#allocateDirect(int)}
55+ *
56+ * @param capacity the capacity
57+ * @return the byte buffer
58+ */
4859 protected ByteBuffer allocate (final int capacity ) {
4960 return ByteBuffer .allocate (capacity );
5061 }
5162
63+ /**
64+ * Encodes the given string into a byte buffer using the UTF-8 charset.
65+ *
66+ * <p>The buffer is allocated using {@link #allocate(int)}.
67+ *
68+ * @param string the String to encode
69+ * @return the byte buffer
70+ */
71+ private ByteBuffer getByteBufferUtf8 (String string ) {
72+ final byte [] bytes = string .getBytes (StandardCharsets .UTF_8 );
73+ final ByteBuffer bb = allocate (bytes .length );
74+ bb .put (bytes );
75+ bb .flip ();
76+ return bb ;
77+ }
78+
5279 private boolean charsetSanityCheck (final String name ) {
5380 final String source = "the quick brown dog jumped over the lazy fox" ;
5481 try {
@@ -331,7 +358,7 @@ public void testDecodeStringEmpty() throws DecoderException {
331358
332359 @ Test
333360 public void testDecodeByteBufferWithLimit () throws DecoderException {
334- final ByteBuffer bb = StringUtils . getByteBufferUtf8 ("000102030405060708090a0b0c0d0e0f" );
361+ final ByteBuffer bb = getByteBufferUtf8 ("000102030405060708090a0b0c0d0e0f" );
335362 final byte [] expected = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 };
336363 // Test pairs of bytes
337364 for (int i = 0 ; i < 15 ; i ++) {
@@ -460,7 +487,7 @@ public void testEncodeHexByteBufferEmpty() {
460487
461488 @ Test
462489 public void testEncodeHexByteBufferHelloWorldLowerCaseHex () {
463- final ByteBuffer b = StringUtils . getByteBufferUtf8 ("Hello World" );
490+ final ByteBuffer b = getByteBufferUtf8 ("Hello World" );
464491 final String expected = "48656c6c6f20576f726c64" ;
465492 char [] actual ;
466493 // Default lower-case
@@ -481,7 +508,7 @@ public void testEncodeHexByteBufferHelloWorldLowerCaseHex() {
481508
482509 @ Test
483510 public void testEncodeHexByteBufferHelloWorldUpperCaseHex () {
484- final ByteBuffer b = StringUtils . getByteBufferUtf8 ("Hello World" );
511+ final ByteBuffer b = getByteBufferUtf8 ("Hello World" );
485512 final String expected = "48656C6C6F20576F726C64" ;
486513 char [] actual ;
487514 // Default lower-case
0 commit comments