X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fopenflowjava%2Futil%2FByteBufUtils.java;h=d68f6f9cd9c83e6811dd203848ff00f4a1eea372;hb=2cf784ba8553237e6c6714a2ca0ee2414f0b029e;hp=269d1735495df4e9b5165fcdcf8ac61c2c93f027;hpb=fc722c75c276ae69b0bf1a8fb9329864e0ba5325;p=openflowjava.git diff --git a/util/src/main/java/org/opendaylight/openflowjava/util/ByteBufUtils.java b/util/src/main/java/org/opendaylight/openflowjava/util/ByteBufUtils.java index 269d1735..d68f6f9c 100644 --- a/util/src/main/java/org/opendaylight/openflowjava/util/ByteBufUtils.java +++ b/util/src/main/java/org/opendaylight/openflowjava/util/ByteBufUtils.java @@ -9,23 +9,18 @@ package org.opendaylight.openflowjava.util; +import com.google.common.base.Preconditions; +import com.google.common.base.Splitter; +import com.google.common.collect.Lists; +import com.google.common.primitives.UnsignedBytes; import io.netty.buffer.ByteBuf; import io.netty.buffer.UnpooledByteBufAllocator; - -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; - import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader; -import com.google.common.base.Preconditions; -import com.google.common.base.Splitter; -import com.google.common.collect.Lists; -import com.google.common.primitives.UnsignedBytes; - /** Class for common operations on ByteBuf * @author michal.polkorab * @author timotej.kubas @@ -34,6 +29,8 @@ public abstract class ByteBufUtils { public static final Splitter DOT_SPLITTER = Splitter.on('.'); public static final Splitter COLON_SPLITTER = Splitter.on(':'); private static final char[] HEX_CHARS = "0123456789ABCDEF".toCharArray(); + private static final Splitter HEXSTRING_SPLITTER = Splitter.onPattern("\\s+").omitEmptyStrings(); + private static final Splitter HEXSTRING_NOSPACE_SPLITTER = Splitter.onPattern("(?<=\\G.{2})").omitEmptyStrings(); private ByteBufUtils() { //not called @@ -67,14 +64,9 @@ public abstract class ByteBufUtils { * @param withSpaces if there are spaces in string * @return byte[] filled with input data */ - public static byte[] hexStringToBytes(final String hexSrc, final boolean withSpaces ) { - String splitPattern = "\\s+"; - if (!withSpaces) { - splitPattern = "(?<=\\G.{2})"; - } - Iterable tmp = Splitter.onPattern(splitPattern) - .omitEmptyStrings().split(hexSrc); - List byteChips = Lists.newArrayList(tmp); + public static byte[] hexStringToBytes(final String hexSrc, final boolean withSpaces) { + final Splitter splitter = withSpaces ? HEXSTRING_SPLITTER : HEXSTRING_NOSPACE_SPLITTER; + List byteChips = Lists.newArrayList(splitter.split(hexSrc)); byte[] result = new byte[byteChips.size()]; int i = 0; for (String chip : byteChips) { @@ -226,10 +218,11 @@ public abstract class ByteBufUtils { } /** - * Converts macAddress to byte array + * Converts macAddress to byte array. + * See also {@link org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress}. + * * @param macAddress * @return byte representation of mac address - * @see {@link MacAddress} */ public static byte[] macAddressToBytes(final String macAddress) { final byte[] result = new byte[EncodeConstants.MAC_ADDRESS_LENGTH]; @@ -281,10 +274,11 @@ public abstract class ByteBufUtils { } /** - * Converts a MAC address represented in bytes to String + * Converts a MAC address represented in bytes to String. + * See also {@link org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress}. + * * @param address * @return String representation of a MAC address - * @see {@link MacAddress} */ public static String macAddressToString(final byte[] address) { Preconditions.checkArgument(address.length == EncodeConstants.MAC_ADDRESS_LENGTH);