OracleBinary.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / ndp / fx / src / DataOracleClient / System / Data / OracleClient / OracleBinary.cs / 1 / OracleBinary.cs

                            //------------------------------------------------------------------------------ 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// [....] 
//-----------------------------------------------------------------------------
 
namespace System.Data.OracleClient 
{
    using System; 
    using System.Data.SqlTypes;
    using System.Data.Common;
    using System.Diagnostics;
    using System.Globalization; 
    using System.Runtime.InteropServices;
    using System.Text; 
 
    //---------------------------------------------------------------------
    // OracleBinary 
    //
    //  This class implements support for Oracle's RAW and LONGRAW internal
    //  data types.  It should simply be a wrapper class around a CLS Byte
    //  Array data type, with the appropriate internal constructors to allow 
    //  data values to be set from the data reader.
    // 
    [StructLayout(LayoutKind.Sequential)] 
    public struct OracleBinary : IComparable, INullable {
 

        private byte[] _value;

        public static readonly OracleBinary Null = new OracleBinary(true); 

 
        // (internal) Construct from nothing -- the value will be null 
        private OracleBinary(bool isNull) {
            _value = (isNull) ? null : new byte[0]; 
        }

        public OracleBinary (byte[] b) {
            _value = (null == b) ? b : (byte[])(b.Clone()); 
        }
 
        // (internal) construct from a row/parameter binding 
        internal OracleBinary( NativeBuffer buffer, int valueOffset, int lengthOffset, MetaType metaType) {
            int  valueLength = GetLength(buffer, lengthOffset, metaType); 

            _value = new byte[valueLength];

            GetBytes(buffer, 
                    valueOffset,
                    metaType, 
                    0, 
                    _value,
                    0, 
                    valueLength);
        }

 
        public bool IsNull {
            get { 
                return (null == _value); 
            }
        } 

        public int Length {
            get {
                if (IsNull) { 
                    throw ADP.DataIsNull();
                } 
                return _value.Length; 
            }
        } 

        public byte[] Value {
            get {
                if (IsNull) { 
                    throw ADP.DataIsNull();
                } 
                return (byte[])(_value.Clone()); 
            }
        } 

        public byte this[int index] {
            get {
                if (IsNull) { 
                    throw ADP.DataIsNull();
                } 
                return _value[index]; 
            }
        } 


        public int CompareTo( object obj ) {
            if (obj.GetType() == typeof(OracleBinary)) { 
                OracleBinary b = (OracleBinary)obj;
 
                // If both values are Null, consider them equal. 
                // Otherwise, Null is less than anything.
                if (IsNull) { 
                    return b.IsNull ? 0  : -1;
                }
                if (b.IsNull) {
                    return 1; 
                }
                // Neither value is null, do the comparison. 
                int result = PerformCompareByte(_value, b._value); 
                return result;
            } 

            throw ADP.WrongType(obj.GetType(), typeof(OracleBinary));
        }
 
        public override bool Equals(object value) {
            if (value is OracleBinary) { 
                return (this == (OracleBinary)value).Value; 
            }
            else { 
                return false;
            }
        }
 
        static internal int GetBytes(
                    NativeBuffer        buffer, 
                    int                 valueOffset, 
                    MetaType            metaType,
                    int                 sourceOffset, 
                    byte[]              destinationBuffer,
                    int                 destinationOffset,
                    int                 byteCount ) {
 
            // This static method allows the GetBytes type getter to do it's job
            // without having to marshal the entire value into managed space. 
            if (!metaType.IsLong) { 
                buffer.ReadBytes(valueOffset+sourceOffset, destinationBuffer, destinationOffset, byteCount);
            } 
            else {
                // Long values are bound out-of-line, which means we have
                // to do this the hard way...
                NativeBuffer_LongColumnData.CopyOutOfLineBytes(buffer.ReadIntPtr(valueOffset), sourceOffset, destinationBuffer, destinationOffset, byteCount); 
            }
            return byteCount; 
        } 

