-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #526 from esirkova/master
Drop dependency on jaxb for converting binary arrays to hex
- Loading branch information
Showing
6 changed files
with
47 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...-producer/src/main/java/com/amazonaws/services/kinesis/producer/BinaryToHexConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.amazonaws.services.kinesis.producer; | ||
|
||
public class BinaryToHexConverter { | ||
|
||
private static final BinaryToHexConverter INSTANCE = new BinaryToHexConverter(); | ||
private static final char[] HEX_CODE = "0123456789ABCDEF".toCharArray(); | ||
|
||
/** | ||
* Converts an array of bytes into a hex string. | ||
* | ||
* @param data An array of bytes | ||
* @return A string containing a lexical representation of xsd:hexBinary | ||
* @throws IllegalArgumentException if {@code data} is null. | ||
*/ | ||
public static String convert(byte[] data) { | ||
return INSTANCE.convertToHex(data); | ||
} | ||
|
||
private String convertToHex(byte[] data) { | ||
StringBuilder r = new StringBuilder(data.length * 2); | ||
for (byte b : data) { | ||
r.append(HEX_CODE[(b >> 4) & 0xF]); | ||
r.append(HEX_CODE[(b & 0xF)]); | ||
} | ||
return r.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...ducer/src/test/java/com/amazonaws/services/kinesis/producer/BinaryToHexConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.amazonaws.services.kinesis.producer; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class BinaryToHexConverterTest { | ||
|
||
@Test | ||
public void testConvert() { | ||
byte[] bytes = {0, 1, 2, 3, 124, 125, 126, 127}; | ||
assertEquals("000102037C7D7E7F", BinaryToHexConverter.convert(bytes)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters