Example usage for org.springframework.messaging.simp.stomp StompCommand toString

List of usage examples for org.springframework.messaging.simp.stomp StompCommand toString

Introduction

In this page you can find the example usage for org.springframework.messaging.simp.stomp StompCommand toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.springframework.messaging.simp.stomp.StompEncoder.java

/**
 * Encodes the given payload and headers into a {@code byte[]}.
 * @param headers the headers//from w w  w  .j ava 2 s .c om
 * @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("Encoding heartbeat");
            output.write(StompDecoder.HEARTBEAT_PAYLOAD);
        }

        else {
            StompCommand command = StompHeaderAccessor.getCommand(headers);
            if (command == null) {
                throw new IllegalStateException("Missing STOMP command: " + headers);
            }

            output.write(command.toString().getBytes(StandardCharsets.UTF_8));
            output.write(LF);
            writeHeaders(command, headers, payload, output);
            output.write(LF);
            writeBody(payload, output);
            output.write((byte) 0);
        }

        return baos.toByteArray();
    } catch (IOException ex) {
        throw new StompConversionException("Failed to encode STOMP frame, headers=" + headers, ex);
    }
}