<commit_before>package org.flexunit.ant;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.text.MessageFormat;

import org.apache.tools.ant.BuildException;

/**
 * Class responsible for managing the connections to the test runner and any boiler plate in the network interactivity. 
 */
public class FlexUnitSocketServer
{
   
   private static final char NULL_BYTE = '\u0000';
   private static final String POLICY_FILE_REQUEST = "<policy-file-request/>";
   
   //Uncomment to use DTD for validation rather than schema
   //private static final String DOMAIN_POLICY = "<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy SYSTEM \"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\"><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"{0}\" /></cross-domain-policy>";

   private static final String DOMAIN_POLICY = 
      "<?xml version=\"1.0\"?>"
      + "<cross-domain-policy xmlns=\"http://localhost\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.adobe.com/xml/schemas PolicyFileSocket.xsd\">"
      + "<allow-access-from domain=\"*\" to-ports=\"{0}\" />"
      + "</cross-domain-policy>";
   
   private static final String START_OF_TEST_RUN_ACK = "<startOfTestRunAck/>";
   private static final String END_OF_TEST_RUN = "<endOfTestRun/>";
   private static final String END_OF_TEST_RUN_ACK = "<endOfTestRunAck/>";
   
   private int port;
   private int timeout;
   private int inboundBufferSize;  //if this is not set high enough, incoming data may clobber unread data in the buffer
   private boolean waitForPolicyFile;

   private ServerSocket serverSocket = null;
   private Socket clientSocket = null;
   private BufferedInputStream inboundStream = null;
   private OutputStream outboundStream = null;

   public FlexUnitSocketServer(int port, int timeout, int inboundBufferSize, boolean waitForPolicyFile)
   {
      this.port = port;
      this.timeout = timeout;
      this.inboundBufferSize = inboundBufferSize;
      this.waitForPolicyFile = waitForPolicyFile;
   }

   /**
    * Starts the socket server, managing policy file requests, and starting the test process.
    */
   public void start() throws IOException
   {
      LoggingUtil.log("Starting server ...");
      
      try
      {
         openServerSocket();
         openClientSocket();
         prepareClientSocket();
      }
      catch (SocketTimeoutException e)
      {
         throw new BuildException("Socket timeout waiting for flexunit report", e);
      }
   }
   
   /**
    * Resets the client connection.
    */
   private void resetInboundStream() throws IOException
   {
      LoggingUtil.log("Resetting client connection ...");
      
      closeClientSocket();
      openClientSocket();
   }
   
   /**
    * Creates a connection on the specified socket. Waits {socketTimeout}
    * seconds for a client connection before throwing an error
    */
   private void openServerSocket() throws IOException
   {
      serverSocket = new ServerSocket(port);
      serverSocket.setSoTimeout(timeout);

      LoggingUtil.log("Opening server socket on port [" + port + "].");
   }

   /**
    * Creates the client connection. This method will pause until the connection
    * is made or the timout limit is reached.
    * 
    * Once a connection is established opens the in and out buffer.
    */
   private void openClientSocket() throws IOException
   {
      LoggingUtil.log("Waiting for client connection ...");
      
      // This method blocks until a connection is made.
      clientSocket = serverSocket.accept();

      LoggingUtil.log("Client connected.");
      LoggingUtil.log("Setting inbound buffer size to " + inboundBufferSize + " bytes.");
      
      inboundStream = new BufferedInputStream(clientSocket.getInputStream(), inboundBufferSize);
      outboundStream = new BufferedOutputStream(clientSocket.getOutputStream());
      
      LoggingUtil.log("Receiving data ...");
   }
   
