Run safelyDisconnect() on event loop
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / handler / MessageParts.java
1 /*
2  * Copyright (c) 2018 Inocybe Technologies 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.netconf.nettyutil.handler;
9
10 import java.nio.CharBuffer;
11 import java.nio.charset.CharacterCodingException;
12 import java.nio.charset.CharsetEncoder;
13 import java.nio.charset.CodingErrorAction;
14 import java.nio.charset.StandardCharsets;
15 import org.opendaylight.netconf.util.messages.FramingMechanism;
16
17 /**
18  * netconf message part constants as bytes.
19  *
20  * @author Thomas Pantelis
21  */
22 final class MessageParts {
23     static final byte[] END_OF_MESSAGE;
24     static final byte[] START_OF_CHUNK;
25     static final byte[] END_OF_CHUNK;
26
27     static {
28         final var encoder = StandardCharsets.US_ASCII.newEncoder()
29             .onMalformedInput(CodingErrorAction.REPORT)
30             .onUnmappableCharacter(CodingErrorAction.REPORT);
31
32         try {
33             END_OF_MESSAGE = getBytes(encoder, FramingMechanism.EOM_STR);
34             START_OF_CHUNK = getBytes(encoder, FramingMechanism.CHUNK_START_STR);
35             END_OF_CHUNK = getBytes(encoder, FramingMechanism.CHUNK_END_STR);
36         } catch (CharacterCodingException e) {
37             throw new ExceptionInInitializerError(e);
38         }
39     }
40
41     private MessageParts() {
42         // Hidden on purpose
43     }
44
45     private static byte[] getBytes(final CharsetEncoder encoder, final String str) throws CharacterCodingException {
46         final var buf = encoder.encode(CharBuffer.wrap(str));
47         final var bytes = new byte[buf.remaining()];
48         buf.get(bytes);
49         return bytes;
50     }
51 }