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 import java.util.List;
\r
9 import java.util.Map;
\r
10 import java.util.Map.Entry;
\r
12 import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;
\r
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
\r
15 /** Class for common operations on ByteBuf
\r
16 * @author michal.polkorab
\r
17 * @author timotej.kubas
\r
19 public abstract class ByteBufUtils {
\r
23 * Converts ByteBuf into String
\r
24 * @param bb input ByteBuf
\r
27 public static String byteBufToHexString(ByteBuf bb) {
\r
28 StringBuffer sb = new StringBuffer();
\r
29 for (int i = 0; i < bb.readableBytes(); i++) {
\r
30 short b = bb.getUnsignedByte(i);
\r
31 sb.append(String.format("%02x ", b));
\r
33 return sb.toString();
\r
37 * Converts String into byte[]
\r
38 * @param hexSrc input String
\r
39 * @return byte[] filled with input data
\r
41 public static byte[] hexStringToBytes(String hexSrc) {
\r
42 return hexStringToBytes(hexSrc, true);
\r
46 * Converts String into byte[]
\r
47 * @param hexSrc input String
\r
48 * @param withSpaces if there are spaces in string
\r
49 * @return byte[] filled with input data
\r
51 public static byte[] hexStringToBytes(String hexSrc, boolean withSpaces ) {
\r
52 String splitPattern = "\\s+";
\r
54 splitPattern = "(?<=\\G.{2})";
\r
57 String[] byteChips = hexSrc.split(splitPattern);
\r
58 byte[] result = new byte[byteChips.length];
\r
59 for (int i = 0; i < byteChips.length; i++) {
\r
60 result[i] = (byte) Short.parseShort(byteChips[i], 16);
\r
66 * Creates ByteBuf filled with specified data
\r
67 * @param hexSrc input String of bytes in hex format
\r
68 * @return ByteBuf with specified hexString converted
\r
70 public static ByteBuf hexStringToByteBuf(String hexSrc) {
\r
71 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
\r
72 hexStringToByteBuf(hexSrc, out);
\r
77 * Creates ByteBuf filled with specified data
\r
78 * @param hexSrc input String of bytes in hex format
\r
79 * @param out ByteBuf with specified hexString converted
\r
81 public static void hexStringToByteBuf(String hexSrc, ByteBuf out) {
\r
82 out.writeBytes(hexStringToBytes(hexSrc));
\r
86 * Fills specified ByteBuf with 0 (zeros) of desired length, used for padding
\r
88 * @param out ByteBuf to be padded
\r
90 public static void padBuffer(int length, ByteBuf out) {
\r
91 for (int i = 0; i < length; i++) {
\r
97 * Create standard OF header
\r
98 * @param factory serialization factory
\r
99 * @param message POJO
\r
100 * @param out writing buffer
\r
102 public static <E extends OfHeader> void writeOFHeader(OFSerializer<E> factory, E message, ByteBuf out) {
\r
103 out.writeByte(message.getVersion());
\r
104 out.writeByte(factory.getMessageType());
\r
105 out.writeShort(factory.computeLength(message));
\r
106 out.writeInt(message.getXid().intValue());
\r
111 * Fills the bitmask from boolean map where key is bit position
\r
112 * @param booleanMap bit to boolean mapping
\r
115 public static int fillBitMaskFromMap(Map<Integer, Boolean> booleanMap) {
\r
118 for (Entry<Integer, Boolean> iterator : booleanMap.entrySet()) {
\r
119 if (iterator.getValue() != null && iterator.getValue().booleanValue()) {
\r
120 bitmask |= 1 << iterator.getKey();
\r
127 * Fills the bitmask from boolean list where key is bit position
\r
128 * @param booleanList bit to boolean mapping
\r
131 public static int[] fillBitMaskFromList(List<Boolean> booleanList) {
\r
134 int arrayIndex = 0;
\r
135 if ((booleanList.size() % Integer.SIZE) != 0) {
\r
136 bitmask = new int[booleanList.size() / Integer.SIZE + 1];
\r
138 bitmask = new int[booleanList.size() / Integer.SIZE];
\r
140 for (Boolean currElement : booleanList) {
\r
141 if (currElement != null && currElement.booleanValue()) {
\r
142 bitmask[arrayIndex] |= 1 << index;
\r
145 arrayIndex = index / Integer.SIZE;
\r
151 * Converts byte array into String
\r
152 * @param array input byte array
\r
155 public static String bytesToHexString(byte[] array) {
\r
156 StringBuffer sb = new StringBuffer();
\r
157 for (int i = 0; i < array.length; i++) {
\r
158 short b = array[i];
\r
159 sb.append(String.format("%02x ", b));
\r
161 return sb.toString();
\r