/// <summary> /// Reads a file filtering its content through the filter chain. /// </summary> /// <param name="fileName">The file to read.</param> /// <param name="filterChain">Chain of filters to apply when reading, or <see langword="null" /> is no filters should be applied.</param> /// <param name="inputEncoding">The encoding used to read the file.</param> /// <remarks> /// If <paramref name="inputEncoding" /> is <see langword="null" />, /// then the system's ANSI code page will be used to read the file. /// </remarks> public static string ReadFile(string fileName, FilterChain filterChain, Encoding inputEncoding) { string content = null; // determine character encoding to use Encoding encoding = (inputEncoding != null) ? inputEncoding : Encoding.Default; // read file using (StreamReader sr = new StreamReader(fileName, encoding, true)) { if (filterChain == null || filterChain.Filters.Count == 0) { content = sr.ReadToEnd(); } else { Filter baseFilter = filterChain.GetBaseFilter( new PhysicalTextReader(sr)); StringWriter sw = new StringWriter(); while (true) { int character = baseFilter.Read(); if (character == -1) { break; } sw.Write((char)character); } content = sw.ToString(); } } return(content); }
/// <summary> /// Copies a file filtering its content through the filter chain. /// </summary> /// <param name="sourceFileName">The file to copy</param> /// <param name="destFileName">The file to copy to</param> /// <param name="filterChain">Chain of filters to apply when copying, or <see langword="null" /> is no filters should be applied.</param> /// <param name="inputEncoding">The encoding used to read the soure file.</param> /// <param name="outputEncoding">The encoding used to write the destination file.</param> public static void CopyFile(string sourceFileName, string destFileName, FilterChain filterChain, Encoding inputEncoding, Encoding outputEncoding) { // determine if filters are available bool filtersAvailable = filterChain != null && filterChain.Filters.Count > 0; // if no filters have been defined, and no input or output encoding // is set, we can just use the File.Copy method if (!filtersAvailable && inputEncoding == null && outputEncoding == null) { File.Copy(sourceFileName, destFileName, true); } else { // determine actual input encoding to use. if no explicit input // encoding is specified, we'll use the system's current ANSI // code page Encoding actualInputEncoding = (inputEncoding != null) ? inputEncoding : Encoding.Default; // get base filter built on the file's reader. Use a 8k buffer. using (StreamReader sourceFileReader = new StreamReader(sourceFileName, actualInputEncoding, true, 8192)) { Encoding actualOutputEncoding = outputEncoding; if (actualOutputEncoding == null) { // if no explicit output encoding is specified, we'll // just use the encoding of the input file as determined // by the runtime // // Note : the input encoding as specified on the filterchain // might not match the current encoding of the streamreader // // eg. when specifing an ANSI encoding, the runtime might // still detect the file is using UTF-8 encoding, because // we use BOM detection actualOutputEncoding = sourceFileReader.CurrentEncoding; } // writer for destination file using (StreamWriter destFileWriter = new StreamWriter(destFileName, false, actualOutputEncoding, 8192)) { if (filtersAvailable) { Filter baseFilter = filterChain.GetBaseFilter(new PhysicalTextReader(sourceFileReader)); bool atEnd = false; int character; while (!atEnd) { character = baseFilter.Read(); if (character > -1) { destFileWriter.Write((char)character); } else { atEnd = true; } } } else { char[] buffer = new char[8192]; while (true) { int charsRead = sourceFileReader.Read(buffer, 0, buffer.Length); if (charsRead == 0) { break; } destFileWriter.Write(buffer, 0, charsRead); } } } } } }