Skip to content

Commit

Permalink
Merge pull request #526 from esirkova/master
Browse files Browse the repository at this point in the history
Drop dependency on jaxb for converting binary arrays to hex
  • Loading branch information
aakkem authored Mar 6, 2024
2 parents 3de7373 + ef87dd5 commit 4355c41
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 24 deletions.
15 changes: 0 additions & 15 deletions java/amazon-kinesis-producer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,6 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.7</version>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty-no-dependencies</artifactId>
Expand Down
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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.bind.DatatypeConverter;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -582,6 +581,6 @@ private static Messages.Message makeSetCredentialsMessage(AWSCredentialsProvider
}

private static String protobufToHex(com.google.protobuf.Message msg) {
return DatatypeConverter.printHexBinary(msg.toByteArray());
return BinaryToHexConverter.convert(msg.toByteArray());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import java.security.MessageDigest;
import java.util.Arrays;

import javax.xml.bind.DatatypeConverter;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -53,7 +51,7 @@ public static File copyFileFrom(InputStream sourceData, File destinationDirector
digestOutputStream.close();
byte[] digest = digestOutputStream.getMessageDigest().digest();
log.debug("Calculated digest of new file: {}", Arrays.toString(digest));
String digestHex = DatatypeConverter.printHexBinary(digest);
String digestHex = BinaryToHexConverter.convert(digest);
File finalFile = new File(destinationDirectory, String.format(fileNameFormat, digestHex));
File lockFile = new File(destinationDirectory, String.format(fileNameFormat + LOCK_SUFFIX, digestHex));
log.debug("Preparing to check and copy {} to {}", tempFile.getAbsolutePath(), finalFile.getAbsolutePath());
Expand Down
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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import java.security.DigestInputStream;
import java.security.MessageDigest;

import javax.xml.bind.DatatypeConverter;

import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -150,7 +148,7 @@ private File makeTestFile() throws Exception {
}

private String hexDigestForTestData() throws Exception {
return DatatypeConverter.printHexBinary(hashForTestData());
return BinaryToHexConverter.convert(hashForTestData());
}

private byte[] testDataBytes() throws Exception {
Expand Down

0 comments on commit 4355c41

Please sign in to comment.