OracleBFile.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / DataOracleClient / System / Data / OracleClient / OracleBFile.cs / 1 / OracleBFile.cs

                            //------------------------------------------------------------------------------ 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// [....] 
//-----------------------------------------------------------------------------
 
namespace System.Data.OracleClient 
{
    using System; 
    using System.Data.Common;
    using System.Data.SqlTypes;
    using System.Diagnostics;
    using System.Globalization; 
    using System.IO;
    using System.Runtime.CompilerServices; 
    using System.Runtime.InteropServices; 
    using System.Text;
 
    //---------------------------------------------------------------------
    // OracleBFile
    //
    //  This class is derived from the OracleLob type class, and implements 
    //  the additional methods necessary to support Oracle's BFILE internal
    //  data type.  Note that Oracle does not allow writing to a BFILE data 
    //  type. 
    //
    sealed public class OracleBFile : Stream, ICloneable, INullable, IDisposable { 

        private OracleLob   _lob;
        private string      _fileName;
        private string      _directoryAlias; 

 
        static public new readonly OracleBFile Null = new OracleBFile(); 

 
        // (internal) Construct a null lob
        internal OracleBFile() {
            _lob = OracleLob.Null;
        } 

 
        // (internal) Construct from a data reader buffer 
        internal OracleBFile(OciLobLocator lobLocator) {
            _lob = new OracleLob(lobLocator); 
        }

        // (internal) Construct from an existing BFile object (copy constructor)
        internal OracleBFile(OracleBFile bfile) { 
            this._lob               = (OracleLob)bfile._lob.Clone();
            this._fileName          = bfile._fileName; 
            this._directoryAlias    = bfile._directoryAlias; 
        }
 
        public override bool CanRead {
            get {
                if (IsNull) {
                    return true; 
                }
                return !IsDisposed; 
            } 
        }
 
        public override bool CanSeek {
            get {
                if (IsNull) {
                    return true; 
                }
                return !IsDisposed; 
            } 
        }
 
        public override bool CanWrite {
            get {
                return false;
            } 
        }
 
        public OracleConnection Connection { 
            get {
                AssertInternalLobIsValid(); 
                return _lob.Connection;
            }
        }
 
        internal OciHandle Descriptor {
            get { 
                return LobLocator.Descriptor; 
            }
        } 

        public string DirectoryName {
            //
 
            // DEVNOTE: OCI doesn't provide a simple API to get the name from the alias, but you could
            //          execute the SQL Query: 
            // 
            //              select directory_path from all_directories where directory_name = 'directoryAlias'
            // 
            //          And you would get the name.  It isn't clear if directory aliases are unique across
            //          all users, however.
            get {
                AssertInternalLobIsValid(); 

                if (IsNull) { 
                    return String.Empty; 
                }
                if (null == _directoryAlias) { 
                    GetNames();
                }
                return _directoryAlias;
            } 
        }
 
        public bool FileExists { 
            get {
                AssertInternalLobIsValid(); 

                if (IsNull) {
                    return false;
                } 
                _lob.AssertConnectionIsOpen();
 
                int flag; 
                int rc = TracedNativeMethods.OCILobFileExists(
                                            ServiceContextHandle, 
                                            ErrorHandle,
                                            Descriptor,
                                            out flag
                                            ); 
                if (0 != rc) {
                    Connection.CheckError(ErrorHandle, rc); 
                } 
                return (flag != 0);
            } 
        }

        public string FileName {
            get { 
                AssertInternalLobIsValid();
 
                if (IsNull) { 
                    return String.Empty;
                } 
                if (null == _fileName) {
                    GetNames();
                }
                return _fileName; 
            }
        } 
 
        internal OciErrorHandle ErrorHandle {
            get { 
                Debug.Assert(null != _lob, "_lob is null?");
                return _lob.ErrorHandle;
            }
        } 