   /**
    * Decides whether to send a policy request or a start ack
    */
   private void prepareClientSocket() throws IOException
   {
      // if it's a policy request, make sure the first thing we send is a policy response
      if (waitForPolicyFile)
      {
         String request = readNextTokenFromSocket();
         if (request.equals(POLICY_FILE_REQUEST))
         {
            LoggingUtil.log("Policy file requested.");
            
            sendPolicyFile();
            resetInboundStream();<commit_msg>Updated debugging output for inbound buffer size.<commit_after>package org.flexunit.ant;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.text.MessageFormat;

import org.apache.tools.ant.BuildException;

/**
 * Class responsible for managing the connections to the test runner and any boiler plate in the network interactivity. 
 */
public class FlexUnitSocketServer
{
   
   private static final char NULL_BYTE = '\u0000';
   private static final String POLICY_FILE_REQUEST = "<policy-file-request/>";
   
   //Uncomment to use DTD for validation rather than schema
   //private static final String DOMAIN_POLICY = "<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy SYSTEM \"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\"><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"{0}\" /></cross-domain-policy>";

   private static final String DOMAIN_POLICY = 
      "<?xml version=\"1.0\"?>"
      + "<cross-domain-policy xmlns=\"http://localhost\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.adobe.com/xml/schemas PolicyFileSocket.xsd\">"
      + "<allow-access-from domain=\"*\" to-ports=\"{0}\" />"
      + "</cross-domain-policy>";
   
   private static final String START_OF_TEST_RUN_ACK = "<startOfTestRunAck/>";
   private static final String END_OF_TEST_RUN = "<endOfTestRun/>";
   private static final String END_OF_TEST_RUN_ACK = "<endOfTestRunAck/>";
   
   private int port;
   private int timeout;
   private int inboundBufferSize;  //if this is not set high enough, incoming data may clobber unread data in the buffer
   private boolean waitForPolicyFile;

   private ServerSocket serverSocket = null;
   private Socket clientSocket = null;
   private BufferedInputStream inboundStream = null;
   private OutputStream outboundStream = null;

   public FlexUnitSocketServer(int port, int timeout, int inboundBufferSize, boolean waitForPolicyFile)
   {
      this.port = port;
      this.timeout = timeout;
      this.inboundBufferSize = inboundBufferSize;
      this.waitForPolicyFile = waitForPolicyFile;
   }

   /**
    * Starts the socket server, managing policy file requests, and starting the test process.
    */
   public void start() throws IOException
   {
      LoggingUtil.log("Starting server ...");
      
      try
      {
         openServerSocket();
         openClientSocket();
         prepareClientSocket();
      }
      catch (SocketTimeoutException e)
      {
         throw new BuildException("Socket timeout waiting for flexunit report", e);
      }
   }
   
   /**
    * Resets the client connection.
    */
   private void resetInboundStream() throws IOException
   {
      LoggingUtil.log("Resetting client connection ...");
      
      closeClientSocket();
      openClientSocket();
   }
   
   /**
    * Creates a connection on the specified socket. Waits {socketTimeout}
    * seconds for a client connection before throwing an error
    */
   private void openServerSocket() throws IOException
   {
      serverSocket = new ServerSocket(port);
      serverSocket.setSoTimeout(timeout);

      LoggingUtil.log("Opening server socket on port [" + port + "].");
   }

   /**
    * Creates the client connection. This method will pause until the connection
    * is made or the timout limit is reached.
    * 
    * Once a connection is established opens the in and out buffer.
    */
   private void openClientSocket() throws IOException
   {
      LoggingUtil.log("Waiting for client connection ...");
      
      // This method blocks until a connection is made.
      clientSocket = serverSocket.accept();

      LoggingUtil.log("Client connected.");
      LoggingUtil.log("Setting inbound buffer size to [" + inboundBufferSize + "] bytes.");
      
      inboundStream = new BufferedInputStream(clientSocket.getInputStream(), inboundBufferSize);
      outboundStream = new BufferedOutputStream(clientSocket.getOutputStream());
      
      LoggingUtil.log("Receiving data ...");
   }
   
   /**
    * Decides whether to send a policy request or a start ack
    */
   private void prepareClientSocket() throws IOException
   {
      // if it's a policy request, make sure the first thing we send is a policy response
      if (waitForPolicyFile)
      {
         String request = readNextTokenFromSocket();
         if (request.equals(POLICY_FILE_REQUEST))
         {
            LoggingUtil.log("Policy file requested.");
            
            sendPolicyFile();
            resetInboundStream();
{'fraction_non_alphanumeric': 0.10352564102564103, 'fraction_numerical': 0.003205128205128205, 'mean_word_length': 3.041882556131261, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 4, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 12}, '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': '417174', 'n_tokens_mistral': 2570, 'n_tokens_neox': 2492, 'n_words': 733}