        static internal int GetLength( 
                    NativeBuffer        buffer,
                    int                 lengthOffset,
                    MetaType            metaType ) {
 
            // Oracle only will write two bytes of length, but LONG data types
            // can exceed that amount; our piecewise callbacks will write a 
            // full DWORD of length, so we need to get the full length for them, 
            // but if we do that for all the other types, we'll be reading
            // un-initialized memory and bad things happen. 

            if (metaType.IsLong) {
                return buffer.ReadInt32(lengthOffset);
            } 
            else {
                return (int)buffer.ReadInt16(lengthOffset); 
            } 
        }
 

        public override int GetHashCode() {
            return IsNull ? 0 : _value.GetHashCode();
        } 

        private static int PerformCompareByte(byte[] x, byte[] y) { 
            int len1 = x.Length; 
            int len2 = y.Length;
 
            bool fXShorter = (len1 < len2);
            // the smaller length of two arrays
            int len = (fXShorter) ? len1 : len2;
            int i; 

            for (i = 0; i < len; i ++) { 
                if (x[i] != y[i]) { 
                    if (x[i] < y[i])
                        return -1; 
                    else
                        return +1;
                }
            } 

            if (len1 == len2) 
                return 0; 
            else {
                // if the remaining bytes are all zeroes, they are still equal. 

                byte bZero = (byte)0;

                if (fXShorter) { 
                    // array X is shorter
                    for (i = len; i < len2; i ++) { 
                        if (y[i] != bZero) 
                            return -1;
                    } 
                }
                else {
                    // array Y is shorter
                    for (i = len; i < len1; i ++) { 
                        if (x[i] != bZero)
                            return +1; 
                    } 
                }
 
                return 0;
            }
        }
 
/* // We didn't expose this in Everett, and now it is a behavioral breaking change to do so...
        public static OracleBinary Parse(string s) { 
            if ((s.Length & 0x1) != 0) { 
                throw ADP.MustBeEvenLengthString("s");
            } 
            char[]  c = s.ToCharArray();
            byte[]  b = new byte[s.Length / 2];

            CultureInfo invariant = CultureInfo.InvariantCulture; 
            for (int i = 0; i < s.Length; i += 2) {
                int h = hexDigits.IndexOf(Char.ToLower(c[i], invariant)); 
                int l = hexDigits.IndexOf(Char.ToLower(c[i+1], invariant)); 

                if (h < 0 || l < 0) { 
                    throw ADP.MustBeStringOfHexDigits("s");
                }
                b[i/2] = (byte)((h << 4) | l);
            } 

            OracleBinary    result = new OracleBinary(b); 
 
            return new OracleBinary(b);
        } 

        // We didn't expose this in Everett, and now it is a behavioral breaking change to do so...
        public override string ToString() {
            if (IsNull) { 
                return ADP.NullString;
            } 
            StringBuilder s = new StringBuilder(); 

            foreach (byte b in _value) { 
                s.Append(String.Format((IFormatProvider)null, "{0,-2:x2}", b));
            }
            string result = s.ToString();
            return result; 
        }
*/ 
 
        // Alternative method for operator +
        public static OracleBinary Concat(OracleBinary x, OracleBinary y) { 
            return (x + y);
        }

        // Alternative method for operator == 
        public static OracleBoolean Equals(OracleBinary x, OracleBinary y) {
            return (x == y); 
        } 

        // Alternative method for operator > 
        public static OracleBoolean GreaterThan(OracleBinary x, OracleBinary y) {
            return (x > y);
        }
 
        // Alternative method for operator >=
        public static OracleBoolean GreaterThanOrEqual(OracleBinary x, OracleBinary y) { 
            return (x >= y); 
        }
 
        // Alternative method for operator <
        public static OracleBoolean LessThan(OracleBinary x, OracleBinary y) {
            return (x < y);
        } 

        // Alternative method for operator <= 
        public static OracleBoolean LessThanOrEqual(OracleBinary x, OracleBinary y) { 
            return (x <= y);
        } 

        // Alternative method for operator !=
        public static OracleBoolean NotEquals(OracleBinary x, OracleBinary y) {
            return (x != y); 
        }
 
