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 io.netty.buffer.ByteBuf;
11 import org.opendaylight.protocol.util.Values;
12 import org.opendaylight.yangtools.yang.common.Uint16;
13 import org.opendaylight.yangtools.yang.common.Uint32;
15 public final class Util {
21 * Given an uint16, this method instead of writing the value in 2B field, compresses the value to lowest required
22 * byte field depending on the value.
24 * @param value uint16 to be written
25 * @param buffer ByteBuf where the value will be written
27 public static void writeShortest(final Uint16 value, final ByteBuf buffer) {
28 final int unsigned = value.toJava();
29 if (unsigned <= Values.UNSIGNED_BYTE_MAX_VALUE) {
30 buffer.writeByte(unsigned);
32 buffer.writeShort(unsigned);
37 * Given an uint32, this method instead of writing the value in 4B field, compresses the value to lowest required
38 * byte field depending on the value.
40 * @param value uint32 to be written
41 * @param buffer ByteBuf where the value will be written
43 public static void writeShortest(final Uint32 value, final ByteBuf buffer) {
44 final long unsigned = value.toJava();
45 if (unsigned <= Values.UNSIGNED_BYTE_MAX_VALUE) {
46 buffer.writeByte((int) unsigned);
47 } else if (unsigned <= Values.UNSIGNED_SHORT_MAX_VALUE) {
48 buffer.writeShort((int) unsigned);
50 buffer.writeInt(value.intValue());