/** * Encodes the given payload and headers into a {@code byte[]}. * * @param headers the headers * @param payload the payload * @return the encoded message */ public byte[] encode(Map<String, Object> headers, byte[] payload) { Assert.notNull(headers, "'headers' is required"); Assert.notNull(payload, "'payload' is required"); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(128 + payload.length); DataOutputStream output = new DataOutputStream(baos); if (SimpMessageType.HEARTBEAT.equals(SimpMessageHeaderAccessor.getMessageType(headers))) { logger.trace("Encoded heartbeat"); output.write(StompDecoder.HEARTBEAT_PAYLOAD); } else { StompCommand command = StompHeaderAccessor.getCommand(headers); Assert.notNull(command, "Missing STOMP command: " + headers); output.write(command.toString().getBytes(StompDecoder.UTF8_CHARSET)); output.write(LF); writeHeaders(command, headers, payload, output); output.write(LF); writeBody(payload, output); output.write((byte) 0); } return baos.toByteArray(); } catch (IOException e) { throw new StompConversionException( "Failed to encode STOMP frame, headers=" + headers + ".", e); } }
/** * Encodes the given STOMP {@code message} into a {@code byte[]} * * @param message The message to encode * @return The encoded message */ public byte[] encode(Message<byte[]> message) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream output = new DataOutputStream(baos); StompHeaderAccessor headers = StompHeaderAccessor.wrap(message); if (isHeartbeat(headers)) { output.write(message.getPayload()); } else { writeCommand(headers, output); writeHeaders(headers, message, output); output.write(LF); writeBody(message, output); output.write((byte) 0); } return baos.toByteArray(); } catch (IOException e) { throw new StompConversionException("Failed to encode STOMP frame", e); } }