        public static implicit operator OracleBinary(byte[] b) { 
            return new OracleBinary(b);
        } 

        public static explicit operator Byte[](OracleBinary x) {
            return x.Value;
        } 

 
 
        public static OracleBinary operator+    (OracleBinary x, OracleBinary y) {
            if (x.IsNull || y.IsNull) { 
                return Null;
            }
            byte[]  newValue = new byte[x._value.Length + y._value.Length];
            x._value.CopyTo(newValue, 0); 
            y._value.CopyTo(newValue, x.Value.Length);
 
            OracleBinary    result = new OracleBinary(newValue); 
            return result;
        } 

        public static OracleBoolean operator==  (OracleBinary x, OracleBinary y) {
            return (x.IsNull || y.IsNull) ? OracleBoolean.Null : new OracleBoolean(x.CompareTo(y) == 0);
        } 

        public static OracleBoolean operator>   (OracleBinary x, OracleBinary y) { 
            return (x.IsNull || y.IsNull) ? OracleBoolean.Null : new OracleBoolean(x.CompareTo(y) > 0); 
        }
 
        public static OracleBoolean operator>=  (OracleBinary x, OracleBinary y) {
            return (x.IsNull || y.IsNull) ? OracleBoolean.Null : new OracleBoolean(x.CompareTo(y) >= 0);
        }
 
        public static OracleBoolean operator<   (OracleBinary x, OracleBinary y) {
            return (x.IsNull || y.IsNull) ? OracleBoolean.Null : new OracleBoolean(x.CompareTo(y) < 0); 
        } 

        public static OracleBoolean operator<=  (OracleBinary x, OracleBinary y) { 
            return (x.IsNull || y.IsNull) ? OracleBoolean.Null : new OracleBoolean(x.CompareTo(y) <= 0);
        }

        public static OracleBoolean operator!=  (OracleBinary x, OracleBinary y) { 
            return (x.IsNull || y.IsNull) ? OracleBoolean.Null : new OracleBoolean(x.CompareTo(y) != 0);
        } 
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------ 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// [....] 
//-----------------------------------------------------------------------------
 
namespace System.Data.OracleClient 
{
    using System; 
    using System.Data.SqlTypes;
    using System.Data.Common;
    using System.Diagnostics;
    using System.Globalization; 
    using System.Runtime.InteropServices;
    using System.Text; 
 
    //---------------------------------------------------------------------
    // OracleBinary 
    //
    //  This class implements support for Oracle's RAW and LONGRAW internal
    //  data types.  It should simply be a wrapper class around a CLS Byte
    //  Array data type, with the appropriate internal constructors to allow 
    //  data values to be set from the data reader.
    // 
    [StructLayout(LayoutKind.Sequential)] 
    public struct OracleBinary : IComparable, INullable {
 

        private byte[] _value;

        public static readonly OracleBinary Null = new OracleBinary(true); 

 
        // (internal) Construct from nothing -- the value will be null 
        private OracleBinary(bool isNull) {
            _value = (isNull) ? null : new byte[0]; 
        }

        public OracleBinary (byte[] b) {
            _value = (null == b) ? b : (byte[])(b.Clone()); 
        }
 
        // (internal) construct from a row/parameter binding 
        internal OracleBinary( NativeBuffer buffer, int valueOffset, int lengthOffset, MetaType metaType) {
            int  valueLength = GetLength(buffer, lengthOffset, metaType); 

            _value = new byte[valueLength];

            GetBytes(buffer, 
                    valueOffset,
                    metaType, 
                    0, 
                    _value,
                    0, 
                    valueLength);
        }

 
        public bool IsNull {
            get { 
                return (null == _value); 
            }
        } 

        public int Length {
            get {
                if (IsNull) { 
                    throw ADP.DataIsNull();
                } 
                return _value.Length; 
            }
        } 

        public byte[] Value {
            get {
                if (IsNull) { 
                    throw ADP.DataIsNull();
                } 
                return (byte[])(_value.Clone()); 
            }
        } 