        private bool IsDisposed { 
            get { 
                return (null == _lob);
            } 
        }

        public bool IsNull {
            get { 
                return (OracleLob.Null == _lob);
            } 
        } 

        public override long Length { 
            get {
                AssertInternalLobIsValid();

                if (IsNull) { 
                    return 0;
                } 
                long value = _lob.Length; 
                return value;
            } 
        }

        internal OciLobLocator LobLocator {
            get { 
                return _lob.LobLocator;
            } 
        } 

        public override long Position { 
            get {
                AssertInternalLobIsValid();

                if (IsNull) { 
                    return 0;
                } 
                return _lob.Position; 
            }
            set { 
                AssertInternalLobIsValid();

                if (!IsNull) {
                    _lob.Position = value; 
                }
            } 
        } 

        internal OciServiceContextHandle ServiceContextHandle { 
            get {
                Debug.Assert(null != _lob, "_lob is null?");
                return _lob.ServiceContextHandle;
            } 
        }
 
        public object Value { 
            get {
                AssertInternalLobIsValid(); 

                if (IsNull) {
                    return DBNull.Value;
                } 
                EnsureLobIsOpened();
                object value = _lob.Value; 
                return value; 
            }
        } 


        internal void AssertInternalLobIsValid() {
            if (IsDisposed) { 
                throw ADP.ObjectDisposed("OracleBFile");
            } 
        } 

        public object Clone() { 
            OracleBFile clone = new OracleBFile(this);
            return clone;
        }
 
        public long CopyTo (OracleLob destination) {
 
            // Copies the entire lob to a compatible lob, starting at the beginning of the target array. 
            return CopyTo (0, destination, 0, Length);
        } 

        public long CopyTo (OracleLob destination, long destinationOffset) {

            // Copies the entire lob to a compatible lob, starting at the specified offset of the target array. 
            return CopyTo (0, destination, destinationOffset, Length);
        } 
 

        public long CopyTo (long sourceOffset, OracleLob destination, long destinationOffset, long amount) { 

            // Copies a range of elements from the lob to a compatible lob, starting at the specified index of the target array.
            AssertInternalLobIsValid();
 
            if (null == destination) {
                throw ADP.ArgumentNull("destination"); 
            } 
            if (destination.IsNull) {
                throw ADP.LobWriteInvalidOnNull(); 
            }
            if (_lob.IsNull) {
                return 0;
            } 
            _lob.AssertConnectionIsOpen();
 
            _lob.AssertAmountIsValid(amount,            "amount"); 
            _lob.AssertAmountIsValid(sourceOffset,      "sourceOffset");
            _lob.AssertAmountIsValid(destinationOffset, "destinationOffset"); 

            _lob.AssertTransactionExists();

            int rc; 

            long dataCount = Math.Min(Length - sourceOffset, amount); 
            long dstOffset = destinationOffset + 1; // Oracle is 1 based, we are zero based. 
            long srcOffset = sourceOffset + 1;          // Oracle is 1 based, we are zero based.
 
            if (0 >= dataCount) {
                return 0;
            }
            rc = TracedNativeMethods.OCILobLoadFromFile( 
                                    ServiceContextHandle,
                                    ErrorHandle, 
                                    destination.Descriptor, 
                                    Descriptor,
                                    (UInt32)dataCount, 
                                    (UInt32)dstOffset,
                                    (UInt32)srcOffset
                                    );
            if (0 != rc) 
                Connection.CheckError(ErrorHandle, rc);
 
            // DEVNOTE: Oracle must not do partial copies, because their API doesn't tell you how many bytes were copied. 
            return dataCount;
        } 

        protected override void Dispose(bool disposing) {
            if (disposing) {
                OracleLob lob = _lob; 

                if (null != lob) { 
                    lob.Close(); 
                }
            } 
            _lob = null;
            _fileName = null;
            _directoryAlias = null;
            base.Dispose(disposing); 
        }
 
