summary refs log tree commit diff
path: root/crypto/src/util/io/LimitedBuffer.cs
blob: 07c9969ad484c8cf8e36c638fc649ea814d33b77 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;

namespace Org.BouncyCastle.Utilities.IO
{
    public sealed class LimitedBuffer
        : BaseOutputStream
    {
        private readonly byte[] m_buf;
        private int m_count;

        public LimitedBuffer(int limit)
        {
            m_buf = new byte[limit];
            m_count = 0;
        }

        public int CopyTo(byte[] buffer, int offset)
        {
            Array.Copy(m_buf, 0, buffer, offset, m_count);
            return m_count;
        }

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
        public int CopyTo(Span<byte> buffer)
        {
            m_buf.AsSpan(0, m_count).CopyTo(buffer);
            return m_count;
        }
#endif

        public int Count => m_count;

        public int Limit => m_buf.Length;

        public void Reset()
        {
            m_count = 0;
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            Array.Copy(buffer, offset, m_buf, m_count, count);
            m_count += count;
        }

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
        public override void Write(ReadOnlySpan<byte> buffer)
        {
            buffer.CopyTo(m_buf.AsSpan(m_count));
        }
#endif

        public override void WriteByte(byte value)
        {
            m_buf[m_count++] = value;
        }
    }
}