How to validate a WebSocket frame in Java?

How to validate a WebSocket frame in Java?

I wrote a WebSocket frame decoder in Java:
private byte[] decodeFrame(byte[] _rawIn) {
int maskIndex = 2;
byte[] maskBytes = new byte[4];
if ((_rawIn[1] & (byte) 127) == 126) {
maskIndex = 4;
} else if ((_rawIn[1] & (byte) 127) == 127) {
maskIndex = 10;
}
System.arraycopy(_rawIn, maskIndex, maskBytes, 0, 4);
byte[] message = new byte[_rawIn.length - maskIndex - 4];
for (int i = maskIndex + 4; i < _rawIn.length; i++) {
message[i - maskIndex - 4] = (byte) (_rawIn[i] ^ maskBytes[(i
- maskIndex - 4) % 4]);
}
return message;
}
It works, but I have no idea how to validate a frame in order to make sure
that it decodes only valid frames.
The protocol description http://tools.ietf.org/html/rfc6455 unfortunately
does not tell much about frame-validation.