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> /// 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; } }
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> /// Gets a copy of these options with the <see cref="SequencePool"/> property set to a new value. /// </summary> /// <param name="pool">The new value for the <see cref="SequencePool"/> property.</param> /// <returns>The new instance.</returns> public MessagePackSerializerOptions WithPool(SequencePool pool) { if (pool is null) { throw new ArgumentNullException(nameof(pool)); } if (this.SequencePool == pool) { return(this); } var result = this.Clone(); result.SequencePool = pool; return(result); }
/// <summary> /// Initializes a new instance of the <see cref="MessagePackWriter"/> struct. /// </summary> internal MessagePackWriter(SequencePool sequencePool, byte[] array) { this.writer = new BufferWriter(sequencePool, array); this.OldSpec = false; }
internal Rental(SequencePool owner, Sequence <byte> value) { this.owner = owner; this.Value = value; }
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()); } }