Пример #1
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);
                            }
                        }
                    }
                }
Пример #2
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);
                            }
                        }
                    }
                }