@Test public void testGetByteArrayDoesNotAlterMessage() { Request req = Request.newGet(); req.setToken(new byte[] { 0x00 }); req.getOptions().setObserve(0); serializer.getByteArray(req); assertNull(req.getBytes()); }
public final byte[] getByteArray(final Message message) { if (message == null) { throw new NullPointerException("message must not be null!"); } DatagramWriter messageWriter = new DatagramWriter(); if (message.getRawCode() == 0) { if (message.getType() == Type.NON) { throw new IllegalArgumentException("NON message must not use code 0 (empty message)!"); } else if (!message.getToken().isEmpty()) { throw new IllegalArgumentException("Empty messages must not use a token!"); } else if (message.getPayloadSize() > 0) { throw new IllegalArgumentException("Empty messages must not contain payload!"); } MessageHeader header = new MessageHeader(CoAP.VERSION, message.getType(), message.getToken(), 0, message.getMID(), 0); serializeHeader(messageWriter, header); messageWriter.writeCurrentByte(); } else { DatagramWriter optionsAndPayloadWriter = new DatagramWriter(); serializeOptionsAndPayload(optionsAndPayloadWriter, message.getOptions(), message.getPayload()); optionsAndPayloadWriter.writeCurrentByte(); MessageHeader header = new MessageHeader(CoAP.VERSION, message.getType(), message.getToken(), message.getRawCode(), message.getMID(), optionsAndPayloadWriter.size()); serializeHeader(messageWriter, header); messageWriter.writeCurrentByte(); messageWriter.write(optionsAndPayloadWriter); } return messageWriter.toByteArray(); }
DataSerializer { public final byte[] getByteArray(final Message message) { if (message == null) { throw new NullPointerException("message must not be null!"); } DatagramWriter messageWriter = new DatagramWriter(); if (message.getRawCode() == 0) { if (message.getType() == Type.NON) { throw new IllegalArgumentException("NON message must not use code 0 (empty message)!"); } else if (!message.getToken().isEmpty()) { throw new IllegalArgumentException("Empty messages must not use a token!"); } else if (message.getPayloadSize() > 0) { throw new IllegalArgumentException("Empty messages must not contain payload!"); } MessageHeader header = new MessageHeader(CoAP.VERSION, message.getType(), message.getToken(), 0, message.getMID(), 0); serializeHeader(messageWriter, header); messageWriter.writeCurrentByte(); } else { DatagramWriter optionsAndPayloadWriter = new DatagramWriter(); serializeOptionsAndPayload(optionsAndPayloadWriter, message.getOptions(), message.getPayload()); optionsAndPayloadWriter.writeCurrentByte(); MessageHeader header = new MessageHeader(CoAP.VERSION, message.getType(), message.getToken(), message.getRawCode(), message.getMID(), optionsAndPayloadWriter.size()); serializeHeader(messageWriter, header); messageWriter.writeCurrentByte(); messageWriter.write(optionsAndPayloadWriter); } return messageWriter.toByteArray(); } }
DataSerializer { public final byte[] getByteArray(final Message message) { if (message == null) { throw new NullPointerException("message must not be null!"); } DatagramWriter messageWriter = new DatagramWriter(); if (message.getRawCode() == 0) { if (message.getType() == Type.NON) { throw new IllegalArgumentException("NON message must not use code 0 (empty message)!"); } else if (!message.getToken().isEmpty()) { throw new IllegalArgumentException("Empty messages must not use a token!"); } else if (message.getPayloadSize() > 0) { throw new IllegalArgumentException("Empty messages must not contain payload!"); } MessageHeader header = new MessageHeader(CoAP.VERSION, message.getType(), message.getToken(), 0, message.getMID(), 0); serializeHeader(messageWriter, header); messageWriter.writeCurrentByte(); } else { DatagramWriter optionsAndPayloadWriter = new DatagramWriter(); serializeOptionsAndPayload(optionsAndPayloadWriter, message.getOptions(), message.getPayload()); optionsAndPayloadWriter.writeCurrentByte(); MessageHeader header = new MessageHeader(CoAP.VERSION, message.getType(), message.getToken(), message.getRawCode(), message.getMID(), optionsAndPayloadWriter.size()); serializeHeader(messageWriter, header); messageWriter.writeCurrentByte(); messageWriter.write(optionsAndPayloadWriter); } return messageWriter.toByteArray(); }  }
DataSerializer { public final byte[] getByteArray(final Message message) { if (message == null) { throw new NullPointerException("message must not be null!"); } DatagramWriter messageWriter = new DatagramWriter(); if (message.getRawCode() == 0) { if (message.getType() == Type.NON) { throw new IllegalArgumentException("NON message must not use code 0 (empty message)!"); } else if (!message.getToken().isEmpty()) { throw new IllegalArgumentException("Empty messages must not use a token!"); } else if (message.getPayloadSize() > 0) { throw new IllegalArgumentException("Empty messages must not contain payload!"); } MessageHeader header = new MessageHeader(CoAP.VERSION, message.getType(), message.getToken(), 0, message.getMID(), 0); serializeHeader(messageWriter, header); messageWriter.writeCurrentByte(); } else { DatagramWriter optionsAndPayloadWriter = new DatagramWriter(); serializeOptionsAndPayload(optionsAndPayloadWriter, message.getOptions(), message.getPayload()); optionsAndPayloadWriter.writeCurrentByte(); MessageHeader header = new MessageHeader(CoAP.VERSION, message.getType(), message.getToken(), message.getRawCode(), message.getMID(), optionsAndPayloadWriter.size()); serializeHeader(messageWriter, header); messageWriter.writeCurrentByte(); messageWriter.write(optionsAndPayloadWriter); } return messageWriter.toByteArray(); }  final byte[] getByteArray(final Message message); final RawData serializeRequest(final Request request); final RawData serializeRequest(final Request request, final MessageCallback outboundCallback); final RawData serializeResponse(final Response response); final RawData serializeResponse(final Response response, final MessageCallback outboundCallback); final RawData serializeEmptyMessage(final EmptyMessage emptyMessage); final RawData serializeEmptyMessage(final EmptyMessage emptyMessage, final MessageCallback outboundCallback); static void serializeOptionsAndPayload(DatagramWriter writer, final OptionSet optionSet,
			final byte[] payload); }
