private static async Task WriteToStream(IFastSerializable serializable, Stream targetStream, CancellationToken token)
        {
            // FastSerialization requests the length of the stream before serializing to the stream.
            // If the stream is a response stream, requesting the length or setting the position is
            // not supported. Create an intermediate buffer if testing the stream fails.
            // This can use a huge amount of memory if the IFastSerializable is very large.
            // CONSIDER: Update FastSerialization to not get the length or attempt to reset the position.
            bool useIntermediateStream = false;

            try
            {
                _ = targetStream.Length;
            }
            catch (NotSupportedException)
            {
                useIntermediateStream = true;
            }

            if (useIntermediateStream)
            {
                using var intermediateStream = new MemoryStream();

                var serializer = new Serializer(intermediateStream, serializable, leaveOpen: true);
                serializer.Close();

                intermediateStream.Position = 0;

                await intermediateStream.CopyToAsync(targetStream, 0x10000, token);
            }
            else
            {
                var serializer = new Serializer(targetStream, serializable, leaveOpen: true);
                serializer.Close();
            }
        }
        public static void ToObjectData(IFastSerializable obj, SerializationInfo info)
        {
            SerializationWriter writer = new SerializationWriter();

            obj.Serialize(writer);

            byte[] result = writer.ToArray();
            info.AddValue("fast$", Convert.ToBase64String(result));
        }
        public static void FromObjectData(IFastSerializable obj, SerializationInfo info)
        {
            string serial = info.GetString("fast$");

            byte[]              binary = Convert.FromBase64String(serial);
            MemoryStream        stream = new MemoryStream(binary);
            SerializationReader reader = new SerializationReader(stream);

            obj.Deserialize(reader);
        }
Beispiel #4
0
        private static Func <Stream, CancellationToken, Task> ConvertFastSerializeAction(Func <CancellationToken, Task <IFastSerializable> > action)
        {
            return(async(stream, token) =>
            {
                IFastSerializable fastSerializable = await action(token);

                // FastSerialization requests the length of the stream before serializing to the stream.
                // If the stream is a response stream, requesting the length or setting the position is
                // not supported. Create an intermediate buffer if testing the stream fails.
                // This can use a huge amount of memory if the IFastSerializable is very large.
                // CONSIDER: Update FastSerialization to not get the length or attempt to reset the position.
                bool useIntermediateStream = false;
                try
                {
                    _ = stream.Length;
                }
                catch (NotSupportedException)
                {
                    useIntermediateStream = true;
                }

                if (useIntermediateStream)
                {
                    using var intermediateStream = new MemoryStream();

                    var serializer = new Serializer(intermediateStream, fastSerializable, leaveOpen: true);
                    serializer.Close();

                    intermediateStream.Position = 0;

                    await intermediateStream.CopyToAsync(stream, 0x10000, token);
                }
                else
                {
                    var serializer = new Serializer(stream, fastSerializable, leaveOpen: true);
                    serializer.Close();
                }
            });
        }
        public static void ToObjectData(IFastSerializable obj, SerializationInfo info)
        {
            SerializationWriter writer = new SerializationWriter();

            obj.Serialize(writer);

            byte[] result = writer.ToArray();
            info.AddValue("fast$", Convert.ToBase64String(result));
        }
        public static void FromObjectData(IFastSerializable obj, SerializationInfo info)
        {
            string serial = info.GetString("fast$");

            byte[] binary = Convert.FromBase64String(serial);
            MemoryStream stream = new MemoryStream(binary);
            SerializationReader reader = new SerializationReader(stream);

            obj.Deserialize(reader);
        }