<commit_before>﻿using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace FluentFTP.Client.BaseClient {
	public partial class BaseFtpClient {


		/// <summary>
		/// Data shouldn't be on the socket, if it is it probably means we've been disconnected.
		/// Read and discard whatever is there and optionally close the connection.
		/// Returns the stale data as text, if any, or null if none was found.
		/// </summary>
		/// <param name="closeStream">close the connection?</param>
		/// <param name="evenEncrypted">even read encrypted data?</param>
		/// <param name="traceData">trace data to logs?</param>
		protected string ReadStaleData(bool closeStream, bool evenEncrypted, bool traceData) {
			string staleData = null;
			if (m_stream != null && m_stream.SocketDataAvailable > 0) {
				if (traceData) {
					LogWithPrefix(FtpTraceLevel.Info, "There is stale data on the socket, maybe our connection timed out or you did not call GetReply(). Re-connecting...");
				}

				if (m_stream.IsConnected && (!m_stream.IsEncrypted || evenEncrypted)) {
					var buf = new byte[m_stream.SocketDataAvailable];
					m_stream.RawSocketRead(buf);
					staleData = Encoding.GetString(buf).TrimEnd('\r', '\n');
					if (traceData) {
						LogWithPrefix(FtpTraceLevel.Verbose, "The stale data was: " + staleData);
					}
					if (string.IsNullOrEmpty(staleData)) {
						closeStream = false;
					}
				}

				if (closeStream) {
					LogWithPrefix(FtpTraceLevel.Verbose, "Closing stream because of stale data");
					m_stream.Close();
				}
			}
			return staleData;
		}

		/// <summary>
		/// Data shouldn't be on the socket, if it is it probably means we've been disconnected.
		/// Read and discard whatever is there and optionally close the connection.
		/// Returns the stale data as text, if any, or null if none was found.
		/// </summary>
		/// <param name="closeStream">close the connection?</param>
		/// <param name="evenEncrypted">even read encrypted data?</param>
		/// <param name="traceData">trace data to logs?</param>
		/// <param name="token">The token that can be used to cancel the entire process</param>
		protected async Task<string> ReadStaleData(bool closeStream, bool evenEncrypted, bool traceData, CancellationToken token) {
			string staleData = null;
			if (m_stream != null && m_stream.SocketDataAvailable > 0) {
				if (traceData) {
					LogWithPrefix(FtpTraceLevel.Info, "There is stale data on the socket, maybe our connection timed out or you did not call GetReply(). Re-connecting...");
				}

				if (m_stream.IsConnected && (!m_stream.IsEncrypted || evenEncrypted)) {
					var buf = new byte[m_stream.SocketDataAvailable];
					await m_stream.RawSocketReadAsync(buf, token);
					staleData = Encoding.GetString(buf).TrimEnd('\r', '\n');
					if (traceData) {
						LogWithPrefix(FtpTraceLevel.Verbose, "The stale data was: " + staleData);
					}
				}

				if (closeStream) {
					LogWithPrefix(FtpTraceLevel.Verbose, "Closing stream because of stale data");
					m_stream.Close();
				}
			}
			return staleData;
		}

	}
}<commit_msg>ReadStaleData<commit_after>﻿using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace FluentFTP.Client.BaseClient {
	public partial class BaseFtpClient {


		/// <summary>
		/// Data shouldn't be on the socket, if it is it probably means we've been disconnected.
		/// Read and discard whatever is there and optionally close the connection.
		/// Returns the stale data as text, if any, or null if none was found.
		/// </summary>
		/// <param name="closeStream">close the connection?</param>
		/// <param name="logData">copy stale data information to logs?</param>
		/// <param name="logFrom">for the log information</param>
		protected string ReadStaleData(bool closeStream, bool logData, string logFrom) {
			string staleData = null;

			if (logData) {
				LogWithPrefix(FtpTraceLevel.Verbose, "Checking for stale data: " + logFrom);
			}

			if (m_stream != null) {
				while (m_stream.SocketDataAvailable > 0) {
					if (logData) {
						LogWithPrefix(FtpTraceLevel.Info, "Socket has stale data");
					}
					byte[] buf = new byte[m_stream.SocketDataAvailable];
					if (m_stream.IsEncrypted) {
						m_stream.Read(buf, 0, buf.Length);
					}
					else {
						m_stream.RawSocketRead(buf);
					}
					staleData = Encoding.GetString(buf).TrimEnd('\0', '\r', '\n');
					if (logData) {
						LogWithPrefix(FtpTraceLevel.Verbose, "The stale data was: " + staleData);
					}
				}
			}

			if (string.IsNullOrEmpty(staleData)) {
				closeStream = false;
			}

			if (closeStream) {
				LogWithPrefix(FtpTraceLevel.Info, "Closing stream because of stale data");
				m_stream.Close();
			}

			return staleData;
		}

		/// <summary>
		/// Data shouldn't be on the socket, if it is it probably means we've been disconnected.
		/// Read and discard whatever is there and optionally close the connection.
		/// Returns the stale data as text, if any, or null if none was found.
		/// </summary>
		/// <param name="closeStream">close the connection?</param>
		/// <param name="logData">copy stale data information to logs?</param>
		/// <param name="token">The token that can be used to cancel the entire process</param>
		protected async Task<string> ReadStaleDataAsync(bool closeStream, bool traceData, string logFrom, CancellationToken token) {
			string staleData = null;

			if (traceData) {
				LogWithPrefix(FtpTraceLevel.Verbose, "Checking for stale data: " + logFrom);
			}

			if (m_stream != null) {
				while (m_stream.SocketDataAvailable > 0) {
					if (traceData) {
						LogWithPrefix(FtpTraceLevel.Info, "Socket has stale data");
					}
					byte[] buf = new byte[m_stream.SocketDataAvailable];
					if (m_stream.IsEncrypted) {
						await m_stream.ReadAsync(buf, 0, buf.Length, token);
					}
					else {
						await m_stream.RawSocketReadAsync(buf, token);
					}
					staleData = Encoding.GetString(buf).TrimEnd('\0', '\r', '\n');
					if (traceData) {
						LogWithPrefix(FtpTraceLevel.Verbose, "The stale data was: " + staleData);
					}
				}
			}

			if (string.IsNullOrEmpty(staleData)) {
				closeStream = false;
			}

			if (closeStream) {
				LogWithPrefix(FtpTraceLevel.Info, "Closing stream because of stale data");
				m_stream.Close();
			}

			return staleData;
		}

	}
}
{'fraction_non_alphanumeric': 0.12280145777214388, 'fraction_numerical': 0.0012676279511963239, 'mean_word_length': 3.285132382892057, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 43, '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': '2001700', 'n_tokens_mistral': 2371, 'n_tokens_neox': 1929, 'n_words': 569}