        public byte this[int index] {
            get {
                if (IsNull) { 
                    throw ADP.DataIsNull();
                } 
                return _value[index]; 
            }
        } 


        public int CompareTo( object obj ) {
            if (obj.GetType() == typeof(OracleBinary)) { 
                OracleBinary b = (OracleBinary)obj;
 
                // If both values are Null, consider them equal. 
                // Otherwise, Null is less than anything.
                if (IsNull) { 
                    return b.IsNull ? 0  : -1;
                }
                if (b.IsNull) {
                    return 1; 
                }
                // Neither value is null, do the comparison. 
                int result = PerformCompareByte(_value, b._value); 
                return result;
            } 

            throw ADP.WrongType(obj.GetType(), typeof(OracleBinary));
        }
 
        public override bool Equals(object value) {
            if (value is OracleBinary) { 
                return (this == (OracleBinary)value).Value; 
            }
            else { 
                return false;
            }
        }
 
        static internal int GetBytes(
                    NativeBuffer        buffer, 
                    int                 valueOffset, 
                    MetaType            metaType,
                    int                 sourceOffset, 
                    byte[]              destinationBuffer,
                    int                 destinationOffset,
                    int                 byteCount ) {
 
            // This static method allows the GetBytes type getter to do it's job
            // without having to marshal the entire value into managed space. 
            if (!metaType.IsLong) { 
                buffer.ReadBytes(valueOffset+sourceOffset, destinationBuffer, destinationOffset, byteCount);
            } 
            else {
                // Long values are bound out-of-line, which means we have
                // to do this the hard way...
                NativeBuffer_LongColumnData.CopyOutOfLineBytes(buffer.ReadIntPtr(valueOffset), sourceOffset, destinationBuffer, destinationOffset, byteCount); 
            }
            return byteCount; 
        } 

        static internal int GetLength( 
                    NativeBuffer        buffer,
                    int                 lengthOffset,
                    MetaType            metaType ) {
 
            // Oracle only will write two bytes of length, but LONG data types
            // can exceed that amount; our piecewise callbacks will write a 
            // full DWORD of length, so we need to get the full length for them, 
            // but if we do that for all the other types, we'll be reading
            // un-initialized memory and bad things happen. 

            if (metaType.IsLong) {
                return buffer.ReadInt32(lengthOffset);
            } 
            else {
                return (int)buffer.ReadInt16(lengthOffset); 
            } 
        }
 

        public override int GetHashCode() {
            return IsNull ? 0 : _value.GetHashCode();
        } 

        private static int PerformCompareByte(byte[] x, byte[] y) { 
            int len1 = x.Length; 
            int len2 = y.Length;
 
            bool fXShorter = (len1 < len2);
            // the smaller length of two arrays
            int len = (fXShorter) ? len1 : len2;
            int i; 

            for (i = 0; i < len; i ++) { 
                if (x[i] != y[i]) { 
                    if (x[i] < y[i])
                        return -1; 
                    else
                        return +1;
                }
            } 

            if (len1 == len2) 
                return 0; 
            else {
                // if the remaining bytes are all zeroes, they are still equal. 

                byte bZero = (byte)0;

                if (fXShorter) { 
                    // array X is shorter
                    for (i = len; i < len2; i ++) { 
                        if (y[i] != bZero) 
                            return -1;
                    } 
                }
                else {
                    // array Y is shorter
                    for (i = len; i < len1; i ++) { 
                        if (x[i] != bZero)
                            return +1; 
                    } 
                }
 
                return 0;
            }
        }
 
/* // We didn't expose this in Everett, and now it is a behavioral breaking change to do so...
        public static OracleBinary Parse(string s) { 
            if ((s.Length & 0x1) != 0) { 
                throw ADP.MustBeEvenLengthString("s");
            } 
            char[]  c = s.ToCharArray();
            byte[]  b = new byte[s.Length / 2];

            CultureInfo invariant = CultureInfo.InvariantCulture; 
            for (int i = 0; i < s.Length; i += 2) {
                int h = hexDigits.IndexOf(Char.ToLower(c[i], invariant)); 
                int l = hexDigits.IndexOf(Char.ToLower(c[i+1], invariant)); 

                if (h < 0 || l < 0) { 
                    throw ADP.MustBeStringOfHexDigits("s");
                }
                b[i/2] = (byte)((h << 4) | l);
            } 

            OracleBinary    result = new OracleBinary(b); 
 
            return new OracleBinary(b);
        } 

        // We didn't expose this in Everett, and now it is a behavioral breaking change to do so...
        public override string ToString() {
            if (IsNull) { 
                return ADP.NullString;
            } 
            StringBuilder s = new StringBuilder(); 

            foreach (byte b in _value) { 
                s.Append(String.Format((IFormatProvider)null, "{0,-2:x2}", b));
            }
            string result = s.ToString();
            return result; 
        }
*/ 
 
