private void SaveMetadata(CommitLogMetadata metadata) { using (MemoryMappedViewStream stream = m_commitLogMetadata.CreateViewStream()) SaveMetadata(stream, metadata); }
public override bool Commit(HashSet <ICmObjectOrSurrogate> newbies, HashSet <ICmObjectOrSurrogate> dirtballs, HashSet <ICmObjectId> goners) { using (m_commitLogMutex.Lock()) { CommitLogMetadata metadata; using (MemoryMappedViewStream stream = m_commitLogMetadata.CreateViewStream()) { metadata = GetMetadata(stream); } List <ICmObjectSurrogate> foreignNewbies; List <ICmObjectSurrogate> foreignDirtballs; List <ICmObjectId> foreignGoners; if (GetUnseenForeignChanges(metadata, out foreignNewbies, out foreignDirtballs, out foreignGoners)) { // we have now seen every commit generation metadata.Peers[m_peerID].Generation = metadata.CurrentGeneration; IUnitOfWorkService uowService = ((IServiceLocatorInternal)m_cache.ServiceLocator).UnitOfWorkService; IReconcileChanges reconciler = uowService.CreateReconciler(foreignNewbies, foreignDirtballs, foreignGoners); if (reconciler.OkToReconcileChanges()) { reconciler.ReconcileForeignChanges(); if (metadata.Master == m_peerID) { var newObjects = new HashSet <ICmObjectOrSurrogate>(foreignNewbies); var editedObjects = new HashSet <ICmObjectOrSurrogate>(foreignDirtballs); var removedObjects = new HashSet <ICmObjectId>(foreignGoners); IEnumerable <CustomFieldInfo> fields; if (HaveAnythingToCommit(newObjects, editedObjects, removedObjects, out fields) && (StartupVersionNumber == ModelVersion)) { PerformCommit(newObjects, editedObjects, removedObjects, fields); } } } else { uowService.ConflictingChanges(reconciler); SaveMetadata(metadata); return(true); } } CheckExitedPeerProcesses(metadata); if (metadata.Master == Guid.Empty) { // Check if the former master left the commit log and XML file in a consistent state. If not, we can't continue. if (metadata.CurrentGeneration != metadata.FileGeneration) { throw new InvalidOperationException("The commit log and XML file are in an inconsistent state."); } base.LockProject(); metadata.Master = m_peerID; } IEnumerable <CustomFieldInfo> cfiList; if (!HaveAnythingToCommit(newbies, dirtballs, goners, out cfiList) && (StartupVersionNumber == ModelVersion)) { SaveMetadata(metadata); return(true); } var commitRec = new CommitLogRecord { Source = m_peerID, WriteGeneration = metadata.CurrentGeneration + 1, ObjectsDeleted = goners.Select(g => g.Guid).ToList(), ObjectsAdded = newbies.Select(n => n.XMLBytes).ToList(), ObjectsUpdated = dirtballs.Select(d => d.XMLBytes).ToList() }; using (var buffer = new MemoryStream()) { Serializer.SerializeWithLengthPrefix(buffer, commitRec, PrefixStyle.Base128, 1); if (metadata.LogLength + buffer.Length > m_settings.SharedXMLBackendCommitLogSize) { // if this peer is the master, then just skip this commit // other peers will not be able to continue when it cannot find the missing commit, but // the master peer can keep going if (metadata.Master != m_peerID) { throw new InvalidOperationException("The current commit cannot be written to the commit log, because it is full."); } } else { byte[] bytes = buffer.GetBuffer(); int commitRecOffset = (metadata.LogOffset + metadata.LogLength) % m_settings.SharedXMLBackendCommitLogSize; // check if the record can fit at the end of the commit log. If not, we wrap around to the beginning. if (commitRecOffset + buffer.Length > m_settings.SharedXMLBackendCommitLogSize) { if (metadata.LogLength == 0) { metadata.LogOffset = 0; } else { metadata.Padding = m_settings.SharedXMLBackendCommitLogSize - commitRecOffset; } metadata.LogLength += metadata.Padding; commitRecOffset = 0; } using (MemoryMappedViewStream stream = m_commitLog.CreateViewStream(commitRecOffset, buffer.Length)) { stream.Write(bytes, 0, (int)buffer.Length); metadata.LogLength += (int)buffer.Length; } } } if (metadata.Master == m_peerID) { PerformCommit(newbies, dirtballs, goners, cfiList); } metadata.CurrentGeneration++; // we've seen our own change metadata.Peers[m_peerID].Generation = metadata.CurrentGeneration; SaveMetadata(metadata); return(true); } }
private static CommitLogMetadata GetMetadata(MemoryMappedViewStream stream) { stream.Seek(0, SeekOrigin.Begin); return(Serializer.DeserializeWithLengthPrefix <CommitLogMetadata>(stream, PrefixStyle.Base128, 1)); }
private static void SaveMetadata(MemoryMappedViewStream stream, CommitLogMetadata metadata) { stream.Seek(0, SeekOrigin.Begin); Serializer.SerializeWithLengthPrefix(stream, metadata, PrefixStyle.Base128, 1); }
public void StreamAccessReadWriteMemoryMappedProjectedFile() { string filename = @"Test_EPF_WorkingDirectoryTests\StreamAccessReadWriteMemoryMappedProjectedFile.cs"; string fileVirtualPath = this.Enlistment.GetVirtualPathTo(filename); string contents = fileVirtualPath.ShouldBeAFile(this.fileSystem).WithContents(); StringBuilder contentsBuilder = new StringBuilder(contents); using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fileVirtualPath)) { // Length of the Byte-order-mark that will be at the start of the memory mapped file. // See https://msdn.microsoft.com/en-us/library/windows/desktop/dd374101(v=vs.85).aspx int bomOffset = 3; // offset -> Number of bytes from the start of the file where the view starts int offset = 64; int size = contents.Length; string newContent = "**NEWCONTENT**"; using (MemoryMappedViewStream streamAccessor = mmf.CreateViewStream(offset, size - offset + bomOffset)) { streamAccessor.CanRead.ShouldEqual(true); streamAccessor.CanWrite.ShouldEqual(true); for (int i = offset; i < size - offset; ++i) { streamAccessor.ReadByte().ShouldEqual(contents[i - bomOffset]); } // Reset to the start of the stream (which will place the streamAccessor at offset in the memory file) streamAccessor.Seek(0, SeekOrigin.Begin); byte[] newContentBuffer = Encoding.ASCII.GetBytes(newContent); streamAccessor.Write(newContentBuffer, 0, newContent.Length); for (int i = 0; i < newContent.Length; ++i) { contentsBuilder[offset + i - bomOffset] = newContent[i]; } contents = contentsBuilder.ToString(); } // Verify the file has the new contents inserted into it using (MemoryMappedViewStream streamAccessor = mmf.CreateViewStream(offset: 0, size: size + bomOffset)) { // Skip the BOM for (int i = 0; i < bomOffset; ++i) { streamAccessor.ReadByte(); } for (int i = 0; i < size; ++i) { streamAccessor.ReadByte().ShouldEqual(contents[i]); } } } // Confirm the new contents was written to disk fileVirtualPath.ShouldBeAFile(this.fileSystem).WithContents(contents); }
protected override void ShutdownInternal() { if (m_commitLogMutex != null && m_commitLogMetadata != null) { CompleteAllCommits(); using (m_commitLogMutex.Lock()) { #if __MonoCS__ bool delete = false; #endif using (MemoryMappedViewStream stream = m_commitLogMetadata.CreateViewStream()) { CommitLogMetadata metadata; if (TryGetMetadata(stream, out metadata)) { if (metadata.Master == m_peerID) { // commit any unseen foreign changes List <ICmObjectSurrogate> foreignNewbies; List <ICmObjectSurrogate> foreignDirtballs; List <ICmObjectId> foreignGoners; if (GetUnseenForeignChanges(metadata, out foreignNewbies, out foreignDirtballs, out foreignGoners)) { var newObjects = new HashSet <ICmObjectOrSurrogate>(foreignNewbies); var editedObjects = new HashSet <ICmObjectOrSurrogate>(foreignDirtballs); var removedObjects = new HashSet <ICmObjectId>(foreignGoners); IEnumerable <CustomFieldInfo> fields; if (HaveAnythingToCommit(newObjects, editedObjects, removedObjects, out fields) && (StartupVersionNumber == ModelVersion)) { base.WriteCommitWork(new CommitWork(newObjects, editedObjects, removedObjects, fields)); } } // XML file is now totally up-to-date metadata.FileGeneration = metadata.CurrentGeneration; } RemovePeer(metadata, m_peerID); #if __MonoCS__ delete = metadata.Peers.Count == 0; #endif SaveMetadata(stream, metadata); } } base.UnlockProject(); m_commitLog.Dispose(); m_commitLog = null; m_commitLogMetadata.Dispose(); m_commitLogMetadata = null; #if __MonoCS__ if (delete) { File.Delete(Path.Combine(m_commitLogDir, CommitLogMetadataName)); File.Delete(Path.Combine(m_commitLogDir, CommitLogName)); m_commitLogMutex.Unlink(); } #endif } } if (m_commitLogMutex != null) { m_commitLogMutex.Dispose(); m_commitLogMutex = null; } if (CommitThread != null) { CommitThread.Stop(); CommitThread.Dispose(); CommitThread = null; } foreach (Process peerProcess in m_peerProcesses.Values) { peerProcess.Close(); } m_peerProcesses.Clear(); }
/// <summary>Performs validation on a view stream.</summary> /// <param name="stream">The stream to verify.</param> /// <param name="capacity">The capacity specified when the stream was created.</param> /// <param name="access">The access specified when the stream was created.</param> protected static void ValidateMemoryMappedViewStream(MemoryMappedViewStream stream, long capacity, MemoryMappedFileAccess access) { // Validate the stream and its handle Assert.NotNull(stream); Assert.NotNull(stream.SafeMemoryMappedViewHandle); Assert.Same(stream.SafeMemoryMappedViewHandle, stream.SafeMemoryMappedViewHandle); // Validate its properties report the values they should Assert.InRange(capacity, 0, stream.Length); // the capacity may be rounded up to page size, so all we guarantee is that the stream's length >= capacity // If it's supposed to be readable, read from it. if (IsReadable(access)) { Assert.True(stream.CanRead); // Seek to the beginning stream.Position = 0; Assert.Equal(0, stream.Position); // Read a byte Assert.Equal(0, stream.ReadByte()); Assert.Equal(1, stream.Position); // Seek to just before the end Assert.Equal(capacity - 1, stream.Seek(capacity - 1, SeekOrigin.Begin)); // Read another byte Assert.Equal(0, stream.ReadByte()); Assert.Equal(capacity, stream.Position); } else { Assert.False(stream.CanRead); } // If it's supposed to be writable, try to write to it. if (IsWritable(access) || access == MemoryMappedFileAccess.CopyOnWrite) { Assert.True(stream.CanWrite); // Seek to the beginning, write a byte, seek to the almost end, write a byte stream.Position = 0; stream.WriteByte(42); stream.Position = stream.Length - 1; stream.WriteByte(42); // Verify the written bytes if possible if (IsReadable(access)) { stream.Position = 0; Assert.Equal(42, stream.ReadByte()); stream.Position = stream.Length - 1; Assert.Equal(42, stream.ReadByte()); } // Reset the written bytes stream.Position = 0; stream.WriteByte(0); stream.Position = stream.Length - 1; stream.WriteByte(0); } else { Assert.False(stream.CanWrite); } }
/// <summary> /// 创建内存映射文件 /// </summary> private static string CreateMemoryMapFile(long ttargetRowNum) { string line = string.Empty; using (FileStream fs = new FileStream(TXT_FILE_PATH, FileMode.Open, FileAccess.ReadWrite)) { long targetRowNum = ttargetRowNum + 1; //目标行 long curRowNum = 1; //当前行 FILE_SIZE = fs.Length; using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fs, "test", fs.Length, MemoryMappedFileAccess.ReadWrite, null, HandleInheritability.None, false)) { long offset = 0; //int limit = 250; int limit = 200; try { StringBuilder sbDefineRowLine = new StringBuilder(); do { long remaining = fs.Length - offset; using (MemoryMappedViewStream mmStream = mmf.CreateViewStream(offset, remaining > limit ? limit : remaining)) //using (MemoryMappedViewStream mmStream = mmf.CreateViewStream(offset, remaining)) { offset += limit; using (StreamReader sr = new StreamReader(mmStream)) { //string ss = sr.ReadToEnd().ToString().Replace("\n", "囧").Replace(Environment.NewLine, "囧"); string ss = sr.ReadToEnd().ToString().Replace("\n", SPLIT_VARCHAR).Replace(Environment.NewLine, SPLIT_VARCHAR); if (curRowNum <= targetRowNum) { if (curRowNum < targetRowNum) { string s = sbDefineRowLine.ToString(); int pos = s.LastIndexOf(SPLIT_CHAR); if (pos > 0) { sbDefineRowLine.Remove(0, pos); } } else { line = sbDefineRowLine.ToString(); return(line); } if (ss.Contains(SPLIT_VARCHAR)) { curRowNum += GetNewLineNumsOfStr(ss); sbDefineRowLine.Append(ss); } else { sbDefineRowLine.Append(ss); } } //sbDefineRowLine.Append(ss); //line = sbDefineRowLine.ToString(); //if (ss.Contains(Environment.NewLine)) //{ // ++curRowNum; // //curRowNum++; // //curRowNum += GetNewLineNumsOfStr(ss); // //sbDefineRowLine.Append(ss); //} //if (curRowNum == targetRowNum) //{ // string s = ""; //} sr.Dispose(); } mmStream.Dispose(); } } while (offset < fs.Length); } catch (Exception e) { Console.WriteLine(e.Message); } return(line); } } }
public void ParallelMMFRead(object taskMMFInfo) { try { TaskMMFInfo taskInfo = (TaskMMFInfo)taskMMFInfo; Debug.Print(string.Format("ParallelMMFRead:enter : position:{0} length:{1} total:{2}", taskInfo.position, taskInfo.length, taskInfo.length + taskInfo.position)); MemoryMappedViewStream viewStream = taskInfo.mmf.CreateViewStream(taskInfo.position, taskInfo.length, MemoryMappedFileAccess.Read); byte[] bytes = new byte[taskInfo.length]; viewStream.Read(bytes, 0, taskInfo.length); byte[] newLine = (taskInfo.logFile.Encoding).GetBytes(Environment.NewLine); int beginningIndex = 0; int indexCount = 0; int fixUp = 0; int x = 0; int step = Math.Max(1, newLine.Length / 2); // check first two bytes to make sure not part of crlf if (bytes[0] == newLine[0] || bytes[0] == newLine[step]) { fixUp = step; if (bytes[step] == newLine[step]) { fixUp = step * 2; } taskInfo.stringList.Add(new LogFileItem()); } for (x = fixUp; x < bytes.Length; x += step) { if (taskInfo.bgWorker.CancellationPending) { taskInfo.completedEvent.Set(); Debug.Print("ParallelMMFRead:cancelled"); return; } if (bytes[x] == newLine[0]) { LogFileItem logFileItem = new LogFileItem() { Content = (taskInfo.logFile.Encoding).GetString(bytes, beginningIndex, indexCount), Background = Settings.BackgroundColor, Foreground = Settings.ForegroundColor, }; taskInfo.stringList.Add(logFileItem); if (x + step <= bytes.Length && bytes[x + step] == newLine[step]) { x += step; } beginningIndex = x + step; indexCount = 0; } else { indexCount += step; } } if (indexCount > 1) { // partial string LogFileItem logFileItem = new LogFileItem() { Content = (taskInfo.logFile.Encoding).GetString(bytes, beginningIndex, bytes.Length - beginningIndex), Background = Settings.BackgroundColor, Foreground = Settings.ForegroundColor, // Index = x }; taskInfo.stringList.Add(logFileItem); logFileItem = new LogFileItem() { Content = _needsPatch, Background = Settings.BackgroundColor, Foreground = Settings.ForegroundColor, // Index = x }; taskInfo.stringList.Add(logFileItem); } taskInfo.completedEvent.Set(); Debug.Print("ParallelMMFRead:exit"); } catch (Exception e) { Debug.Print("ParallelMMFRead:exception" + e.ToString()); return; } }
/// <summary> /// Write the given <see cref="long"/> value into the <see cref="MemoryMappedViewStream"/>. /// </summary> /// <param name="mmv"><see cref="MemoryMappedViewStream"/> to write to.</param> /// <param name="value">The <see cref="long"/> value to write.</param> public static Task WriteLongAsync(MemoryMappedViewStream mmv, long value) { byte[] valueBytes = BitConverter.GetBytes(value); return(mmv.WriteAsync(valueBytes, 0, SharedMemoryConstants.ContentLengthHeaderBytes)); }
public bool runTest() { try { //////////////////////////////////////////////////////////////////////// // Dispose() //////////////////////////////////////////////////////////////////////// MemoryMappedFile mmf = MemoryMappedFile.CreateNew("Dispose_mapname101", 100); MemoryMappedViewStream viewStream = mmf.CreateViewStream(); MemoryMappedViewAccessor viewAccessor = mmf.CreateViewAccessor(); mmf.Dispose(); // new ViewStream cannot be created from a disposed MMF try { _iCountTestcases++; mmf.CreateViewStream(); _iCountErrors++; Console.WriteLine("ERROR, Loc001a: No exception thrown, expected ObjectDisposeException"); } catch (ObjectDisposedException) { } catch (Exception ex) { _iCountErrors++; Console.WriteLine("ERROR, Loc001b: Unexpected exception, {0}", ex); } // new ViewAccessor cannot be created from a disposed MMF try { _iCountTestcases++; mmf.CreateViewAccessor(); _iCountErrors++; Console.WriteLine("ERROR, Loc002a: No exception thrown, expected ObjectDisposeException"); } catch (ObjectDisposedException) { } catch (Exception ex) { _iCountErrors++; Console.WriteLine("ERROR, Loc002b: Unexpected exception, {0}", ex); } // existing views can still be used try { _iCountTestcases++; StreamWriter writer = new StreamWriter(viewStream); writer.Write('a'); writer.Flush(); _iCountTestcases++; Char result = viewAccessor.ReadChar(0); if (result != 'a') { _iCountErrors++; Console.WriteLine("ERROR, Loc010a: ViewAccessor read/write was wrong. expected 'a', got '{0}'", result); } } catch (Exception ex) { _iCountErrors++; Console.WriteLine("ERROR, Loc010c: Unexpected exception, {0}", ex); } // Dispose twice succeeds try { _iCountTestcases++; mmf.Dispose(); } catch (Exception ex) { _iCountErrors++; Console.WriteLine("ERROR, Loc010c: Unexpected exception, {0}", ex); } /// END TEST CASES if (_iCountErrors == 0) { return(true); } else { Console.WriteLine("Fail! iCountErrors==" + _iCountErrors); return(false); } } catch (Exception ex) { Console.WriteLine("ERR999: Unexpected exception in runTest, {0}", ex); return(false); } }
public static string _CMDshell(string _Command) { string final = ""; string xtemp = ""; Console.ForegroundColor = ConsoleColor.Gray; try { string yourcmd = ""; bool getcmdagain = false; string oldcmd = ""; string s = ""; bool show = false; ops: Console.ForegroundColor = ConsoleColor.Gray; show = false; using (MemoryMappedFile mmf2 = MemoryMappedFile.OpenExisting("ClientMapper")) { show = false; Mutex mutex = Mutex.OpenExisting("_ClientMapper"); using (MemoryMappedViewStream stream = mmf2.CreateViewStream()) { Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine(DateTime.Now.ToString() + " [!] Searching in-Memory... Dumping in-Memory from [NativePayload_MPAgent]"); Console.ForegroundColor = ConsoleColor.Green; BinaryReader reader = new BinaryReader(stream); s = reader.ReadString(); Console.WriteLine(DateTime.Now.ToString() + " " + s); Console.ForegroundColor = ConsoleColor.Gray; show = true; } if (s.Contains("@getcmd=") || getcmdagain && lastcmd != _Command) { Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine("[>] Set Command and press enter"); Console.ForegroundColor = ConsoleColor.Blue; yourcmd = _Command; lastcmd = yourcmd; show = false; Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine("[>] Sending Command to Memory"); using (MemoryMappedViewStream streamw = mmf2.CreateViewStream()) { Console.ForegroundColor = ConsoleColor.DarkYellow; BinaryWriter writer = new BinaryWriter(streamw); writer.Write("[!] " + DateTime.Now.ToString() + " NativePayload_MP.CS.cmd =>" + yourcmd); } getcmdagain = false; oldcmd = yourcmd; } else if (s.Contains("cmd output => ") || getcmdagain == false && oldcmd != yourcmd && show) { /// bug fixed, i think ;) if (show && s.Split('>')[1] != string.Empty) { Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("[>] {0} Command Output Downloaded from Memory", DateTime.Now.ToString()); Console.WriteLine("========================================"); Console.ForegroundColor = ConsoleColor.Green; //strOutput = Convert.ToBase64String(UnicodeEncoding.UTF8.GetBytes(outputs.StandardOutput.ReadToEnd())); string temp = s.Split('>')[1]; final = UTF8Encoding.UTF8.GetString(Convert.FromBase64String(temp)); Console.WriteLine(final); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("========================================"); } getcmdagain = true; Console.ForegroundColor = ConsoleColor.Gray; show = false; xtemp = final; return(xtemp); } Console.ForegroundColor = ConsoleColor.Gray; Thread.Sleep(5000); show = false; goto ops; } } catch (Exception) { } /// these lines does not matter ;) // xtemp = "[" + _AllIPs + "] => " + final; xtemp = final; return(xtemp); }
internal SharedMemoryReader(SharedMemory source, MemoryMappedViewStream mmvs) { MMVS_ = mmvs; BinReader_ = new BinaryReader(MMVS_); Source = source; }
/// <summary>Performs many reads and writes of against the stream.</summary> private static void AssertWritesReads(MemoryMappedViewStream s) { // Write and read at the beginning s.Position = 0; s.WriteByte(42); s.Position = 0; Assert.Equal(42, s.ReadByte()); // Write and read at the end byte[] data = new byte[] { 1, 2, 3 }; s.Position = s.Length - data.Length; s.Write(data, 0, data.Length); s.Position = s.Length - data.Length; Array.Clear(data, 0, data.Length); Assert.Equal(3, s.Read(data, 0, data.Length)); Assert.Equal(new byte[] { 1, 2, 3 }, data); // Fail reading/writing past the end s.Position = s.Length; Assert.Equal(-1, s.ReadByte()); Assert.Throws<NotSupportedException>(() => s.WriteByte(42)); }
public void StreamAndRandomAccessReadWriteMemoryMappedProjectedFile() { string filename = @"Test_EPF_WorkingDirectoryTests\StreamAndRandomAccessReadWriteMemoryMappedProjectedFile.cs"; string fileVirtualPath = this.Enlistment.GetVirtualPathTo(filename); StringBuilder contentsBuilder = new StringBuilder(); // Length of the Byte-order-mark that will be at the start of the memory mapped file. // See https://msdn.microsoft.com/en-us/library/windows/desktop/dd374101(v=vs.85).aspx int bomOffset = 3; using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fileVirtualPath)) { // The text length of StreamAndRandomAccessReadWriteMemoryMappedProjectedFile.cs was determined // outside of this test so that the test would not hydrate the file before we access via MemoryMappedFile int fileTextLength = 13762; int size = bomOffset + fileTextLength; int streamAccessWriteOffset = 64; int randomAccessWriteOffset = 128; string newStreamAccessContent = "**NEW_STREAM_CONTENT**"; string newRandomAccessConents = "&&NEW_RANDOM_CONTENT&&"; // Read (and modify) contents using stream accessor using (MemoryMappedViewStream streamAccessor = mmf.CreateViewStream(offset: 0, size: size)) { streamAccessor.CanRead.ShouldEqual(true); streamAccessor.CanWrite.ShouldEqual(true); for (int i = 0; i < size; ++i) { contentsBuilder.Append((char)streamAccessor.ReadByte()); } // Reset to the start of the stream (which will place the streamAccessor at offset in the memory file) streamAccessor.Seek(streamAccessWriteOffset, SeekOrigin.Begin); byte[] newContentBuffer = Encoding.ASCII.GetBytes(newStreamAccessContent); streamAccessor.Write(newContentBuffer, 0, newStreamAccessContent.Length); for (int i = 0; i < newStreamAccessContent.Length; ++i) { contentsBuilder[streamAccessWriteOffset + i] = newStreamAccessContent[i]; } } // Read (and modify) contents using random accessor using (MemoryMappedViewAccessor randomAccessor = mmf.CreateViewAccessor(offset: 0, size: size)) { randomAccessor.CanRead.ShouldEqual(true); randomAccessor.CanWrite.ShouldEqual(true); // Confirm the random accessor reads the same content that was read (and written) by the stream // accessor for (int i = 0; i < size; ++i) { ((char)randomAccessor.ReadByte(i)).ShouldEqual(contentsBuilder[i]); } // Write some new content for (int i = 0; i < newRandomAccessConents.Length; ++i) { // Convert to byte before writing rather than writing as char, because char version will write a 16-bit // unicode char randomAccessor.Write(i + randomAccessWriteOffset, Convert.ToByte(newRandomAccessConents[i])); ((char)randomAccessor.ReadByte(i + randomAccessWriteOffset)).ShouldEqual(newRandomAccessConents[i]); } for (int i = 0; i < newRandomAccessConents.Length; ++i) { contentsBuilder[randomAccessWriteOffset + i] = newRandomAccessConents[i]; } } // Verify the file one more time with a stream accessor using (MemoryMappedViewStream streamAccessor = mmf.CreateViewStream(offset: 0, size: size)) { for (int i = 0; i < size; ++i) { streamAccessor.ReadByte().ShouldEqual(contentsBuilder[i]); } } } // Remove the BOM before comparing with the contents of the file on disk contentsBuilder.Remove(0, bomOffset); // Confirm the new contents was written to the file fileVirtualPath.ShouldBeAFile(this.fileSystem).WithContents(contentsBuilder.ToString()); }
public bool runTest() { try { //////////////////////////////////////////////////////////////////////// // SetLength() //////////////////////////////////////////////////////////////////////// using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("MMVS_SetLength0", 100)) { using (MemoryMappedViewStream viewStream = mmf.CreateViewStream()) { long length = viewStream.Length; // throws NotSupportedException _iCountTestcases++; try { viewStream.SetLength(1000); _iCountErrors++; Console.WriteLine("ERROR, No exception thrown, expected NotSupportedException"); } catch (NotSupportedException) { // Expected } catch (Exception ex) { _iCountErrors++; Console.WriteLine("ERROR, Unexpected exception, {0}", ex); } // length was unchanged _iCountTestcases++; if (viewStream.Length != length) { _iCountErrors++; Console.WriteLine("ERROR, Length was wrong, expected {0}, got {1}", length, viewStream.Length); } } } /// END TEST CASES if (_iCountErrors == 0) { return(true); } else { Console.WriteLine("Fail! iCountErrors==" + _iCountErrors); return(false); } } catch (Exception ex) { Console.WriteLine("ERR999: Unexpected exception in runTest, {0}", ex); return(false); } }