コード例 #1
0
            public override async Task <ResponseDisposeHandling> ProcessResponse(JsonOperationContext context, HttpCache cache, HttpResponseMessage response, string url)
            {
                var    state  = new JsonParserState();
                Stream stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                using (context.GetMemoryBuffer(out var buffer))
                    using (var parser = new UnmanagedJsonParser(context, state, "attachments/receive"))
                        using (var builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, "attachments/list", parser, state))
                            using (var peepingTomStream = new PeepingTomStream(stream, context))
                            {
                                if (await UnmanagedJsonParserHelper.ReadAsync(peepingTomStream, parser, state, buffer).ConfigureAwait(false) == false)
                                {
                                    throw new Exception("cannot parse stream");
                                }

                                if (state.CurrentTokenType != JsonParserToken.StartObject)
                                {
                                    throw new Exception($"Expected token {nameof(JsonParserToken.StartObject)}, but got {nameof(state.CurrentTokenType)}.");
                                }

                                await UnmanagedJsonParserHelper.ReadObjectAsync(builder, peepingTomStream, parser, buffer).ConfigureAwait(false);

                                using (var data = builder.CreateReader())
                                {
                                    if (data.TryGetMember(nameof(AttachmentsMetadata), out object obj) && obj is BlittableJsonReaderArray bjra)
                                    {
                                        foreach (BlittableJsonReaderObject e in bjra)
                                        {
                                            var cur = JsonDeserializationClient.AttachmentDetails(e);
                                            AttachmentsMetadata.Add(cur);
                                        }
                                    }
                                }

                                var bufferSize = parser.BufferSize - parser.BufferOffset;
                                var copy       = ArrayPool <byte> .Shared.Rent(bufferSize);

                                var copyMemory = new Memory <byte>(copy);
                                buffer.Memory.Memory.Slice(parser.BufferOffset, bufferSize).CopyTo(copyMemory);

                                Result = Iterate(stream, copy, bufferSize).GetEnumerator();
                            }

                return(ResponseDisposeHandling.Manually);
            }
コード例 #2
0
 public override void SetResponse(JsonOperationContext context, BlittableJsonReaderObject response, bool fromCache)
 {
     Result = JsonDeserializationClient.AttachmentDetails(response);
 }