<commit_before>package com.intellij.rt.execution.junit2.segments;

import junit.framework.Test;
import junit.runner.BaseTestRunner;

import java.io.*;
import java.util.Vector;

public class Packet extends PacketWriter implements PoolOfKnownObjects {
  private OutputObjectRegistry myRegistry;
  private PacketProcessor myTransport;
  public static final char ourSpecialSymbol = '$';
  public static final char[] ourSymbolsToEncode = new char[] {'\n', '\r', SegmentedStream.SPECIAL_SYMBOL};
  public static final int CODE_LENGTH = 2;

  public Packet(PacketProcessor transport, OutputObjectRegistry registry) {
    myTransport = transport;
    myRegistry = registry;
  }

  public Packet addObject(Test test) {
    return addReference(myRegistry.referenceTo(test));
  }

  public Packet addReference(String reference) {
    appendString(reference + REFERENCE_END);
    return this;
  }

  public Packet switchInputTo(Test test) {
    appendString(INPUT_COSUMER);
    return addObject(test);
  }

  public Packet addString(String string) {
    appendString(string);
    return this;
  }

  public void send() {
    sendThrough(myTransport);
  }

  public Packet addLong(long integer) {
    appendLong(integer);
    return this;
  }

  public Packet setTestState(Test test, int state) {
    return addString(CHANGE_STATE).addObject(test).addLong(state);
  }

  public Packet addLimitedString(String message) {
    appendLimitedString(message);
    return this;
  }

  public Packet addThrowable(Throwable throwable) {
    String filteredTrace = BaseTestRunner.getFilteredTrace(throwable);
    String message = makeNewLinesCompatibleWithJUnit(throwable.toString());
    addLimitedString(message);
    if (filteredTrace.startsWith(message))
      filteredTrace = filteredTrace.substring(message.length());
    addLimitedString(new TraceFilter(filteredTrace).execute());
    return this;
  }

  private static String makeNewLinesCompatibleWithJUnit(String string) {
    try {
      StringWriter buffer = new StringWriter();
      PrintWriter writer = new PrintWriter(buffer);
      BufferedReader reader = new BufferedReader(new StringReader(string));
      String line;
      while ((line = reader.readLine()) != null)
        writer.println(line);
      return buffer.getBuffer().toString();
    } catch (IOException e) {return null;}
  }

  public static String encode(String packet) {
    StringBuffer buffer = new StringBuffer(packet.length());
    for (int i = 0; i < packet.length(); i++) {
      char chr = packet.charAt(i);
      if (chr == ourSpecialSymbol) {
        buffer.append(chr);<commit_msg>NPE when throwable.toString() returns null (wierd...) fixed.<commit_after>package com.intellij.rt.execution.junit2.segments;

import junit.framework.Test;
import junit.runner.BaseTestRunner;

import java.io.*;
import java.util.Vector;

public class Packet extends PacketWriter implements PoolOfKnownObjects {
  private OutputObjectRegistry myRegistry;
  private PacketProcessor myTransport;
  public static final char ourSpecialSymbol = '$';
  public static final char[] ourSymbolsToEncode = new char[] {'\n', '\r', SegmentedStream.SPECIAL_SYMBOL};
  public static final int CODE_LENGTH = 2;

  public Packet(PacketProcessor transport, OutputObjectRegistry registry) {
    myTransport = transport;
    myRegistry = registry;
  }

  public Packet addObject(Test test) {
    return addReference(myRegistry.referenceTo(test));
  }

  public Packet addReference(String reference) {
    appendString(reference + REFERENCE_END);
    return this;
  }

  public Packet switchInputTo(Test test) {
    appendString(INPUT_COSUMER);
    return addObject(test);
  }

  public Packet addString(String string) {
    appendString(string);
    return this;
  }

  public void send() {
    sendThrough(myTransport);
  }

  public Packet addLong(long integer) {
    appendLong(integer);
    return this;
  }

  public Packet setTestState(Test test, int state) {
    return addString(CHANGE_STATE).addObject(test).addLong(state);
  }

  public Packet addLimitedString(String message) {
    appendLimitedString(message);
    return this;
  }

  public Packet addThrowable(Throwable throwable) {
    String filteredTrace = BaseTestRunner.getFilteredTrace(throwable);
    String message = makeNewLinesCompatibleWithJUnit(throwableToString(throwable));
    addLimitedString(message);
    if (filteredTrace.startsWith(message))
      filteredTrace = filteredTrace.substring(message.length());
    addLimitedString(new TraceFilter(filteredTrace).execute());
    return this;
  }

  private String throwableToString(final Throwable throwable) {
    final String tostring = throwable.toString();
    return tostring != null ? tostring : throwable.getClass().getName();
  }

  private static String makeNewLinesCompatibleWithJUnit(String string) {
    try {
      StringWriter buffer = new StringWriter();
      PrintWriter writer = new PrintWriter(buffer);
      BufferedReader reader = new BufferedReader(new StringReader(string));
      String line;
      while ((line = reader.readLine()) != null)
        writer.println(line);
      return buffer.getBuffer().toString();
    } catch (IOException e) {return null;}
  }

  public static String encode(String packet) {
    StringBuffer buffer = new StringBuffer(packet.length());
    for (int i = 0; i < packet.length(); i++) {
      char chr = packet.charAt(i);
      if (chr == ourSpecialSymbol) {
        buffer.append(chr);
{'fraction_non_alphanumeric': 0.09902805794975243, 'fraction_numerical': 0.0011003117549972493, 'mean_word_length': 4.17948717948718, 'pattern_counts': {'":': 0, '<': 5, '<?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': '7660676', 'n_tokens_mistral': 1555, 'n_tokens_neox': 1492, 'n_words': 383}