public WebSocketFrame(InputStream input) throws IOException {
  byte firstByte=(byte)input.read();
  setFinAndOpCode(firstByte);
  if (this.opcode == 2) {
    byte maskLengthByte=(byte)input.read();
    boolean masked=((maskLengthByte & 0x80) != 0);
    int payloadLength=(byte)(0x7F & maskLengthByte);
    int byteCount=0;
    if (payloadLength == 0X7F) {
      byteCount=8;
    }
 else     if (payloadLength == 0X7E) {
      byteCount=2;
    }
    if (byteCount > 0) {
      payloadLength=0;
    }
    while (--byteCount >= 0) {
      maskLengthByte=(byte)input.read();
      payloadLength|=(maskLengthByte & 0xFF) << (8 * byteCount);
    }
    byte maskingKey[]=null;
    if (masked) {
      maskingKey=new byte[4];
      input.read(maskingKey,0,4);
    }
    this.payload=new byte[payloadLength];
    int offsetIndex=0;
    int tempLength=payloadLength;
    int bytesRead=0;
    while (offsetIndex != payloadLength) {
      bytesRead=input.read(this.payload,offsetIndex,tempLength);
      offsetIndex+=bytesRead;
      tempLength-=bytesRead;
    }
    if (masked) {
      for (int i=0; i < this.payload.length; i++) {
        this.payload[i]^=maskingKey[i % 4];
      }
    }
    return;
  }
 else   if (this.opcode == 8) {
    closeFlag=true;
  }
 else {
    throw new IOException("Invalid Frame: Opcode: " + this.opcode);
  }
}

public WebSocketFrame ( InputStream input ) throws IOException { byte firstByte = ( byte ) input . read ( ) ; setFinAndOpCode ( firstByte ) ; if ( this . opcode == 2 ) { byte maskLengthByte = ( byte ) input . read ( ) ; boolean masked = ( ( maskLengthByte & 0x80 ) != 0 ) ; int payloadLength = ( byte ) ( 0x7F & maskLengthByte ) ; int byteCount = 0 ; if ( payloadLength == 0X7F ) { byteCount = 8 ; } else if ( payloadLength == 0X7E ) { byteCount = 2 ; } if ( byteCount > 0 ) { payloadLength = 0 ; } while ( -- byteCount >= 0 ) { maskLengthByte = ( byte ) input . read ( ) ; payloadLength |= ( maskLengthByte & 0xFF ) << ( 8 * byteCount ) ; } byte maskingKey [ ] = null ; if ( masked ) { maskingKey = new byte [ 4 ] ; input . read ( maskingKey , 0 , 4 ) ; } this . payload = new byte [ payloadLength ] ; int offsetIndex = 0 ; int tempLength = payloadLength ; int bytesRead = 0 ; while ( offsetIndex != payloadLength ) { bytesRead = input . read ( this . payload , offsetIndex , tempLength ) ; offsetIndex += bytesRead ; tempLength -= bytesRead ; } if ( masked ) { for ( int i = 0 ; i < this . payload . length ; i ++ ) { this . payload [ i ] ^= maskingKey [ i % 4 ] ; } } return ; } else if ( this . opcode == 8 ) { closeFlag = true ; } else { throw new IOException ( "Invalid Frame: Opcode: " + this . opcode ) ; } }
takes an input stream and parses it into a websocket frame .
