Beispiel #1
0
 /// <summary>
 /// Serializes a given value to the specified stream.
 /// </summary>
 /// <param name="stream">The stream to serialize to.</param>
 /// <param name="value">The value to serialize.</param>
 /// <param name="options">The options. Use <c>null</c> to use default options.</param>
 /// <param name="cancellationToken">A cancellation token.</param>
 public static void Serialize <T>(Stream stream, T value, MessagePackSerializerOptions options = null, CancellationToken cancellationToken = default)
 {
     cancellationToken.ThrowIfCancellationRequested();
     using (SequencePool.Rental sequenceRental = ReusableSequenceWithMinSize.Rent())
     {
         Serialize <T>(sequenceRental.Value, value, options, cancellationToken);
         foreach (ReadOnlyMemory <byte> segment in sequenceRental.Value.AsReadOnlySequence)
         {
             cancellationToken.ThrowIfCancellationRequested();
             stream.Write(segment.Span);
         }
     }
 }
Beispiel #2
0
 internal static byte[] GetWriterBytes <TArg>(TArg arg, GetWriterBytesAction <TArg> action, SequencePool pool)
 {
     using (var sequenceRental = pool.Rent())
     {
         var writer = new MessagePackWriter(sequenceRental.Value);
         action(ref writer, arg);
         writer.Flush();
         return(sequenceRental.Value.AsReadOnlySequence.ToArray());
     }
 }
Beispiel #3
0
 /// <summary>
 /// Serializes a given value to the specified stream.
 /// </summary>
 /// <param name="stream">The stream to serialize to.</param>
 /// <param name="value">The value to serialize.</param>
 /// <param name="options">The options. Use <c>null</c> to use default options.</param>
 public static void Serialize <T>(Stream stream, T value, MessagePackSerializerOptions options = null)
 {
     using (SequencePool.Rental sequenceRental = ReusableSequenceWithMinSize.Rent())
     {
         Serialize <T>(sequenceRental.Value, value, options);
         foreach (ReadOnlyMemory <byte> segment in sequenceRental.Value.AsReadOnlySequence)
         {
             stream.Write(segment.Span);
         }
     }
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="MessagePackStreamReader"/> class.
        /// </summary>
        /// <param name="stream">The stream to read from.</param>
        /// <param name="leaveOpen">If true, leaves the stream open after this <see cref="MessagePackStreamReader"/> is disposed; otherwise, false.</param>
        /// <param name="sequencePool">The pool to rent a <see cref="Sequence{T}"/> object from.</param>
        public MessagePackStreamReader(Stream stream, bool leaveOpen, SequencePool sequencePool)
        {
            if (sequencePool == null)
            {
                throw new ArgumentNullException(nameof(sequencePool));
            }

            this.stream         = stream ?? throw new ArgumentNullException(nameof(stream));
            this.leaveOpen      = leaveOpen;
            this.sequenceRental = sequencePool.Rent();
        }
 private void MigrateToSequence()
 {
     if (this._sequencePool != null)
     {
         // We were writing to our private scratch memory, so we have to copy it into the actual writer.
         _rental = _sequencePool.Rent();
         _output = _rental.Value;
         var realSpan = _output.GetSpan(_buffered);
         _segment.AsSpan(0, _buffered).CopyTo(realSpan);
         _sequencePool = null;
     }
 }
        /// <summary>
        /// Serializes a given value to the specified stream.
        /// </summary>
        /// <param name="stream">The stream to serialize to.</param>
        /// <param name="value">The value to serialize.</param>
        /// <param name="options">The options. Use <c>null</c> to use default options.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <exception cref="MessagePackSerializationException">Thrown when any error occurs during serialization.</exception>
        public static void Serialize <T>(Stream stream, T value, MessagePackSerializerOptions options = null, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (SequencePool.Rental sequenceRental = ReusableSequenceWithMinSize.Rent())
            {
                Serialize <T>(sequenceRental.Value, value, options, cancellationToken);

                try
                {
                    foreach (ReadOnlyMemory <byte> segment in sequenceRental.Value.AsReadOnlySequence)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        stream.Write(segment.Span);
                    }
                }
                catch (Exception ex)
                {
                    throw new MessagePackSerializationException("Error occurred while writing the serialized data to the stream.", ex);
                }
            }
        }