        private void EnsureLobIsOpened() { 
            LobLocator.Open(OracleLobOpenMode.ReadOnly);
        } 

        public override void Flush ()   {}

        private const short MaxDirectoryAliasChars = 30; 
        private const short MaxFileAliasChars = 255;
 
        internal void GetNames() { 
            _lob.AssertConnectionIsOpen();
 
            short           charSize = (short)((Connection.EnvironmentHandle.IsUnicode) ? 2 : 1);
            ushort          directoryAliasLength = checked((ushort)(MaxDirectoryAliasChars * charSize));
            int             fileOffset           = directoryAliasLength;
            ushort          fileAliasLength      = checked((ushort)(MaxFileAliasChars * charSize)); 
            NativeBuffer    buffer = Connection.GetScratchBuffer(directoryAliasLength + fileAliasLength);
            bool            mustRelease = false; 
 
            RuntimeHelpers.PrepareConstrainedRegions();
            try { 
                buffer.DangerousAddRef(ref mustRelease);

                int rc = TracedNativeMethods.OCILobFileGetName(
                                            Connection.EnvironmentHandle, 
                                            ErrorHandle,
                                            Descriptor, 
                                            buffer.DangerousGetDataPtr(), 
                                            ref directoryAliasLength,
                                            buffer.DangerousGetDataPtr(fileOffset), 
                                            ref fileAliasLength
                                            );
                if (0 != rc) {
                    Connection.CheckError(ErrorHandle, rc); 
                }
                _directoryAlias = Connection.GetString(buffer.ReadBytes(0, directoryAliasLength)); 
                _fileName       = Connection.GetString(buffer.ReadBytes(fileOffset, fileAliasLength)); 
            }
            finally { 
                if (mustRelease) {
                    buffer.DangerousRelease();
                }
            } 
        }
 
 
        public override int Read (byte[] buffer, int offset, int count) {
            AssertInternalLobIsValid(); 

            if (!IsNull) {
                EnsureLobIsOpened();
            } 
            int result = _lob.Read(buffer, offset, count);
            return result; 
        } 

        public override long Seek (long offset, SeekOrigin origin) { 
            AssertInternalLobIsValid();
            long result = _lob.Seek(offset, origin);
            return result;
        } 

        public void SetFileName (string directory, string file) { 
            AssertInternalLobIsValid(); 

            if (!IsNull) { 
                _lob.AssertConnectionIsOpen();
                _lob.AssertTransactionExists();

                OciFileDescriptor bfileDescriptor = (OciFileDescriptor)(LobLocator.Descriptor); 

                if (null != bfileDescriptor) { 
                    LobLocator.ForceClose();     // MDAC 86200: must be closed or the ForceOpen below will leak opens... 

                    int rc = TracedNativeMethods.OCILobFileSetName( 
                                                Connection.EnvironmentHandle,
                                                ErrorHandle,
                                                bfileDescriptor,
                                                directory, 
                                                file
                                                ); 
 
                    if (0 != rc) {
                        Connection.CheckError(ErrorHandle, rc); 
                    }
                    LobLocator.ForceOpen();  // SetFileName automatically closes the BFILE on the server, we reopen it

                    _fileName = null; 
                    _directoryAlias = null;
                    try { 
                        _lob.Position = 0; 
                    }
                    catch (Exception e) { 
                        if (!ADP.IsCatchableExceptionType(e)) {
                            throw;
                        }
 
                        // MDAC 86202 - If the file that they specified doesn't exist,
                        // we'll get an ORA-22888 error, when we try and set the 
                        // position. We just eat the error because they'll get it 
                        // soon enough, and they don't expect it when they set the file
                        // name. 
                    }
                }
            }
        } 

        public override void SetLength (long value) { 
            AssertInternalLobIsValid(); 
            throw ADP.ReadOnlyLob();
        } 

