<commit_before>/*
 * socket.io-java-client XhrTransport.java
 *
 * Copyright (c) 2012, <NAME>
 * socket.io-java-client is a implementation of the socket.io protocol in Java.
 * 
 * See LICENSE file for more information
 */
package io.socket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * The Class XhrTransport.
 */
class XhrTransport implements IOTransport {

	/** The String to identify this Transport. */
	public static final String TRANSPORT_NAME = "xhr-polling";

	/** The connection. */
	private IOConnection connection;

	/** The url. */
	private URL url;

	/** The queue holding elements to send. */
	ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();

	/** background thread for managing the server connection. */
	PollThread pollThread = null;

	/** Indicates whether the {@link IOConnection} wants us to be connected. */
	private boolean connect;

	/** Indicates whether {@link PollThread} is blocked. */
	private boolean blocked;

	HttpURLConnection urlConnection;

	/**
	 * The Class ReceiverThread.
	 */
	private class PollThread extends Thread {

		/**
		 * Instantiates a new receiver thread.
		 */
		public PollThread() {
			super(TRANSPORT_NAME);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Thread#run()
		 */
		@Override
		public void run() {
			connection.transportConnected();
			while (isConnect()) {
				try {
					String line;
					URL url = new URL(XhrTransport.this.url.toString() + "?t=" + System.currentTimeMillis());
					urlConnection = (HttpURLConnection) url.openConnection();
					if (!queue.isEmpty()) {
						urlConnection.setDoOutput(true);
						urlConnection.setRequestMethod("POST") ;
						BufferedWriter output = new BufferedWriter(
								new OutputStreamWriter(
										urlConnection.getOutputStream()));
						if (queue.size() == 1) {
							line = queue.poll();
							output.write(line);
							System.out.println(line);
						} else {
							Iterator<String> iter = queue.iterator();
							while (iter.hasNext()) {
								line = iter.next();
								output.write("\ufffd" + line.length()
										+ "\ufffd" + line);
								System.out.println("\ufffd" + line.length()
										+ "\ufffd" + line);
								iter.remove();
							}
						}
						output.close();
					} else {
						setBlocked(true);
						InputStream plainInput = urlConnection.getInputStream();
						if (plainInput != null) {
							BufferedReader input = new BufferedReader(
									new InputStreamReader(plainInput));
							while ((line = input.readLine()) != null) {
								if (connection != null)
									connection.transportMessage(line);
							}
						}
						setBlocked(false);
					}

				} catch (IOException e) {
					if (connection != null && interrupted() == false) {
						connection.transportError(e);
						return;
					}
				}
			}
			connection.transportDisconnected();
		}
	}

	/**
	 * Creates a new Transport for the given url an {@link IOConnection}.
	 * 
	 * @param url
	 *            the url
	 * @param connection
	 *            the connection
	 * @return the iO transport
	 */
	public static IOTransport create(URL url, IOConnection connection) {
		try {
			URL xhrUrl = new URL(url.toString() + IOConnection.SOCKET_IO_1
					+ TRANSPORT_NAME + "/" + connection.getSessionId());
			return new XhrTransport(xhrUrl, connection);<commit_msg>XhrTransport: Can now receive unframed data.<commit_after>/*
 * socket.io-java-client XhrTransport.java
 *
 * Copyright (c) 2012, <NAME>
 * socket.io-java-client is a implementation of the socket.io protocol in Java.
 * 
 * See LICENSE file for more information
 */
package io.socket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * The Class XhrTransport.
 */
class XhrTransport implements IOTransport {

	/** The String to identify this Transport. */
	public static final String TRANSPORT_NAME = "xhr-polling";

	/** The connection. */
	private IOConnection connection;

	/** The url. */
	private URL url;

	/** The queue holding elements to send. */
	ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();

	/** background thread for managing the server connection. */
	PollThread pollThread = null;

	/** Indicates whether the {@link IOConnection} wants us to be connected. */
	private boolean connect;

	/** Indicates whether {@link PollThread} is blocked. */
	private boolean blocked;

	HttpURLConnection urlConnection;

	/**
	 * The Class ReceiverThread.
	 */
	private class PollThread extends Thread {

		private static final String CHARSET = "UTF-8";

		/**
		 * Instantiates a new receiver thread.
		 */
		public PollThread() {
			super(TRANSPORT_NAME);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Thread#run()
		 */
		@Override
		public void run() {
			connection.transportConnected();
			while (isConnect()) {
				try {
					String line;
					URL url = new URL(XhrTransport.this.url.toString() + "?t="
							+ System.currentTimeMillis());
					urlConnection = (HttpURLConnection) url.openConnection();
					if (!queue.isEmpty()) {
						urlConnection.setDoOutput(true);
						OutputStream output = urlConnection.getOutputStream();
						if (queue.size() == 1) {
							line = queue.poll();
							output.write(line.getBytes(CHARSET));
						} else {
							Iterator<String> iter = queue.iterator();
							while (iter.hasNext()) {
								line = iter.next() + "\n";
								line = "\ufffd" + line.length()
										+ "\ufffd";
								output.write(line.getBytes(CHARSET));
								iter.remove();
							}
						}
						output.close();
						InputStream input = urlConnection.getInputStream();
						byte[] buffer = new byte[1024];
						while(input.read(buffer) > 0) {
						}
						input.close();
					} else {
						setBlocked(true);
						InputStream plainInput = urlConnection.getInputStream();
						BufferedReader input = new BufferedReader(
								new InputStreamReader(plainInput));
						while ((line = input.readLine()) != null) {
							if (connection != null)
								connection.transportMessage(line);
						}
						setBlocked(false);
					}

				} catch (IOException e) {
					if (connection != null && interrupted() == false) {
						connection.transportError(e);
						return;
					}
				}
				try {
					sleep(100);
				} catch (InterruptedException e) {
				}
			}
			connection.transportDisconnected();
		}
	}

	/**
	 * Creates a new Transport for the given url an {@link IOConnection}.
	 * 
	 * @param url
	 *            the url
	 * @param connection
	 *            the connection
	 * @return the iO transport
	 */
	public static IOTransport create(URL url, IOConnection connection) {
		try {
			URL xhrUrl = new URL(url.toString() + IOConnection.SOCKET_IO_1
					+ TRANSPORT_NAME + "/" + connection.getSessionId());
			return new XhrTransport(xhrUrl, connection);
{'fraction_non_alphanumeric': 0.1228023441662227, 'fraction_numerical': 0.002797016515716569, 'mean_word_length': 2.94794952681388, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, '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': '6251893', 'n_tokens_mistral': 2763, 'n_tokens_neox': 2122, 'n_words': 562}