internal async Task <ValueTuple <SourceText, string> > ReadFileContentAsync(CommandLineSourceFile file, IList <DiagnosticInfo> diagnostics, Encoding encoding) { try { using (var data = new FileStream(file.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, bufferSize: 4096, useAsync: true)) { MemoryStream stream = memoryStreamPool.Allocate(); stream.SetLength(0); await data.CopyToAsync(stream).ConfigureAwait(false); var normalizedFilePath = data.Name; var text = new EncodedStringText(stream, encoding); memoryStreamPool.Free(stream); return(ValueTuple.Create <SourceText, string>(text, normalizedFilePath)); } } catch (Exception e) { diagnostics.Add(ToFileReadDiagostics(e, file)); return(default(ValueTuple <SourceText, string>)); } }
/// <summary> /// Reads content of a source file. /// </summary> /// <param name="file">Source file information.</param> /// <param name="diagnostics">Storage for diagnostics.</param> /// <param name="encoding">Encoding to use or 'null' for autodetect/default</param> /// <param name="normalizedFilePath">If given <paramref name="file"/> opens successfully, set to normalized absolute path of the file, null otherwise.</param> /// <returns>File content or null on failure.</returns> internal SourceText ReadFileContent(CommandLineSourceFile file, IList <DiagnosticInfo> diagnostics, Encoding encoding, out string normalizedFilePath) { try { using (var data = new FileStream(file.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { normalizedFilePath = data.Name; return(new EncodedStringText(data, encoding)); } } catch (Exception e) { DiagnosticInfo diagnosticInfo; if (e is FileNotFoundException || e is DirectoryNotFoundException) { diagnosticInfo = new DiagnosticInfo(MessageProvider, (int)MessageProvider.ERR_FileNotFound, file.Path); } else if (e is InvalidDataException) { diagnosticInfo = new DiagnosticInfo(MessageProvider, (int)MessageProvider.ERR_BinaryFile, file.Path); } else { diagnosticInfo = new DiagnosticInfo(MessageProvider, (int)MessageProvider.ERR_NoSourceFile, file.Path, e.Message); } diagnostics.Add(diagnosticInfo); normalizedFilePath = null; return(null); } }
public AdditionalTextFile(CommandLineSourceFile sourceFile, CommonCompiler compiler) { if (compiler == null) { throw new ArgumentNullException(nameof(compiler)); } _sourceFile = sourceFile; _compiler = compiler; _diagnostics = SpecializedCollections.EmptyList<DiagnosticInfo>(); }
public AdditionalTextFile(CommandLineSourceFile sourceFile, CommonCompiler compiler) { if (compiler == null) { throw new ArgumentNullException(nameof(compiler)); } _sourceFile = sourceFile; _compiler = compiler; _diagnostics = SpecializedCollections.EmptyList <DiagnosticInfo>(); }
/// <summary> /// Reads content of a source file. /// </summary> /// <param name="file">Source file information.</param> /// <param name="diagnostics">Storage for diagnostics.</param> /// <param name="encoding">Encoding to use or 'null' for autodetect/default</param> /// <param name="normalizedFilePath">If given <paramref name="file"/> opens successfully, set to normalized absolute path of the file, null otherwise.</param> /// <returns>File content or null on failure.</returns> internal SourceText ReadFileContent(CommandLineSourceFile file, IList <DiagnosticInfo> diagnostics, Encoding encoding, out string normalizedFilePath) { try { using (var data = new FileStream(file.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { normalizedFilePath = data.Name; return(new EncodedStringText(data, encoding)); } } catch (Exception e) { diagnostics.Add(ToFileReadDiagostics(e, file)); normalizedFilePath = null; return(null); } }
/// <summary> /// Reads content of a source file. /// </summary> /// <param name="file">Source file information.</param> /// <param name="diagnostics">Storage for diagnostics.</param> /// <param name="normalizedFilePath">If given <paramref name="file"/> opens successfully, set to normalized absolute path of the file, null otherwise.</param> /// <returns>File content or null on failure.</returns> internal SourceText TryReadFileContent(CommandLineSourceFile file, IList <DiagnosticInfo> diagnostics, out string normalizedFilePath) { var filePath = file.Path; try { using (var data = OpenFileForReadWithSmallBufferOptimization(filePath)) { normalizedFilePath = data.Name; return(EncodedStringText.Create(data, Arguments.Encoding, Arguments.ChecksumAlgorithm, canBeEmbedded: EmbeddedSourcePaths.Contains(file.Path))); } } catch (Exception e) { diagnostics.Add(ToFileReadDiagnostics(this.MessageProvider, e, filePath)); normalizedFilePath = null; return(null); } }
private DiagnosticInfo ToFileReadDiagnostics(Exception e, CommandLineSourceFile file) { DiagnosticInfo diagnosticInfo; if (e is FileNotFoundException || e.GetType().Name == "DirectoryNotFoundException") { diagnosticInfo = new DiagnosticInfo(MessageProvider, MessageProvider.ERR_FileNotFound, file.Path); } else if (e is InvalidDataException) { diagnosticInfo = new DiagnosticInfo(MessageProvider, MessageProvider.ERR_BinaryFile, file.Path); } else { diagnosticInfo = new DiagnosticInfo(MessageProvider, MessageProvider.ERR_NoSourceFile, file.Path, e.Message); } return(diagnosticInfo); }
/// <summary> /// Reads content of a source file. /// </summary> /// <param name="file">Source file information.</param> /// <param name="diagnostics">Storage for diagnostics.</param> /// <param name="encoding">Encoding to use or 'null' for autodetect/default</param> /// <param name="checksumAlgorithm">Hash algorithm used to calculate file checksum.</param> /// <param name="normalizedFilePath">If given <paramref name="file"/> opens successfully, set to normalized absolute path of the file, null otherwise.</param> /// <returns>File content or null on failure.</returns> internal SourceText ReadFileContent(CommandLineSourceFile file, IList <DiagnosticInfo> diagnostics, Encoding encoding, SourceHashAlgorithm checksumAlgorithm, out string normalizedFilePath) { try { // PERF: Using a very small buffer size for the FileStream opens up an optimization within EncodedStringText where // we read the entire FileStream into a byte array in one shot. For files that are actually smaller than the buffer // size, FileStream.Read still allocates the internal buffer. using (var data = PortableShim.FileStream.Create(file.Path, PortableShim.FileMode.Open, PortableShim.FileAccess.Read, PortableShim.FileShare.ReadWrite, bufferSize: 1, options: PortableShim.FileOptions.None)) { normalizedFilePath = (string)PortableShim.FileStream.Name.GetValue(data); return(EncodedStringText.Create(data, encoding, checksumAlgorithm)); } } catch (Exception e) { diagnostics.Add(ToFileReadDiagnostics(e, file)); normalizedFilePath = null; return(null); } }
/// <summary> /// Reads content of a source file. /// </summary> /// <param name="file">Source file information.</param> /// <param name="diagnostics">Storage for diagnostics.</param> /// <param name="normalizedFilePath">If given <paramref name="file"/> opens successfully, set to normalized absolute path of the file, null otherwise.</param> /// <returns>File content or null on failure.</returns> internal SourceText ReadFileContent(CommandLineSourceFile file, IList <DiagnosticInfo> diagnostics, out string normalizedFilePath) { var filePath = file.Path; try { // PERF: Using a very small buffer size for the FileStream opens up an optimization within EncodedStringText where // we read the entire FileStream into a byte array in one shot. For files that are actually smaller than the buffer // size, FileStream.Read still allocates the internal buffer. using (var data = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, bufferSize: 1, options: FileOptions.None)) { normalizedFilePath = data.Name; return(ReflEncodedStringText.Create(data, Arguments.Encoding, Arguments.ChecksumAlgorithm, false)); } } catch (Exception e) { diagnostics.Add(ToFileReadDiagnostics(this.MessageProvider, e, filePath)); normalizedFilePath = null; return(null); } }
/// <summary> /// Reads content of a source file. /// </summary> /// <param name="file">Source file information.</param> /// <param name="diagnostics">Storage for diagnostics.</param> /// <param name="encoding">Encoding to use or 'null' for autodetect/default</param> /// <param name="checksumAlgorithm">Hash algorithm used to calculate file checksum.</param> /// <returns>File content or null on failure.</returns> internal SourceText ReadFileContent(CommandLineSourceFile file, IList <DiagnosticInfo> diagnostics, Encoding encoding, SourceHashAlgorithm checksumAlgorithm) { string discarded; return(ReadFileContent(file, diagnostics, encoding, checksumAlgorithm, out discarded)); }
/// <summary> /// Reads content of a source file. /// </summary> /// <param name="file">Source file information.</param> /// <param name="diagnostics">Storage for diagnostics.</param> /// <param name="normalizedFilePath">If given <paramref name="file"/> opens successfully, set to normalized absolute path of the file, null otherwise.</param> /// <returns>File content or null on failure.</returns> internal SourceText ReadFileContent(CommandLineSourceFile file, IList<DiagnosticInfo> diagnostics, out string normalizedFilePath) { var filePath = file.Path; try { // PERF: Using a very small buffer size for the FileStream opens up an optimization within EncodedStringText where // we read the entire FileStream into a byte array in one shot. For files that are actually smaller than the buffer // size, FileStream.Read still allocates the internal buffer. using (var data = PortableShim.FileStream.Create(filePath, PortableShim.FileMode.Open, PortableShim.FileAccess.Read, PortableShim.FileShare.ReadWrite, bufferSize: 1, options: PortableShim.FileOptions.None)) { normalizedFilePath = (string)PortableShim.FileStream.Name.GetValue(data); return EncodedStringText.Create(data, Arguments.Encoding, Arguments.ChecksumAlgorithm); } } catch (Exception e) { diagnostics.Add(ToFileReadDiagnostics(this.MessageProvider, e, filePath)); normalizedFilePath = null; return null; } }
/// <summary> /// Reads content of a source file. /// </summary> /// <param name="file">Source file information.</param> /// <param name="diagnostics">Storage for diagnostics.</param> /// <returns>File content or null on failure.</returns> internal SourceText ReadFileContent(CommandLineSourceFile file, IList<DiagnosticInfo> diagnostics) { string discarded; return ReadFileContent(file, diagnostics, out discarded); }
/// <summary> /// Reads content of a source file. /// </summary> /// <param name="file">Source file information.</param> /// <param name="diagnostics">Storage for diagnostics.</param> /// <param name="encoding">Encoding to use or 'null' for autodetect/default</param> /// <param name="checksumAlgorithm">Hash algorithm used to calculate file checksum.</param> /// <param name="normalizedFilePath">If given <paramref name="file"/> opens successfully, set to normalized absolute path of the file, null otherwise.</param> /// <returns>File content or null on failure.</returns> internal SourceText ReadFileContent(CommandLineSourceFile file, IList<DiagnosticInfo> diagnostics, Encoding encoding, SourceHashAlgorithm checksumAlgorithm, out string normalizedFilePath) { var filePath = file.Path; try { return ReadFileContentHelper(filePath, encoding, checksumAlgorithm, out normalizedFilePath); } catch (Exception e) { diagnostics.Add(ToFileReadDiagnostics(this.MessageProvider, e, filePath)); normalizedFilePath = null; return null; } }
/// <summary> /// Reads content of a source file. /// </summary> /// <param name="file">Source file information.</param> /// <param name="diagnostics">Storage for diagnostics.</param> /// <param name="encoding">Encoding to use or 'null' for autodetect/default</param> /// <param name="checksumAlgorithm">Hash algorithm used to calculate file checksum.</param> /// <returns>File content or null on failure.</returns> internal SourceText ReadFileContent(CommandLineSourceFile file, IList<DiagnosticInfo> diagnostics, Encoding encoding, SourceHashAlgorithm checksumAlgorithm) { string discarded; return ReadFileContent(file, diagnostics, encoding, checksumAlgorithm, out discarded); }
/// <summary> /// Reads content of a source file. /// </summary> /// <param name="file">Source file information.</param> /// <param name="diagnostics">Storage for diagnostics.</param> /// <param name="encoding">Encoding to use or 'null' for autodetect/default</param> /// <param name="checksumAlgorithm">Hash algorithm used to calculate file checksum.</param> /// <param name="normalizedFilePath">If given <paramref name="file"/> opens successfully, set to normalized absolute path of the file, null otherwise.</param> /// <returns>File content or null on failure.</returns> internal SourceText ReadFileContent(CommandLineSourceFile file, IList<DiagnosticInfo> diagnostics, Encoding encoding, SourceHashAlgorithm checksumAlgorithm, out string normalizedFilePath) { try { // PERF: Using a very small buffer size for the FileStream opens up an optimization within EncodedStringText where // we read the entire FileStream into a byte array in one shot. For files that are actually smaller than the buffer // size, FileStream.Read still allocates the internal buffer. using (var data = new FileStream(file.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, bufferSize: 1)) { normalizedFilePath = data.Name; return EncodedStringText.Create(data, encoding, checksumAlgorithm); } } catch (Exception e) { diagnostics.Add(ToFileReadDiagnostics(e, file)); normalizedFilePath = null; return null; } }
private DiagnosticInfo ToFileReadDiagnostics(Exception e, CommandLineSourceFile file) { DiagnosticInfo diagnosticInfo; if (e is FileNotFoundException || e.GetType().Name == "DirectoryNotFoundException") { diagnosticInfo = new DiagnosticInfo(MessageProvider, MessageProvider.ERR_FileNotFound, file.Path); } else if (e is InvalidDataException) { diagnosticInfo = new DiagnosticInfo(MessageProvider, MessageProvider.ERR_BinaryFile, file.Path); } else { diagnosticInfo = new DiagnosticInfo(MessageProvider, MessageProvider.ERR_NoSourceFile, file.Path, e.Message); } return diagnosticInfo; }
internal async Task<ValueTuple<SourceText, string>> ReadFileContentAsync(CommandLineSourceFile file, IList<DiagnosticInfo> diagnostics, Encoding encoding) { try { using (var data = new FileStream(file.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, bufferSize:4096, useAsync: true)) { MemoryStream stream = memoryStreamPool.Allocate(); stream.SetLength(0); await data.CopyToAsync(stream).ConfigureAwait(false); var normalizedFilePath = data.Name; var text = new EncodedStringText(stream, encoding); memoryStreamPool.Free(stream); return ValueTuple.Create<SourceText, string>(text, normalizedFilePath); } } catch (Exception e) { diagnostics.Add(ToFileReadDiagostics(e, file)); return default(ValueTuple<SourceText, string>); } }
/// <summary> /// Reads content of a source file. /// </summary> /// <param name="file">Source file information.</param> /// <param name="diagnostics">Storage for diagnostics.</param> /// <param name="encoding">Encoding to use or 'null' for autodetect/default</param> /// <param name="normalizedFilePath">If given <paramref name="file"/> opens successfully, set to normalized absolute path of the file, null otherwise.</param> /// <returns>File content or null on failure.</returns> internal SourceText ReadFileContent(CommandLineSourceFile file, IList<DiagnosticInfo> diagnostics, Encoding encoding, out string normalizedFilePath) { try { using (var data = new FileStream(file.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { normalizedFilePath = data.Name; return new EncodedStringText(data, encoding); } } catch (Exception e) { diagnostics.Add(ToFileReadDiagostics(e, file)); normalizedFilePath = null; return null; } }
/// <summary> /// Reads content of a source file. /// </summary> /// <param name="file">Source file information.</param> /// <param name="diagnostics">Storage for diagnostics.</param> /// <returns>File content or null on failure.</returns> internal SourceText ReadFileContent(CommandLineSourceFile file, IList <DiagnosticInfo> diagnostics) { string discarded; return(ReadFileContent(file, diagnostics, out discarded)); }
/// <summary> /// Reads content of a source file. /// </summary> /// <param name="file">Source file information.</param> /// <param name="diagnostics">Storage for diagnostics.</param> /// <param name="normalizedFilePath">If given <paramref name="file"/> opens successfully, set to normalized absolute path of the file, null otherwise.</param> /// <returns>File content or null on failure.</returns> internal SourceText TryReadFileContent(CommandLineSourceFile file, IList<DiagnosticInfo> diagnostics, out string normalizedFilePath) { var filePath = file.Path; try { using (var data = OpenFileForReadWithSmallBufferOptimization(filePath)) { normalizedFilePath = data.Name; return EncodedStringText.Create(data, Arguments.Encoding, Arguments.ChecksumAlgorithm, canBeEmbedded: EmbeddedSourcePaths.Contains(file.Path)); } } catch (Exception e) { diagnostics.Add(ToFileReadDiagnostics(this.MessageProvider, e, filePath)); normalizedFilePath = null; return null; } }