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; |
Yuta HIGUCHI | f148aac | 2014-05-05 14:59:06 -0700 | [diff] [blame] | 4 | import java.nio.ByteOrder; |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 5 | |
Ray Milkey | 1584ec8 | 2014-04-10 11:58:30 -0700 | [diff] [blame] | 6 | public final class ByteArrayUtil { |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 7 | |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 8 | // Suppresses default constructor, ensuring non-instantiability. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 9 | private ByteArrayUtil() { |
| 10 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 11 | |
| 12 | /** |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 13 | * Returns a StringBuilder with each byte in {@code bytes} |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 14 | * converted to a String with {@link Integer#toHexString(int)}, |
| 15 | * separated by {@code sep}. |
| 16 | * |
| 17 | * @param bytes byte array to convert |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 18 | * @param sep separator between each bytes |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 19 | * @return {@code bytes} converted to a StringBuilder |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 20 | */ |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 21 | public static StringBuilder toHexStringBuilder(final byte[] bytes, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 22 | final String sep) { |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 23 | return toHexStringBuilder(bytes, sep, new StringBuilder()); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 24 | } |
| 25 | |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 26 | /** |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 27 | * Returns a StringBuilder with each byte in {@code bytes} |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 28 | * converted to a String with {@link Integer#toHexString(int)}, |
| 29 | * separated by {@code sep}. |
| 30 | * |
| 31 | * @param bytes byte array to convert |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 32 | * @param sep separator between each bytes |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 33 | * @param buf StringBuilder to append to. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 34 | * @return {@code buf} |
| 35 | */ |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 36 | public static StringBuilder toHexStringBuilder(final byte[] bytes, |
| 37 | final String sep, final StringBuilder buf) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 38 | if (bytes == null) { |
| 39 | return buf; |
| 40 | } |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 41 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 42 | ByteBuffer wrap = ByteBuffer.wrap(bytes); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 43 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 44 | boolean hasWritten = false; |
| 45 | while (wrap.hasRemaining()) { |
| 46 | if (hasWritten) { |
| 47 | buf.append(sep); |
| 48 | } |
| 49 | buf.append(Integer.toHexString(wrap.get())); |
| 50 | hasWritten = true; |
| 51 | } |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 52 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 53 | return buf; |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 54 | } |
Yuta HIGUCHI | f148aac | 2014-05-05 14:59:06 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Convert {@code value} to Little Endian byte array. |
| 58 | * |
| 59 | * @param value |
| 60 | * @return {@code value} as Little Endian byte array |
| 61 | */ |
| 62 | public static byte[] toLEBytes(final long value) { |
| 63 | return ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN) |
| 64 | .putLong(value).array(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Convert Little Endian byte array to long. |
| 69 | * |
| 70 | * @param value 8 byte Little Endian byte array |
| 71 | * @return {@code value} converted to long |
| 72 | */ |
| 73 | public static long fromLEBytes(final byte[] value) { |
| 74 | ByteBuffer counter = ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN); |
| 75 | return counter.getLong(); |
| 76 | } |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 77 | } |