<commit_before>﻿using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;

namespace PCx.IO.Compression
{
	internal sealed class CompressStream : IDisposable
	{
		#region Fields

		private Stream _Stream;

		private readonly CompressionLevel _CompressionLevel;

		private CompressGraph _CompressGraph;

		private readonly byte[] _Buffer;
		private int _BufferPosition;

		#endregion

		#region Constructor

		public CompressStream(Stream stream, CompressionLevel compressionLevel, int bufferSize)
		{
			_Stream = stream;

			_CompressionLevel = compressionLevel;

			_Buffer = new byte[bufferSize];
		}

		#endregion

		#region Methods

		public void Write(byte[] buffer, int offset, int count)
		{
			InitializeGraph();

			var bufferPosition = 0;

			while (true)
			{
				if (_BufferPosition + (count - bufferPosition) >= _Buffer.Length)
				{
					var copyCount = _Buffer.Length - _BufferPosition;
					System.Buffer.BlockCopy(buffer, offset + bufferPosition, _Buffer, _BufferPosition, copyCount);

					_BufferPosition += copyCount;
					bufferPosition += copyCount;

					SendBuffer();
				}
				else
				{
					var copyCount = count - bufferPosition;
					System.Buffer.BlockCopy(buffer, offset + bufferPosition, _Buffer, _BufferPosition, copyCount);

					_BufferPosition += copyCount;

					break;
				}
			}
		}

		public void Flush()
		{
			SendBuffer();
		}

		private void InitializeGraph()
		{
			if (_CompressGraph == null)
			{
				Debug.Assert(_Stream != null);
<commit_msg>Refactoring of Write operation<commit_after>﻿using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;

namespace PCx.IO.Compression
{
	internal sealed class CompressStream : IDisposable
	{
		#region Fields

		private Stream _Stream;

		private readonly CompressionLevel _CompressionLevel;

		private CompressGraph _CompressGraph;

		private readonly byte[] _Buffer;
		private int _BufferPosition;

		#endregion

		#region Constructor

		public CompressStream(Stream stream, CompressionLevel compressionLevel, int bufferSize)
		{
			_Stream = stream;

			_CompressionLevel = compressionLevel;

			_Buffer = new byte[bufferSize];
		}

		#endregion

		#region Methods

		public void Write(byte[] buffer, int offset, int count)
		{
			InitializeGraph();

			var writeCount = 0;

			while (true)
			{
				var requiredCount = count - writeCount;

				var remainingCount = _Buffer.Length - _BufferPosition;

				if (requiredCount < remainingCount)
				{
					System.Buffer.BlockCopy(buffer, offset + writeCount, _Buffer, _BufferPosition, requiredCount);

					writeCount += requiredCount;

					_BufferPosition += requiredCount;

					break;
				}
				else
				{
					System.Buffer.BlockCopy(buffer, offset + writeCount, _Buffer, _BufferPosition, remainingCount);

					writeCount += remainingCount;

					_BufferPosition += remainingCount;

					SendBuffer();
				}
			}

			Debug.Assert(writeCount == count);
		}

		public void Flush()
		{
			SendBuffer();
		}

		private void InitializeGraph()
		{
			if (_CompressGraph == null)
			{
				Debug.Assert(_Stream != null);

{'fraction_non_alphanumeric': 0.08702633269107257, 'fraction_numerical': 0.0006422607578676942, 'mean_word_length': 3.5276162790697674, 'pattern_counts': {'":': 0, '<': 4, '<?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': '15946', 'n_tokens_mistral': 1135, 'n_tokens_neox': 885, 'n_words': 233}