diff options
Diffstat (limited to 'lib/Utils/src/IO')
-rw-r--r-- | lib/Utils/src/IO/ArrayPoolStreamBuffer.cs | 26 | ||||
-rw-r--r-- | lib/Utils/src/IO/VnStreamWriter.cs | 36 |
2 files changed, 23 insertions, 39 deletions
diff --git a/lib/Utils/src/IO/ArrayPoolStreamBuffer.cs b/lib/Utils/src/IO/ArrayPoolStreamBuffer.cs index b62412f..a943a5b 100644 --- a/lib/Utils/src/IO/ArrayPoolStreamBuffer.cs +++ b/lib/Utils/src/IO/ArrayPoolStreamBuffer.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Utils @@ -28,7 +28,13 @@ using System.Buffers; namespace VNLib.Utils.IO { - internal class ArrayPoolStreamBuffer<T> : ISlindingWindowBuffer<T> + /// <summary> + /// Creates a new <see cref="ArrayPoolStreamBuffer{T}"/> from the + /// given array instance and <see cref="ArrayPool{T}"/> it came from. + /// </summary> + /// <param name="array">The rented array to use</param> + /// <param name="pool">The pool to return the array to when completed</param> + internal class ArrayPoolStreamBuffer<T>(T[] array, ArrayPool<T> pool) : ISlindingWindowBuffer<T> { /// <summary> /// The shared <see cref="IStreamBufferFactory{T}"/> instance to allocate buffers @@ -36,20 +42,8 @@ namespace VNLib.Utils.IO /// </summary> public static IStreamBufferFactory<T> Shared { get; } = new DefaultFactory(); - private readonly ArrayPool<T> _pool; - private T[] _buffer; - - /// <summary> - /// Creates a new <see cref="ArrayPoolStreamBuffer{T}"/> from the - /// given array instance and <see cref="ArrayPool{T}"/> it came from. - /// </summary> - /// <param name="array">The rented array to use</param> - /// <param name="pool">The pool to return the array to when completed</param> - public ArrayPoolStreamBuffer(T[] array, ArrayPool<T> pool) - { - _pool = pool; - _buffer = array; - } + private readonly ArrayPool<T> _pool = pool; + private T[] _buffer = array; ///<inheritdoc/> public int WindowStartPos { get; set; } diff --git a/lib/Utils/src/IO/VnStreamWriter.cs b/lib/Utils/src/IO/VnStreamWriter.cs index ddebc07..7d43f48 100644 --- a/lib/Utils/src/IO/VnStreamWriter.cs +++ b/lib/Utils/src/IO/VnStreamWriter.cs @@ -41,21 +41,29 @@ namespace VNLib.Utils.IO /// Provides a memory optimized <see cref="TextWriter"/> implementation. Optimized for writing /// to network streams /// </summary> - public class VnStreamWriter : TextWriter + /// <remarks> + /// Creates a new <see cref="VnStreamWriter"/> that writes encoded data to the base stream + /// and uses the specified buffer. + /// </remarks> + /// <param name="baseStream">The underlying stream to write data to</param> + /// <param name="encoding">The <see cref="Encoding"/> to use when writing to the stream</param> + /// <param name="buffer">The internal <see cref="ISlindingWindowBuffer{T}"/> to use</param> + /// <exception cref="ArgumentNullException"></exception> + public class VnStreamWriter(Stream baseStream, Encoding encoding, ISlindingWindowBuffer<byte> buffer) : TextWriter { - private readonly Encoder Enc; + private readonly Encoder Enc = encoding.GetEncoder(); - private readonly ISlindingWindowBuffer<byte> _buffer; + private readonly ISlindingWindowBuffer<byte> _buffer = buffer ?? throw new ArgumentNullException(nameof(buffer)); private bool closed; /// <summary> /// Gets the underlying stream that interfaces with the backing store /// </summary> - public virtual Stream BaseStream { get; } + public virtual Stream BaseStream { get; } = baseStream ?? throw new ArgumentNullException(nameof(buffer)); ///<inheritdoc/> - public override Encoding Encoding { get; } + public override Encoding Encoding { get; } = encoding ?? throw new ArgumentNullException(nameof(encoding)); /// <summary> /// Line termination to use when writing lines to the output @@ -97,24 +105,6 @@ namespace VNLib.Utils.IO { } - /// <summary> - /// Creates a new <see cref="VnStreamWriter"/> that writes encoded data to the base stream - /// and uses the specified buffer. - /// </summary> - /// <param name="baseStream">The underlying stream to write data to</param> - /// <param name="encoding">The <see cref="Encoding"/> to use when writing to the stream</param> - /// <param name="buffer">The internal <see cref="ISlindingWindowBuffer{T}"/> to use</param> - /// <exception cref="ArgumentNullException"></exception> - public VnStreamWriter(Stream baseStream, Encoding encoding, ISlindingWindowBuffer<byte> buffer) - { - BaseStream = baseStream ?? throw new ArgumentNullException(nameof(buffer)); - Encoding = encoding ?? throw new ArgumentNullException(nameof(encoding)); - _buffer = buffer ?? throw new ArgumentNullException(nameof(buffer)); - - //Get an encoder - Enc = encoding.GetEncoder(); - } - ///<inheritdoc/> public void Write(byte value) { |