Ejemplo n.º 1
0
                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);

                        var buffer = SharedPools.CharArray.Allocate();
                        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))
                            {
                                for (int index = 0; index < text.Length; index += buffer.Length)
                                {
                                    cancellationToken.ThrowIfCancellationRequested();
                                    var count = Math.Min(buffer.Length, text.Length - index);
                                    text.CopyTo(index, buffer, 0, count);
                                    writer.Write(buffer, 0, count);
                                }
                            }
                        }

                        SharedPools.CharArray.Free(buffer);
                    }
                }
Ejemplo n.º 2
0
                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.CreateTemporaryStorage(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);
                            }
                        }
                    }
                }
Ejemplo n.º 3
0
                private async Task WriteStreamMaybeAsync(Stream stream, bool useAsync, CancellationToken cancellationToken)
                {
                    if (_memoryMappedInfo != null)
                    {
                        throw new InvalidOperationException(WorkspacesResources.Temporary_storage_cannot_be_written_more_than_once);
                    }

                    if (stream.Length == 0)
                    {
                        throw new ArgumentOutOfRangeException();
                    }

                    using (Logger.LogBlock(FunctionId.TemporaryStorageServiceFactory_WriteStream, cancellationToken))
                    {
                        var size = stream.Length;
                        _memoryMappedInfo = _service.CreateTemporaryStorage(size);
                        using (var viewStream = _memoryMappedInfo.CreateWritableStream())
                        {
                            var buffer = SharedPools.ByteArray.Allocate();
                            try
                            {
                                while (true)
                                {
                                    int count;
                                    if (useAsync)
                                    {
                                        count = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
                                    }
                                    else
                                    {
                                        count = stream.Read(buffer, 0, buffer.Length);
                                    }

                                    if (count == 0)
                                    {
                                        break;
                                    }

                                    viewStream.Write(buffer, 0, count);
                                }
                            }
                            finally
                            {
                                SharedPools.ByteArray.Free(buffer);
                            }
                        }
                    }
                }
                public void WriteText(SourceText text, CancellationToken cancellationToken)
                {
                    if (memoryMappedInfo != null)
                    {
                        throw new InvalidOperationException();
                    }

                    using (Logger.LogBlock(FunctionId.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);
                            }
                        }
                    }
                }
                private async Task WriteStreamMaybeAsync(Stream stream, bool useAsync, CancellationToken cancellationToken)
                {
                    if (_memoryMappedInfo != null)
                    {
                        throw new InvalidOperationException(WorkspacesResources.Temporary_storage_cannot_be_written_more_than_once);
                    }

                    if (stream.Length == 0)
                    {
                        throw new ArgumentOutOfRangeException();
                    }

                    using (Logger.LogBlock(FunctionId.TemporaryStorageServiceFactory_WriteStream, cancellationToken))
                    {
                        var size = stream.Length;
                        _memoryMappedInfo = _service._memoryMappedFileManager.CreateViewInfo(size);
                        using (var viewStream = _memoryMappedInfo.CreateWritableStream())
                        {
                            var buffer = SharedPools.ByteArray.Allocate();
                            try
                            {
                                while (true)
                                {
                                    int count;
                                    if (useAsync)
                                    {
                                        count = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
                                    }
                                    else
                                    {
                                        count = stream.Read(buffer, 0, buffer.Length);
                                    }

                                    if (count == 0)
                                    {
                                        break;
                                    }

                                    viewStream.Write(buffer, 0, count);
                                }
                            }
                            finally
                            {
                                SharedPools.ByteArray.Free(buffer);
                            }
                        }
                    }
                }
                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);
                            }
                        }
                    }
                }
                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);

                        var buffer = SharedPools.CharArray.Allocate();
                        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))
                            {
                                for (int index = 0; index < text.Length; index += buffer.Length)
                                {
                                    cancellationToken.ThrowIfCancellationRequested();
                                    var count = Math.Min(buffer.Length, text.Length - index);
                                    text.CopyTo(index, buffer, 0, count);
                                    writer.Write(buffer, 0, count);
                                }
                            }
                        }

                        SharedPools.CharArray.Free(buffer);
                    }
                }
                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);
                            }
                        }
                    }
                }