
Efficiently and safely byte array to string and vice versa in java
Posted by andy.song in Twins Father on Dec 29, 2010 4:48:00 AMMore often in the large system you may need transfer the byte array - which may serialized stream or read from binary file or socket - via the network. And normally most of system design will took string as the protocol rather than byte array. So here you may need some way safely and efficiently transfer byte arrary to string and vice versa.
So today I will post one toolbox for you resolving that particular problem.
1. ByteArray to HexString
static final String HEXES = "0123456789ABCDEF";
public static String getHex(byte[] raw) {
if (raw == null) {
return null;
}
final StringBuilder hex = new StringBuilder(2 * raw.length);
for (final byte b : raw) {
hex.append(HEXES.charAt((b & 0xF0) >> 4))
.append(HEXES.charAt((b & 0x0F)));
}
return hex.toString();
}
2. HexString to ByteArray
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
3. Testing codes
byte[] valueBytes = value.getBytes();
long nanoTime = System.nanoTime();
String hexString = getHex(valueBytes);
System.out.println(hexString.length());
System.out.println(System.nanoTime() - nanoTime);
nanoTime = System.nanoTime();
System.out.println(hexStringToBytes(hexString));
System.out.println(System.nanoTime() - nanoTime);
with the large xml string testing overall the two action took no more than 2 ms. It much better than other alternative solutions.
Note: The value definition for testing list below - the length for that string is 1024:
String value = "<soap:Body>\n" +
"\t\t<ns0:ChangeRFPProposalStatusAction\n" +
"\t\t\txmlns:ns0=\"http://localhost/MeetingBrokerServices\">\n" +
"\t\t\t<StatusChange xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
"\t\t\t\txmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://localhost/MeetingBrokerServices\">\n" +
"\t\t\t\t<DocumentId>b1f10399-40bd-4e10-9426-9f8291ab3a9f</DocumentId>\n" +
"\t\t\t\t<TransactionId>b1f10399-40bd-4e10-9426-9f8291ab3a9f</TransactionId>\n" +
"\t\t\t\t<DocumentDate>2010-12-25T02:39:31.1241257-05:00</DocumentDate>\n" +
"\t\t\t\t<RfpId>5690777</RfpId>\n" +
"\t\t\t\t<Sites>\n" +
"\t\t\t\t\t<Site>\n" +
"\t\t\t\t\t\t<SiteId>213</SiteId>\n" +
"\t\t\t\t\t</Site>\n" +
"\t\t\t\t</Sites>\n" +
"\t\t\t\t<Status>Declined</Status>\n" +
"\t\t\t\t<DeclineReason>No reason provided</DeclineReason>\n" +
"\t\t\t\t<Message>\n" +
"\t\t\t\t\t<To>OnVantage Public Channel</To>\n" +
"\t\t\t\t\t<From>MB Test, User</From>\n" +
"\t\t\t\t\t<Subject>Turndown RFP</Subject>\n" +
"\t\t\t\t\t<Body>with Attach</Body>\n" +
"\t\t\t\t\t<FromEmail />\n" +
"\t\t\t\t\t<MarketingText />\n" +
"\t\t\t\t\t<MarketingHtml />\n" +
"\t\t\t\t\t<Attachments>\n" +
"\t\t\t\t\t\t<Attachment>\n" +
"\t\t\t\t\t\t\t<FileName>file.txt</FileName>\n" +
"\t\t\t\t\t\t\t<ContentType>text/plain</ContentType>\n" +
"\t\t\t\t\t\t\t<FileData>\n" +
"\t\t\t\t\t\t\t\t<char>111</char>\n" +
"\t\t\t\t\t\t\t\t<char>110</char>\n" +
"\t\t\t\t\t\t\t\t<char>101</char>\n" +
"\t\t\t\t\t\t\t</FileData>\n" +
"\t\t\t\t\t\t</Attachment>\n" +
"\t\t\t\t\t</Attachments>\n" +
"\t\t\t\t</Message>\n" +
"\t\t\t</StatusChange>\n" +
"\t\t</ns0:ChangeRFPProposalStatusAction>\n" +
"\t</soap:Body>";
Comments