Bug-1771: Private constuctors in utility static classes throws UnsupportedOperationEx...
[bgpcep.git] / bgp / parser-spi / src / main / java / org / opendaylight / protocol / bgp / parser / spi / MessageUtil.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.parser.spi;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.primitives.UnsignedBytes;
12 import io.netty.buffer.ByteBuf;
13 import java.util.Arrays;
14
15 public final class MessageUtil {
16
17     @VisibleForTesting
18     public static final int MARKER_LENGTH = 16;
19     @VisibleForTesting
20     public static final int COMMON_HEADER_LENGTH = 19;
21     private static final byte[] MARKER = new byte[MARKER_LENGTH];
22
23     static {
24         Arrays.fill(MARKER, 0, MARKER_LENGTH, UnsignedBytes.MAX_VALUE);
25     }
26
27     private MessageUtil() {
28         throw new UnsupportedOperationException();
29     }
30
31     /**
32      * Adds header to message value.
33      *
34      * @param type of the message
35      * @param value message value
36      * @param buffer ByteBuf where the message will be copied with its header
37      */
38     public static void formatMessage(final int type, final ByteBuf body, final ByteBuf buffer) {
39         buffer.writeBytes(MARKER);
40         buffer.writeShort(body.writerIndex() + COMMON_HEADER_LENGTH);
41         buffer.writeByte(type);
42         buffer.writeBytes(body);
43     }
44 }