in coap/src/main/java/org/apache/mina/coap/codec/CoapDecoder.java [62:116]
public CoapMessage decode(ByteBuffer input, Void context) {
LOG.debug("decode");
if (input.remaining() <= 0) {
LOG.debug("nothing to decode");
return null;
}
int byte0 = input.get() & 0xFF;
int version = (byte0 >> 6) & 0x3;
MessageType type = MessageType.fromCode((byte0 >> 4) & 0x3);
byte[] token = new byte[byte0 & 0xF];
int code = input.get() & 0xFF;
int id = input.getShort() & 0xFFFF;
input.get(token);
// decode options
int optionCode = 0;
byte[] payload = EMPTY_PAYLOAD;
List<CoapOption> options = new ArrayList<CoapOption>();
while (input.hasRemaining()) {
int next = input.get() & 0xFF;
// start of payload ?
if (next == 0xFF) {
payload = new byte[input.remaining()];
input.get(payload);
break;
} else {
int optionDeltaQuartet = (next >> 4) & 0xF;
optionCode += optionFromQuartet(optionDeltaQuartet, input);
// decode the option length
int optionLenQuartet = next & 0x0F;
int optionLength = optionFromQuartet(optionLenQuartet, input);
// create the option DTO
CoapOptionType optType = CoapOptionType.fromCode(optionCode);
if (optType == null) {
throw new ProtocolDecoderException("unknown option code : " + optionCode);
}
// get the value
byte[] optionValue = new byte[optionLength];
input.get(optionValue);
options.add(new CoapOption(optType, optionValue));
}
}
if (input.hasRemaining()) {
throw new ProtocolDecoderException("trailling " + input.remaining() + " bytes in the UDP datagram");
}
return new CoapMessage(version, type, code, id, token, options.toArray(EMPTY_OPTION), payload);
}