/// <summary> /// Auxiliary function of IPersistFile.Load. /// </summary> /// <returns> /// <value>A MemoryStream of the package file or a FileStream if the file is too big.</value> /// </returns> /// <remarks> /// <para>Use this method to load a package file completely to a memory buffer. /// After loading the file we can close the file, thus we can release the file /// lock quickly. However, there is a size limit on the file. If the /// file is too big (greater than _maxMemoryStreamBuffer), we cannot allow /// this method to consume too much memory. So we simply return the fileStream.</para> /// <para>Mode, access and sharing have already been checked or adjusted and can be assumed /// to be compatible with the goal of reading from the file.</para> /// </remarks> private static Stream FileToStream( string filePath, FileMode fileMode, FileAccess fileAccess, FileShare fileSharing, long maxMemoryStream) { FileInfo fi = new FileInfo(filePath); long byteCount = fi.Length; Stream s = new FileStream(filePath, fileMode, fileAccess, fileSharing); // There is a size limit of the file that we allow to be uploaded to a // memory stream. If the file size is bigger than the limit, simply return the fileStream. if (byteCount < maxMemoryStream) { // unchecked cast is safe because _maxMemoryStreamBuffer is less than Int32.Max MemoryStream ms = new MemoryStream(unchecked ((int)byteCount)); using (s) { PackagingUtilities.CopyStream(s, ms, byteCount, 0x1000); } s = ms; } return(s); }
private void SwitchModeIfNecessary() { if (_isolatedStorageMode) { Debug.Assert(_memoryStreamList.Count == 0); // it must be empty in isolated storage mode // if we are in isolated storage mode we need to check the Low Water Mark crossing if (_isolatedStorageStream.Length < _lowWaterMark) { if (_isolatedStorageStream.Length > 0) { //build memory stream MemoryStreamBlock newMemStreamBlock = new MemoryStreamBlock (_trackingMemoryStreamFactory.Create((int)_isolatedStorageStream.Length), 0); //copy data from iso storage to memory stream _isolatedStorageStream.Seek(0, SeekOrigin.Begin); newMemStreamBlock.Stream.Seek(0, SeekOrigin.Begin); PackagingUtilities.CopyStream(_isolatedStorageStream, newMemStreamBlock.Stream, Int64.MaxValue /*bytes to copy*/, 0x80000 /*512K buffer size */); Debug.Assert(newMemStreamBlock.Stream.Length > 0); _memoryStreamList.Add(newMemStreamBlock); } //switch mode _isolatedStorageMode = false; // release isolated storage disk space by setting its length to 0 // This way we don't have to re-open the isolated storage again if the memory consumption // goes above the High Water Mark _isolatedStorageStream.SetLength(0); _isolatedStorageStream.Flush(); } } else { // if we are in Memory Stream mode we need to check the High Water Mark crossing if (_trackingMemoryStreamFactory.CurrentMemoryConsumption > _highWaterMark) { //copy data to isolated storage EnsureIsolatedStoreStream(); CopyMemoryBlocksToStream(_isolatedStorageStream); //switch mode _isolatedStorageMode = true; //release memory stream resources foreach (MemoryStreamBlock memStreamBlock in _memoryStreamList) { // this will report the appropriate Memory usage back to the ITrackingMemoryStreamFactory memStreamBlock.Stream.Close(); } _memoryStreamList.Clear(); } } }
// Token: 0x06006CB1 RID: 27825 RVA: 0x001F44D8 File Offset: 0x001F26D8 private static Stream FileToStream(string filePath, FileMode fileMode, FileAccess fileAccess, FileShare fileSharing, long maxMemoryStream) { FileInfo fileInfo = new FileInfo(filePath); long length = fileInfo.Length; Stream stream = new FileStream(filePath, fileMode, fileAccess, fileSharing); if (length < maxMemoryStream) { MemoryStream memoryStream = new MemoryStream((int)length); using (stream) { PackagingUtilities.CopyStream(stream, memoryStream, length, 4096); } stream = memoryStream; } return(stream); }
//------------------------------------------------------ // // Internal Methods // //------------------------------------------------------ /// <summary> /// WriteToStream(Stream stream) writes the sparse Memory stream to the Stream provided as parameter /// starting at the current position in the stream /// </summary> internal void WriteToStream(Stream stream) { checked { if (_isolatedStorageMode) { _isolatedStorageStream.Seek(0, SeekOrigin.Begin); PackagingUtilities.CopyStream(_isolatedStorageStream, stream, Int64.MaxValue /*bytes to copy*/, 0x80000 /*512K buffer size */); } else { CopyMemoryBlocksToStream(stream); } } }