<commit_before>using System;
using System.Data;
using System.IO;
using System.Text;
using NHibernate.SqlTypes;
using NHibernate.Util;
using Environment = NHibernate.Cfg.Environment;

namespace NHibernate.Type
{
	/// <summary>
	/// BinaryType.
	/// </summary>
	public class BinaryType : MutableType
	{
		/// <summary></summary>
		internal BinaryType() : this( new BinarySqlType() )
		{
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sqlType"></param>
		internal BinaryType( BinarySqlType sqlType ) : base( sqlType )
		{
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="cmd"></param>
		/// <param name="value"></param>
		/// <param name="index"></param>
		public override void Set( IDbCommand cmd, object value, int index )
		{
			//TODO: research into byte streams
			//if ( Cfg.Environment.UseStreamsForBinary ) {
			// Is this really necessary?
			// How do we do????

			//TODO: st.setBinaryStream( index, new ByteArrayInputStream( (byte[]) value ), ( (byte[]) value ).length );
			//}
			//else {
			//Need to set DbType in parameter????
			( ( IDataParameter ) cmd.Parameters[ index ] ).Value = ( byte[ ] ) value;
			//}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="rs"></param>
		/// <param name="index"></param>
		/// <returns></returns>
		public override object Get( IDataReader rs, int index )
		{
			if( Environment.UseStreamsForBinary )
			{
				// Is this really necessary?
				// see http://msdn.microsoft.com/library/en-us/cpguide/html/cpconobtainingblobvaluesfromdatabase.asp?frame=true 
				// for a how to on reading binary/blob values from a db...
				MemoryStream outputStream = new MemoryStream( 2048 );
				byte[ ] buffer = new byte[2048];
				long fieldOffset = 0;

				try
				{
					while( true )
					{
						long amountRead = rs.GetBytes( index, fieldOffset, buffer, 0, 2048 );

						if( amountRead == 0 )
						{
							break;
						}

						fieldOffset += amountRead;
						outputStream.Write( buffer, 0, ( int ) amountRead );
					}
					outputStream.Close();
				}
				catch( IOException ioe )
				{
					throw new HibernateException( "IOException occurred reading a binary value", ioe );
				}

				return outputStream.ToArray();

			}
			else
			{
				//TODO: not sure if this will work with all dbs
				return ( byte[ ] ) rs[ index ];
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="rs"></param>
		/// <param name="name"></param>
		/// <returns></returns>
		public override object Get( IDataReader rs, string name )
		{
			return Get( rs, rs.GetOrdinal( name ) );
		}

		/// <summary></summary>
		public override System.Type ReturnedClass
		{
			get { return typeof( byte[ ] ); }
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="x"></param>
		/// <param name="y"></param>
		/// <returns></returns>
		public override bool Equals( object x, object y )
		{
			if( x == y )
			{
				return true;
			}
			if( x == null || y == null )
			{
				return false;
			}

			return ArrayHelper.Equals( ( byte[ ] ) x, ( byte[ ] ) y );
		}

		/// <summary></summary>
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		/// <summary></summary>
		public override string Name
		{
			get { return "Byte[]"; }
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="val"></param>
		/// <returns></returns>
		public override string ToXML( object val )
		{
			byte[ ] bytes = ( byte[ ] ) val;
			StringBuilder buf = new StringBuilder();
			for( int i = 0; i < bytes.Length; i++ )
			{
				string hexStr = ( bytes[ i ] - byte.MinValue ).ToString( "x" ); //Why no ToBase64?
				if( hexStr.Length == 1 )
				{
					buf.Append( '0' );
				}
				buf.Append( hexStr );
			}
			return buf.ToString();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public override object DeepCopyNotNull( Object value )
		{
			byte[ ] bytes = ( byte[ ] ) value;
			byte[ ] result = new byte[bytes.Length];
			Array.Copy( bytes, 0, result, 0, bytes.Length );
			return result;
		}
	}
}<commit_msg>Altered logic in Get to work with MySQL's faulty GetBytes<commit_after>using System;
using System.Data;
using System.IO;
using System.Text;
using NHibernate.SqlTypes;
using NHibernate.Util;
using Environment = NHibernate.Cfg.Environment;

namespace NHibernate.Type
{
	/// <summary>
	/// BinaryType.
	/// </summary>
	public class BinaryType : MutableType
	{
		/// <summary></summary>
		internal BinaryType() : this( new BinarySqlType() )
		{
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sqlType"></param>
		internal BinaryType( BinarySqlType sqlType ) : base( sqlType )
		{
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="cmd"></param>
		/// <param name="value"></param>
		/// <param name="index"></param>
		public override void Set( IDbCommand cmd, object value, int index )
		{
			//TODO: research into byte streams
			//if ( Cfg.Environment.UseStreamsForBinary ) {
			// Is this really necessary?
			// How do we do????

			//TODO: st.setBinaryStream( index, new ByteArrayInputStream( (byte[]) value ), ( (byte[]) value ).length );
			//}
			//else {
			//Need to set DbType in parameter????
			( ( IDataParameter ) cmd.Parameters[ index ] ).Value = ( byte[ ] ) value;
			//}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="rs"></param>
		/// <param name="index"></param>
		/// <returns></returns>
		public override object Get( IDataReader rs, int index )
		{
			if( Environment.UseStreamsForBinary )
			{
				// Is this really necessary?
				// see http://msdn.microsoft.com/library/en-us/cpguide/html/cpconobtainingblobvaluesfromdatabase.asp?frame=true 
				// for a how to on reading binary/blob values from a db...
				MemoryStream outputStream = new MemoryStream( 2048 );
				byte[ ] buffer = new byte[2048];
				long fieldOffset = 0;

				try
				{
					while( true )
					{
						long amountRead = rs.GetBytes( index, fieldOffset, buffer, 0, buffer.Length );

						fieldOffset += amountRead;
						outputStream.Write( buffer, 0, ( int ) amountRead );

						if( amountRead < buffer.Length )
						{
							break;
						}
					}
					outputStream.Close();
				}
				catch( IOException ioe )
				{
					throw new HibernateException( "IOException occurred reading a binary value", ioe );
				}

				return outputStream.ToArray();

			}
			else
			{
				//TODO: not sure if this will work with all dbs
				return ( byte[ ] ) rs[ index ];
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="rs"></param>
		/// <param name="name"></param>
		/// <returns></returns>
		public override object Get( IDataReader rs, string name )
		{
			return Get( rs, rs.GetOrdinal( name ) );
		}

		/// <summary></summary>
		public override System.Type ReturnedClass
		{
			get { return typeof( byte[ ] ); }
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="x"></param>
		/// <param name="y"></param>
		/// <returns></returns>
		public override bool Equals( object x, object y )
		{
			if( x == y )
			{
				return true;
			}
			if( x == null || y == null )
			{
				return false;
			}

			return ArrayHelper.Equals( ( byte[ ] ) x, ( byte[ ] ) y );
		}

		/// <summary></summary>
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		/// <summary></summary>
		public override string Name
		{
			get { return "Byte[]"; }
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="val"></param>
		/// <returns></returns>
		public override string ToXML( object val )
		{
			byte[ ] bytes = ( byte[ ] ) val;
			StringBuilder buf = new StringBuilder();
			for( int i = 0; i < bytes.Length; i++ )
			{
				string hexStr = ( bytes[ i ] - byte.MinValue ).ToString( "x" ); //Why no ToBase64?
				if( hexStr.Length == 1 )
				{
					buf.Append( '0' );
				}
				buf.Append( hexStr );
			}
			return buf.ToString();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public override object DeepCopyNotNull( Object value )
		{
			byte[ ] bytes = ( byte[ ] ) value;
			byte[ ] result = new byte[bytes.Length];
			Array.Copy( bytes, 0, result, 0, bytes.Length );
			return result;
		}
	}
}
{'fraction_non_alphanumeric': 0.17387057387057386, 'fraction_numerical': 0.0050061050061050065, 'mean_word_length': 2.3054882970137207, 'pattern_counts': {'":': 0, '<': 122, '<?xml version=': 0, '>': 119, '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': '5429610', 'n_tokens_mistral': 3319, 'n_tokens_neox': 2648, 'n_words': 697}