IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
MemoryBufferWriter.cs
Go to the documentation of this file.
1using System;
2using System.Buffers;
3using System.Diagnostics.Contracts;
5
7{
8
13 class MemoryBufferWriter<T> : IBufferWriter<T>
14 {
15
16 readonly Memory<T> _memory;
17 int _index;
18
23 public MemoryBufferWriter(Memory<T> memory)
24 {
25 _memory = memory;
26 }
27
28
30 public ReadOnlyMemory<T> WrittenMemory
31 {
32 [MethodImpl(MethodImplOptions.AggressiveInlining)]
33 get => _memory.Slice(0, _index);
34 }
35
37 public ReadOnlySpan<T> WrittenSpan
38 {
39 [MethodImpl(MethodImplOptions.AggressiveInlining)]
40 get => _memory.Slice(0, _index).Span;
41 }
42
44 public int WrittenCount
45 {
46 [MethodImpl(MethodImplOptions.AggressiveInlining)]
47 get => _index;
48 }
49
51 public int Capacity
52 {
53 [MethodImpl(MethodImplOptions.AggressiveInlining)]
54 get => _memory.Length;
55 }
56
58 public int FreeCapacity
59 {
60 [MethodImpl(MethodImplOptions.AggressiveInlining)]
61 get => _memory.Length - _index;
62 }
63
65 public void Clear()
66 {
67 _memory.Slice(0, _index).Span.Clear();
68 _index = 0;
69 }
70
72 public void Advance(int count)
73 {
74 if (count < 0)
75 {
76 ThrowArgumentOutOfRangeExceptionForNegativeCount();
77 }
78
79 if (_index > _memory.Length - count)
80 {
81 ThrowArgumentExceptionForAdvancedTooFar();
82 }
83
84 _index += count;
85 }
86
88 public Memory<T> GetMemory(int sizeHint = 0)
89 {
90 ValidateSizeHint(sizeHint);
91
92 return _memory.Slice(_index);
93 }
94
96 public Span<T> GetSpan(int sizeHint = 0)
97 {
98 ValidateSizeHint(sizeHint);
99
100 return _memory.Slice(_index).Span;
101 }
102
107 [MethodImpl(MethodImplOptions.AggressiveInlining)]
108 void ValidateSizeHint(int sizeHint)
109 {
110 if (sizeHint < 0)
111 {
112 ThrowArgumentOutOfRangeExceptionForNegativeSizeHint();
113 }
114
115 if (sizeHint == 0)
116 sizeHint = 1;
117
118 if (sizeHint > FreeCapacity)
119 {
120 ThrowArgumentExceptionForCapacityExceeded();
121 }
122 }
123
125 [Pure]
126 public override string ToString()
127 {
128 // See comments in MemoryOwner<T> about this
129 if (typeof(T) == typeof(char))
130 {
131 return _memory.Slice(0, _index).ToString();
132 }
133
134 // Same representation used in Span<T>
135 return $"IKVM.CoreLib.Buffers.MemoryBufferWriter<{typeof(T)}>[{_index}]";
136 }
137
141 static void ThrowArgumentOutOfRangeExceptionForNegativeCount()
142 {
143 throw new ArgumentOutOfRangeException("count", "The count can't be a negative value");
144 }
145
149 static void ThrowArgumentOutOfRangeExceptionForNegativeSizeHint()
150 {
151 throw new ArgumentOutOfRangeException("sizeHint", "The size hint can't be a negative value");
152 }
153
157 static void ThrowArgumentExceptionForAdvancedTooFar()
158 {
159 throw new ArgumentException("The buffer writer has advanced too far");
160 }
161
165 static void ThrowArgumentExceptionForCapacityExceeded()
166 {
167 throw new ArgumentException("The buffer writer doesn't have enough capacity left");
168 }
169
170 }
171
172}
Implements the IBufferWriter<T> interface around a Memory<T>.
MemoryBufferWriter(Memory< T > memory)
Initializes a new instance.