Example #1
0
		/// <summary>
		/// Create a memory map instance that views can be opened from, for reading.
		/// </summary>
		/// <param name="fileName">name of the backing file</param>
		/// <exception cref="IOException">Memory mapping failed at the Win32 layer</exception>
		/// <returns>A Stream for a view on the file</returns>
		public static Stream MemoryMappedStream(string fileName)
		{
			using (MemoryMappedFile mmf = new MemoryMappedFile(fileName))
			{
				return mmf.MapView();
			}
		}
Example #2
0
		/// <summary>
		/// Create a memory map instance that views can be opened from.
		/// </summary>
		/// <param name="fileName">name of the backing file (or null for a pagefile-backed map)</param>
		/// <param name="write">if true, write access is allowed; if false, it is read-only</param>
		/// <param name="writeCopy">if true, copy on write; if false, write to original memory</param>
		/// <exception cref="IOException">Memory mapping failed at the Win32 layer</exception>
		/// <returns>A Stream for a view on the file</returns>
		public static Stream MemoryMappedStream(string fileName, bool write, bool writeCopy)
		{
			using (MemoryMappedFile mmf = new MemoryMappedFile(fileName, write, writeCopy))
			{
				return mmf.MapView();
			}
		}
Example #3
0
        private static Stream _ConstructorInit(string filename, bool mmapFile)
        {
            Stream stream = null;
            bool mmapAble = true;

            // the MemoryMappedFile object doesn't handle files > 4GB
            FileInfo fi = new FileInfo(filename);
            if (fi.Length >= uint.MaxValue) mmapAble = false;

            if (!mmapFile) {
                Stream fs = ZStreamIn.Open(filename);
                //Stream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                try {
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, (int)fs.Length);
                    stream = new MemoryStream(buffer);
                }

                catch {
                    stream = null;
                }
            }

            // either malloc failed or we wanted to mmap from the start
            MemoryMappedFile mmap = null;
            if (stream == null && mmapAble) {
                try {
                    mmap = new MemoryMappedFile(filename);
                    if (mmap != null) stream = mmap.MapView();
                } catch {
                    if (mmap != null) mmap.Dispose();
                }
            }

            // we failed at mmap so open a filestream
            if (stream == null) {
            #if NAMEDPIPEMODE
                stream = ZStreamIn.Open(filename);
            #else
                stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            #endif
            }

            return stream;
        }