@Test public void testEncode() throws URISyntaxException { URI uri = new URI("coap: CoAPMessageType type = CoAPMessageType.NON_CONFIRMABLE; int msgCode = 2; int version = 1; short id = 2; }
public byte[] encode() throws IncorrectMessageException { LOGGER.debug("CoAPMessageWriter: encode a message with message ID " + message.getIdentifier()); int version = message.getVersion(); int messageType = message.getMessageType().getNo(); byte[] token = message.getToken(); int tokenLength = token == null ? 0 : token.length; if (tokenLength > 8) { throw new IncorrectMessageException("Wrong token length: " + tokenLength); } CoAPCode messageCode = message.getCode(); byte headerByte = setBitsInByte(0, VERSION_START, VERSION_LENGTH, getBitsInIntAsByte(version, 0, VERSION_LENGTH)); headerByte = setBitsInByte(headerByte, TYPE_START, TYPE_LENGTH, getBitsInIntAsByte(messageType, 0, TYPE_LENGTH)); headerByte = setBitsInByte(headerByte, TOKEN_LENGTH_START, TOKEN_LENGTH_LENGTH, getBitsInIntAsByte(tokenLength, 0, TOKEN_LENGTH_LENGTH)); outputStream.write(headerByte); byte codeByte = setBitsInByte(0, CODE_START, CODE_LENGTH, getBitsInIntAsByte(messageCode.getNo(), 0, CODE_LENGTH)); outputStream.write(codeByte); byte[] messageIdBytes = splitIntToBytes(message.getMessageId()); outputStream.write(messageIdBytes[2]); outputStream.write(messageIdBytes[3]); for (int n = 0; n < tokenLength; n++) { outputStream.write(token[n]); } this.encodeOptionHeaders(); if (message.getPayload() != null && message.getPayload().length > 0) { try { outputStream.write(PAYLOAD_MARKER); outputStream.write(message.getPayload()); } catch (IOException e) { LOGGER.debug("Writing payload failed.", e); } } byte[] byteArray = outputStream.toByteArray(); try { outputStream.flush(); outputStream.close(); } catch (IOException e) { LOGGER.debug("Ending message writing failed.", e); } return byteArray; }
CoAPMessageWriter implements CoAPMessageFormat { public byte[] encode() throws IncorrectMessageException { LOGGER.debug("CoAPMessageWriter: encode a message with message ID " + message.getIdentifier()); int version = message.getVersion(); int messageType = message.getMessageType().getNo(); byte[] token = message.getToken(); int tokenLength = token == null ? 0 : token.length; if (tokenLength > 8) { throw new IncorrectMessageException("Wrong token length: " + tokenLength); } CoAPCode messageCode = message.getCode(); byte headerByte = setBitsInByte(0, VERSION_START, VERSION_LENGTH, getBitsInIntAsByte(version, 0, VERSION_LENGTH)); headerByte = setBitsInByte(headerByte, TYPE_START, TYPE_LENGTH, getBitsInIntAsByte(messageType, 0, TYPE_LENGTH)); headerByte = setBitsInByte(headerByte, TOKEN_LENGTH_START, TOKEN_LENGTH_LENGTH, getBitsInIntAsByte(tokenLength, 0, TOKEN_LENGTH_LENGTH)); outputStream.write(headerByte); byte codeByte = setBitsInByte(0, CODE_START, CODE_LENGTH, getBitsInIntAsByte(messageCode.getNo(), 0, CODE_LENGTH)); outputStream.write(codeByte); byte[] messageIdBytes = splitIntToBytes(message.getMessageId()); outputStream.write(messageIdBytes[2]); outputStream.write(messageIdBytes[3]); for (int n = 0; n < tokenLength; n++) { outputStream.write(token[n]); } this.encodeOptionHeaders(); if (message.getPayload() != null && message.getPayload().length > 0) { try { outputStream.write(PAYLOAD_MARKER); outputStream.write(message.getPayload()); } catch (IOException e) { LOGGER.debug("Writing payload failed.", e); } } byte[] byteArray = outputStream.toByteArray(); try { outputStream.flush(); outputStream.close(); } catch (IOException e) { LOGGER.debug("Ending message writing failed.", e); } return byteArray; } }
CoAPMessageWriter implements CoAPMessageFormat { public byte[] encode() throws IncorrectMessageException { LOGGER.debug("CoAPMessageWriter: encode a message with message ID " + message.getIdentifier()); int version = message.getVersion(); int messageType = message.getMessageType().getNo(); byte[] token = message.getToken(); int tokenLength = token == null ? 0 : token.length; if (tokenLength > 8) { throw new IncorrectMessageException("Wrong token length: " + tokenLength); } CoAPCode messageCode = message.getCode(); byte headerByte = setBitsInByte(0, VERSION_START, VERSION_LENGTH, getBitsInIntAsByte(version, 0, VERSION_LENGTH)); headerByte = setBitsInByte(headerByte, TYPE_START, TYPE_LENGTH, getBitsInIntAsByte(messageType, 0, TYPE_LENGTH)); headerByte = setBitsInByte(headerByte, TOKEN_LENGTH_START, TOKEN_LENGTH_LENGTH, getBitsInIntAsByte(tokenLength, 0, TOKEN_LENGTH_LENGTH)); outputStream.write(headerByte); byte codeByte = setBitsInByte(0, CODE_START, CODE_LENGTH, getBitsInIntAsByte(messageCode.getNo(), 0, CODE_LENGTH)); outputStream.write(codeByte); byte[] messageIdBytes = splitIntToBytes(message.getMessageId()); outputStream.write(messageIdBytes[2]); outputStream.write(messageIdBytes[3]); for (int n = 0; n < tokenLength; n++) { outputStream.write(token[n]); } this.encodeOptionHeaders(); if (message.getPayload() != null && message.getPayload().length > 0) { try { outputStream.write(PAYLOAD_MARKER); outputStream.write(message.getPayload()); } catch (IOException e) { LOGGER.debug("Writing payload failed.", e); } } byte[] byteArray = outputStream.toByteArray(); try { outputStream.flush(); outputStream.close(); } catch (IOException e) { LOGGER.debug("Ending message writing failed.", e); } return byteArray; } CoAPMessageWriter(CoAPMessage message); }
CoAPMessageWriter implements CoAPMessageFormat { public byte[] encode() throws IncorrectMessageException { LOGGER.debug("CoAPMessageWriter: encode a message with message ID " + message.getIdentifier()); int version = message.getVersion(); int messageType = message.getMessageType().getNo(); byte[] token = message.getToken(); int tokenLength = token == null ? 0 : token.length; if (tokenLength > 8) { throw new IncorrectMessageException("Wrong token length: " + tokenLength); } CoAPCode messageCode = message.getCode(); byte headerByte = setBitsInByte(0, VERSION_START, VERSION_LENGTH, getBitsInIntAsByte(version, 0, VERSION_LENGTH)); headerByte = setBitsInByte(headerByte, TYPE_START, TYPE_LENGTH, getBitsInIntAsByte(messageType, 0, TYPE_LENGTH)); headerByte = setBitsInByte(headerByte, TOKEN_LENGTH_START, TOKEN_LENGTH_LENGTH, getBitsInIntAsByte(tokenLength, 0, TOKEN_LENGTH_LENGTH)); outputStream.write(headerByte); byte codeByte = setBitsInByte(0, CODE_START, CODE_LENGTH, getBitsInIntAsByte(messageCode.getNo(), 0, CODE_LENGTH)); outputStream.write(codeByte); byte[] messageIdBytes = splitIntToBytes(message.getMessageId()); outputStream.write(messageIdBytes[2]); outputStream.write(messageIdBytes[3]); for (int n = 0; n < tokenLength; n++) { outputStream.write(token[n]); } this.encodeOptionHeaders(); if (message.getPayload() != null && message.getPayload().length > 0) { try { outputStream.write(PAYLOAD_MARKER); outputStream.write(message.getPayload()); } catch (IOException e) { LOGGER.debug("Writing payload failed.", e); } } byte[] byteArray = outputStream.toByteArray(); try { outputStream.flush(); outputStream.close(); } catch (IOException e) { LOGGER.debug("Ending message writing failed.", e); } return byteArray; } CoAPMessageWriter(CoAPMessage message); byte[] encode(); }
CoAPMessageWriter implements CoAPMessageFormat { public byte[] encode() throws IncorrectMessageException { LOGGER.debug("CoAPMessageWriter: encode a message with message ID " + message.getIdentifier()); int version = message.getVersion(); int messageType = message.getMessageType().getNo(); byte[] token = message.getToken(); int tokenLength = token == null ? 0 : token.length; if (tokenLength > 8) { throw new IncorrectMessageException("Wrong token length: " + tokenLength); } CoAPCode messageCode = message.getCode(); byte headerByte = setBitsInByte(0, VERSION_START, VERSION_LENGTH, getBitsInIntAsByte(version, 0, VERSION_LENGTH)); headerByte = setBitsInByte(headerByte, TYPE_START, TYPE_LENGTH, getBitsInIntAsByte(messageType, 0, TYPE_LENGTH)); headerByte = setBitsInByte(headerByte, TOKEN_LENGTH_START, TOKEN_LENGTH_LENGTH, getBitsInIntAsByte(tokenLength, 0, TOKEN_LENGTH_LENGTH)); outputStream.write(headerByte); byte codeByte = setBitsInByte(0, CODE_START, CODE_LENGTH, getBitsInIntAsByte(messageCode.getNo(), 0, CODE_LENGTH)); outputStream.write(codeByte); byte[] messageIdBytes = splitIntToBytes(message.getMessageId()); outputStream.write(messageIdBytes[2]); outputStream.write(messageIdBytes[3]); for (int n = 0; n < tokenLength; n++) { outputStream.write(token[n]); } this.encodeOptionHeaders(); if (message.getPayload() != null && message.getPayload().length > 0) { try { outputStream.write(PAYLOAD_MARKER); outputStream.write(message.getPayload()); } catch (IOException e) { LOGGER.debug("Writing payload failed.", e); } } byte[] byteArray = outputStream.toByteArray(); try { outputStream.flush(); outputStream.close(); } catch (IOException e) { LOGGER.debug("Ending message writing failed.", e); } return byteArray; } CoAPMessageWriter(CoAPMessage message); byte[] encode();  }