Class WebSocketFrame
- java.lang.Object
-
- NeoVisionaries.WebSockets.WebSocketFrame
-
public class WebSocketFrame extends java.lang.Object
WebSocket frame.
Read included Apache License 2.0:HERE
AllNeoVisionaries.WebSockets.*
Code Obtained From:GitHub 'NV' (Takahiko Kawasaki)
Public Archive.- See Also:
- RFC 6455, 5. Data Framing
Hi-Lited Source-Code:- View Here: NeoVisionaries/WebSockets/WebSocketFrame.java
- Open New Browser-Tab: NeoVisionaries/WebSockets/WebSocketFrame.java
File Size: 31,315 Bytes Line Count: 1,227 '\n' Characters Found
-
-
Constructor Summary
Constructors Constructor Description WebSocketFrame()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static WebSocketFrame
createBinaryFrame(byte[] payload)
Create a binary frame.static WebSocketFrame
createCloseFrame()
Create a close frame.static WebSocketFrame
createCloseFrame(int closeCode)
Create a close frame.static WebSocketFrame
createCloseFrame(int closeCode, String reason)
Create a close frame.static WebSocketFrame
createContinuationFrame()
Create a continuation frame.static WebSocketFrame
createContinuationFrame(byte[] payload)
Create a continuation frame.static WebSocketFrame
createContinuationFrame(String payload)
Create a continuation frame.static WebSocketFrame
createPingFrame()
Create a ping frame.static WebSocketFrame
createPingFrame(byte[] payload)
Create a ping frame.static WebSocketFrame
createPingFrame(String payload)
Create a ping frame.static WebSocketFrame
createPongFrame()
Create a pong frame.static WebSocketFrame
createPongFrame(byte[] payload)
Create a pong frame.static WebSocketFrame
createPongFrame(String payload)
Create a pong frame.static WebSocketFrame
createTextFrame(String payload)
Create a text frame.int
getCloseCode()
Parse the first two bytes of the payload as a close code.String
getCloseReason()
Parse the third and subsequent bytes of the payload as a close reason.boolean
getFin()
Get the value of FIN bit.int
getOpcode()
Get the opcode.byte[]
getPayload()
Get the unmasked payload.int
getPayloadLength()
Get the payload length.String
getPayloadText()
Get the unmasked payload as a text.boolean
getRsv1()
Get the value of RSV1 bit.boolean
getRsv2()
Get the value of RSV2 bit.boolean
getRsv3()
Get the value of RSV3 bit.boolean
hasPayload()
Check if this frame has payload.boolean
isBinaryFrame()
Check if this frame is a binary frame.boolean
isCloseFrame()
Check if this frame is a close frame.boolean
isContinuationFrame()
Check if this frame is a continuation frame.boolean
isControlFrame()
Check if this frame is a control frame.boolean
isDataFrame()
Check if this frame is a data frame.boolean
isPingFrame()
Check if this frame is a ping frame.boolean
isPongFrame()
Check if this frame is a pong frame.boolean
isTextFrame()
Check if this frame is a text frame.WebSocketFrame
setCloseFramePayload(int closeCode, String reason)
Set the payload that conforms to the payload format of close frames.WebSocketFrame
setFin(boolean fin)
Set the value of FIN bit.WebSocketFrame
setOpcode(int opcode)
Set the opcodeWebSocketFrame
setPayload(byte[] payload)
Set the unmasked payload.WebSocketFrame
setPayload(String payload)
Set the payload.WebSocketFrame
setRsv1(boolean rsv1)
Set the value of RSV1 bit.WebSocketFrame
setRsv2(boolean rsv2)
Set the value of RSV2 bit.WebSocketFrame
setRsv3(boolean rsv3)
Set the value of RSV3 bit.String
toString()
-
-
-
Constructor Detail
-
WebSocketFrame
public WebSocketFrame()
-
-
Method Detail
-
getFin
public boolean getFin()
Get the value of FIN bit.- Returns:
- The value of FIN bit.
- Code:
- Exact Method Body:
return mFin;
-
setFin
public WebSocketFrame setFin(boolean fin)
Set the value of FIN bit.- Parameters:
fin
- The value of FIN bit.- Returns:
this
object.- Code:
- Exact Method Body:
mFin = fin; return this;
-
getRsv1
public boolean getRsv1()
Get the value of RSV1 bit.- Returns:
- The value of RSV1 bit.
- Code:
- Exact Method Body:
return mRsv1;
-
setRsv1
public WebSocketFrame setRsv1(boolean rsv1)
Set the value of RSV1 bit.- Parameters:
rsv1
- The value of RSV1 bit.- Returns:
this
object.- Code:
- Exact Method Body:
mRsv1 = rsv1; return this;
-
getRsv2
public boolean getRsv2()
Get the value of RSV2 bit.- Returns:
- The value of RSV2 bit.
- Code:
- Exact Method Body:
return mRsv2;
-
setRsv2
public WebSocketFrame setRsv2(boolean rsv2)
Set the value of RSV2 bit.- Parameters:
rsv2
- The value of RSV2 bit.- Returns:
this
object.- Code:
- Exact Method Body:
mRsv2 = rsv2; return this;
-
getRsv3
public boolean getRsv3()
Get the value of RSV3 bit.- Returns:
- The value of RSV3 bit.
- Code:
- Exact Method Body:
return mRsv3;
-
setRsv3
public WebSocketFrame setRsv3(boolean rsv3)
Set the value of RSV3 bit.- Parameters:
rsv3
- The value of RSV3 bit.- Returns:
this
object.- Code:
- Exact Method Body:
mRsv3 = rsv3; return this;
-
getOpcode
public int getOpcode()
Get the opcode.WebSocket opcode Value Description 0x0 Frame continuation 0x1 Text frame 0x2 Binary frame 0x3-0x7 Reserved 0x8 Connection close 0x9 Ping 0xA Pong 0xB-0xF Reserved - Returns:
- The opcode.
- See Also:
WebSocketOpcode
- Code:
- Exact Method Body:
return mOpcode;
-
setOpcode
public WebSocketFrame setOpcode(int opcode)
Set the opcode- Parameters:
opcode
- The opcode.- Returns:
this
object.- See Also:
WebSocketOpcode
- Code:
- Exact Method Body:
mOpcode = opcode; return this;
-
isContinuationFrame
public boolean isContinuationFrame()
Check if this frame is a continuation frame.This method returns
true
when the value of the opcode is 0x0 (WebSocketOpcode.CONTINUATION
).- Returns:
true
if this frame is a continuation frame (= if the opcode is 0x0).- Code:
- Exact Method Body:
return (mOpcode == CONTINUATION);
-
isTextFrame
public boolean isTextFrame()
Check if this frame is a text frame.This method returns
true
when the value of the opcode is 0x1 (WebSocketOpcode.TEXT
).- Returns:
true
if this frame is a text frame (= if the opcode is 0x1).- Code:
- Exact Method Body:
return (mOpcode == TEXT);
-
isBinaryFrame
public boolean isBinaryFrame()
Check if this frame is a binary frame.This method returns
true
when the value of the opcode is 0x2 (WebSocketOpcode.BINARY
).- Returns:
true
if this frame is a binary frame (= if the opcode is 0x2).- Code:
- Exact Method Body:
return (mOpcode == BINARY);
-
isCloseFrame
public boolean isCloseFrame()
Check if this frame is a close frame.This method returns
true
when the value of the opcode is 0x8 (WebSocketOpcode.CLOSE
).- Returns:
true
if this frame is a close frame (= if the opcode is 0x8).- Code:
- Exact Method Body:
return (mOpcode == CLOSE);
-
isPingFrame
public boolean isPingFrame()
Check if this frame is a ping frame.This method returns
true
when the value of the opcode is 0x9 (WebSocketOpcode.PING
).- Returns:
true
if this frame is a ping frame (= if the opcode is 0x9).- Code:
- Exact Method Body:
return (mOpcode == PING);
-
isPongFrame
public boolean isPongFrame()
Check if this frame is a pong frame.This method returns
true
when the value of the opcode is 0xA (WebSocketOpcode.PONG
).- Returns:
true
if this frame is a pong frame (= if the opcode is 0xA).- Code:
- Exact Method Body:
return (mOpcode == PONG);
-
isDataFrame
public boolean isDataFrame()
Check if this frame is a data frame.This method returns
true
when the value of the opcode is in between 0x1 and 0x7.- Returns:
true
if this frame is a data frame (= if the opcode is in between 0x1 and 0x7).- Code:
- Exact Method Body:
return (0x1 <= mOpcode && mOpcode <= 0x7);
-
isControlFrame
public boolean isControlFrame()
Check if this frame is a control frame.This method returns
true
when the value of the opcode is in between 0x8 and 0xF.- Returns:
true
if this frame is a control frame (= if the opcode is in between 0x8 and 0xF).- Code:
- Exact Method Body:
return (0x8 <= mOpcode && mOpcode <= 0xF);
-
hasPayload
public boolean hasPayload()
Check if this frame has payload.- Returns:
true
if this frame has payload.- Code:
- Exact Method Body:
return mPayload != null;
-
getPayloadLength
public int getPayloadLength()
Get the payload length.- Returns:
- The payload length.
- Code:
- Exact Method Body:
if (mPayload == null) { return 0; } return mPayload.length;
-
getPayload
public byte[] getPayload()
Get the unmasked payload.- Returns:
- The unmasked payload.
null
may be returned. - Code:
- Exact Method Body:
return mPayload;
-
getPayloadText
public java.lang.String getPayloadText()
Get the unmasked payload as a text.- Returns:
- A string constructed by interrupting the payload as a UTF-8 bytes.
- Code:
- Exact Method Body:
if (mPayload == null) { return null; } return Misc.toStringUTF8(mPayload);
-
setPayload
public WebSocketFrame setPayload(byte[] payload)
Set the unmasked payload.Note that the payload length of a control frame must be 125 bytes or less.
- Parameters:
payload
- The unmasked payload.null
is accepted. An empty byte array is treated in the same way asnull
.- Returns:
this
object.- Code:
- Exact Method Body:
if (payload != null && payload.length == 0) { payload = null; } mPayload = payload; return this;
-
setPayload
public WebSocketFrame setPayload(java.lang.String payload)
Set the payload. The given string is converted to a byte array in UTF-8 encoding.Note that the payload length of a control frame must be 125 bytes or less.
- Parameters:
payload
- The unmasked payload.null
is accepted. An empty string is treated in the same way asnull
.- Returns:
this
object.- Code:
- Exact Method Body:
if (payload == null || payload.length() == 0) { return setPayload((byte[])null); } return setPayload(Misc.getBytesUTF8(payload));
-
setCloseFramePayload
public WebSocketFrame setCloseFramePayload(int closeCode, java.lang.String reason)
Set the payload that conforms to the payload format of close frames.The given parameters are encoded based on the rules described in "5.5.1. Close" of RFC 6455.
Note that the reason should not be too long because the payload length of a control frame must be 125 bytes or less.
- Parameters:
closeCode
- The close code.reason
- The reason.null
is accepted. An empty string is treated in the same way asnull
.- Returns:
this
object.- See Also:
- RFC 6455, 5.5.1. Close,
WebSocketCloseCode
- Code:
- Exact Method Body:
// Convert the close code to a 2-byte unsigned integer // in network byte order. byte[] encodedCloseCode = new byte[] { (byte)((closeCode >> 8) & 0xFF), (byte)((closeCode ) & 0xFF) }; // If a reason string is not given. if (reason == null || reason.length() == 0) { // Use the close code only. return setPayload(encodedCloseCode); } // Convert the reason into a byte array. byte[] encodedReason = Misc.getBytesUTF8(reason); // Concatenate the close code and the reason. byte[] payload = new byte[2 + encodedReason.length]; System.arraycopy(encodedCloseCode, 0, payload, 0, 2); System.arraycopy(encodedReason, 0, payload, 2, encodedReason.length); // Use the concatenated string. return setPayload(payload);
-
getCloseCode
public int getCloseCode()
Parse the first two bytes of the payload as a close code.If any payload is not set or the length of the payload is less than 2, this method returns 1005 (
WebSocketCloseCode.NONE
).The value returned from this method is meaningless if this frame is not a close frame.
- Returns:
- The close code.
- See Also:
- RFC 6455, 5.5.1. Close,
WebSocketCloseCode
- Code:
- Exact Method Body:
if (mPayload == null || mPayload.length < 2) { return WebSocketCloseCode.NONE; } // A close code is encoded in network byte order. int closeCode = (((mPayload[0] & 0xFF) << 8) | (mPayload[1] & 0xFF)); return closeCode;
-
getCloseReason
public java.lang.String getCloseReason()
Parse the third and subsequent bytes of the payload as a close reason.If any payload is not set or the length of the payload is less than 3, this method returns
null
.The value returned from this method is meaningless if this frame is not a close frame.
- Returns:
- The close reason.
- Code:
- Exact Method Body:
if (mPayload == null || mPayload.length < 3) { return null; } return Misc.toStringUTF8(mPayload, 2, mPayload.length - 2);
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
- Code:
- Exact Method Body:
StringBuilder builder = new StringBuilder() .append("WebSocketFrame(FIN=").append(mFin ? "1" : "0") .append(",RSV1=").append(mRsv1 ? "1" : "0") .append(",RSV2=").append(mRsv2 ? "1" : "0") .append(",RSV3=").append(mRsv3 ? "1" : "0") .append(",Opcode=").append(Misc.toOpcodeName(mOpcode)) .append(",Length=").append(getPayloadLength()); switch (mOpcode) { case TEXT: appendPayloadText(builder); break; case BINARY: appendPayloadBinary(builder); break; case CLOSE: appendPayloadClose(builder); break; } return builder.append(")").toString();
-
createContinuationFrame
public static WebSocketFrame createContinuationFrame()
Create a continuation frame. Note that the FIN bit of the returned frame is false.- Returns:
- A WebSocket frame whose FIN bit is false, opcode is
CONTINUATION
and payload isnull
. - Code:
- Exact Method Body:
return new WebSocketFrame() .setOpcode(CONTINUATION);
-
createContinuationFrame
public static WebSocketFrame createContinuationFrame(byte[] payload)
Create a continuation frame. Note that the FIN bit of the returned frame is false.- Parameters:
payload
- The payload for a newly create frame.- Returns:
- A WebSocket frame whose FIN bit is false, opcode is
CONTINUATION
and payload is the given one. - Code:
- Exact Method Body:
return createContinuationFrame().setPayload(payload);
-
createContinuationFrame
public static WebSocketFrame createContinuationFrame (java.lang.String payload)
Create a continuation frame. Note that the FIN bit of the returned frame is false.- Parameters:
payload
- The payload for a newly create frame.- Returns:
- A WebSocket frame whose FIN bit is false, opcode is
CONTINUATION
and payload is the given one. - Code:
- Exact Method Body:
return createContinuationFrame().setPayload(payload);
-
createTextFrame
public static WebSocketFrame createTextFrame(java.lang.String payload)
Create a text frame.- Parameters:
payload
- The payload for a newly created frame.- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
TEXT
and payload is the given one. - Code:
- Exact Method Body:
return new WebSocketFrame() .setFin(true) .setOpcode(TEXT) .setPayload(payload);
-
createBinaryFrame
public static WebSocketFrame createBinaryFrame(byte[] payload)
Create a binary frame.- Parameters:
payload
- The payload for a newly created frame.- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
BINARY
and payload is the given one. - Code:
- Exact Method Body:
return new WebSocketFrame() .setFin(true) .setOpcode(BINARY) .setPayload(payload);
-
createCloseFrame
public static WebSocketFrame createCloseFrame()
Create a close frame.- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
CLOSE
and payload isnull
. - Code:
- Exact Method Body:
return new WebSocketFrame() .setFin(true) .setOpcode(CLOSE);
-
createCloseFrame
public static WebSocketFrame createCloseFrame(int closeCode)
Create a close frame.- Parameters:
closeCode
- The close code.- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
CLOSE
and payload contains a close code. - See Also:
WebSocketCloseCode
- Code:
- Exact Method Body:
return createCloseFrame().setCloseFramePayload(closeCode, null);
-
createCloseFrame
public static WebSocketFrame createCloseFrame(int closeCode, java.lang.String reason)
Create a close frame.- Parameters:
closeCode
- The close code.reason
- The close reason. Note that a control frame's payload length must be 125 bytes or less (RFC 6455, 5.5. Control Frames).- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
CLOSE
and payload contains a close code and a close reason. - See Also:
WebSocketCloseCode
- Code:
- Exact Method Body:
return createCloseFrame().setCloseFramePayload(closeCode, reason);
-
createPingFrame
public static WebSocketFrame createPingFrame()
Create a ping frame.- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
PING
and payload isnull
. - Code:
- Exact Method Body:
return new WebSocketFrame() .setFin(true) .setOpcode(PING);
-
createPingFrame
public static WebSocketFrame createPingFrame(byte[] payload)
Create a ping frame.- Parameters:
payload
- The payload for a newly created frame. Note that a control frame's payload length must be 125 bytes or less (RFC 6455, 5.5. Control Frames).- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
PING
and payload is the given one. - Code:
- Exact Method Body:
return createPingFrame().setPayload(payload);
-
createPingFrame
public static WebSocketFrame createPingFrame(java.lang.String payload)
Create a ping frame.- Parameters:
payload
- The payload for a newly created frame. Note that a control frame's payload length must be 125 bytes or less (RFC 6455, 5.5. Control Frames).- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
PING
and payload is the given one. - Code:
- Exact Method Body:
return createPingFrame().setPayload(payload);
-
createPongFrame
public static WebSocketFrame createPongFrame()
Create a pong frame.- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
PONG
and payload isnull
. - Code:
- Exact Method Body:
return new WebSocketFrame() .setFin(true) .setOpcode(PONG);
-
createPongFrame
public static WebSocketFrame createPongFrame(byte[] payload)
Create a pong frame.- Parameters:
payload
- The payload for a newly created frame. Note that a control frame's payload length must be 125 bytes or less (RFC 6455, 5.5. Control Frames).- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
PONG
and payload is the given one. - Code:
- Exact Method Body:
return createPongFrame().setPayload(payload);
-
createPongFrame
public static WebSocketFrame createPongFrame(java.lang.String payload)
Create a pong frame.- Parameters:
payload
- The payload for a newly created frame. Note that a control frame's payload length must be 125 bytes or less (RFC 6455, 5.5. Control Frames).- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
PONG
and payload is the given one. - Code:
- Exact Method Body:
return createPongFrame().setPayload(payload);
-
-