@Override
	public void run() {

		// Read buffer
		ByteBuffer readBuffer = ByteBuffer.allocate(Math.min(maxPacketSize, 1024 * 1024));
		byte[] readArray = readBuffer.array();

		// Processing variables
		Iterator<SelectionKey> keys;
		SocketChannel channel;
		SelectionKey key;

		byte[] bytes;
		byte[] remaining;
		int processed;
		int pos;

		// Loop
		while (true) {

			// Waiting for sockets
			int n;
			try {
				n = selector.select(3000);
			} catch (NullPointerException nullPointer) {
				continue;
			} catch (Exception anyError) {
				break;
			}
			if (n < 1) {
				continue;
			}
			keys = selector.selectedKeys().iterator();
			while (keys.hasNext()) {
				key = keys.next();
				if (key == null) {
					continue;
				}
				if (!key.isValid()) {
					keys.remove();
					continue;
				}
				if (key.isAcceptable()) {

					// Accept channel
					try {

						// Register socket
						channel = serverChannel.accept();
						channel.configureBlocking(false);

						channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
						channel.setOption(StandardSocketOptions.TCP_NODELAY, true);
						channel.setOption(StandardSocketOptions.SO_LINGER, -1);

						channel.register(selector, SelectionKey.OP_READ);

						// Debug
						if (debug) {
							logger.info("Client channel opened from " + channel.getRemoteAddress() + ".");
						}

					} catch (Exception cause) {
						close(key, cause);
					} finally {
						keys.remove();
					}

				} else if (key.isReadable()) {
					try {

						// Read bytes into the read buffer
						channel = (SocketChannel) key.channel();
						readBuffer.clear();
						n = channel.read(readBuffer);
						if (n < 0) {
							throw new EOFException();
						}
						if (n == 0) {
							keys.remove();
							continue;
						}

						// Debug
						if (debug) {
							logger.info(n + " bytes received from " + channel.getRemoteAddress() + ".");
						}

						// Get the incoming bytes
						bytes = new byte[n];
						System.arraycopy(readArray, 0, bytes, 0, n);

						// Add remaining bytes
						remaining = (byte[]) key.attachment();
						if (remaining != null) {
							byte[] tmp = new byte[remaining.length + bytes.length];
							System.arraycopy(remaining, 0, tmp, 0, remaining.length);
							System.arraycopy(bytes, 0, tmp, remaining.length, bytes.length);
							bytes = tmp;
							key.attach(null);
						}

						// Split data
						pos = 0;
						while ((processed = processPacket(bytes, pos)) > 0) {
							pos += processed;
						}

						// Has remaining?
						if (pos < bytes.length) {
							if (pos == 0) {
								remaining = bytes;
							} else {
								remaining = new byte[bytes.length - pos];
								System.arraycopy(bytes, pos, remaining, 0, remaining.length);
							}
							key.attach(remaining);
						}

					} catch (EOFException eof) {

						// Peer closed
						close(key, null);

					} catch (Exception cause) {
						close(key, cause);
					} finally {
						keys.remove();
					}
				}
			}
		}
	}
@ Override public void run ( ) { ByteBuffer readBuffer = ByteBuffer . allocate ( Math . min ( maxPacketSize , 1024 * 1024 ) ) ; byte [ ] readArray = readBuffer . array ( ) ; Iterator < SelectionKey > keys ; SocketChannel channel ; SelectionKey key ; byte [ ] bytes ; byte [ ] remaining ; int processed ; int pos ; while ( true ) { int n ; try { n = selector . select ( 3000 ) ; } catch ( NullPointerException nullPointer ) { continue ; } catch ( Exception anyError ) { break ; } if ( n < 1 ) { continue ; } keys = selector . selectedKeys ( ) . iterator ( ) ; while ( keys . hasNext ( ) ) { key = keys . next ( ) ; if ( key == null ) { continue ; } if ( ! key . isValid ( ) ) { keys . remove ( ) ; continue ; } if ( key . isAcceptable ( ) ) { try { channel = serverChannel . accept ( ) ; channel . configureBlocking ( false ) ; channel . setOption ( StandardSocketOptions . SO_KEEPALIVE , true ) ; channel . setOption ( StandardSocketOptions . TCP_NODELAY , true ) ; channel . setOption ( StandardSocketOptions . SO_LINGER , - 1 ) ; channel . register ( selector , SelectionKey . OP_READ ) ; if ( debug ) { logger . info ( "Client channel opened from " + channel . getRemoteAddress ( ) + "." ) ; } } catch ( Exception cause ) { close ( key , cause ) ; } finally { keys . remove ( ) ; } } else if ( key . isReadable ( ) ) { try { channel = ( SocketChannel ) key . channel ( ) ; readBuffer . clear ( ) ; n = channel . read ( readBuffer ) ; if ( n < 0 ) { throw new EOFException ( ) ; } if ( n == 0 ) { keys . remove ( ) ; continue ; } if ( debug ) { logger . info ( n + " bytes received from " + channel . getRemoteAddress ( ) + "." ) ; } bytes = new byte [ n ] ; System . arraycopy ( readArray , 0 , bytes , 0 , n ) ; remaining = ( byte [ ] ) key . attachment ( ) ; if ( remaining != null ) { byte [ ] tmp = new byte [ remaining . length + bytes . length ] ; System . arraycopy ( remaining , 0 , tmp , 0 , remaining . length ) ; System . arraycopy ( bytes , 0 , tmp , remaining . length , bytes . length ) ; bytes = tmp ; key . attach ( null ) ; } pos = 0 ; while ( ( processed = processPacket ( bytes , pos ) ) > 0 ) { pos += processed ; } if ( pos < bytes . length ) { if ( pos == 0 ) { remaining = bytes ; } else { remaining = new byte [ bytes . length - pos ] ; System . arraycopy ( bytes , pos , remaining , 0 , remaining . length ) ; } key . attach ( remaining ) ; } } catch ( EOFException eof ) { close ( key , null ) ; } catch ( Exception cause ) { close ( key , cause ) ; } finally { keys . remove ( ) ; } } } } }
--- READER LOOP ---