2 * Copyright (c) 2014 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
9 package org.opendaylight.protocol.util;
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import java.math.BigInteger;
14 import java.util.BitSet;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
23 * Utility class for ByteBuf's write methods.
25 public final class ByteBufWriteUtil {
27 public static final int SHORT_BYTES_LENGTH = Short.SIZE / Byte.SIZE;
29 public static final int MEDIUM_BYTES_LENGTH = 3;
31 public static final int INT_BYTES_LENGTH = Integer.SIZE / Byte.SIZE;
33 public static final int LONG_BYTES_LENGTH = Long.SIZE / Byte.SIZE;
35 public static final int FLOAT32_BYTES_LENGTH = INT_BYTES_LENGTH;
37 public static final int ONE_BYTE_LENGTH = 1;
39 public static final int IPV4_PREFIX_BYTE_LENGTH = Ipv4Util.IP4_LENGTH + 1;
41 public static final int IPV6_PREFIX_BYTE_LENGTH = Ipv6Util.IPV6_LENGTH + 1;
43 public static final int AFI_BYTES_LENGTH = 2;
45 public static final int SAFI_BYTES_LENGTH = 1;
47 private ByteBufWriteUtil() {
48 throw new UnsupportedOperationException();
52 * Writes 32-bit integer <code>value</code> if not null, otherwise writes
53 * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
57 * Integer value to be written to the output.
59 * ByteBuf, where value or zeros are written.
61 public static void writeInt(final Integer value, final ByteBuf output) {
63 output.writeInt(value);
65 output.writeZero(INT_BYTES_LENGTH);
70 * Writes 24-bit integer <code>value</code> if not null, otherwise writes
71 * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
75 * Medium value to be written to the output.
77 * ByteBuf, where value or zeros are written.
79 public static void writeMedium(final Integer value, final ByteBuf output) {
81 output.writeMedium(value);
83 output.writeZero(MEDIUM_BYTES_LENGTH);
88 * Writes 16-bit short <code>value</code> if not null, otherwise writes
89 * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
93 * Short value to be written to the output.
95 * ByteBuf, where value or zeros are written.
97 public static void writeShort(final Short value, final ByteBuf output) {
99 output.writeShort(value);
101 output.writeZero(SHORT_BYTES_LENGTH);
106 * Writes 64-bit long <code>value</code> if not null, otherwise writes zeros
107 * to the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by
111 * Long value to be written to the output.
113 * ByteBuf, where value or zeros are written.
115 public static void writeLong(final Long value, final ByteBuf output) {
117 output.writeLong(value);
119 output.writeZero(LONG_BYTES_LENGTH);
124 * Writes boolean <code>value</code> if not null, otherwise writes zero to
125 * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 1.
128 * Boolean value to be written to the output.
130 * ByteBuf, where value or zero is written.
132 public static void writeBoolean(final Boolean value, final ByteBuf output) {
134 output.writeBoolean(value);
136 output.writeZero(ONE_BYTE_LENGTH);
141 * Writes unsigned byte <code>value</code> if not null, otherwise writes
142 * zero to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
146 * Short value to be write to the output.
148 * ByteBuf, where value or zeros are written.
150 public static void writeUnsignedByte(final Short value, final ByteBuf output) {
152 output.writeByte(value);
154 output.writeZero(ONE_BYTE_LENGTH);
159 * Writes unsigned 16-bit short integer <code>value</code> if not null,
160 * otherwise writes zeros to the <code>output</code> ByteBuf. ByteBuf's
161 * writerIndex is increased by 2.
164 * Integer value to be written to the output.
166 * ByteBuf, where value or zeros are written.
168 public static void writeUnsignedShort(final Integer value, final ByteBuf output) {
170 output.writeShort(value.shortValue());
172 output.writeZero(SHORT_BYTES_LENGTH);
177 * Writes unsigned 32-bit integer <code>value</code> if not null, otherwise
178 * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
182 * Long value to be written to the output.
184 * ByteBuf, where value or zeros are written.
186 public static void writeUnsignedInt(final Long value, final ByteBuf output) {
188 output.writeInt(value.intValue());
190 output.writeZero(INT_BYTES_LENGTH);
195 * Writes unsigned 64-bit integer <code>value</code> if not null, otherwise
196 * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
200 * BigInteger value to be written to the output.
202 * ByteBuf, where value or zeros are written.
204 public static void writeUnsignedLong(final BigInteger value, final ByteBuf output) {
206 output.writeLong(value.longValue());
208 output.writeZero(LONG_BYTES_LENGTH);
213 * Writes IPv4 address if not null, otherwise writes zeros to the
214 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
217 * IPv4 address to be written to the output.
219 * ByteBuf, where ipv4Address or zeros are written.
221 public static void writeIpv4Address(final Ipv4Address ipv4Address, final ByteBuf output) {
222 if (ipv4Address != null) {
223 output.writeBytes(Ipv4Util.bytesForAddress(ipv4Address));
225 output.writeZero(Ipv4Util.IP4_LENGTH);
230 * Writes IPv4 prefix if not null, otherwise writes zeros to the
231 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 5.
234 * IPv4 prefix value to be written to the output. Prefix is
235 * written in the last byte.
237 * ByteBuf, where ipv4Prefix or zeros are written.
239 public static void writeIpv4Prefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
240 if (ipv4Prefix != null) {
241 output.writeBytes(Ipv4Util.bytesForPrefix(ipv4Prefix));
243 output.writeZero(IPV4_PREFIX_BYTE_LENGTH);
248 * Writes IPv6 address if not null, otherwise writes zeros to the
249 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
252 * IPv6 address to be written to the output.
254 * ByteBuf, where ipv6Address or zeros are written.
256 public static void writeIpv6Address(final Ipv6Address ipv6Address, final ByteBuf output) {
257 if (ipv6Address != null) {
258 output.writeBytes(Ipv6Util.bytesForAddress(ipv6Address));
260 output.writeZero(Ipv6Util.IPV6_LENGTH);
265 * Writes IPv6 prefix if not null, otherwise writes zeros to the
266 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 17.
269 * IPv6 prefix to be written to the output. Prefix is written in
272 * ByteBuf, where ipv6Prefix or zeros are written.
274 public static void writeIpv6Prefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
275 if (ipv6Prefix != null) {
276 output.writeBytes(Ipv6Util.bytesForPrefix(ipv6Prefix));
278 output.writeZero(IPV6_PREFIX_BYTE_LENGTH);
282 public static void writeMinimalPrefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
283 final byte[] bytes = IetfInetUtil.INSTANCE.ipv4PrefixToBytes(ipv4Prefix);
284 writeMinimalPrefix(output, bytes, bytes[Ipv4Util.IP4_LENGTH]);
287 public static void writeMinimalPrefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
288 final byte[] bytes = IetfInetUtil.INSTANCE.ipv6PrefixToBytes(ipv6Prefix);
289 writeMinimalPrefix(output, bytes, bytes[Ipv6Util.IPV6_LENGTH]);
292 private static void writeMinimalPrefix(final ByteBuf output, final byte[] bytes, final byte prefixBits) {
293 output.writeByte(prefixBits);
294 output.writeBytes(bytes, 0, Ipv4Util.prefixBitsToBytes(Byte.toUnsignedInt(prefixBits)));
298 * Writes Float32 <code>value</code> if not null, otherwise writes zeros to
299 * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
302 * Float32 value to be written to the output.
304 * ByteBuf, where value or zeros are written.
306 public static void writeFloat32(final Float32 value, final ByteBuf output) {
308 output.writeBytes(value.getValue());
310 output.writeZero(FLOAT32_BYTES_LENGTH);
315 * Writes BitSet's bits if not null, otherwise writes zeros to the
316 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by
320 * BitSet values to be written to the output, starting from left most bit.
321 * @param outputLength
322 * Number of bytes to be written, must be greater than 0.
324 * ByteBuf, where bitSet or zeros are written.
326 public static void writeBitSet(final BitSet bitSet, final int outputLength, final ByteBuf output) {
327 Preconditions.checkArgument(outputLength > 0);
328 if (bitSet != null) {
329 output.writeBytes(ByteArray.bitSetToBytes(bitSet, outputLength));
331 output.writeZero(outputLength);