Ejemplo n.º 1
0
        void IPersistStream.Load(MS.Internal.Interop.IStream stream)
        {
            // Check argument.
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            // Only one of _package and _encryptedPackage can be non-null at a time.
            Invariant.Assert(_package == null || _encryptedPackage == null);

            // If there has been a previous call to Load, reinitialize everything.
            // Note closing a closed stream does not cause any exception.
            ReleaseResources();

            _filter      = null;
            _xpsFileName = null;

            try
            {
                _packageStream = new UnsafeIndexingFilterStream(stream);

                // different filter for encrypted package
                if (EncryptedPackageEnvelope.IsEncryptedPackageEnvelope(_packageStream))
                {
                    // Open the encrypted package.
                    _encryptedPackage = EncryptedPackageEnvelope.Open(_packageStream);
                    _filter           = new EncryptedPackageFilter(_encryptedPackage);
                }
                else
                {
                    // Open the package.
                    _package = Package.Open(_packageStream);
                    _filter  = new PackageFilter(_package);
                }
            }
            catch (IOException ex)
            {
                throw new COMException(ex.Message, (int)FilterErrorCode.FILTER_E_ACCESS);
            }
            catch (Exception ex)
            {
                throw new COMException(ex.Message, (int)FilterErrorCode.FILTER_E_UNKNOWNFORMAT);
            }
            finally
            {
                // clean-up if we failed
                if (_filter == null)
                {
                    ReleaseResources();
                }
            }
        }
Ejemplo n.º 2
0
        void IPersistStream.Load(MS.Internal.Interop.IStream stream)
        {
            // Check argument. 
            if (stream == null)
            { 
                throw new ArgumentNullException("stream"); 
            }
 
            // Only one of _package and _encryptedPackage can be non-null at a time.
            Invariant.Assert(_package == null || _encryptedPackage == null);

            // If there has been a previous call to Load, reinitialize everything. 
            // Note closing a closed stream does not cause any exception.
            ReleaseResources(); 
 
            _filter = null;
            _xpsFileName = null; 

            try
            {
                _packageStream = new UnsafeIndexingFilterStream(stream); 

                // different filter for encrypted package 
                if (EncryptedPackageEnvelope.IsEncryptedPackageEnvelope(_packageStream)) 
                {
                    // Open the encrypted package. 
                    _encryptedPackage = EncryptedPackageEnvelope.Open(_packageStream);
                    _filter = new EncryptedPackageFilter(_encryptedPackage);
                }
                else 
                {
                    // Open the package. 
                    _package = Package.Open(_packageStream); 
                    _filter = new PackageFilter(_package);
                } 
            }
            catch (IOException ex)
            {
                throw new COMException(ex.Message, (int)FilterErrorCode.FILTER_E_ACCESS); 
            }
            catch (Exception ex) 
            { 
                throw new COMException(ex.Message, (int)FilterErrorCode.FILTER_E_UNKNOWNFORMAT);
            } 
            finally
            {
                // clean-up if we failed
                if (_filter == null) 
                {
                    ReleaseResources(); 
                } 
            }
        } 
Ejemplo n.º 3
0
        /// <summary> 
        /// Opens the specified file with the specified mode.. 
        /// This can return any of the STG_E_* error codes, along
        /// with S_OK, E_OUTOFMEMORY, and E_FAIL. 
        /// </summary>
        /// <param name="pszFileName">
        /// A zero-terminated string containing the absolute path of the file to open.
        /// </param> 
        /// <param name="dwMode">The mode in which to open pszFileName. </param>
        void IPersistFile.Load(string pszFileName, int dwMode) 
        { 
            FileMode fileMode;
            FileAccess fileAccess; 
            FileShare fileSharing;

            // Check argument.
            if (pszFileName == null || pszFileName == String.Empty) 
            {
                throw new ArgumentException(SR.Get(SRID.FileNameNullOrEmpty), "pszFileName"); 
            } 

            // Convert mode information in flag. 
            switch ((STGM_FLAGS)(dwMode & (int)STGM_FLAGS.MODE))
            {
                case STGM_FLAGS.CREATE:
                    throw new ArgumentException(SR.Get(SRID.FilterLoadInvalidModeFlag), "dwMode"); 

                default: 
                    fileMode = FileMode.Open; 
                    break;
            } 

            // Convert access flag.
            switch ((STGM_FLAGS)(dwMode & (int)STGM_FLAGS.ACCESS))
            { 
                case STGM_FLAGS.READ:
                case STGM_FLAGS.READWRITE: 
                    fileAccess = FileAccess.Read; 
                    break;
 
                default:
                    throw new ArgumentException(SR.Get(SRID.FilterLoadInvalidModeFlag), "dwMode");
            }
 
            // Sharing flags are ignored. Since managed filters do not have the equivalent
            // of a destructor to release locks on files as soon as they get disposed of from 
            // unmanaged code, the option taken is not to lock at all while filtering. 
            // (See call to FileToStream further down.)
            fileSharing = FileShare.ReadWrite; 

            // Only one of _package and _encryptedPackage can be non-null at a time.
            Invariant.Assert(_package == null || _encryptedPackage == null);
 
            // If there has been a previous call to Load, reinitialize everything.
            // Note closing a closed stream does not cause any exception. 
            ReleaseResources(); 

            _filter = null; 
            _xpsFileName = null;

            bool encrypted = EncryptedPackageEnvelope.IsEncryptedPackageEnvelope(pszFileName);
 
            try
            { 
                // opens to MemoryStream or just returns FileStream if file exceeds _maxMemoryStreamBuffer 
                _packageStream = FileToStream(pszFileName, fileMode, fileAccess, fileSharing, _maxMemoryStreamBuffer);
 
                if (encrypted)
                {
                    // Open the encrypted package.
                    _encryptedPackage = EncryptedPackageEnvelope.Open(_packageStream); 
                    _filter = new EncryptedPackageFilter(_encryptedPackage);
                } 
                else 
                {
                    // Open the package. 
                    _package = Package.Open(_packageStream);
                    _filter = new PackageFilter(_package);
                }
            } 
            catch (IOException ex)
            { 
                throw new COMException(ex.Message, (int)FilterErrorCode.FILTER_E_ACCESS); 
            }
            catch (FileFormatException ex) 
            {
                throw new COMException(ex.Message, (int)FilterErrorCode.FILTER_E_UNKNOWNFORMAT);
            }
            finally 
            {
                // failure? 
                if (_filter == null) 
                {
                    // clean up 
                    ReleaseResources();
                }

            } 

            _xpsFileName = pszFileName; 
        } 
