Ejemplo n.º 1
0
        public StreamItem GetStream(string strFilePath,
                                    FileMode mode,
                                    FileAccess access,
                                    bool bAddToCollection = true)
        {
            if (bAddToCollection == false)
            {
                StreamItem item = NewItem(strFilePath, mode, access, false);
                Debug.Assert(item.FileStream != null, "");
                return(item);
            }

            {
                StreamItem item = this.FindItem(strFilePath, access);
                if (item != null)
                {
                    item.Touch();
                    Debug.Assert(item.FileStream != null, "");
                    return(item);
                }

                item = NewItem(strFilePath, mode, access);
                Debug.Assert(item.FileStream != null, "");
                return(item);
            }
        }
Ejemplo n.º 2
0
        public void ReturnStream(StreamItem item)
        {
            if (item.Fly)
            {
                /*
                 * if (item.FileStream != null)
                 * {
                 *  if ((item.FileAccess & FileAccess.Write) != 0)
                 *      item.FileStream.Flush();    // 2019/9/2
                 *  item.FileStream.Close();
                 *  item.FileStream = null;
                 * }
                 */
                item.Close();
                item.DecUse();
                item.Dispose();
                return;
            }

            if (item.FileStream != null)
            {
                if ((item.FileAccess & FileAccess.Write) != 0)
                {
                    item.FileStream.Flush();
                }
            }

            item.DecUse();
        }
Ejemplo n.º 3
0
        public StreamItem GetWriteStream(string strFilePath)
        {
            FileAccess access = FileAccess.Write;
            StreamItem item   = this.FindItem(strFilePath, access);

            if (item != null)
            {
                item.Touch();
                return(item);
            }

            return(NewItem(strFilePath, FileMode.OpenOrCreate, access));
        }
Ejemplo n.º 4
0
        public void ReturnStream(StreamItem item)
        {
            if (item.Fly)
            {
                if (item.FileStream != null)
                {
                    item.FileStream.Close();
                    item.FileStream = null;
                }
                item.DecUse();
                return;
            }

            if (item.FileStream != null)
            {
                item.FileStream.Flush();
            }

            item.DecUse();
        }
Ejemplo n.º 5
0
        public StreamItem NewItem(string strFilePath,
                                  FileMode mode,
                                  FileAccess access,
                                  bool bAddToCollection = true)
        {
            // 防备尺寸过大
            if (bAddToCollection && _items.Count > MAX_ITEMS)
            {
                ClearAll();
            }

            StreamItem item = new StreamItem
            {
                Fly        = !bAddToCollection,
                FileAccess = access
            };

            item.Touch();
            item.FilePath   = strFilePath;
            item.FileStream = File.Open(
                strFilePath,
                mode,   // FileMode.OpenOrCreate,
                access, // FileAccess.Write,
                FileShare.ReadWrite);
            item.IncUse();

            if (bAddToCollection)
            {
                m_lock.EnterWriteLock();
                try
                {
                    _items.Add(item);
                }
                finally
                {
                    m_lock.ExitWriteLock();
                }
            }
            Debug.Assert(item.FileStream != null, "");
            return(item);
        }
Ejemplo n.º 6
0
        public StreamItem NewItem(string strFilePath,
                                  FileMode mode,
                                  FileAccess access,
                                  bool bAddToCollection = true)
        {
            // 防备尺寸过大
            if (bAddToCollection && _items.Count > MAX_ITEMS)
            {
                ClearAll(true);
            }

            StreamItem item = new StreamItem
            {
                Fly        = !bAddToCollection,
                FileAccess = access
            };

            item.Touch();
            item.FilePath = strFilePath;

            int nRedoCount = 0;

REDO:
            try
            {
                item.FileStream = File.Open(
                    strFilePath,
                    mode,   // FileMode.OpenOrCreate,
                    access, // FileAccess.Write,
                    FileShare.ReadWrite);
            }
            catch (DirectoryNotFoundException ex)
            {
                if ((item.FileAccess & FileAccess.Write) != 0 &&
                    nRedoCount == 0)
                {
                    // 创建中间子目录
                    PathUtil.TryCreateDir(PathUtil.PathPart(strFilePath));
                    nRedoCount++;
                    goto REDO;
                }
                throw new Exception(ex.Message, ex);
            }

            item.IncUse();

            if (bAddToCollection)
            {
                m_lock.EnterWriteLock();
                try
                {
                    _items.Add(item);
                }
                finally
                {
                    m_lock.ExitWriteLock();
                }
            }
            Debug.Assert(item.FileStream != null, "");
            return(item);
        }