/** * Serializes the specified response. Message identifier, message code, * token, options and payload are converted into a byte array and wrapped in * a {@link RawData} object. The response's destination address and port are * stored as address and port in the RawData object. * * @param response the response * @return the response as raw data */ public RawData serialize(Response response) { byte[] bytes = response.getBytes(); if (bytes == null) bytes = new DataSerializer().serializeResponse(response); response.setBytes(bytes); return new RawData(bytes, response.getDestination(), response.getDestinationPort()); }
/** * Serializes response and caches bytes on the request object to skip future serializations. * * @param response The response to serialize. * @return The object containing the serialized response. */ public final RawData serializeResponse(final Response response) { if (response.getBytes() == null) { DatagramWriter writer = new DatagramWriter(); byte[] body = serializeOptionsAndPayload(response); MessageHeader header = new MessageHeader(CoAP.VERSION, response.getType(), response.getToken(), response.getCode().value, response.getMID(), body.length); serializeHeader(writer, header); writer.writeBytes(body); byte[] bytes = writer.toByteArray(); response.setBytes(bytes); } return new RawData(response.getBytes(), response.getDestination(), response.getDestinationPort()); }
private static Response responseFor(final Request request) { Response response = new Response(ResponseCode.CONTENT); response.setMID(request.getMID()); response.setToken(request.getToken()); response.setBytes(new byte[]{}); response.setSource(request.getDestination()); response.setSourcePort(request.getDestinationPort()); response.setDestination(request.getSource()); response.setDestinationPort(request.getSourcePort()); return response; } }
private Response responseFor(final Request request) { Response response = new Response(ResponseCode.CONTENT); response.setMID(request.getMID()); response.setToken(request.getToken()); response.setBytes(new byte[]{}); response.setSource(request.getDestination()); response.setSourcePort(request.getDestinationPort()); response.setDestination(request.getSource()); response.setDestinationPort(request.getSourcePort()); return response; } }