<commit_before>/*
 * WireSpider
 *
 * Copyright (c) 2015 kazyx
 *
 * This software is released under the MIT License.
 * http://opensource.org/licenses/mit-license.php
 */

package net.kazyx.wirespider;

import net.kazyx.wirespider.util.IOUtil;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.CancelledKeyException;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.LinkedList;

class SocketChannelProxy implements SocketChannelWriter {
    private static final String TAG = SocketChannelProxy.class.getSimpleName();

    private final SocketEngine mEngine;
    private final Listener mListener;

    private SelectionKey mKey;

    private boolean mIsClosed = false;

    private final LinkedList<ByteBuffer> mWriteQueue = new LinkedList<>();

    SocketChannelProxy(SocketEngine engine, Listener listener) {
        mEngine = engine;
        this.mListener = listener;
    }

    void onSelected(SelectionKey key) {
        mKey = key;
        try {
            if (!key.isValid()) {
                Log.d(TAG, "Skip invalid key");
                return;
            }
            if (key.isConnectable()) {
                onConnectReady();
            }
            if (key.isReadable()) {
                onReadReady();
            }
            if (key.isWritable() && key.isValid()) {
                onWriteReady();
            }
        } catch (IOException | CancelledKeyException e) {
            onClosed();
        }
    }

    void onCancelled() {
        Log.d(TAG, "onCancelled");
        onClosed();
    }

    private void onClosed() {
        Log.d(TAG, "onClosed");
        close();
        mListener.onClosed();
    }

    private void onConnectReady() throws IOException {
        // Log.d(TAG, "onConnectReady");
        try {
            if (((SocketChannel) mKey.channel()).finishConnect()) {
                mKey.interestOps(SelectionKey.OP_READ);
                mListener.onSocketConnected();
                return;
            }
        } catch (CancelledKeyException e) {
            // Connected but SelectionKey is cancelled. Fall through to failure
        }
        Log.d(TAG, "Failed to connect");
        onClosed();
    }

    private void onReadReady() throws IOException {
        // Log.d(TAG, "onReadReady");
        SocketChannel ch = (SocketChannel) mKey.channel();
        LinkedList<byte[]> list = new LinkedList<>();


        while (true) {
            byte[] buff = mEngine.read(ch);
            if (buff == null) {
                break;
            }
            list.add(buff);
        }
<commit_msg>Delete empty line<commit_after>/*
 * WireSpider
 *
 * Copyright (c) 2015 kazyx
 *
 * This software is released under the MIT License.
 * http://opensource.org/licenses/mit-license.php
 */

package net.kazyx.wirespider;

import net.kazyx.wirespider.util.IOUtil;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.CancelledKeyException;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.LinkedList;

class SocketChannelProxy implements SocketChannelWriter {
    private static final String TAG = SocketChannelProxy.class.getSimpleName();

    private final SocketEngine mEngine;
    private final Listener mListener;

    private SelectionKey mKey;

    private boolean mIsClosed = false;

    private final LinkedList<ByteBuffer> mWriteQueue = new LinkedList<>();

    SocketChannelProxy(SocketEngine engine, Listener listener) {
        mEngine = engine;
        this.mListener = listener;
    }

    void onSelected(SelectionKey key) {
        mKey = key;
        try {
            if (!key.isValid()) {
                Log.d(TAG, "Skip invalid key");
                return;
            }
            if (key.isConnectable()) {
                onConnectReady();
            }
            if (key.isReadable()) {
                onReadReady();
            }
            if (key.isWritable() && key.isValid()) {
                onWriteReady();
            }
        } catch (IOException | CancelledKeyException e) {
            onClosed();
        }
    }

    void onCancelled() {
        Log.d(TAG, "onCancelled");
        onClosed();
    }

    private void onClosed() {
        Log.d(TAG, "onClosed");
        close();
        mListener.onClosed();
    }

    private void onConnectReady() throws IOException {
        // Log.d(TAG, "onConnectReady");
        try {
            if (((SocketChannel) mKey.channel()).finishConnect()) {
                mKey.interestOps(SelectionKey.OP_READ);
                mListener.onSocketConnected();
                return;
            }
        } catch (CancelledKeyException e) {
            // Connected but SelectionKey is cancelled. Fall through to failure
        }
        Log.d(TAG, "Failed to connect");
        onClosed();
    }

    private void onReadReady() throws IOException {
        // Log.d(TAG, "onReadReady");
        SocketChannel ch = (SocketChannel) mKey.channel();
        LinkedList<byte[]> list = new LinkedList<>();

        while (true) {
            byte[] buff = mEngine.read(ch);
            if (buff == null) {
                break;
            }
            list.add(buff);
        }

{'fraction_non_alphanumeric': 0.1096982349591953, 'fraction_numerical': 0.0015183146707155057, 'mean_word_length': 1.9706877113866967, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '5712794', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1430, 'n_words': 334}