<commit_before>package com.neofect.communicator.tcp;

import android.util.Log;

import com.neofect.communicator.Connection;
import com.neofect.communicator.ConnectionType;
import com.neofect.communicator.Controller;
import com.neofect.communicator.Device;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class TcpConnection extends Connection {

	private static final String LOG_TAG = "TcpConnection";

	private static final int BUFFER_SIZE = 1024;

	private String ip;
	private int port;
	private Socket socket;
	private OutputStream outputStream;
	private InputStream inputStream;
	private byte[] buffer = new byte[BUFFER_SIZE];

	public TcpConnection(String ip, int port, Controller<? extends Device> controller) {
		super(ConnectionType.TCP, controller);
		this.ip = ip;
		this.port = port;
	}

	@Override
	protected void connect() {
		handleConnecting();
		new Thread(() -> {
			try {
				socket = new Socket(ip, port);
				outputStream = socket.getOutputStream();
				inputStream = socket.getInputStream();
			} catch (IOException e) {
				clear();
				handleFailedToConnect(new RuntimeException("Failed to connect to '" + getDeviceIdentifier() + "'", e));
				return;
			}
			startReadThread();
			handleConnected();
		}).start();
	}

	@Override
	public void disconnect() {
		clear();
		handleDisconnected();
	}

	@Override
	public void write(byte[] data) {
		if (outputStream == null) {
			Log.e(LOG_TAG, "write: Not connected!");
			return;
		}
		try {
			outputStream.write(data);
		} catch (IOException e) {
			Log.e(LOG_TAG, "write: ", e);
		}
	}

	private void startReadThread() {
		new Thread(() -> {
			while (true) {
				try {
					int numberOfReadBytes = inputStream.read(buffer, 0, buffer.length);
					byte[] readData = new byte[numberOfReadBytes];
					System.arraycopy(buffer, 0, readData, 0, numberOfReadBytes);
					handleReadData(readData);
				} catch (IOException e) {
					Log.d(LOG_TAG, "run: IOException on read() '" + getDeviceIdentifier() + "'", e);
					clear();
					handleDisconnected();
					break;
				}
			}
		}).start();
	}

	private void clear() {
		if (socket != null) {
			try {
				socket.close();
			} catch (IOException e1) {
				Log.e(LOG_TAG, "clear: ", e1);<commit_msg>Fix some errors in TcpConnection<commit_after>package com.neofect.communicator.tcp;

import android.util.Log;

import com.neofect.communicator.Connection;
import com.neofect.communicator.ConnectionType;
import com.neofect.communicator.Controller;
import com.neofect.communicator.Device;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class TcpConnection extends Connection {

	private static final String LOG_TAG = "TcpConnection";

	private static final int BUFFER_SIZE = 1024;

	private Executor executor = Executors.newSingleThreadExecutor();
	private String ip;
	private int port;
	private Socket socket;
	private OutputStream outputStream;
	private InputStream inputStream;
	private byte[] buffer = new byte[BUFFER_SIZE];

	public TcpConnection(String ip, int port, Controller<? extends Device> controller) {
		super(ConnectionType.TCP, controller);
		this.ip = ip;
		this.port = port;
	}

	@Override
	protected void connect() {
		handleConnecting();
		new Thread(() -> {
			try {
				socket = new Socket(ip, port);
				outputStream = socket.getOutputStream();
				inputStream = socket.getInputStream();
			} catch (IOException e) {
				clear();
				handleFailedToConnect(new RuntimeException("Failed to connect to '" + getDeviceIdentifier() + "'", e));
				return;
			}
			startReadThread();
			handleConnected();
		}).start();
	}

	@Override
	public void disconnect() {
		clear();
		handleDisconnected();
	}

	@Override
	public void write(byte[] data) {
		executor.execute(() -> {
			if (outputStream == null) {
				Log.e(LOG_TAG, "write: Not connected!");
				return;
			}
			try {
				outputStream.write(data);
			} catch (IOException e) {
				Log.e(LOG_TAG, "write: ", e);
			}
		});
	}

	private void startReadThread() {
		new Thread(() -> {
			while (true) {
				try {
					int numberOfReadBytes = inputStream.read(buffer, 0, buffer.length);
					if (numberOfReadBytes < 0) {
						onDisconnected();
						return;
					}
					byte[] readData = new byte[numberOfReadBytes];
					System.arraycopy(buffer, 0, readData, 0, numberOfReadBytes);
					handleReadData(readData);
				} catch (IOException e) {
					Log.d(LOG_TAG, "run: IOException on read() '" + getDeviceIdentifier() + "'", e);
					onDisconnected();
					return;
				}
			}
		}).start();
	}

	private void onDisconnected() {
		clear();
		handleDisconnected();
	}

	private void clear() {
		if (socket != null) {
			try {
				socket.close();
			} catch (IOException e1) {
				Log.e(LOG_TAG, "clear: ", e1);
{'fraction_non_alphanumeric': 0.12973523421588595, 'fraction_numerical': 0.003869653767820774, 'mean_word_length': 3.8099902056807053, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 10, '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': '3525093', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1502, 'n_words': 383}