<commit_before>package com.testingtech.playits.canfilter;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.TimerTask;

import org.json.JSONException;
import org.json.JSONObject;

public class ResponderTask extends TimerTask {

  private String key;
  private Car2XEntry car2xEntry;
  private Socket socket;

  /**
   * Periodically sends a key-value par encoded as a JSON String over a socket's
   * output stream.
   * @param key openxc key
   * @param car2xEntry corresponding value
   * @param socket TCP responding channel
   */
  public ResponderTask(String key, Car2XEntry car2xEntry, Socket socket) {
    this.key = key;
    this.car2xEntry = car2xEntry;
    this.socket = socket;
  }

  @Override
  public void run() {
    if (car2xEntry.getTimestamp() > 0) {
      try {
        JSONObject response = createResponse();
        send(response);
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }
  }

  private JSONObject createResponse() throws JSONException {
    JSONObject response = new JSONObject();
    response.put("Timestamp", car2xEntry.getTimestamp());
    response.put("OpenXCKey", key);
    response.put("OBD2Key", car2xEntry.getObd2key());
    response.put("valueA", car2xEntry.getValueA());
    response.put("valueB", car2xEntry.getValueB());
    return response;
  }

  private void send(JSONObject jsonObject) {
    String jsonAsString = jsonObject.toString();
    byte[] bytes = jsonAsString.getBytes(Charset.forName("UTF-8"));
    OutputStream outputStream;
    try {
      outputStream = socket.getOutputStream();
      outputStream.write(bytes);
      outputStream.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

}<commit_msg>Cancel task if error occurs.<commit_after>package com.testingtech.playits.canfilter;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.TimerTask;

import org.json.JSONException;
import org.json.JSONObject;

public class ResponderTask extends TimerTask {

  private String key;
  private Car2XEntry car2xEntry;
  private Socket socket;

  /**
   * Periodically sends a key-value par encoded as a JSON String over a
   * socket's output stream.
   * 
   * @param key
   *            openxc key
   * @param car2xEntry
   *            corresponding value entry
   * @param socket
   *            TCP responding channel
   */
  public ResponderTask(String key, Car2XEntry car2xEntry, Socket socket) {
    this.key = key;
    this.car2xEntry = car2xEntry;
    this.socket = socket;
  }

  @Override
  public void run() {
    try {
      JSONObject response = createResponse();
      System.out.println("[ResponderTask] Sending " + response);
      sendJSONObject(response);
    } catch (JSONException e) {
      System.out.println("[ResponderTask] Error creating JSON response. "
          + e.getMessage());
    }
  }

  private JSONObject createResponse() throws JSONException {
    JSONObject response = new JSONObject();
    response.put("OpenXCKey", key);
    response.put("OBD2Key", car2xEntry.getObd2key());
    response.put("valueA", car2xEntry.getValueA());
    response.put("valueB", car2xEntry.getValueB());
    response.put("respTimestamp", car2xEntry.getTimestamp());
    return response;
  }

  private void sendJSONObject(JSONObject jsonObject) {
    String jsonAsString = jsonObject.toString();
    byte[] bytes = jsonAsString.getBytes(Charset.forName("UTF-8"));
    try {
      sendBytes(bytes);
    } catch (IOException e) {
      System.err
          .println("[ResponderTask] Error while sending response for "
              + key + ": " + e.getMessage());
      cancel();
    }
  }

  private void sendBytes(byte[] bytes) throws IOException {
    OutputStream outputStream = socket.getOutputStream();
    outputStream.write(bytes);
    outputStream.flush();
  }
}
{'fraction_non_alphanumeric': 0.11032388663967611, 'fraction_numerical': 0.0075910931174089065, 'mean_word_length': 3.3155021834061134, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 4, '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': '1921135', 'n_tokens_mistral': 1182, 'n_tokens_neox': 1157, 'n_words': 283}