/// <inheritdoc/> public void Dispose() { if (!this.leaveOpen) { this.stream.Dispose(); } this.sequenceRental.Dispose(); this.sequenceRental = default; }
internal BufferWriter(SequencePool sequencePool, byte[] array) { _buffered = 0; _bytesCommitted = 0; _sequencePool = sequencePool ?? throw new ArgumentNullException(nameof(sequencePool)); _rental = default; _output = null; _segment = new ArraySegment <byte>(array); _span = _segment.AsSpan(); }
/// <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> /// Disposes of managed and unmanaged resources. /// </summary> /// <param name="disposing"><see langword="true"/> if this instance is being disposed; <see langword="false"/> if it is being finalized.</param> protected virtual void Dispose(bool disposing) { if (disposing) { if (!this.leaveOpen) { this.stream.Dispose(); } this.sequenceRental.Dispose(); this.sequenceRental = default; } }
/// <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> /// <returns>A task that completes with the result of the async serialization operation.</returns> public static async Task SerializeAsync <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(); await stream.WriteAsync(segment, cancellationToken).ConfigureAwait(false); } } }
/// <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); } } }
public BufferWriter(IBufferWriter<byte> output) { _buffered = 0; _bytesCommitted = 0; _output = output ?? throw new ArgumentNullException(nameof(output)); _sequencePool = default; _rental = default; var memory = _output.GetMemory(); MemoryMarshal.TryGetArray(memory, out _segment); _span = memory.Span; }
/// <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> /// <returns>A task that completes with the result of the async serialization operation.</returns> /// <exception cref="MessagePackSerializationException">Thrown when any error occurs during serialization.</exception> public static async Task SerializeAsync <T>(Stream stream, T value, MessagePackSerializerOptions options = null, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); using (SequencePool.Rental sequenceRental = SequencePool.Shared.Rent()) { Serialize <T>(sequenceRental.Value, value, options, cancellationToken); try { foreach (ReadOnlyMemory <byte> segment in sequenceRental.Value.AsReadOnlySequence) { cancellationToken.ThrowIfCancellationRequested(); await stream.WriteAsync(segment, cancellationToken).ConfigureAwait(false); } } catch (Exception ex) { throw new MessagePackSerializationException("Error occurred while writing the serialized data to the stream.", ex); } } }
/// <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); } } }
/// <inheritdoc/> public void Dispose() { this.stream.Dispose(); this.sequenceRental.Dispose(); this.sequenceRental = default; }