Ejemplo n.º 4
0
        /// <summary>
        /// Opens the specified file with the specified mode..
        /// This can return any of the STG_E_* error codes, along
        /// with S_OK, E_OUTOFMEMORY, and E_FAIL.
        /// </summary>
        /// <param name="pszFileName">
        /// A zero-terminated string containing the absolute path of the file to open.
        /// </param>
        /// <param name="dwMode">The mode in which to open pszFileName. </param>
        void IPersistFile.Load(string pszFileName, int dwMode)
        {
            FileMode   fileMode;
            FileAccess fileAccess;
            FileShare  fileSharing;

            // Check argument.
            if (pszFileName == null || pszFileName == String.Empty)
            {
                throw new ArgumentException(SR.Get(SRID.FileNameNullOrEmpty), "pszFileName");
            }

            // Convert mode information in flag.
            switch ((STGM_FLAGS)(dwMode & (int)STGM_FLAGS.MODE))
            {
            case STGM_FLAGS.CREATE:
                throw new ArgumentException(SR.Get(SRID.FilterLoadInvalidModeFlag), "dwMode");

            default:
                fileMode = FileMode.Open;
                break;
            }

            // Convert access flag.
            switch ((STGM_FLAGS)(dwMode & (int)STGM_FLAGS.ACCESS))
            {
            case STGM_FLAGS.READ:
            case STGM_FLAGS.READWRITE:
                fileAccess = FileAccess.Read;
                break;

            default:
                throw new ArgumentException(SR.Get(SRID.FilterLoadInvalidModeFlag), "dwMode");
            }

            // Sharing flags are ignored. Since managed filters do not have the equivalent
            // of a destructor to release locks on files as soon as they get disposed of from
            // unmanaged code, the option taken is not to lock at all while filtering.
            // (See call to FileToStream further down.)
            fileSharing = FileShare.ReadWrite;

            // Only one of _package and _encryptedPackage can be non-null at a time.
            Invariant.Assert(_package == null || _encryptedPackage == null);

            // If there has been a previous call to Load, reinitialize everything.
            // Note closing a closed stream does not cause any exception.
            ReleaseResources();

            _filter      = null;
            _xpsFileName = null;

            bool encrypted = EncryptedPackageEnvelope.IsEncryptedPackageEnvelope(pszFileName);

            try
            {
                // opens to MemoryStream or just returns FileStream if file exceeds _maxMemoryStreamBuffer
                _packageStream = FileToStream(pszFileName, fileMode, fileAccess, fileSharing, _maxMemoryStreamBuffer);

                if (encrypted)
                {
                    // Open the encrypted package.
                    _encryptedPackage = EncryptedPackageEnvelope.Open(_packageStream);
                    _filter           = new EncryptedPackageFilter(_encryptedPackage);
                }
                else
                {
                    // Open the package.
                    _package = Package.Open(_packageStream);
                    _filter  = new PackageFilter(_package);
                }
            }
            catch (IOException ex)
            {
                throw new COMException(ex.Message, (int)FilterErrorCode.FILTER_E_ACCESS);
            }
            catch (FileFormatException ex)
            {
                throw new COMException(ex.Message, (int)FilterErrorCode.FILTER_E_UNKNOWNFORMAT);
            }
            finally
            {
                // failure?
                if (_filter == null)
                {
                    // clean up
                    ReleaseResources();
                }
            }

            _xpsFileName = pszFileName;
        }