Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/libraries/System.Formats.Cbor/ref/System.Formats.Cbor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ public enum CborTag : ulong
}
public partial class CborWriter
{
public CborWriter(System.Formats.Cbor.CborConformanceMode conformanceMode = System.Formats.Cbor.CborConformanceMode.Strict, bool convertIndefiniteLengthEncodings = false, bool allowMultipleRootLevelValues = false) { }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public CborWriter(System.Formats.Cbor.CborConformanceMode conformanceMode, bool convertIndefiniteLengthEncodings, bool allowMultipleRootLevelValues) { }
public CborWriter(System.Formats.Cbor.CborConformanceMode conformanceMode = System.Formats.Cbor.CborConformanceMode.Strict, bool convertIndefiniteLengthEncodings = false, bool allowMultipleRootLevelValues = false, int initialCapacity = -1) { }
public bool AllowMultipleRootLevelValues { get { throw null; } }
public int BytesWritten { get { throw null; } }
public System.Formats.Cbor.CborConformanceMode ConformanceMode { get { throw null; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Buffers;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;

Expand All @@ -11,9 +12,10 @@ namespace System.Formats.Cbor
/// <summary>A writer for Concise Binary Object Representation (CBOR) encoded data.</summary>
public partial class CborWriter
{
private const int DefaultCapacitySentinel = -1;
private static readonly ArrayPool<byte> s_bufferPool = ArrayPool<byte>.Create();

private byte[] _buffer = null!;
private byte[] _buffer;
private int _offset;

private Stack<StackFrame>? _nestedDataItems;
Expand Down Expand Up @@ -60,14 +62,42 @@ public partial class CborWriter
/// <param name="convertIndefiniteLengthEncodings"><see langword="true" /> to enable automatically converting indefinite-length encodings into definite-length equivalents and allow use of indefinite-length write APIs in conformance modes that otherwise do not permit it; otherwise, <see langword="false" />.</param>
/// <param name="allowMultipleRootLevelValues"><see langword="true" /> to allow multiple root-level values to be written by the writer; otherwise, <see langword="false" />.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="conformanceMode" /> is not a defined <see cref="CborConformanceMode" />.</exception>
public CborWriter(CborConformanceMode conformanceMode = CborConformanceMode.Strict, bool convertIndefiniteLengthEncodings = false, bool allowMultipleRootLevelValues = false)
[EditorBrowsable(EditorBrowsableState.Never)]
public CborWriter(CborConformanceMode conformanceMode, bool convertIndefiniteLengthEncodings, bool allowMultipleRootLevelValues)
: this(conformanceMode, convertIndefiniteLengthEncodings, allowMultipleRootLevelValues, DefaultCapacitySentinel)
{
}

/// <summary>Initializes a new instance of <see cref="CborWriter" /> class using the specified configuration.</summary>
/// <param name="conformanceMode">One of the enumeration values that specifies the guidance on the conformance checks performed on the encoded data.
/// Defaults to <see cref="CborConformanceMode.Strict" /> conformance mode.</param>
/// <param name="convertIndefiniteLengthEncodings"><see langword="true" /> to enable automatically converting indefinite-length encodings into definite-length equivalents and allow use of indefinite-length write APIs in conformance modes that otherwise do not permit it; otherwise, <see langword="false" />.</param>
/// <param name="allowMultipleRootLevelValues"><see langword="true" /> to allow multiple root-level values to be written by the writer; otherwise, <see langword="false" />.</param>
/// <param name="initialCapacity">The initial capacity of the underlying buffer. The value -1 can be used to use the default capacity.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// <para><paramref name="conformanceMode" /> is not a defined <see cref="CborConformanceMode" />.</para>
/// <para>-or-</para>
/// <para><paramref name="initialCapacity"/> is not zero, positive, or the default value indicator -1.</para>
/// </exception>
public CborWriter(
CborConformanceMode conformanceMode = CborConformanceMode.Strict,
bool convertIndefiniteLengthEncodings = false,
bool allowMultipleRootLevelValues = false,
int initialCapacity = DefaultCapacitySentinel)
{
CborConformanceModeHelpers.Validate(conformanceMode);

ConformanceMode = conformanceMode;
ConvertIndefiniteLengthEncodings = convertIndefiniteLengthEncodings;
AllowMultipleRootLevelValues = allowMultipleRootLevelValues;
_definiteLength = allowMultipleRootLevelValues ? null : (int?)1;

_buffer = initialCapacity switch
{
DefaultCapacitySentinel or 0 => Array.Empty<byte>(),
< -1 => throw new ArgumentOutOfRangeException(nameof(initialCapacity)),
_ => new byte[initialCapacity],
};
}

/// <summary>Resets the writer to have no data, without releasing resources.</summary>
Expand Down Expand Up @@ -205,7 +235,7 @@ private void EnsureWriteCapacity(int pendingCount)
throw new OverflowException();
}

if (_buffer is null || _buffer.Length - _offset < pendingCount)
if (_buffer.Length - _offset < pendingCount)
{
const int BlockSize = 1024;
int blocks = checked(_offset + pendingCount + (BlockSize - 1)) / BlockSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using Test.Cryptography;
using Xunit;
Expand Down Expand Up @@ -332,7 +333,43 @@ public static void WriteEncodedValue_ValidPayloadWithTrailingBytes_ShouldThrowAr
[InlineData((CborConformanceMode)(-1))]
public static void InvalidConformanceMode_ShouldThrowArgumentOutOfRangeException(CborConformanceMode mode)
{
Assert.Throws<ArgumentOutOfRangeException>(() => new CborWriter(conformanceMode: mode));
Assert.Throws<ArgumentOutOfRangeException>("conformanceMode", () => new CborWriter(conformanceMode: mode));
}

[Theory]
[InlineData(-2)]
[InlineData(int.MinValue)]
public static void InvalidInitialCapacity_ShouldThrowArgumentOutOfRangeException(int capacity)
{
Assert.Throws<ArgumentOutOfRangeException>("initialCapacity", () => new CborWriter(initialCapacity: capacity));
}

[Theory]
[InlineData(-1, 0)]
[InlineData(0, 0)]
[InlineData(1, 1)]
[InlineData(1023, 1023)]
public static void InitialCapacity_ShouldSetInitialBuffer(int capacity, int expectedBufferLength)
{
CborWriter writer = new CborWriter(initialCapacity: capacity);
byte[]? buffer = (byte[]?)typeof(CborWriter).GetField("_buffer", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(writer);

Assert.NotNull(buffer);
Assert.Equal(expectedBufferLength, buffer.Length);
}

[Theory]
[InlineData(0)]
[InlineData(-1)]
[InlineData(1)]
public static void Encode_InitialCapacity_Grows(int capacity)
{
CborWriter writer = new CborWriter(initialCapacity: capacity);
writer.WriteByteString((ReadOnlySpan<byte>)new byte[] { 1, 2, 3, 4, 5, 6 });
byte[] encoded = writer.Encode();

ReadOnlySpan<byte> expected = new byte[] { (2 << 5) | 6, 1, 2, 3, 4, 5, 6 };
AssertExtensions.SequenceEqual(expected, encoded);
}

public static IEnumerable<object[]> EncodedValueInputs => CborReaderTests.SampleCborValues.Select(x => new [] { x });
Expand Down