<commit_before>import java.nio.ByteOrder;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.channels.ServerSocketChannel;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class SSserver {

    private class SServerInstance implements Runnable {

        final private int HEAD_BUFF_LEN = 255;

        final private int BUFF_LEN = 4096;

        final private int ADDR_TYPE_HOST = 0x03;

        private SocketChannel mClientChannel;
        private String mRemoteAddr;
        private int mRemotePort;

        /*
         *  |addr type: 1 byte| addr | port: 2 bytes with big endian|
         *
         *  addr type 0x1: addr = ipv4 | 4 bytes
         *  addr type 0x3: addr = host address string | 1 byte(string length) + string
         *  addr type 0x4: addr = ipv6 | 19 bytes?
         *
         */
        private void parseHead() throws Exception
        {
            //still use socket I/O here.
            DataInputStream in = new DataInputStream(mClientChannel.socket().getInputStream());

            int len = 0;
            int addrtype = in.read();
            byte buf[] = new byte[HEAD_BUFF_LEN];

            //get addr
            if (addrtype == ADDR_TYPE_HOST) {
                len = in.read(buf, 0, in.read());
            } else {
                //do not support other addrtype now.
                throw new Exception("Unsupport addr type!");
            }
            mRemoteAddr = new String(buf, 0, len);

            //get port
            ByteBuffer port = ByteBuffer.allocate(2);
            port.order(ByteOrder.BIG_ENDIAN);
            in.read(buf, 0, 2);
            port.put(buf[0]);
            port.put(buf[1]);
            mRemotePort = port.getShort(0);

            System.out.println("Addr type: " + addrtype + " addr: " + mRemoteAddr + ":" + mRemotePort);
            //don't close this input stream
        }

        private void close()
        {
            if (mClientChannel == null)
                return;
            try{
                mClientChannel.close();
                mClientChannel = null;
            }catch(Exception e){
                e.printStackTrace();
            }
        }

        private void connectAndSendData()<commit_msg>Add addr type 0x01(IPV4) support.<commit_after>import java.nio.ByteOrder;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.channels.ServerSocketChannel;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.InetSocketAddress;
import java.net.InetAddress;
import java.net.StandardSocketOptions;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class SSserver {

    private class SServerInstance implements Runnable {

        final private int HEAD_BUFF_LEN = 255;

        final private int BUFF_LEN = 4096;

        final private int ADDR_TYPE_IPV4 = 0x01;
        final private int ADDR_TYPE_HOST = 0x03;

        private SocketChannel mClientChannel;
        private InetAddress mRemoteAddr;
        private int mRemotePort;

        /*
         *  |addr type: 1 byte| addr | port: 2 bytes with big endian|
         *
         *  addr type 0x1: addr = ipv4 | 4 bytes
         *  addr type 0x3: addr = host address string | 1 byte(string length) + string
         *  addr type 0x4: addr = ipv6 | 19 bytes?
         *
         */
        private void parseHead() throws Exception
        {
            //still use socket I/O here.
            DataInputStream in = new DataInputStream(mClientChannel.socket().getInputStream());

            int len = 0;
            int addrtype = in.read();
            byte buf[] = new byte[HEAD_BUFF_LEN];

            //get addr
            if (addrtype == ADDR_TYPE_IPV4) {
                byte ipv4[] = new byte[4];
                in.read(ipv4, 0, 4);
                mRemoteAddr = InetAddress.getByAddress(ipv4);
            }else if (addrtype == ADDR_TYPE_HOST) {
                len = in.read(buf, 0, in.read());
                mRemoteAddr = InetAddress.getByName(new String(buf, 0, len));
            } else {
                //do not support other addrtype now.
                throw new Exception("Unsupport addr type: " + addrtype + "!");
            }

            //get port
            ByteBuffer port = ByteBuffer.allocate(2);
            port.order(ByteOrder.BIG_ENDIAN);
            in.read(buf, 0, 2);
            port.put(buf[0]);
            port.put(buf[1]);
            mRemotePort = port.getShort(0);

            System.out.println("Addr type: " + addrtype + " addr: " + mRemoteAddr + ":" + mRemotePort);
            //don't close this input stream
        }

        private void close()
        {
            if (mClientChannel == null)
                return;
            try{
                mClientChannel.close();
                mClientChannel = null;
            }catch(Exception e){
                e.printStackTrace();
            }
        }

        private void connectAndSendData()
{'fraction_non_alphanumeric': 0.09540702016430172, 'fraction_numerical': 0.015123226288274831, 'mean_word_length': 1.863174772848744, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '5479419', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1453, 'n_words': 394}