Package com.jsgalactic.axiom.websocket
Class WebSocketMessage
java.lang.Object
com.jsgalactic.axiom.websocket.WebSocketMessage
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 Summary
Modifier and TypeMethodDescriptionstatic WebSocketMessagebinary(byte[] bytes) Creates a binary message from a copy of the supplied bytes.static WebSocketMessagebinary(ByteBuffer bytes) Creates a binary message from the remaining bytes of a buffer, without changing its position.byte[]bytes()Returns a copy of the binary payload.booleanCompares payloads by kind and content; copies are equal to their originals.inthashCode()Hashes the payload by kind and content.booleanisText()Reports whether this is a text message.intlength()Returns the payload length: characters for text, bytes for binary.text()Returns the text payload.static WebSocketMessageCreates a text message.toString()
-
Method Details
-
text
Creates a text message.- Parameters:
text- text payload; copied by immutability ofString- Returns:
- text message
- Throws:
NullPointerException- if text is null
-
binary
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
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
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
-
equals
Compares payloads by kind and content; copies are equal to their originals. -
hashCode
public int hashCode()Hashes the payload by kind and content.
-