<commit_before>package org.zenframework.easyservices.net;

import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zenframework.easyservices.util.io.QueueInputStream;

public class NioTcpServer implements TcpServer {

    private static final Logger LOG = LoggerFactory.getLogger(NioTcpServer.class);

    private static final ThreadFactory SERVER_THREAD_FACTORY = new ThreadFactory() {

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "NioTcpServer");
        }

    };

    private static CompletionHandler<AsynchronousSocketChannel, ConnectionAttachment> CONN_HANDLER = new CompletionHandler<AsynchronousSocketChannel, ConnectionAttachment>() {

        @Override
        public void completed(final AsynchronousSocketChannel client, final ConnectionAttachment connAttach) {
            if (!connAttach.server.isOpen())
                return;
            connAttach.server.accept(connAttach, this);
            final ReadAttachment readAttach = new ReadAttachment(client, new QueueInputStream(), ByteBuffer.allocate(8192));
            client.read(readAttach.buf, readAttach, READ_HANDLER);
            try {
                final SocketAddress clientAddr = client.getRemoteAddress();
                final OutputStream out = new ClientOutputStream(client);
                connAttach.executor.execute(new Runnable() {

                    @Override
                    public void run() {
                        Thread.currentThread().setName("Client-" + clientAddr.toString());
                        try {
                            while (connAttach.handler.handleRequest(clientAddr, readAttach.in, out))
                                ;
                        } catch (IOException e) {
                            if (!(e instanceof EOFException))
                                LOG.error(e.getMessage(), e);
                            IOUtils.closeQuietly(client);
                        }
                    }

                });
            } catch (IOException e) {
                LOG.error("Start client thread failed", e);
            }
        }

        @Override
        public void failed(Throwable e, ConnectionAttachment attach) {
            if (!(e instanceof AsynchronousCloseException))
                LOG.error("Accept connection failed", e);
            IOUtils.closeQuietly(attach.server);
        }

    };

    private static CompletionHandler<Integer, ReadAttachment> READ_HANDLER = new CompletionHandler<Integer, ReadAttachment>() {

        @Override
        public void completed(Integer result, ReadAttachment attach) {<commit_msg>Set name to client thread<commit_after>package org.zenframework.easyservices.net;

import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zenframework.easyservices.util.io.QueueInputStream;

public class NioTcpServer implements TcpServer {

    private static final Logger LOG = LoggerFactory.getLogger(NioTcpServer.class);

    private static final ThreadFactory SERVER_THREAD_FACTORY = new ThreadFactory() {

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "Client-<unused>");
        }

    };

    private static CompletionHandler<AsynchronousSocketChannel, ConnectionAttachment> CONN_HANDLER = new CompletionHandler<AsynchronousSocketChannel, ConnectionAttachment>() {

        @Override
        public void completed(final AsynchronousSocketChannel client, final ConnectionAttachment connAttach) {
            if (!connAttach.server.isOpen())
                return;
            connAttach.server.accept(connAttach, this);
            final ReadAttachment readAttach = new ReadAttachment(client, new QueueInputStream(), ByteBuffer.allocate(8192));
            client.read(readAttach.buf, readAttach, READ_HANDLER);
            try {
                final SocketAddress clientAddr = client.getRemoteAddress();
                final OutputStream out = new ClientOutputStream(client);
                connAttach.executor.execute(new Runnable() {

                    @Override
                    public void run() {
                        Thread.currentThread().setName("Client-" + clientAddr.toString());
                        try {
                            while (connAttach.handler.handleRequest(clientAddr, readAttach.in, out))
                                ;
                        } catch (IOException e) {
                            if (!(e instanceof EOFException))
                                LOG.error(e.getMessage(), e);
                            IOUtils.closeQuietly(client);
                        } finally {
                            Thread.currentThread().setName("Client-<unused>");
                        }
                    }

                });
            } catch (IOException e) {
                LOG.error("Start client thread failed", e);
            }
        }

        @Override
        public void failed(Throwable e, ConnectionAttachment attach) {
            if (!(e instanceof AsynchronousCloseException))
                LOG.error("Accept connection failed", e);
            IOUtils.closeQuietly(attach.server);
        }

    };

    private static CompletionHandler<Integer, ReadAttachment> READ_HANDLER = new CompletionHandler<Integer, ReadAttachment>() {

        @Override
        public void completed(Integer result, ReadAttachment attach) {
{'fraction_non_alphanumeric': 0.08413030484160192, 'fraction_numerical': 0.0017931858936043037, 'mean_word_length': 2.409577177789098, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '379920', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1640, 'n_words': 350}