Class WebSocketMessage

java.lang.Object
com.jsgalactic.axiom.websocket.WebSocketMessage

public final class WebSocketMessage extends Object
An immutable text or binary WebSocket message delivered to or from a WebSocketSession.

A message owns a private copy of its payload: factories copy their input and bytes() returns a fresh copy, so neither the sender nor a receiver can change what another sees. Messages are safe to share between threads. Text is validated as UTF-8 when it is created; binary payloads are uninterpreted bytes. toString() reports only the kind and length, never content.

  • Method Details

    • text

      public static WebSocketMessage text(String text)
      Creates a text message.
      Parameters:
      text - text payload; copied by immutability of String
      Returns:
      text message
      Throws:
      NullPointerException - if text is null
    • binary

      public static WebSocketMessage binary(byte[] bytes)
      Creates a binary message from a copy of the supplied bytes.
      Parameters:
      bytes - binary payload; copied
      Returns:
      binary message
      Throws:
      NullPointerException - if bytes is null
    • binary

      public static WebSocketMessage binary(ByteBuffer bytes)
      Creates a binary message from the remaining bytes of a buffer, without changing its position.
      Parameters:
      bytes - buffer whose remaining bytes become the payload; read, not retained
      Returns:
      binary message
      Throws:
      NullPointerException - if bytes is null
    • isText

      public boolean isText()
      Reports whether this is a text message.
      Returns:
      true for text, false for binary
    • text

      public String text()
      Returns the text payload.
      Returns:
      text payload
      Throws:
      IllegalStateException - if this is a binary message
    • bytes

      public byte[] bytes()
      Returns a copy of the binary payload.
      Returns:
      binary payload; copied
      Throws:
      IllegalStateException - if this is a text message
    • length

      public int length()
      Returns the payload length: characters for text, bytes for binary.
      Returns:
      payload length
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object other)
      Compares payloads by kind and content; copies are equal to their originals.
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Hashes the payload by kind and content.
      Overrides:
      hashCode in class Object