#fixed code
                                               void handleRead() throws IOException {
		if( !socketBuffer.hasRemaining() ) {
			socketBuffer.rewind();
			socketBuffer.limit( socketBuffer.capacity() );
			if( sockchannel.read( socketBuffer ) == -1 ) {
				if( draft == null ) {
					closeConnection( CloseFrame.ABNROMAL_CLOSE, true );
				} else if( draft.getCloseHandshakeType() == CloseHandshakeType.NONE ) {
					closeConnection( CloseFrame.NORMAL, true );
				} else if( draft.getCloseHandshakeType() == CloseHandshakeType.ONEWAY ) {
					if( role == Role.SERVER )
						closeConnection( CloseFrame.ABNROMAL_CLOSE, true );
					else
						closeConnection( CloseFrame.NORMAL, true );
				} else {
					closeConnection( CloseFrame.ABNROMAL_CLOSE, true );
				}

			}

			socketBuffer.flip();
		}

		if( socketBuffer.hasRemaining() ) {
			if( DEBUG )
				System.out.println( "process(" + socketBuffer.remaining() + "): {" + ( socketBuffer.remaining() > 1000 ? "too big to display" : new String( socketBuffer.array(), socketBuffer.position(), socketBuffer.remaining() ) ) + "}" );
			if( !handshakeComplete ) {
				if( draft == null ) {
					HandshakeState isflashedgecase = isFlashEdgeCase( socketBuffer );
					if( isflashedgecase == HandshakeState.MATCHED ) {
						channelWriteDirect( ByteBuffer.wrap( Charsetfunctions.utf8Bytes( wsl.getFlashPolicy( this ) ) ) );
						closeDirect( CloseFrame.FLASHPOLICY, "" );
						return;
					} else if( isflashedgecase == HandshakeState.MATCHING ) {
						return;
					}
				}
				HandshakeState handshakestate = null;
				socketBuffer.mark();
				try {
					if( role == Role.SERVER ) {
						if( draft == null ) {
							for( Draft d : known_drafts ) {
								try {
									d.setParseMode( role );
									socketBuffer.reset();
									Handshakedata tmphandshake = d.translateHandshake( socketBuffer );
									if( tmphandshake instanceof ClientHandshake == false ) {
										closeConnection( CloseFrame.PROTOCOL_ERROR, "wrong http function", false );
										return;
									}
									ClientHandshake handshake = (ClientHandshake) tmphandshake;
									handshakestate = d.acceptHandshakeAsServer( handshake );
									if( handshakestate == HandshakeState.MATCHED ) {
										ServerHandshakeBuilder response;
										try {
											response = wsl.onWebsocketHandshakeReceivedAsServer( this, d, handshake );
										} catch ( InvalidDataException e ) {
											closeConnection( e.getCloseCode(), e.getMessage(), false );
											return;
										}
										writeDirect( d.createHandshake( d.postProcessHandshakeResponseAsServer( handshake, response ), role ) );
										draft = d;
										open( handshake );
										handleRead();
										return;
									} else if( handshakestate == HandshakeState.MATCHING ) {
										if( draft != null ) {
											throw new InvalidHandshakeException( "multible drafts matching" );
										}
										draft = d;
									}
								} catch ( InvalidHandshakeException e ) {
									// go on with an other draft
								} catch ( IncompleteHandshakeException e ) {
									if( socketBuffer.limit() == socketBuffer.capacity() ) {
										close( CloseFrame.TOOBIG, "handshake is to big" );
									}
									// read more bytes for the handshake
									socketBuffer.position( socketBuffer.limit() );
									socketBuffer.limit( socketBuffer.capacity() );
									return;
								}
							}
							if( draft == null ) {
								close( CloseFrame.PROTOCOL_ERROR, "no draft matches" );
							}
							return;
						} else {
							// special case for multiple step handshakes
							Handshakedata tmphandshake = draft.translateHandshake( socketBuffer );
							if( tmphandshake instanceof ClientHandshake == false ) {
								closeConnection( CloseFrame.PROTOCOL_ERROR, "wrong http function", false );
								return;
							}
							ClientHandshake handshake = (ClientHandshake) tmphandshake;
							handshakestate = draft.acceptHandshakeAsServer( handshake );

							if( handshakestate == HandshakeState.MATCHED ) {
								open( handshake );
								handleRead();
							} else if( handshakestate != HandshakeState.MATCHING ) {
								close( CloseFrame.PROTOCOL_ERROR, "the handshake did finaly not match" );
							}
							return;
						}
					} else if( role == Role.CLIENT ) {
						draft.setParseMode( role );
						Handshakedata tmphandshake = draft.translateHandshake( socketBuffer );
						if( tmphandshake instanceof ServerHandshake == false ) {
							closeConnection( CloseFrame.PROTOCOL_ERROR, "Wwrong http function", false );
							return;
						}
						ServerHandshake handshake = (ServerHandshake) tmphandshake;
						handshakestate = draft.acceptHandshakeAsClient( handshakerequest, handshake );
						if( handshakestate == HandshakeState.MATCHED ) {
							try {
								wsl.onWebsocketHandshakeReceivedAsClient( this, handshakerequest, handshake );
							} catch ( InvalidDataException e ) {
								closeConnection( e.getCloseCode(), e.getMessage(), false );
								return;
							}
							open( handshake );
							handleRead();
						} else if( handshakestate == HandshakeState.MATCHING ) {
							return;
						} else {
							close( CloseFrame.PROTOCOL_ERROR, "draft " + draft + " refuses handshake" );
						}
					}
				} catch ( InvalidHandshakeException e ) {
					close( e );
				}
			} else {
				// Receiving frames
				List<Framedata> frames;
				try {
					frames = draft.translateFrame( socketBuffer );
					for( Framedata f : frames ) {
						if( DEBUG )
							System.out.println( "matched frame: " + f );
						Opcode curop = f.getOpcode();
						if( curop == Opcode.CLOSING ) {
							int code = CloseFrame.NOCODE;
							String reason = "";
							if( f instanceof CloseFrame ) {
								CloseFrame cf = (CloseFrame) f;
								code = cf.getCloseCode();
								reason = cf.getMessage();
							}
							if( closeHandshakeSent ) {
								// complete the close handshake by disconnecting
								closeConnection( code, reason, true );
							} else {
								// echo close handshake
								if( draft.getCloseHandshakeType() == CloseHandshakeType.TWOWAY )
									close( code, reason );
								closeConnection( code, reason, false );
							}
							continue;
						} else if( curop == Opcode.PING ) {
							wsl.onWebsocketPing( this, f );
							continue;
						} else if( curop == Opcode.PONG ) {
							wsl.onWebsocketPong( this, f );
							continue;
						} else {
							// process non control frames
							if( currentframe == null ) {
								if( f.getOpcode() == Opcode.CONTINIOUS ) {
									throw new InvalidFrameException( "unexpected continious frame" );
								} else if( f.isFin() ) {
									// receive normal onframe message
									deliverMessage( f );
								} else {
									// remember the frame whose payload is about to be continued
									currentframe = f;
								}
							} else if( f.getOpcode() == Opcode.CONTINIOUS ) {
								currentframe.append( f );
								if( f.isFin() ) {
									deliverMessage( currentframe );
									currentframe = null;
								}
							} else {
								throw new InvalidDataException( CloseFrame.PROTOCOL_ERROR, "non control or continious frame expected" );
							}
						}
					}
				} catch ( InvalidDataException e1 ) {
					wsl.onWebsocketError( this, e1 );
					close( e1 );
					return;
				}
			}
		}
	}
#vulnerable code 
                             long bufferedDataAmount() {
		return bufferQueueTotalAmount;
	} 
                            #location 2 
                            #vulnerability type THREAD_SAFETY_VIOLATION