team-ljyzipelo

공개
63줄2.2 KB
1using Zipelo.GzipBoundaryProbe;
2
3// 검증과 정리 이후에만 시험 결과를 확정함. 파일 게시를 구현하지 않음.
4internal sealed class PendingValidation
5{
6 private bool used;
7 public int CommitCount { get; private set; }
8 public string? CommittedHash { get; private set; }
9 public BoundaryResult? CommittedBoundary { get; private set; }
10
11 public void Execute(ReadOnlyMemory<byte> input, ProbeBudget budget, bool selected,
12 CancellationToken token = default, int feedSize = ProbeBudget.ChunkSize,
13 Action? afterChunk = null, Action? beforeCommit = null, IRawInflater? inflater = null,
14 Action? onSinkDispose = null)
15 {
16 if (used)
17 {
18 throw new InvalidOperationException("실패 여부와 관계없이 시험 인스턴스를 재사용하지 않음");
19 }
20 used = true;
21 BoundaryResult result;
22 string? pendingHash = null;
23 using (var sink = new TemporarySink(onSinkDispose))
24 {
25 result = BoundaryProbe.Read(input, budget, selected, bytes =>
26 {
27 if (!selected)
28 {
29 throw new InvalidOperationException("비선택 출력이 전달됨");
30 }
31 sink.Append(bytes);
32 afterChunk?.Invoke();
33 }, token, feedSize, inflater);
34 budget.Check(token);
35 if (selected)
36 {
37 sink.Complete(result);
38 pendingHash = sink.CommittedHash;
39 }
40 }
41 beforeCommit?.Invoke();
42 budget.Check(token);
43 CommittedBoundary = result;
44 CommittedHash = pendingHash;
45 CommitCount = 1;
46 }
47
48 // 실제 해시 자원을 정리한 뒤 Dispose 내부에서 시험 오류를 주입함.
49 private sealed class TemporarySink(Action? onDispose) : IDisposable
50 {
51 private readonly HashSink inner = new();
52 public string? CommittedHash => inner.CommittedHash;
53 public void Append(ReadOnlyMemory<byte> bytes) => inner.Append(bytes);
54 public void Complete(BoundaryResult result) => inner.Complete(result);
55
56 public void Dispose()
57 {
58 inner.Dispose();
59 onDispose?.Invoke();
60 }
61 }
62
63}