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 io.netty.buffer.ByteBuf;
12 import java.math.BigInteger;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
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;
21 import org.opendaylight.yangtools.yang.common.Uint16;
22 import org.opendaylight.yangtools.yang.common.Uint32;
23 import org.opendaylight.yangtools.yang.common.Uint64;
24 import org.opendaylight.yangtools.yang.common.Uint8;
27 * Utility class for ByteBuf's write methods.
29 public final class ByteBufWriteUtil {
31 public static final int SHORT_BYTES_LENGTH = Short.SIZE / Byte.SIZE;
33 public static final int INT_BYTES_LENGTH = Integer.SIZE / Byte.SIZE;
35 public static final int LONG_BYTES_LENGTH = Long.SIZE / Byte.SIZE;
37 public static final int FLOAT32_BYTES_LENGTH = INT_BYTES_LENGTH;
39 public static final int ONE_BYTE_LENGTH = 1;
41 public static final int IPV4_PREFIX_BYTE_LENGTH = Ipv4Util.IP4_LENGTH + 1;
43 public static final int IPV6_PREFIX_BYTE_LENGTH = Ipv6Util.IPV6_LENGTH + 1;
45 private ByteBufWriteUtil() {
46 throw new UnsupportedOperationException();
50 * Writes 32-bit integer <code>value</code> if not null, otherwise writes
51 * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
55 * Integer value to be written to the output.
57 * ByteBuf, where value or zeros are written.
59 public static void writeInt(final Integer value, final ByteBuf output) {
60 output.writeInt(value != null ? value : 0);
64 * Writes 24-bit integer <code>value</code> if not null, otherwise writes
65 * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
69 * Medium value to be written to the output.
71 * ByteBuf, where value or zeros are written.
73 public static void writeMedium(final Integer value, final ByteBuf output) {
74 output.writeMedium(value != null ? value : 0);
78 * Writes 16-bit short <code>value</code> if not null, otherwise writes
79 * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
83 * Short value to be written to the output.
85 * ByteBuf, where value or zeros are written.
87 public static void writeShort(final Short value, final ByteBuf output) {
88 output.writeShort(value != null ? value : 0);
92 * Writes 64-bit long <code>value</code> if not null, otherwise writes zeros
93 * to the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by
97 * Long value to be written to the output.
99 * ByteBuf, where value or zeros are written.
101 public static void writeLong(final Long value, final ByteBuf output) {
102 output.writeLong(value != null ? value : 0L);
106 * Writes boolean <code>value</code> if not null, otherwise writes zero to
107 * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 1.
110 * Boolean value to be written to the output.
112 * ByteBuf, where value or zero is written.
114 public static void writeBoolean(final Boolean value, final ByteBuf output) {
116 output.writeBoolean(value);
123 * Writes unsigned byte <code>value</code> if not null, otherwise writes
124 * zero to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
128 * Short value to be write to the output.
130 * ByteBuf, where value or zeros are written.
131 * @deprecated Use {@link #writeUnsignedByte(Uint8, ByteBuf)} instead.
133 @Deprecated(forRemoval = true)
134 public static void writeUnsignedByte(final Short value, final ByteBuf output) {
135 output.writeByte(value != null ? value : 0);
139 * Writes unsigned byte <code>value</code> if not null, otherwise writes
140 * zero to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
144 * Short value to be write to the output.
146 * ByteBuf, where value or zeros are written.
148 public static void writeUnsignedByte(final Uint8 value, final ByteBuf output) {
149 output.writeByte(value != null ? value.byteValue() : 0);
153 * Writes unsigned 16-bit short integer <code>value</code> if not null,
154 * otherwise writes zeros to the <code>output</code> ByteBuf. ByteBuf's
155 * writerIndex is increased by 2.
158 * Integer value to be written to the output.
160 * ByteBuf, where value or zeros are written.
161 * @deprecated Use {@link #writeUnsignedShort(Uint16, ByteBuf)} instead.
163 @Deprecated(forRemoval = true)
164 public static void writeUnsignedShort(final Integer value, final ByteBuf output) {
165 output.writeShort(value != null ? value.shortValue() : 0);
169 * Writes unsigned 16-bit short integer <code>value</code> if not null,
170 * otherwise writes zeros to the <code>output</code> ByteBuf. ByteBuf's
171 * writerIndex is increased by 2.
174 * Integer value to be written to the output.
176 * ByteBuf, where value or zeros are written.
178 public static void writeUnsignedShort(final Uint16 value, final ByteBuf output) {
179 output.writeShort(value != null ? value.shortValue() : 0);
183 * Writes unsigned 32-bit integer <code>value</code> if not null, otherwise
184 * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
188 * Long value to be written to the output.
190 * ByteBuf, where value or zeros are written.
191 * @deprecated Use {@link #writeUnsignedInt(Uint32, ByteBuf)} instead.
193 @Deprecated(forRemoval = true)
194 public static void writeUnsignedInt(final Long value, final ByteBuf output) {
195 output.writeInt(value != null ? value.intValue() : 0);
199 * Writes unsigned 32-bit integer <code>value</code> if not null, otherwise
200 * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
204 * Long value to be written to the output.
206 * ByteBuf, where value or zeros are written.
208 public static void writeUnsignedInt(final Uint32 value, final ByteBuf output) {
209 output.writeInt(value != null ? value.intValue() : 0);
213 * Writes unsigned 64-bit integer <code>value</code> if not null, otherwise
214 * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
218 * BigInteger value to be written to the output.
220 * ByteBuf, where value or zeros are written.
221 * @deprecated Use {@link #writeUnsignedLong(Uint64, ByteBuf)} instead.
223 @Deprecated(forRemoval = true)
224 public static void writeUnsignedLong(final BigInteger value, final ByteBuf output) {
225 output.writeLong(value != null ? value.longValue() : 0L);
229 * Writes unsigned 64-bit integer <code>value</code> if not null, otherwise
230 * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
234 * BigInteger value to be written to the output.
236 * ByteBuf, where value or zeros are written.
238 public static void writeUnsignedLong(final Uint64 value, final ByteBuf output) {
239 output.writeLong(value != null ? value.longValue() : 0L);
243 * Writes IPv4 address if not null, otherwise writes zeros to the
244 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
247 * IPv4 address to be written to the output.
249 * ByteBuf, where ipv4Address or zeros are written.
251 public static void writeIpv4Address(final Ipv4Address ipv4Address, final ByteBuf output) {
252 if (ipv4Address != null) {
253 output.writeBytes(Ipv4Util.bytesForAddress(ipv4Address));
260 * Writes IPv4 address if not null, otherwise writes zeros to the
261 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
264 * IPv4 address to be written to the output.
266 * ByteBuf, where ipv4Address or zeros are written.
268 public static void writeIpv4Address(final Ipv4AddressNoZone ipv4Address, final ByteBuf output) {
269 if (ipv4Address != null) {
270 output.writeBytes(IetfInetUtil.INSTANCE.ipv4AddressNoZoneBytes(ipv4Address));
277 * Writes IPv4 prefix if not null, otherwise writes zeros to the
278 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 5.
281 * IPv4 prefix value to be written to the output. Prefix is
282 * written in the last byte.
284 * ByteBuf, where ipv4Prefix or zeros are written.
286 public static void writeIpv4Prefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
287 if (ipv4Prefix != null) {
288 output.writeBytes(Ipv4Util.bytesForPrefix(ipv4Prefix));
290 output.writeZero(IPV4_PREFIX_BYTE_LENGTH);
295 * Writes IPv6 address if not null, otherwise writes zeros to the
296 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
299 * IPv6 address to be written to the output.
301 * ByteBuf, where ipv6Address or zeros are written.
303 public static void writeIpv6Address(final Ipv6Address ipv6Address, final ByteBuf output) {
304 if (ipv6Address != null) {
305 output.writeBytes(Ipv6Util.bytesForAddress(ipv6Address));
307 output.writeZero(Ipv6Util.IPV6_LENGTH);
312 * Writes IPv6 address if not null, otherwise writes zeros to the
313 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
316 * IPv6 address to be written to the output.
318 * ByteBuf, where ipv6Address or zeros are written.
320 public static void writeIpv6Address(final Ipv6AddressNoZone ipv6Address, final ByteBuf output) {
321 if (ipv6Address != null) {
322 output.writeBytes(IetfInetUtil.INSTANCE.ipv6AddressNoZoneBytes(ipv6Address));
324 output.writeZero(Ipv6Util.IPV6_LENGTH);
329 * Writes IPv6 prefix if not null, otherwise writes zeros to the
330 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 17.
333 * IPv6 prefix to be written to the output. Prefix is written in
336 * ByteBuf, where ipv6Prefix or zeros are written.
338 public static void writeIpv6Prefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
339 if (ipv6Prefix != null) {
340 output.writeBytes(Ipv6Util.bytesForPrefix(ipv6Prefix));
342 output.writeZero(IPV6_PREFIX_BYTE_LENGTH);
346 public static void writeMinimalPrefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
347 final byte[] bytes = IetfInetUtil.INSTANCE.ipv4PrefixToBytes(ipv4Prefix);
348 writeMinimalPrefix(output, bytes, bytes[Ipv4Util.IP4_LENGTH]);
351 public static void writeMinimalPrefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
352 final byte[] bytes = IetfInetUtil.INSTANCE.ipv6PrefixToBytes(ipv6Prefix);
353 writeMinimalPrefix(output, bytes, bytes[Ipv6Util.IPV6_LENGTH]);
356 private static void writeMinimalPrefix(final ByteBuf output, final byte[] bytes, final byte prefixBits) {
357 output.writeByte(prefixBits);
358 output.writeBytes(bytes, 0, Ipv4Util.prefixBitsToBytes(Byte.toUnsignedInt(prefixBits)));
362 * Writes Float32 <code>value</code> if not null, otherwise writes zeros to
363 * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
366 * Float32 value to be written to the output.
368 * ByteBuf, where value or zeros are written.
370 public static void writeFloat32(final Float32 value, final ByteBuf output) {
372 output.writeBytes(value.getValue());