public override char GetChar() { char c = _accessor.ReadChar(Position); Position += 2; //conform to how the index was written return(Number.FlipEndian(c)); }
public override char GetChar() { var littleEndian = _accessor.ReadChar(Ix(NextGetIndex(2))); if (bigEndian) { return(Number.FlipEndian(littleEndian)); } return(littleEndian); }
public void ShowFileContents() { StringBuilder sb = new StringBuilder(FILE_SIZE); for (int i = 0; i < FILE_SIZE; i += 2) { char ch = accessor.ReadChar(i); sb.Append(ch); var result = sb.ToString(); } Console.Write(sb.ToString()); }
private void ShowFileMessage() { StringBuilder strBuild = new StringBuilder(512); strBuild.Append("开始读取:\r\n"); int j = 0; for (int i = 0; i < 512; i += 2, j++) { strBuild.Append("\t"); char ch = viewAccessor.ReadChar(i); strBuild.Append(j).Append(":").Append(ch); } textBox_output2.Text += strBuild; }
public string Read() { var str = new StringBuilder(); while (true) { char c = acc.ReadChar(position); if (c == '\x0') { return(null); } position += sizeof(char); if (c == '\x2') { return(str.ToString()); } str.Append(c); } }
/// <summary>Performs many reads and writes of various data types against the accessor.</summary> private static void AssertWritesReads(MemoryMappedViewAccessor acc) { // Successful reads and writes at the beginning for each data type AssertWriteRead <bool>(false, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadBoolean(pos)); AssertWriteRead <bool>(true, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadBoolean(pos)); AssertWriteRead <byte>(42, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadByte(pos)); AssertWriteRead <char>('c', 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadChar(pos)); AssertWriteRead <decimal>(9, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadDecimal(pos)); AssertWriteRead <double>(10, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadDouble(pos)); AssertWriteRead <short>(11, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadInt16(pos)); AssertWriteRead <int>(12, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadInt32(pos)); AssertWriteRead <long>(13, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadInt64(pos)); AssertWriteRead <sbyte>(14, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadSByte(pos)); AssertWriteRead <float>(15, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadSingle(pos)); AssertWriteRead <ushort>(16, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadUInt16(pos)); AssertWriteRead <uint>(17, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadUInt32(pos)); AssertWriteRead <ulong>(17, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadUInt64(pos)); // Successful reads and writes at the end for each data type long end = acc.Capacity; AssertWriteRead <bool>(false, end - sizeof(bool), (pos, value) => acc.Write(pos, value), pos => acc.ReadBoolean(pos)); AssertWriteRead <bool>(true, end - sizeof(bool), (pos, value) => acc.Write(pos, value), pos => acc.ReadBoolean(pos)); AssertWriteRead <byte>(42, end - sizeof(byte), (pos, value) => acc.Write(pos, value), pos => acc.ReadByte(pos)); AssertWriteRead <char>('c', end - sizeof(char), (pos, value) => acc.Write(pos, value), pos => acc.ReadChar(pos)); AssertWriteRead <decimal>(9, end - sizeof(decimal), (pos, value) => acc.Write(pos, value), pos => acc.ReadDecimal(pos)); AssertWriteRead <double>(10, end - sizeof(double), (pos, value) => acc.Write(pos, value), pos => acc.ReadDouble(pos)); AssertWriteRead <short>(11, end - sizeof(short), (pos, value) => acc.Write(pos, value), pos => acc.ReadInt16(pos)); AssertWriteRead <int>(12, end - sizeof(int), (pos, value) => acc.Write(pos, value), pos => acc.ReadInt32(pos)); AssertWriteRead <long>(13, end - sizeof(long), (pos, value) => acc.Write(pos, value), pos => acc.ReadInt64(pos)); AssertWriteRead <sbyte>(14, end - sizeof(sbyte), (pos, value) => acc.Write(pos, value), pos => acc.ReadSByte(pos)); AssertWriteRead <float>(15, end - sizeof(float), (pos, value) => acc.Write(pos, value), pos => acc.ReadSingle(pos)); AssertWriteRead <ushort>(16, end - sizeof(ushort), (pos, value) => acc.Write(pos, value), pos => acc.ReadUInt16(pos)); AssertWriteRead <uint>(17, end - sizeof(uint), (pos, value) => acc.Write(pos, value), pos => acc.ReadUInt32(pos)); AssertWriteRead <ulong>(17, end - sizeof(ulong), (pos, value) => acc.Write(pos, value), pos => acc.ReadUInt64(pos)); // Failed reads and writes just at the border of the end. This triggers different exception types // for some types than when we're completely beyond the end. long beyondEnd = acc.Capacity + 1; AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadBoolean(beyondEnd - sizeof(bool))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadByte(beyondEnd - sizeof(byte))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadSByte(beyondEnd - sizeof(sbyte))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadChar(beyondEnd - sizeof(char))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadDecimal(beyondEnd - sizeof(decimal))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadDouble(beyondEnd - sizeof(double))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadInt16(beyondEnd - sizeof(short))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadInt32(beyondEnd - sizeof(int))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadInt64(beyondEnd - sizeof(long))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadSingle(beyondEnd - sizeof(float))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadUInt16(beyondEnd - sizeof(ushort))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadUInt32(beyondEnd - sizeof(uint))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadUInt64(beyondEnd - sizeof(ulong))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(bool), false)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(byte), (byte)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(sbyte), (sbyte)0)); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(char), 'c')); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(decimal), (decimal)0)); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(double), (double)0)); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(short), (short)0)); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(int), (int)0)); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(long), (long)0)); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(float), (float)0)); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(ushort), (ushort)0)); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(uint), (uint)0)); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(ulong), (ulong)0)); // Failed reads and writes well past the end beyondEnd = acc.Capacity + 20; AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadBoolean(beyondEnd - sizeof(bool))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadByte(beyondEnd - sizeof(byte))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadSByte(beyondEnd - sizeof(sbyte))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadChar(beyondEnd - sizeof(char))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadDecimal(beyondEnd - sizeof(decimal))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadDouble(beyondEnd - sizeof(double))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadInt16(beyondEnd - sizeof(short))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadInt32(beyondEnd - sizeof(int))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadInt64(beyondEnd - sizeof(long))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadSingle(beyondEnd - sizeof(float))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadUInt16(beyondEnd - sizeof(ushort))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadUInt32(beyondEnd - sizeof(uint))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadUInt64(beyondEnd - sizeof(ulong))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(bool), false)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(byte), (byte)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(sbyte), (sbyte)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(char), 'c')); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(decimal), (decimal)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(double), (double)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(short), (short)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(int), (int)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(long), (long)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(float), (float)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(ushort), (ushort)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(uint), (uint)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(ulong), (ulong)0)); }
public char GetValueChar(long offset) { return(_viewAccessor.ReadChar(offset)); }
public bool runTest() { try { //////////////////////////////////////////////////////////////////////// // Dispose() //////////////////////////////////////////////////////////////////////// MemoryMappedFile mmf = MemoryMappedFile.CreateNew("Dispose_mapname101" + s_uniquifier, 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); } }