public void Ctor_String(string replacement) { EncoderReplacementFallback fallback = new EncoderReplacementFallback(replacement); Assert.Equal(replacement.Length, fallback.MaxCharCount); Assert.Equal(replacement, fallback.DefaultString); Assert.Equal(replacement.GetHashCode(), fallback.GetHashCode()); }
public void Ctor_Empty() { EncoderReplacementFallback exception = new EncoderReplacementFallback(); Assert.Equal(1, exception.MaxCharCount); Assert.Equal("?", exception.DefaultString); Assert.Equal("?".GetHashCode(), exception.GetHashCode()); }
public void CreateFallbackBuffer() { EncoderFallbackBuffer buffer = new EncoderReplacementFallback().CreateFallbackBuffer(); Assert.Equal((char)0, buffer.GetNextChar()); Assert.False(buffer.MovePrevious()); Assert.Equal(0, buffer.Remaining); }
public EncoderReplacementFallbackBuffer ( EncoderReplacementFallback fallback) { if (fallback == null) throw new ArgumentNullException ("fallback"); replacement = fallback.DefaultString; current = 0; }
public static Encoding GetEncoding(int codepage, string fallback) { if (fallback == null) { throw new ArgumentNullException(nameof(fallback)); } var encoderFallback = new EncoderReplacementFallback(fallback); var decoderFallback = new DecoderReplacementFallback(fallback); return(Encoding.GetEncoding(codepage, encoderFallback, decoderFallback)); }
public static Encoding GetEncoding(string charset, string fallback) { if (charset == null) { throw new ArgumentNullException("charset"); } if (fallback == null) { throw new ArgumentNullException("fallback"); } int codePage = GetCodePage(charset); if (codePage == -1) { throw new NotSupportedException($"The '{charset}' encoding is not supported."); } EncoderReplacementFallback encoderFallback = new EncoderReplacementFallback(fallback); DecoderReplacementFallback decoderFallback = new DecoderReplacementFallback(fallback); return(Encoding.GetEncoding(codePage, encoderFallback, decoderFallback)); }
public static Encoding GetEncoding(string charset, string fallback) { int codepage; if (charset == null) { throw new ArgumentNullException("charset"); } if (fallback == null) { throw new ArgumentNullException("fallback"); } if ((codepage = GetCodePage(charset)) == -1) { throw new NotSupportedException(); } var encoderFallback = new EncoderReplacementFallback(fallback); var decoderFallback = new DecoderReplacementFallback(fallback); return(Encoding.GetEncoding(codepage, encoderFallback, decoderFallback)); }
public static Encoding GetEncoding(string charset, string fallback) { int codepage; if (charset == null) { throw new ArgumentNullException(nameof(charset)); } if (fallback == null) { throw new ArgumentNullException(nameof(fallback)); } if ((codepage = GetCodePage(charset)) == -1) { throw new NotSupportedException(string.Format("The '{0}' encoding is not supported.", charset)); } var encoderFallback = new EncoderReplacementFallback(fallback); var decoderFallback = new DecoderReplacementFallback(fallback); return(Encoding.GetEncoding(codepage, encoderFallback, decoderFallback)); }
public void CreateFallbackBuffer_Fallback_Char_Char(string replacement, bool expected) { EncoderFallbackBuffer buffer = new EncoderReplacementFallback(replacement).CreateFallbackBuffer(); Assert.Equal(expected, buffer.Fallback('\uD800', '\uDC00', 0)); }
public void CreateFallbackBuffer_Fallback_Char(string replacement, char charUnknown, bool expected) { EncoderFallbackBuffer buffer = new EncoderReplacementFallback(replacement).CreateFallbackBuffer(); Assert.Equal(expected, buffer.Fallback(charUnknown, 0)); }
public void EqualsTest(EncoderReplacementFallback fallback, object value, bool expected) { Assert.Equal(expected, fallback.Equals(value)); }
// Construction public EncoderReplacementFallbackBuffer(EncoderReplacementFallback fallback) { // 2X in case we're a surrogate pair _strDefault = fallback.DefaultString + fallback.DefaultString; }
public void CreateFallbackBuffer_Fallback_InvalidSurrogateChars_ThrowsArgumentOutOfRangeException() { EncoderFallbackBuffer buffer = new EncoderReplacementFallback().CreateFallbackBuffer(); Assert.Throws<ArgumentOutOfRangeException>("charUnknownHigh", () => buffer.Fallback('a', '\uDC00', 0)); Assert.Throws<ArgumentOutOfRangeException>("CharUnknownLow", () => buffer.Fallback('\uD800', 'a', 0)); }
public override bool Equals(object value) { EncoderReplacementFallback f = value as EncoderReplacementFallback; return(f != null && replacement == f.replacement); }
// This either opens the originally specified log file, opens an // alternate log file (e.g. with "(A)" in the name), or throws an exception. protected override void InternalOpen() { // Use this to generate alternate file names with (A)-(Z) if file can't be opened. char c = 'A'; string renamedFile = null; FileStream fileStream = null; bool appending = false; string simpleName = _logFileName; // no extension, no (A), no _00. while (fileStream == null) { try { FileInfo outFile = new FileInfo(FullPath); if (outFile.Exists) { if (outFile.Length < AppendIfSmallerThanMb << _shift && outFile.Length < MaxSizeMb << _shift) { // Open the file in append mode. // If the file is in use, this throws an exception. fileStream = new FileStream(FullPath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read); // Check the format version of the existing file. var reader = new BinaryReader(fileStream); string msg = null; fileStream.Position = 0; int oldVersion = reader.ReadInt32(); if (oldVersion == FormatVersion) { // If the original file is password-protected, verify the // user specified the same password this time. bool oldHasPassword = reader.ReadBoolean(); if (oldHasPassword && _hasPassword) { // Check that passwords (actually the hashes) match. byte[] oldHash = reader.ReadBytes(20); if (oldHash.SequenceEqual(_sha1PasswordHash)) { // Read and discard the password hint because we need to read the file GUID later. var hint = reader.ReadString(); } else { // We don't allow different sessions to have different passwords. msg = string.Format("TracerX could not append to the existing log file\n{0}\nbecause it has a different password than the current file.", FullPath); Logger.EventLogging.Log(msg, Logger.EventLogging.AppendPasswordConflict); } } else if (!oldHasPassword && !_hasPassword) { // It's OK if both don't have passwords. msg = null; } else { // We don't allow different sessions to have different passwords. msg = string.Format("TracerX could not append to the existing log file\n{0}\nbecause it has a different password than the current file.", FullPath); Logger.EventLogging.Log(msg, Logger.EventLogging.AppendPasswordConflict); } } else { // We don't mix format versions in one file. msg = string.Format("TracerX could not append to the existing log file\n{0}\nbecause its format version ({1}) was not equal to the current format version ({2}).", FullPath, oldVersion, FormatVersion); Logger.EventLogging.Log(msg, Logger.EventLogging.AppendVersionConflict); } if (msg == null) { // Success! We'll start writing at the end, but first we need to read the // file's original GUID to use in the name of an event used to signal the // viewer(s) when new messages are logged. string asmversion = reader.ReadString(); uint maxsize = reader.ReadUInt32(); long maxfilepos = reader.ReadInt64(); FileGuid = new Guid(reader.ReadBytes(16)); fileStream.Position = fileStream.Length; _openSize = fileStream.Length; appending = true; } else { // Failure! Turn Append off and try again. AppendIfSmallerThanMb = 0; fileStream.Dispose(); fileStream = null; continue; } } // Opening in append mode. else { // Create a new file. if (Archives > 0) { // Temporarily rename the existing file so we can create the new // file before "rolling" the archives. Basically, we don't want // to "roll" the archives until we know we can create the new file // by actually creating it. DateTime originalCreateTime = outFile.CreationTime; renamedFile = FullPath + ".tempname"; // DO NOT use outFile.Exists or outFile.Move() in here. for (int attempts = 3; attempts > 0 && File.Exists(FullPath); --attempts) { try { // The "temp" file shouldn't exist, but delete it if it does. // If the file is in use, this throws an exception. File.Delete(renamedFile); File.Move(FullPath, renamedFile); File.SetCreationTime(renamedFile, originalCreateTime); } catch (Exception ex) { if (attempts <= 1) { // That was the last try. throw; } else { // Give whoever has the file open (possibly the viewer) a // chance to close it. Thread.Sleep(100); } } } } fileStream = new FileStream(FullPath, FileMode.Create, FileAccess.Write, FileShare.Read); } // Creating new file. } else { // If the file is in use, this throws an exception. fileStream = new FileStream(FullPath, FileMode.Create, FileAccess.Write, FileShare.Read); } } catch (System.IO.IOException ex) { // File is probably in use, try next alternate name. if (c >= 'Z') { // That was the last chance. Rethrow the exception to // end the loop and cause the exception to be logged. throw; } else { // Try the next alternative file name, up to Z. // Changing _logFileName also changes Name and FullName. // Note that _logFileName has no extension or _00. Debug.Print("Opening alternate file name due to exception:\n" + ex.ToString()); _logFileName = string.Format("{0}({1})", simpleName, c); ++c; renamedFile = null; continue; } } } // while // Use an EncoderReplacementFallback to replace any invalid UTF-16 chars // found in logged strings with '?' (System.String uses UTF-16 internally). var EncoderFallback = new EncoderReplacementFallback("?"); var utf8WithFallback = Encoding.GetEncoding("UTF-8", EncoderFallback, new DecoderExceptionFallback()); _logfile = new BinaryWriter(fileStream, utf8WithFallback); _fileHandle = fileStream.SafeFileHandle; _maxFilePosition = MaxSizeMb << _shift; _openTimeUtc = DateTime.UtcNow; if (!appending) { // Due to a bizarre Windows feature called "File System Tunneling", the // file's creation time will be wrong unless we set it explicitly like this. long now = _openTimeUtc.ToLocalTime().ToFileTime(); SetFileTime(_fileHandle, ref now, IntPtr.Zero, IntPtr.Zero); } if (CircularStartDelaySeconds == 0) { _circularStartTime = DateTime.MaxValue; } else { _circularStartTime = _openTimeUtc.AddSeconds(CircularStartDelaySeconds); } WritePreamble(appending); ++CurrentFile; if (_hasPassword) { _encryptor = new Encryptor(_logfile, _encryptionKey); } // The _viewerSignaler must be created before the first log message // is written because BinaryFile.WriteLine() references it. _viewerSignaler = new NamedEventsManager(FileGuid, FullPath); if (this == Logger.DefaultBinaryFile) { Logger.StandardData.LogEnvironmentInfo(); } ManageArchives(renamedFile); }
// Constructors public EncoderReplacementFallbackBuffer(EncoderReplacementFallback fallback) {}
// Constructors public EncoderReplacementFallbackBuffer(EncoderReplacementFallback fallback) { }
public void CreateFallbackBuffer_MultipleFallback_ThrowsArgumentException(string replacement) { EncoderFallbackBuffer buffer = new EncoderReplacementFallback(replacement).CreateFallbackBuffer(); buffer.Fallback('a', 0); Assert.Throws<ArgumentException>("chars", () => buffer.Fallback('a', 0)); Assert.Throws<ArgumentException>("chars", () => buffer.Fallback('\uD800', '\uDC00', 0)); }
public void Equals(EncoderReplacementFallback fallback, object value, bool expected) { Assert.Equal(expected, fallback.Equals(value)); }