2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.bgp.flowspec.handlers;
10 import com.google.common.primitives.UnsignedBytes;
11 import com.google.common.primitives.UnsignedInts;
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.protocol.util.ByteBufWriteUtil;
14 import org.opendaylight.protocol.util.Values;
16 public final class Util {
19 throw new UnsupportedOperationException();
23 * Given the integer values, this method instead of writing the value
24 * in 4B field, compresses the value to lowest required byte field
25 * depending on the value.
27 * @param value integer to be written
28 * @param buffer ByteBuf where the value will be written
30 public static void writeShortest(final int value, final ByteBuf buffer) {
31 if (value <= Values.UNSIGNED_BYTE_MAX_VALUE) {
32 buffer.writeByte(UnsignedBytes.checkedCast(value));
33 } else if (value <= Values.UNSIGNED_SHORT_MAX_VALUE) {
34 ByteBufWriteUtil.writeUnsignedShort(value, buffer);
36 //value <= Values.UNSIGNED_INT_MAX_VALUE
37 ByteBufWriteUtil.writeUnsignedInt(UnsignedInts.toLong(value), buffer);