        // Alternative method for operator +
        public static OracleBinary Concat(OracleBinary x, OracleBinary y) { 
            return (x + y);
        }

        // Alternative method for operator == 
        public static OracleBoolean Equals(OracleBinary x, OracleBinary y) {
            return (x == y); 
        } 

        // Alternative method for operator > 
        public static OracleBoolean GreaterThan(OracleBinary x, OracleBinary y) {
            return (x > y);
        }
 
        // Alternative method for operator >=
        public static OracleBoolean GreaterThanOrEqual(OracleBinary x, OracleBinary y) { 
            return (x >= y); 
        }
 
        // Alternative method for operator <
        public static OracleBoolean LessThan(OracleBinary x, OracleBinary y) {
            return (x < y);
        } 

        // Alternative method for operator <= 
        public static OracleBoolean LessThanOrEqual(OracleBinary x, OracleBinary y) { 
            return (x <= y);
        } 

        // Alternative method for operator !=
        public static OracleBoolean NotEquals(OracleBinary x, OracleBinary y) {
            return (x != y); 
        }
 
        public static implicit operator OracleBinary(byte[] b) { 
            return new OracleBinary(b);
        } 

        public static explicit operator Byte[](OracleBinary x) {
            return x.Value;
        } 

 
 
        public static OracleBinary operator+    (OracleBinary x, OracleBinary y) {
            if (x.IsNull || y.IsNull) { 
                return Null;
            }
            byte[]  newValue = new byte[x._value.Length + y._value.Length];
            x._value.CopyTo(newValue, 0); 
            y._value.CopyTo(newValue, x.Value.Length);
 
            OracleBinary    result = new OracleBinary(newValue); 
            return result;
        } 

        public static OracleBoolean operator==  (OracleBinary x, OracleBinary y) {
            return (x.IsNull || y.IsNull) ? OracleBoolean.Null : new OracleBoolean(x.CompareTo(y) == 0);
        } 

        public static OracleBoolean operator>   (OracleBinary x, OracleBinary y) { 
            return (x.IsNull || y.IsNull) ? OracleBoolean.Null : new OracleBoolean(x.CompareTo(y) > 0); 
        }
 
        public static OracleBoolean operator>=  (OracleBinary x, OracleBinary y) {
            return (x.IsNull || y.IsNull) ? OracleBoolean.Null : new OracleBoolean(x.CompareTo(y) >= 0);
        }
 
        public static OracleBoolean operator<   (OracleBinary x, OracleBinary y) {
            return (x.IsNull || y.IsNull) ? OracleBoolean.Null : new OracleBoolean(x.CompareTo(y) < 0); 
        } 

        public static OracleBoolean operator<=  (OracleBinary x, OracleBinary y) { 
            return (x.IsNull || y.IsNull) ? OracleBoolean.Null : new OracleBoolean(x.CompareTo(y) <= 0);
        }

        public static OracleBoolean operator!=  (OracleBinary x, OracleBinary y) { 
            return (x.IsNull || y.IsNull) ? OracleBoolean.Null : new OracleBoolean(x.CompareTo(y) != 0);
        } 
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK