Package ej.websocket.frame
Class RawFrame
- java.lang.Object
-
- ej.websocket.frame.RawFrame
-
public final class RawFrame extends Object
This is just a convenient class to encapsulate a byte array that contains a complete websocket frame. It provides methods to get information and data from the frame.The structure of a frame is described in RFC6455, section 5.2. Base Framing Protocol.
-
-
Field Summary
Fields Modifier and Type Field Description static byteOPCODE_BINARY_FRAMEOpcode to denote a binary frame.static byteOPCODE_CONNECTION_CLOSEOpcode to denote a connection close.static byteOPCODE_CONTINUATION_FRAMEOpcode to denote a continuation frame.static byteOPCODE_PINGOpcode to denote a ping.static byteOPCODE_PONGOpcode to denote a pong.static byteOPCODE_TEXT_FRAMEOpcode to denote a text frame.
-
Constructor Summary
Constructors Constructor Description RawFrame(byte[] bytes)byteswill be copied internally so that it is not tied to the newly created instance and can then be reused.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidappend(RawFrame f)Appends the payload of a frame into the current frame.byte[]getBytes()Note: this is not a copy of the internal representation.longgetExtendedLength()Get the extended length of the frame.bytegetFIN()FIN is a bit.bytegetLength()A frame has a field called payload length.bytegetMASK()MASK is a bit.byte[]getMaskingKey()Masking key is an optional field of the websocket protocol.bytegetOpcode()Opcode is 4 bits.byte[]getPayload(boolean unmaskIfNeeded)The payload (= 'real' data) of this frame.StringgetPayloadAsString(boolean unmaskIfNeeded)longgetPayloadLength()Gets the payload length.bytegetRSV()RSV are 3 bits.booleanhasExtendedLength()Tell whether this frame has an extended length field.booleanhasValidOpcode()Test the opcode of this frame against possible opcodes provided by RFC6455.booleanhasVeryExtendedLength()Tell whether this frame has an extended length field.booleanisControlFrame()Tell whether this frame is a control frame or not.booleanisMasked()A convenient method to test whether the frame is masked or not.booleanisValid()Tell whether is valid or not, according to the RFC6455.voidsetLength(long length)
-
-
-
Field Detail
-
OPCODE_CONTINUATION_FRAME
public static final byte OPCODE_CONTINUATION_FRAME
Opcode to denote a continuation frame.- See Also:
- Constant Field Values
-
OPCODE_TEXT_FRAME
public static final byte OPCODE_TEXT_FRAME
Opcode to denote a text frame.- See Also:
- Constant Field Values
-
OPCODE_BINARY_FRAME
public static final byte OPCODE_BINARY_FRAME
Opcode to denote a binary frame.- See Also:
- Constant Field Values
-
OPCODE_CONNECTION_CLOSE
public static final byte OPCODE_CONNECTION_CLOSE
Opcode to denote a connection close.- See Also:
- Constant Field Values
-
OPCODE_PING
public static final byte OPCODE_PING
Opcode to denote a ping.- See Also:
- Constant Field Values
-
OPCODE_PONG
public static final byte OPCODE_PONG
Opcode to denote a pong.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
RawFrame
public RawFrame(byte[] bytes) throws IllegalArgumentExceptionbyteswill be copied internally so that it is not tied to the newly created instance and can then be reused.- Parameters:
bytes- the data used to create the frame- Throws:
IllegalArgumentException- ifbytesis null
-
-
Method Detail
-
getBytes
public byte[] getBytes()
Note: this is not a copy of the internal representation. Handle it with care so as not to alter the frame.- Returns:
- the byte array of the frame
-
getFIN
public byte getFIN()
FIN is a bit. It is the LSB of the returned byte.- Returns:
- FIN
-
getRSV
public byte getRSV()
RSV are 3 bits. They are the LSB of the returned byte.- Returns:
- RSV[1..3]
-
getOpcode
public byte getOpcode()
Opcode is 4 bits. They are the LSB of the returned byte.- Returns:
- opcode
-
hasValidOpcode
public boolean hasValidOpcode()
Test the opcode of this frame against possible opcodes provided by RFC6455.- Returns:
- 'true' if 'code' is a valid opcode, 'false' otherwise
-
getMASK
public byte getMASK()
MASK is a bit. It is the LSB of the returned byte.- Returns:
- FIN
-
isControlFrame
public boolean isControlFrame()
Tell whether this frame is a control frame or not.- Returns:
- 'true' is this is a control frame, 'false' otherwise
-
isMasked
public boolean isMasked()
A convenient method to test whether the frame is masked or not.- Returns:
- true if MASK bit is set to 1; false otherwise
-
isValid
public boolean isValid() throws IOExceptionTell whether is valid or not, according to the RFC6455.- Returns:
- 'true' if the frame is valid, 'false' otherwise
- Throws:
IOException- if the frame does not respect the RFC6455UnsupportedEncodingException- if the frame does not validate the UTF8 format
-
getLength
public byte getLength()
A frame has a field called payload length. This is the value returned here. This may not be the actual length since a frame can have an additional field called "extended payload length".- Returns:
- the payload length
-
hasExtendedLength
public boolean hasExtendedLength()
Tell whether this frame has an extended length field.- Returns:
- 'true' if it has an extended length field.
-
hasVeryExtendedLength
public boolean hasVeryExtendedLength()
Tell whether this frame has an extended length field.- Returns:
- 'true' if it has an extended length field.
-
getExtendedLength
public long getExtendedLength()
Get the extended length of the frame.- Returns:
- the extended length is such a field is present in this frame; 0 otherwise
-
setLength
public void setLength(long length)
-
getMaskingKey
public byte[] getMaskingKey()
Masking key is an optional field of the websocket protocol. If present, it is 4 byte long.- Returns:
- null if the frame is not masked; otherwise, the masking key in a fresh new 4 byte array
-
getPayload
public byte[] getPayload(boolean unmaskIfNeeded)
The payload (= 'real' data) of this frame.- Parameters:
unmaskIfNeeded- if 'true' and MASK is 1 then it returns the unmasked payload; otherwise, the payload is returned directly- Returns:
- the payload in a fresh new array
-
getPayloadAsString
public String getPayloadAsString(boolean unmaskIfNeeded)
-
getPayloadLength
public long getPayloadLength()
Gets the payload length.- Returns:
- the payload length.
-
append
public void append(RawFrame f)
Appends the payload of a frame into the current frame.- Parameters:
f- the frame to gets the payload from.
-
-