Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop dependency on jaxb for converting binary arrays to hex #526

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation here is the same as in the original jaxb DatatypeConverter.printHexBinary.

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