private void readPayloadInfo(InputStream in) throws IOException { byte b = (byte) checkedRead(in.read()); boolean masked = (b & 0x80) != 0; if (this._payloadLength == 126) { this._payloadLength = (checkedRead(in.read()) << 8 | checkedRead(in.read())) & 0xFFFF; if (this._payloadLength < 126) { throw new WebSocketException(CloseCode.ProtocolError, "Invalid data frame 2byte length. (not using minimal length encoding)"); (long) checkedRead(in.read()) << 56 | (long) checkedRead(in.read()) << 48 | (long) checkedRead(in.read()) << 40 | (long) checkedRead(in.read()) << 32 | checkedRead(in.read()) << 24 | checkedRead(in.read()) << 16 | checkedRead(in.read()) << 8 | checkedRead(in.read()); if (_payloadLength < 65536) { throw new WebSocketException(CloseCode.ProtocolError, "Invalid data frame 4byte length. (not using minimal length encoding)"); int read = 0; while (read < this.maskingKey.length) { read += checkedRead(in.read(this.maskingKey, read, this.maskingKey.length - read));
private void readPayload(InputStream in) throws IOException { this.payload = new byte[this._payloadLength]; int read = 0; while (read < this._payloadLength) { read += checkedRead(in.read(this.payload, read, this._payloadLength - read)); } if (isMasked()) { for (int i = 0; i < this.payload.length; i++) { this.payload[i] ^= this.maskingKey[i % 4]; } } // Test for Unicode errors if (getOpCode() == OpCode.Text) { this._payloadString = binary2Text(getBinaryPayload()); } }
public static WebSocketFrame read(InputStream in) throws IOException { byte head = (byte) checkedRead(in.read()); boolean fin = (head & 0x80) != 0; OpCode opCode = OpCode.find((byte) (head & 0x0F)); if ((head & 0x70) != 0) { throw new WebSocketException(CloseCode.ProtocolError, "The reserved bits (" + Integer.toBinaryString(head & 0x70) + ") must be 0."); } if (opCode == null) { throw new WebSocketException(CloseCode.ProtocolError, "Received frame with reserved/unknown opcode " + (head & 0x0F) + "."); } else if (opCode.isControlFrame() && !fin) { throw new WebSocketException(CloseCode.ProtocolError, "Fragmented control frame."); } WebSocketFrame frame = new WebSocketFrame(opCode, fin); frame.readPayloadInfo(in); frame.readPayload(in); if (frame.getOpCode() == OpCode.Close) { return new CloseFrame(frame); } else { return frame; } }