// Independent LF framing example and executable regression checks; no project source is reproduced.
using System.Text;
/// Extracts LF-delimited byte payloads using fixed-capacity candidate storage.
/// Single-consumer example. Returned frames allocate; input errors terminate this instance.
public sealed class BoundedLfFramer
{
private readonly byte[] _candidate;
private int _length;
private bool _stopped;
/// Allocates storage for a positive payload limit, excluding LF; otherwise throws.
public BoundedLfFramer(int maximumPayloadLength)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(maximumPayloadLength);
_candidate = new byte[maximumPayloadLength];
}
/// Returns complete payload copies and retains incomplete input; throws on excess size or reuse.
public IReadOnlyList Append(ReadOnlySpan input)
{
ObjectDisposedException.ThrowIf(_stopped, this);
var frames = new List();
foreach (byte value in input)
{
if (value == (byte)'\n')
{
frames.Add(_candidate.AsSpan(0, _length).ToArray());
_length = 0;
continue;
}
if (_length == _candidate.Length)
Reject("Payload exceeds the configured limit.");
_candidate[_length++] = value;
}
return frames;
}
/// Ends the stream, rejecting incomplete input or reuse with an exception.
public void Complete()
{
ObjectDisposedException.ThrowIf(_stopped, this);
if (_length != 0)
Reject("Stream ended with an incomplete payload.");
_stopped = true;
}
/// Marks this instance unusable and throws an input error with the supplied reason.
private void Reject(string reason)
{
_stopped = true;
throw new InvalidDataException(reason);
}
}
/// Runs dependency-free framing checks, returning zero only when every check passes.
internal static class Program
{
/// Executes the example checks; a failed assertion terminates with an exception.
private static void Main()
{
PreservesFramesAcrossEveryPartition();
AcceptsBoundaryAndEmptyPayloads();
RejectsOversizedPayloads();
RejectsIncompleteEofAndReuse();
RejectsInvalidLimits();
Console.WriteLine("Passed: 256 input partitions and boundary, empty-input, oversize, EOF and lifecycle checks.");
}
/// Checks all 256 possible partitions of the nine-byte sample against identical ordered output.
private static void PreservesFramesAcrossEveryPartition()
{
// Arrange
byte[] stream = Encoding.ASCII.GetBytes("RED\nBLUE\n");
for (int partition = 0; partition < 1 << (stream.Length - 1); partition++)
{
var framer = new BoundedLfFramer(4);
var actual = new List();
int start = 0;
// Act
for (int end = 1; end <= stream.Length; end++)
{
if (end != stream.Length && (partition & (1 << (end - 1))) == 0)
continue;
actual.AddRange(framer.Append(stream.AsSpan(start, end - start)).Select(Encoding.ASCII.GetString));
start = end;
}
framer.Complete();
// Assert
Require(actual.SequenceEqual(new[] { "RED", "BLUE" }), "Partition changed frame output.");
}
}
/// Checks exact-limit, empty-frame and zero-length-input semantics.
private static void AcceptsBoundaryAndEmptyPayloads()
{
// Arrange
var framer = new BoundedLfFramer(4);
// Act
var empty = framer.Append(ReadOnlySpan.Empty);
var frames = framer.Append("ABCD\n\n"u8);
framer.Complete();
// Assert
Require(empty.Count == 0, "Empty input emitted a frame.");
Require(frames.Count == 2 && frames[0].AsSpan().SequenceEqual("ABCD"u8) && frames[1].Length == 0,
"Exact-limit or empty-frame handling failed.");
}
/// Checks oversized payloads with a delimiter, without one, and split across chunks.
private static void RejectsOversizedPayloads()
{
// Arrange
var complete = new BoundedLfFramer(4);
var incomplete = new BoundedLfFramer(4);
var fragmented = new BoundedLfFramer(4);
fragmented.Append("ABCD"u8);
// Act / Assert
Expect(() => complete.Append("ABCDE\n"u8));
Expect(() => incomplete.Append("ABCDE"u8));
Expect(() => fragmented.Append("E\n"u8));
}
/// Checks rejection of an incomplete EOF and all subsequent use of closed instances.
private static void RejectsIncompleteEofAndReuse()
{
// Arrange
var incomplete = new BoundedLfFramer(4);
incomplete.Append("ABC"u8);
var completed = new BoundedLfFramer(4);
// Act / Assert
Expect(incomplete.Complete);
Expect(() => incomplete.Append("\n"u8));
completed.Complete();
Expect(completed.Complete);
}
/// Checks that zero and negative candidate capacities are rejected.
private static void RejectsInvalidLimits()
{
// Arrange / Act / Assert
Expect(() => new BoundedLfFramer(0));
Expect(() => new BoundedLfFramer(-1));
}
/// Throws when the supplied condition fails, identifying the assertion.
private static void Require(bool condition, string reason)
{
if (!condition)
throw new InvalidOperationException(reason);
}
/// Verifies that an operation throws the expected exception type; otherwise throws.
private static void Expect(Action operation) where TException : Exception
{
try
{
operation();
}
catch (TException)
{
return;
}
throw new InvalidOperationException($"Expected {typeof(TException).Name}.");
}
}