DataSerializer { public final byte[] getByteArray(final Message message) { if (message == null) { throw new NullPointerException("message must not be null!"); } DatagramWriter messageWriter = new DatagramWriter(); if (message.getRawCode() == 0) { if (message.getType() == Type.NON) { throw new IllegalArgumentException("NON message must not use code 0 (empty message)!"); } else if (!message.getToken().isEmpty()) { throw new IllegalArgumentException("Empty messages must not use a token!"); } else if (message.getPayloadSize() > 0) { throw new IllegalArgumentException("Empty messages must not contain payload!"); } MessageHeader header = new MessageHeader(CoAP.VERSION, message.getType(), message.getToken(), 0, message.getMID(), 0); serializeHeader(messageWriter, header); messageWriter.writeCurrentByte(); } else { DatagramWriter optionsAndPayloadWriter = new DatagramWriter(); serializeOptionsAndPayload(optionsAndPayloadWriter, message.getOptions(), message.getPayload()); optionsAndPayloadWriter.writeCurrentByte(); MessageHeader header = new MessageHeader(CoAP.VERSION, message.getType(), message.getToken(), message.getRawCode(), message.getMID(), optionsAndPayloadWriter.size()); serializeHeader(messageWriter, header); messageWriter.writeCurrentByte(); messageWriter.write(optionsAndPayloadWriter); } return messageWriter.toByteArray(); }  final byte[] getByteArray(final Message message); final RawData serializeRequest(final Request request); final RawData serializeRequest(final Request request, final MessageCallback outboundCallback); final RawData serializeResponse(final Response response); final RawData serializeResponse(final Response response, final MessageCallback outboundCallback); final RawData serializeEmptyMessage(final EmptyMessage emptyMessage); final RawData serializeEmptyMessage(final EmptyMessage emptyMessage, final MessageCallback outboundCallback); static void serializeOptionsAndPayload(DatagramWriter writer, final OptionSet optionSet,
			final byte[] payload);  }