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 private ByteBufWriteUtil() {
44 throw new UnsupportedOperationException();
48 * Writes 32-bit integer <code>value</code> if not null, otherwise writes
49 * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
53 * Integer value to be written to the output.
55 * ByteBuf, where value or zeros are written.
57 public static void writeInt(final Integer value, final ByteBuf output) {
59 output.writeInt(value);
61 output.writeZero(INT_BYTES_LENGTH);
66 * Writes 24-bit integer <code>value</code> if not null, otherwise writes
67 * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
71 * Medium value to be written to the output.
73 * ByteBuf, where value or zeros are written.
75 public static void writeMedium(final Integer value, final ByteBuf output) {
77 output.writeMedium(value);
79 output.writeZero(MEDIUM_BYTES_LENGTH);
84 * Writes 16-bit short <code>value</code> if not null, otherwise writes
85 * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
89 * Short value to be written to the output.
91 * ByteBuf, where value or zeros are written.
93 public static void writeShort(final Short value, final ByteBuf output) {
95 output.writeShort(value);
97 output.writeZero(SHORT_BYTES_LENGTH);
102 * Writes 64-bit long <code>value</code> if not null, otherwise writes zeros
103 * to the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by
107 * Long value to be written to the output.
109 * ByteBuf, where value or zeros are written.
111 public static void writeLong(final Long value, final ByteBuf output) {
113 output.writeLong(value);
115 output.writeZero(LONG_BYTES_LENGTH);
120 * Writes boolean <code>value</code> if not null, otherwise writes zero to
121 * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 1.
124 * Boolean value to be written to the output.
126 * ByteBuf, where value or zero is written.
128 public static void writeBoolean(final Boolean value, final ByteBuf output) {
130 output.writeBoolean(value);
132 output.writeZero(ONE_BYTE_LENGTH);
137 * Writes unsigned byte <code>value</code> if not null, otherwise writes
138 * zero to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
142 * Short value to be write to the output.
144 * ByteBuf, where value or zeros are written.
146 public static void writeUnsignedByte(final Short value, final ByteBuf output) {
148 output.writeByte(value);
150 output.writeZero(ONE_BYTE_LENGTH);
155 * Writes unsigned 16-bit short integer <code>value</code> if not null,
156 * otherwise writes zeros to the <code>output</code> ByteBuf. ByteBuf's
157 * writerIndex is increased by 2.
160 * Integer value to be written to the output.
162 * ByteBuf, where value or zeros are written.
164 public static void writeUnsignedShort(final Integer value, final ByteBuf output) {
166 output.writeShort(value.shortValue());
168 output.writeZero(SHORT_BYTES_LENGTH);
173 * Writes unsigned 32-bit integer <code>value</code> if not null, otherwise
174 * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
178 * Long value to be written to the output.
180 * ByteBuf, where value or zeros are written.
182 public static void writeUnsignedInt(final Long value, final ByteBuf output) {
184 output.writeInt(value.intValue());
186 output.writeZero(INT_BYTES_LENGTH);
191 * Writes unsigned 64-bit integer <code>value</code> if not null, otherwise
192 * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
196 * BigInteger value to be written to the output.
198 * ByteBuf, where value or zeros are written.
200 public static void writeUnsignedLong(final BigInteger value, final ByteBuf output) {
202 output.writeLong(value.longValue());
204 output.writeZero(LONG_BYTES_LENGTH);
209 * Writes IPv4 address if not null, otherwise writes zeros to the
210 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
213 * IPv4 address to be written to the output.
215 * ByteBuf, where ipv4Address or zeros are written.
217 public static void writeIpv4Address(final Ipv4Address ipv4Address, final ByteBuf output) {
218 if (ipv4Address != null) {
219 output.writeBytes(Ipv4Util.bytesForAddress(ipv4Address));
221 output.writeZero(Ipv4Util.IP4_LENGTH);
226 * Writes IPv4 prefix if not null, otherwise writes zeros to the
227 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 5.
230 * IPv4 prefix value to be written to the output. Prefix is
231 * written in the last byte.
233 * ByteBuf, where ipv4Prefix or zeros are written.
235 public static void writeIpv4Prefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
236 if (ipv4Prefix != null) {
237 output.writeBytes(Ipv4Util.bytesForPrefix(ipv4Prefix));
239 output.writeZero(IPV4_PREFIX_BYTE_LENGTH);
244 * Writes IPv6 address if not null, otherwise writes zeros to the
245 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
248 * IPv6 address to be written to the output.
250 * ByteBuf, where ipv6Address or zeros are written.
252 public static void writeIpv6Address(final Ipv6Address ipv6Address, final ByteBuf output) {
253 if (ipv6Address != null) {
254 output.writeBytes(Ipv6Util.bytesForAddress(ipv6Address));
256 output.writeZero(Ipv6Util.IPV6_LENGTH);
261 * Writes IPv6 prefix if not null, otherwise writes zeros to the
262 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 17.
265 * IPv6 prefix to be written to the output. Prefix is written in
268 * ByteBuf, where ipv6Prefix or zeros are written.
270 public static void writeIpv6Prefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
271 if (ipv6Prefix != null) {
272 output.writeBytes(Ipv6Util.bytesForPrefix(ipv6Prefix));
274 output.writeZero(IPV6_PREFIX_BYTE_LENGTH);
278 public static void writeMinimalPrefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
279 final byte[] bytes = IetfInetUtil.INSTANCE.ipv4PrefixToBytes(ipv4Prefix);
280 writeMinimalPrefix(output, bytes, bytes[Ipv4Util.IP4_LENGTH]);
283 public static void writeMinimalPrefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
284 final byte[] bytes = IetfInetUtil.INSTANCE.ipv6PrefixToBytes(ipv6Prefix);
285 writeMinimalPrefix(output, bytes, bytes[Ipv6Util.IPV6_LENGTH]);
288 private static void writeMinimalPrefix(final ByteBuf output, final byte[] bytes, final byte prefixBits) {
289 output.writeByte(prefixBits);
290 output.writeBytes(bytes, 0, Ipv4Util.prefixBitsToBytes(Byte.toUnsignedInt(prefixBits)));
294 * Writes Float32 <code>value</code> if not null, otherwise writes zeros to
295 * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
298 * Float32 value to be written to the output.
300 * ByteBuf, where value or zeros are written.
302 public static void writeFloat32(final Float32 value, final ByteBuf output) {
304 output.writeBytes(value.getValue());
306 output.writeZero(FLOAT32_BYTES_LENGTH);
311 * Writes BitSet's bits if not null, otherwise writes zeros to the
312 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by
316 * BitSet values to be written to the output, starting from left most bit.
317 * @param outputLength
318 * Number of bytes to be written, must be greater than 0.
320 * ByteBuf, where bitSet or zeros are written.
322 public static void writeBitSet(final BitSet bitSet, final int outputLength, final ByteBuf output) {
323 Preconditions.checkArgument(outputLength > 0);
324 if (bitSet != null) {
325 output.writeBytes(ByteArray.bitSetToBytes(bitSet, outputLength));
327 output.writeZero(outputLength);