• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xoofx / Blake3.NET / 8123751251

02 Mar 2024 03:32PM UTC coverage: 69.393% (+0.2%) from 69.211%
8123751251

push

github

xoofx
Update ci with reusable workflows

37 of 70 branches covered (52.86%)

Branch coverage included in aggregate %.

226 of 309 relevant lines covered (73.14%)

3529.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

31.52
/src/Blake3/Blake3Stream.cs
1
// Copyright (c) Alexandre Mutel. All rights reserved.
2
// Licensed under the BSD-Clause 2 license.
3
// See license.txt file in the project root for full license information.
4

5
using System;
6
using System.IO;
7
using System.Threading;
8
using System.Threading.Tasks;
9

10
namespace Blake3;
11

12
/// <summary>
13
/// A stream that allows to calculate a hash while reading/writing from a backend stream.
14
///
15
/// Use the <see cref="ComputeHash()"/> or <see cref="ComputeHash(System.Span{byte})"/> methods to calculate the hash before disposing the stream
16
/// </summary>
17
public class Blake3Stream : Stream
18
{
19
    private readonly Stream _stream;
20
    private readonly bool _dispose;
21
    private Hasher _hasher;
22

23
    /// <summary>
24
    /// Creates an instance of <see cref="Blake3Stream"/> using the specified backend stream.
25
    /// </summary>
26
    /// <param name="backendStream"></param>
27
    /// <param name="dispose">A boolean that indicates if this stream will dispose the backend stream. Default is true.</param>
28
    public Blake3Stream(Stream backendStream, bool dispose = true)
3✔
29
    {
30
        _stream = backendStream ?? throw new ArgumentNullException(nameof(backendStream));
3!
31
        _dispose = dispose;
3✔
32
        _hasher = Hasher.New();
3✔
33
    }
3✔
34

35
    protected override void Dispose(bool disposing)
36
    {
37
        _hasher.Dispose();
3✔
38
        if (_dispose)
3✔
39
        {
40
            _stream.Dispose();
3✔
41
        }
42
    }
3✔
43

44
    public override async ValueTask DisposeAsync()
45
    {
46
        _hasher.Dispose();
×
47
        if (_dispose)
×
48
        {
49
            await _stream.DisposeAsync();
×
50
        }
51
    }
×
52

53
    public override void Flush()
54
    {
55
        _stream.Flush();
×
56
    }
×
57

58
    public override async Task FlushAsync(CancellationToken cancellationToken)
59
    {
60
        await _stream.FlushAsync(cancellationToken);
×
61
    }
×
62

63
    public void ResetHash()
64
    {
65
        _hasher.Reset();
×
66
    }
×
67

68
    public Hash ComputeHash()
69
    {
70
        return _hasher.Finalize();
3✔
71
    }
72

73
    public void ComputeHash(Span<byte> hash)
74
    {
75
        _hasher.Finalize(hash);
×
76
    }
×
77

78
    public override int Read(byte[] buffer, int offset, int count)
79
    {
80
        var length = _stream.Read(buffer, offset, count);
129✔
81
        if (length > 0)
129✔
82
        {
83
            var span = new Span<byte>(buffer, offset, length);
128✔
84
            _hasher.Update(span);
128✔
85
        }
86
        return length;
129✔
87
    }
88

89
    public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
90
    {
91
        var length = await _stream.ReadAsync(buffer, offset, count, cancellationToken);
×
92
        if (length > 0)
×
93
        {
94
            _hasher.Update(new Span<byte>(buffer, offset, length));
×
95
        }
96
        return length;
×
97
    }
×
98

99
    public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = new CancellationToken())
100
    {
101
        var length = await _stream.ReadAsync(buffer, cancellationToken);
×
102
        if (length > 0)
×
103
        {
104
            _hasher.Update(buffer.Span);
×
105
        }
106
        return length;
×
107
    }
×
108

109
    public override int Read(Span<byte> buffer)
110
    {
111
        var length = _stream.Read(buffer);
1✔
112
        if (length > 0)
1✔
113
        {
114
            _hasher.Update(buffer);
1✔
115
        }
116
        return length;
1✔
117
    }
118

119
    public override unsafe int ReadByte()
120
    {
121
        var value = _stream.ReadByte();
×
122
        if (value < 0) return value;
×
123
        var bValue = (byte) value;
×
124
        var span = new ReadOnlySpan<byte>(&bValue, 1);
×
125
        _hasher.Update(span);
×
126
        return value;
×
127
    }
128
        
129
    public override void Write(byte[] buffer, int offset, int count)
130
    {
131
        _stream.Write(buffer, offset, count);
×
132
        if (count > 0)
×
133
        {
134
            var span = new Span<byte>(buffer, offset, count);
×
135
            _hasher.Update(span);
×
136
        }
137
    }
×
138

139
    public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
140
    { 
141
        await _stream.WriteAsync(buffer, offset, count, cancellationToken);
×
142
        if (count > 0)
×
143
        {
144
            _hasher.Update(new Span<byte>(buffer, offset, count));
×
145
        }
146
    }
×
147

148
    public override void Write(ReadOnlySpan<byte> buffer)
149
    {
150
        _stream.Write(buffer);
1✔
151
        _hasher.Update(buffer);
1✔
152
    }
1✔
153

154
    public override unsafe void WriteByte(byte value)
155
    {
156
        _stream.WriteByte(value);
×
157
        var span = new ReadOnlySpan<byte>(&value, 1);
×
158
        _hasher.Update(span);
×
159
    }
×
160

161
    public override long Seek(long offset, SeekOrigin origin)
162
    {
163
        return _stream.Seek(offset, origin);
×
164
    }
165

166
    public override void SetLength(long value)
167
    {
168
        _stream.SetLength(value);
×
169
    }
×
170
        
171
    public override bool CanRead => _stream.CanRead;
×
172
    public override bool CanSeek => _stream.CanSeek;
×
173
    public override bool CanWrite => _stream.CanWrite;
×
174
    public override long Length => _stream.Length;
×
175

176
    public override long Position
177
    {
178
        get => _stream.Position;
×
179
        set => _stream.Position = value;
×
180
    }
181
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc