Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.datastore.utils; |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 2 | |
| 3 | import java.nio.ByteBuffer; |
| 4 | |
| 5 | public class ByteArrayUtil { |
| 6 | |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 7 | // Suppresses default constructor, ensuring non-instantiability. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 8 | private ByteArrayUtil() { |
| 9 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 10 | |
| 11 | /** |
| 12 | * Returns a StringBuffer with each byte in {@code bytes} |
| 13 | * converted to a String with {@link Integer#toHexString(int)}, |
| 14 | * separated by {@code sep}. |
| 15 | * |
| 16 | * @param bytes byte array to convert |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 17 | * @param sep separator between each bytes |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 18 | * @return {@code bytes} converted to a StringBuffer |
| 19 | */ |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 20 | public static StringBuffer toHexStringBuffer(final byte[] bytes, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 21 | final String sep) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 22 | return toHexStringBuffer(bytes, sep, new StringBuffer()); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 23 | } |
| 24 | |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Returns a StringBuffer with each byte in {@code bytes} |
| 27 | * converted to a String with {@link Integer#toHexString(int)}, |
| 28 | * separated by {@code sep}. |
| 29 | * |
| 30 | * @param bytes byte array to convert |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 31 | * @param sep separator between each bytes |
| 32 | * @param buf StringBuffer to append to. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 33 | * @return {@code buf} |
| 34 | */ |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 35 | public static StringBuffer toHexStringBuffer(final byte[] bytes, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 36 | final String sep, final StringBuffer buf) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 37 | if (bytes == null) { |
| 38 | return buf; |
| 39 | } |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 40 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 41 | ByteBuffer wrap = ByteBuffer.wrap(bytes); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 42 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 43 | boolean hasWritten = false; |
| 44 | while (wrap.hasRemaining()) { |
| 45 | if (hasWritten) { |
| 46 | buf.append(sep); |
| 47 | } |
| 48 | buf.append(Integer.toHexString(wrap.get())); |
| 49 | hasWritten = true; |
| 50 | } |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 51 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 52 | return buf; |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 53 | } |
| 54 | } |