1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
\r
3 package org.opendaylight.openflowjava.protocol.impl.util;
\r
5 import io.netty.buffer.ByteBuf;
\r
6 import io.netty.buffer.UnpooledByteBufAllocator;
\r
8 /** Class for common operations on ByteBuf
\r
10 * @author michal.polkorab
\r
12 public abstract class ByteBufUtils {
\r
15 * Converts ByteBuf into String
\r
16 * @param bb input ByteBuf
\r
19 public static String byteBufToHexString(ByteBuf bb) {
\r
20 StringBuffer sb = new StringBuffer();
\r
21 for (int i = 0; i < bb.readableBytes(); i++) {
\r
22 short b = bb.getUnsignedByte(i);
\r
23 sb.append(String.format("%02x ", b));
\r
25 return sb.toString();
\r
29 * Converts String into byte[]
\r
30 * @param hexSrc input String
\r
31 * @return byte[] filled with input data
\r
33 public static byte[] hexStringToBytes(String hexSrc) {
\r
34 String[] byteChips = hexSrc.split("\\s+");
\r
35 byte[] result = new byte[byteChips.length];
\r
36 for (int i = 0; i < byteChips.length; i++) {
\r
37 result[i] = (byte) Short.parseShort(byteChips[i], 16);
\r
43 * Creates ByteBuf filled with specified data
\r
44 * @param hexSrc input String of bytes in hex format
\r
45 * @return ByteBuf with specified hexString converted
\r
47 public static ByteBuf hexStringToByteBuf(String hexSrc) {
\r
48 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
\r
49 hexStringToByteBuf(hexSrc, out);
\r
54 * Creates ByteBuf filled with specified data
\r
55 * @param hexSrc input String of bytes in hex format
\r
56 * @param out ByteBuf with specified hexString converted
\r
58 public static void hexStringToByteBuf(String hexSrc, ByteBuf out) {
\r
59 out.writeBytes(hexStringToBytes(hexSrc));
\r
63 * Fills specified ByteBuf with 0 (zeros) of desired length, used for padding
\r
65 * @param out ByteBuf to be padded
\r
67 public static void padBuffer(int length, ByteBuf out) {
\r
68 for (int i = 0; i < length; i++) {
\r