protected override void ApplyDocumentTextChanged(DocumentId documentId, SourceText text) { var document = this.CurrentSolution.GetDocument(documentId); if (document != null) { try { using (var writer = new StreamWriter(document.FilePath, append: false, encoding: text.Encoding ?? s_utf8WithoutBom)) { text.Write(writer); } } catch (IOException e) { this.OnWorkspaceFailed(new DocumentDiagnostic(WorkspaceDiagnosticKind.Failure, e.Message, documentId)); } catch (UnauthorizedAccessException e) { this.OnWorkspaceFailed(new DocumentDiagnostic(WorkspaceDiagnosticKind.Failure, e.Message, documentId)); } this.OnDocumentTextChanged(documentId, text, PreservationMode.PreserveValue); } }
private void SaveDocumentText(DocumentId id, string fullPath, SourceText newText, Encoding encoding) { try { using (ExceptionHelpers.SuppressFailFast()) { var dir = Path.GetDirectoryName(fullPath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } Debug.Assert(encoding != null); using (var writer = new StreamWriter(fullPath, append: false, encoding: encoding)) { newText.Write(writer); } } } catch (IOException exception) { this.OnWorkspaceFailed(new DocumentDiagnostic(WorkspaceDiagnosticKind.Failure, exception.Message, id)); } }
public void WriteText(SourceText text, CancellationToken cancellationToken) { if (_memoryMappedInfo != null) { throw new InvalidOperationException(); } using (Logger.LogBlock(FunctionId.TemporaryStorageServiceFactory_WriteText, cancellationToken)) { _encoding = text.Encoding; // the method we use to get text out of SourceText uses Unicode (2bytes per char). var size = Encoding.Unicode.GetMaxByteCount(text.Length); _memoryMappedInfo = _service._memoryMappedFileManager.CreateViewInfo(size); // Write the source text out as Unicode. We expect that to be cheap. using (var stream = _memoryMappedInfo.CreateWritableStream()) { using (var writer = new StreamWriter(stream, Encoding.Unicode)) { text.Write(writer, cancellationToken); } } } }
private static ImmutableArray<byte> CreateBlob(SourceText text) { Debug.Assert(text != null); Debug.Assert(text.CanBeEmbedded); Debug.Assert(text.Encoding != null); Debug.Assert(text.PrecomputedEmbeddedTextBlob.IsDefault); int maxByteCount; try { maxByteCount = text.Encoding.GetMaxByteCount(text.Length); } catch (ArgumentOutOfRangeException) { // Encoding does not provide a way to predict that max byte count would not // fit in Int32 and we must therefore catch ArgumentOutOfRange to handle that // case. maxByteCount = int.MaxValue; } using (var builder = BlobBuildingStream.GetInstance()) { if (maxByteCount < CompressionThreshold) { builder.WriteInt32(0); using (var writer = new StreamWriter(builder, text.Encoding, bufferSize: Math.Max(1, text.Length), leaveOpen: true)) { text.Write(writer); } } else { Blob reserved = builder.ReserveBytes(4); using (var deflater = new CountingDeflateStream(builder, CompressionLevel.Optimal, leaveOpen: true)) { using (var writer = new StreamWriter(deflater, text.Encoding, bufferSize: 1024, leaveOpen: true)) { text.Write(writer); } new BlobWriter(reserved).WriteInt32(deflater.BytesWritten); } } return builder.ToImmutableArray(); } }
private void SaveDocumentText(DocumentId id, string fullPath, SourceText newText) { try { var dir = Path.GetDirectoryName(fullPath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } using (var writer = new StreamWriter(fullPath)) { newText.Write(writer); } } catch (System.IO.IOException exception) { this.OnWorkspaceFailed(new DocumentDiagnostic(WorkspaceDiagnosticKind.FileAccessFailure, exception.Message, id)); } }
public void WriteText(SourceText text, CancellationToken cancellationToken) { if (memoryMappedInfo != null) { throw new InvalidOperationException(); } using (Logger.LogBlock(FeatureId.TemporaryStorage, FunctionId.Host_TemporaryStorageServiceFactory_WriteText, cancellationToken)) { var size = Encoding.Unicode.GetMaxByteCount(text.Length); memoryMappedInfo = service.memoryMappedFileManager.CreateViewInfo(size); using (var stream = memoryMappedInfo.CreateWritableStream()) { // PERF: Don't call text.Write(writer) directly since it can cause multiple large string // allocations from String.Substring. Instead use one of our pooled char[] buffers. using (var writer = new StreamWriter(stream, Encoding.Unicode)) { text.Write(writer, cancellationToken); } } } }
public void WriteText(SourceText text, CancellationToken cancellationToken = default(CancellationToken)) { lock (writeTextLocker) { if (fileName == null) this.fileName = Path.GetTempFileName (); string tmpPath = Path.Combine (Path.GetDirectoryName (fileName), ".#" + Path.GetFileName (fileName)); encoding = text.Encoding ?? Encoding.Default; using (var writer = new StreamWriter (tmpPath, false, encoding)) text.Write (writer, cancellationToken); sourceText = new WeakReference<SourceText>(text); FileService.SystemRename (tmpPath, fileName); } }