Example #1
0
        public ValueTask <ImmutableArray <(Checksum, object)> > GetAssetsAsync(
            int serviceId, ISet <Checksum> checksums, ISerializerService deserializerService, CancellationToken cancellationToken)
        {
            var results = new List <(Checksum, object)>();

            foreach (var checksum in checksums)
            {
                if (_map.TryGetValue(checksum, out var data))
                {
                    using var stream = new MemoryStream();
                    using (var writer = new ObjectWriter(stream, leaveOpen: true, cancellationToken))
                    {
                        _serializerService.Serialize(data, writer, cancellationToken);
                    }

                    stream.Position  = 0;
                    using var reader = ObjectReader.GetReader(stream, leaveOpen: true, cancellationToken);
                    var asset = deserializerService.Deserialize <object>(data.GetWellKnownSynchronizationKind(), reader, cancellationToken);
                    Contract.ThrowIfTrue(asset is null);
                    results.Add((checksum, asset));
                }
                else
                {
                    throw ExceptionUtilities.UnexpectedValue(checksum);
                }
            }

            return(ValueTaskFactory.FromResult(results.ToImmutableArray()));
        }
Example #2
0
        /// <inheritdoc/>
        public object?Read(Type type, BinaryReader reader)
        {
            var length = reader.ReadInt32();

            if (length == -1)
            {
                return(null !);
            }
            var et    = type.GetElementType();
            var r     = ObjectReader.GetReader(et);
            var array = Array.CreateInstance(et, length);

            for (var j = 0; j < length; j++)
            {
                var o = r.Read(et, reader);
                array.SetValue(o, j);
            }
            return(array);
        }