bt.protocol.handler.PortMessageHandler Maven / Gradle / Ivy
/*
* Copyright (c) 2016—2017 Andrei Tomashpolskiy and individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package bt.protocol.handler;
import bt.BtException;
import bt.protocol.DecodingContext;
import bt.protocol.EncodingContext;
import bt.protocol.InvalidMessageException;
import bt.protocol.Port;
import bt.protocol.Protocols;
import java.nio.ByteBuffer;
import static bt.protocol.Protocols.getShortBytes;
import static bt.protocol.Protocols.verifyPayloadHasLength;
/**
* @since 1.1
*/
public final class PortMessageHandler extends UniqueMessageHandler {
public static final int PORT_ID = 9;
private static final int EXPECTED_PAYLOAD_LENGTH = Short.BYTES;
public PortMessageHandler() {
super(Port.class);
}
@Override
public int doDecode(DecodingContext context, ByteBuffer buffer) {
verifyPayloadHasLength(Port.class, EXPECTED_PAYLOAD_LENGTH, buffer.remaining());
return decodePort(context, buffer);
}
@Override
public boolean doEncode(EncodingContext context, Port message, ByteBuffer buffer) {
return writePort(message.getPort(), buffer);
}
// port:
private static boolean writePort(int port, ByteBuffer buffer) {
if (port < 0 || port > Short.MAX_VALUE * 2 + 1) {
throw new BtException("Invalid port: " + port);
}
if (buffer.remaining() < Short.BYTES) {
return false;
}
buffer.put(getShortBytes(port));
return true;
}
private static int decodePort(DecodingContext context, ByteBuffer buffer) throws InvalidMessageException {
int consumed = 0;
int length = Short.BYTES;
Short s;
if ((s = Protocols.readShort(buffer)) != null) {
int port = s & 0x0000FFFF;
context.setMessage(new Port(port));
consumed = length;
}
return consumed;
}
}