        public override void Write (byte[] buffer, int offset, int count) {
            AssertInternalLobIsValid();
            throw ADP.ReadOnlyLob(); 
        }
 
    } 
}

// 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.Common;
    using System.Data.SqlTypes;
    using System.Diagnostics;
    using System.Globalization; 
    using System.IO;
    using System.Runtime.CompilerServices; 
    using System.Runtime.InteropServices; 
    using System.Text;
 
    //---------------------------------------------------------------------
    // OracleBFile
    //
    //  This class is derived from the OracleLob type class, and implements 
    //  the additional methods necessary to support Oracle's BFILE internal
    //  data type.  Note that Oracle does not allow writing to a BFILE data 
    //  type. 
    //
    sealed public class OracleBFile : Stream, ICloneable, INullable, IDisposable { 

        private OracleLob   _lob;
        private string      _fileName;
        private string      _directoryAlias; 

 
        static public new readonly OracleBFile Null = new OracleBFile(); 

 
        // (internal) Construct a null lob
        internal OracleBFile() {
            _lob = OracleLob.Null;
        } 

 
        // (internal) Construct from a data reader buffer 
        internal OracleBFile(OciLobLocator lobLocator) {
            _lob = new OracleLob(lobLocator); 
        }

        // (internal) Construct from an existing BFile object (copy constructor)
        internal OracleBFile(OracleBFile bfile) { 
            this._lob               = (OracleLob)bfile._lob.Clone();
            this._fileName          = bfile._fileName; 
            this._directoryAlias    = bfile._directoryAlias; 
        }
 
        public override bool CanRead {
            get {
                if (IsNull) {
                    return true; 
                }
                return !IsDisposed; 
            } 
        }
 
        public override bool CanSeek {
            get {
                if (IsNull) {
                    return true; 
                }
                return !IsDisposed; 
            } 
        }
 
        public override bool CanWrite {
            get {
                return false;
            } 
        }
 
        public OracleConnection Connection { 
            get {
                AssertInternalLobIsValid(); 
                return _lob.Connection;
            }
        }
 
        internal OciHandle Descriptor {
            get { 
                return LobLocator.Descriptor; 
            }
        } 

        public string DirectoryName {
            //
 
            // DEVNOTE: OCI doesn't provide a simple API to get the name from the alias, but you could
            //          execute the SQL Query: 
            // 
            //              select directory_path from all_directories where directory_name = 'directoryAlias'
            // 
            //          And you would get the name.  It isn't clear if directory aliases are unique across
            //          all users, however.
            get {
                AssertInternalLobIsValid(); 

                if (IsNull) { 
                    return String.Empty; 
                }
                if (null == _directoryAlias) { 
                    GetNames();
                }
                return _directoryAlias;
            } 
        }
 
        public bool FileExists { 
            get {
                AssertInternalLobIsValid(); 

                if (IsNull) {
                    return false;
                } 
                _lob.AssertConnectionIsOpen();
 
                int flag; 
                int rc = TracedNativeMethods.OCILobFileExists(
                                            ServiceContextHandle, 
                                            ErrorHandle,
                                            Descriptor,
                                            out flag
                                            ); 
                if (0 != rc) {
                    Connection.CheckError(ErrorHandle, rc); 
                } 
                return (flag != 0);
            } 
        }

        public string FileName {
            get { 
                AssertInternalLobIsValid();
 
                if (IsNull) { 
                    return String.Empty;
                } 
                if (null == _fileName) {
                    GetNames();
                }
                return _fileName; 
            }
        } 
 
        internal OciErrorHandle ErrorHandle {
            get { 
                Debug.Assert(null != _lob, "_lob is null?");
                return _lob.ErrorHandle;
            }
        } 

        private bool IsDisposed { 
            get { 
                return (null == _lob);
            } 
        }

        public bool IsNull {
            get { 
                return (OracleLob.Null == _lob);
            } 
        } 

        public override long Length { 
            get {
                AssertInternalLobIsValid();

                if (IsNull) { 
                    return 0;
                } 
                long value = _lob.Length; 
                return value;
            } 
        }

        internal OciLobLocator LobLocator {
            get { 
                return _lob.LobLocator;
            } 
        } 

        public override long Position { 
            get {
                AssertInternalLobIsValid();

                if (IsNull) { 
                    return 0;
                } 
                return _lob.Position; 
            }
            set { 
                AssertInternalLobIsValid();

                if (!IsNull) {
                    _lob.Position = value; 
                }
            } 
        } 

        internal OciServiceContextHandle ServiceContextHandle { 
            get {
                Debug.Assert(null != _lob, "_lob is null?");
                return _lob.ServiceContextHandle;
            } 
        }
 
        public object Value { 
            get {
                AssertInternalLobIsValid(); 

                if (IsNull) {
                    return DBNull.Value;
                } 
                EnsureLobIsOpened();
                object value = _lob.Value; 
                return value; 
            }
        } 


        internal void AssertInternalLobIsValid() {
            if (IsDisposed) { 
                throw ADP.ObjectDisposed("OracleBFile");
            } 
        } 

        public object Clone() { 
            OracleBFile clone = new OracleBFile(this);
            return clone;
        }
 
        public long CopyTo (OracleLob destination) {
 
            // Copies the entire lob to a compatible lob, starting at the beginning of the target array. 
            return CopyTo (0, destination, 0, Length);
        } 

        public long CopyTo (OracleLob destination, long destinationOffset) {

            // Copies the entire lob to a compatible lob, starting at the specified offset of the target array. 
            return CopyTo (0, destination, destinationOffset, Length);
        } 
 

        public long CopyTo (long sourceOffset, OracleLob destination, long destinationOffset, long amount) { 

            // Copies a range of elements from the lob to a compatible lob, starting at the specified index of the target array.
            AssertInternalLobIsValid();
 
            if (null == destination) {
                throw ADP.ArgumentNull("destination"); 
            } 
            if (destination.IsNull) {
                throw ADP.LobWriteInvalidOnNull(); 
            }
            if (_lob.IsNull) {
                return 0;
            } 
            _lob.AssertConnectionIsOpen();
 
            _lob.AssertAmountIsValid(amount,            "amount"); 
            _lob.AssertAmountIsValid(sourceOffset,      "sourceOffset");
            _lob.AssertAmountIsValid(destinationOffset, "destinationOffset"); 

            _lob.AssertTransactionExists();

            int rc; 

            long dataCount = Math.Min(Length - sourceOffset, amount); 
            long dstOffset = destinationOffset + 1; // Oracle is 1 based, we are zero based. 
            long srcOffset = sourceOffset + 1;          // Oracle is 1 based, we are zero based.
 
            if (0 >= dataCount) {
                return 0;
            }
            rc = TracedNativeMethods.OCILobLoadFromFile( 
                                    ServiceContextHandle,
                                    ErrorHandle, 
                                    destination.Descriptor, 
                                    Descriptor,
                                    (UInt32)dataCount, 
                                    (UInt32)dstOffset,
                                    (UInt32)srcOffset
                                    );
            if (0 != rc) 
                Connection.CheckError(ErrorHandle, rc);
 
            // DEVNOTE: Oracle must not do partial copies, because their API doesn't tell you how many bytes were copied. 
            return dataCount;
        } 

        protected override void Dispose(bool disposing) {
            if (disposing) {
                OracleLob lob = _lob; 

                if (null != lob) { 
                    lob.Close(); 
                }
            } 
            _lob = null;
            _fileName = null;
            _directoryAlias = null;
            base.Dispose(disposing); 
        }
 
        private void EnsureLobIsOpened() { 
            LobLocator.Open(OracleLobOpenMode.ReadOnly);
        } 

        public override void Flush ()   {}

        private const short MaxDirectoryAliasChars = 30; 
        private const short MaxFileAliasChars = 255;
 
        internal void GetNames() { 
            _lob.AssertConnectionIsOpen();
 
            short           charSize = (short)((Connection.EnvironmentHandle.IsUnicode) ? 2 : 1);
            ushort          directoryAliasLength = checked((ushort)(MaxDirectoryAliasChars * charSize));
            int             fileOffset           = directoryAliasLength;
            ushort          fileAliasLength      = checked((ushort)(MaxFileAliasChars * charSize)); 
            NativeBuffer    buffer = Connection.GetScratchBuffer(directoryAliasLength + fileAliasLength);
            bool            mustRelease = false; 
 
            RuntimeHelpers.PrepareConstrainedRegions();
            try { 
                buffer.DangerousAddRef(ref mustRelease);

                int rc = TracedNativeMethods.OCILobFileGetName(
                                            Connection.EnvironmentHandle, 
                                            ErrorHandle,
                                            Descriptor, 
                                            buffer.DangerousGetDataPtr(), 
                                            ref directoryAliasLength,
                                            buffer.DangerousGetDataPtr(fileOffset), 
                                            ref fileAliasLength
                                            );
                if (0 != rc) {
                    Connection.CheckError(ErrorHandle, rc); 
                }
                _directoryAlias = Connection.GetString(buffer.ReadBytes(0, directoryAliasLength)); 
                _fileName       = Connection.GetString(buffer.ReadBytes(fileOffset, fileAliasLength)); 
            }
            finally { 
                if (mustRelease) {
                    buffer.DangerousRelease();
                }
            } 
        }
 
 
        public override int Read (byte[] buffer, int offset, int count) {
            AssertInternalLobIsValid(); 

            if (!IsNull) {
                EnsureLobIsOpened();
            } 
            int result = _lob.Read(buffer, offset, count);
            return result; 
        } 

        public override long Seek (long offset, SeekOrigin origin) { 
            AssertInternalLobIsValid();
            long result = _lob.Seek(offset, origin);
            return result;
        } 

        public void SetFileName (string directory, string file) { 
            AssertInternalLobIsValid(); 

            if (!IsNull) { 
                _lob.AssertConnectionIsOpen();
                _lob.AssertTransactionExists();

                OciFileDescriptor bfileDescriptor = (OciFileDescriptor)(LobLocator.Descriptor); 

                if (null != bfileDescriptor) { 
                    LobLocator.ForceClose();     // MDAC 86200: must be closed or the ForceOpen below will leak opens... 

                    int rc = TracedNativeMethods.OCILobFileSetName( 
                                                Connection.EnvironmentHandle,
                                                ErrorHandle,
                                                bfileDescriptor,
                                                directory, 
                                                file
                                                ); 
 
                    if (0 != rc) {
                        Connection.CheckError(ErrorHandle, rc); 
                    }
                    LobLocator.ForceOpen();  // SetFileName automatically closes the BFILE on the server, we reopen it

                    _fileName = null; 
                    _directoryAlias = null;
                    try { 
                        _lob.Position = 0; 
                    }
                    catch (Exception e) { 
                        if (!ADP.IsCatchableExceptionType(e)) {
                            throw;
                        }
 
                        // MDAC 86202 - If the file that they specified doesn't exist,
                        // we'll get an ORA-22888 error, when we try and set the 
                        // position. We just eat the error because they'll get it 
                        // soon enough, and they don't expect it when they set the file
                        // name. 
                    }
                }
            }
        } 

        public override void SetLength (long value) { 
            AssertInternalLobIsValid(); 
            throw ADP.ReadOnlyLob();
        } 

        public override void Write (byte[] buffer, int offset, int count) {
            AssertInternalLobIsValid();
            throw ADP.ReadOnlyLob(); 
        }
 
    } 
}

// 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