protected void handleClose(WebSocketFragment aFragment) { // parse close message boolean hasInvalidUtf8 = false; try { byte[] data = aFragment.getPayloadData(); if (data != null) { if (data.length >= 2) { closeStatus = WebSocketUtil.convertBytesToShort(data, 0); if (data.length > 2) { closeMessage = convertFromBytesToString(WebSocketUtil.copySubArray(data, 2, data.length - 2)); } } } } catch (CharacterCodingException e) { logger.error("An error occurred while decoding from UTF8 to get text in close message.", e); sendErrorToObserver(e); hasInvalidUtf8 = true; } // actually close if (isClosing()) { closeSocket(); } else { setClosing(true); if (hasInvalidUtf8) { close(WebSocketCloseStatusInvalidUtf8, null); } else { close(0, null); } } }
public boolean parseHeader(byte[] aData, int aOffset) { // get header data bits int bufferLength = 14; byte[] data = aData; // do we have an existing fragment to work with if (getFragment() != null) { if (getFragment().length >= bufferLength) { data = getFragment(); } else { byte[] both; if ((aData != null ? aData.length : 0) - aOffset >= bufferLength - getFragment().length) { both = WebSocketUtil.appendPartialArray( getFragment(), aData, aOffset, bufferLength - getFragment().length); } else { both = WebSocketUtil.appendArray(getFragment(), aData); } data = both; } } if (data != null && data.length - aOffset < bufferLength) { bufferLength = data.length - aOffset; } if (bufferLength < 0 || data == null) { return false; } byte[] buffer = WebSocketUtil.copySubArray(data, 0, bufferLength); // determine opcode if (bufferLength > 0) { int index = 0; setFinal((buffer[index] & 0x80) != 0); setRSV1((buffer[index] & 0x40) != 0); setRSV2((buffer[index] & 0x20) != 0); setRSV3((buffer[index] & 0x20) != 0); setOpCode(buffer[index++] & 0x0F); // handle data depending on opcode switch (getOpCode()) { case TEXT: setPayloadType(PayloadType.TEXT); break; case BINARY: setPayloadType(PayloadType.BINARY); break; } // handle content, if any if (bufferLength > 1) { // do we have a mask boolean hasMask = (buffer[index] & 0x80) != 0; // get payload length Long dataLength = new Integer(buffer[index++] & 0x7F).longValue(); if (dataLength == 126) { // exit if we are missing bytes if (bufferLength < 4) { return false; } dataLength = new Integer(WebSocketUtil.convertBytesToShort(buffer, index)).longValue(); index += 2; } else if (dataLength == 127) { // exit if we are missing bytes if (bufferLength < 10) { return false; } dataLength = WebSocketUtil.convertBytesToLong(buffer, index); index += 8; } // if applicable, set mask value if (hasMask) { // exit if we are missing bytes if (bufferLength < index + 4) { return false; } // grab mask setMask(WebSocketUtil.convertBytesToInt(buffer, index)); index += 4; } payloadStart = index; if (dataLength > Integer.MAX_VALUE) { throw new IllegalArgumentException( "Implementation does not support payload lengths in excess of " + Integer.MAX_VALUE + ": " + dataLength); } payloadLength = dataLength.intValue(); return true; } } return false; }