Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BinaryWriter.cs
Go to the documentation of this file.
4using System.Text;
6
7namespace System.IO;
8
10{
11 public static readonly BinaryWriter Null = new BinaryWriter();
12
13 protected Stream OutStream;
14
15 private readonly Encoding _encoding;
16
17 private readonly bool _leaveOpen;
18
19 private readonly bool _useFastUtf8;
20
21 public virtual Stream BaseStream
22 {
23 get
24 {
25 Flush();
26 return OutStream;
27 }
28 }
29
30 protected BinaryWriter()
31 {
34 _useFastUtf8 = true;
35 }
36
37 public BinaryWriter(Stream output)
38 : this(output, Encoding.UTF8, leaveOpen: false)
39 {
40 }
41
42 public BinaryWriter(Stream output, Encoding encoding)
43 : this(output, encoding, leaveOpen: false)
44 {
45 }
46
47 public BinaryWriter(Stream output, Encoding encoding, bool leaveOpen)
48 {
49 if (output == null)
50 {
51 throw new ArgumentNullException("output");
52 }
53 if (encoding == null)
54 {
55 throw new ArgumentNullException("encoding");
56 }
57 if (!output.CanWrite)
58 {
60 }
61 OutStream = output;
62 _encoding = encoding;
63 _leaveOpen = leaveOpen;
64 _useFastUtf8 = encoding.IsUTF8CodePage && encoding.EncoderFallback.MaxCharCount <= 1;
65 }
66
67 public virtual void Close()
68 {
69 Dispose(disposing: true);
70 }
71
72 protected virtual void Dispose(bool disposing)
73 {
74 if (disposing)
75 {
76 if (_leaveOpen)
77 {
79 }
80 else
81 {
83 }
84 }
85 }
86
87 public void Dispose()
88 {
89 Dispose(disposing: true);
90 }
91
92 public virtual ValueTask DisposeAsync()
93 {
94 try
95 {
96 if (GetType() == typeof(BinaryWriter))
97 {
98 if (_leaveOpen)
99 {
100 return new ValueTask(OutStream.FlushAsync());
101 }
103 }
104 else
105 {
106 Dispose();
107 }
108 return default(ValueTask);
109 }
110 catch (Exception exception)
111 {
113 }
114 }
115
116 public virtual void Flush()
117 {
119 }
120
121 public virtual long Seek(int offset, SeekOrigin origin)
122 {
123 return OutStream.Seek(offset, origin);
124 }
125
126 public virtual void Write(bool value)
127 {
128 OutStream.WriteByte((byte)(value ? 1u : 0u));
129 }
130
131 public virtual void Write(byte value)
132 {
134 }
135
136 [CLSCompliant(false)]
137 public virtual void Write(sbyte value)
138 {
140 }
141
142 public virtual void Write(byte[] buffer)
143 {
144 if (buffer == null)
145 {
146 throw new ArgumentNullException("buffer");
147 }
148 OutStream.Write(buffer, 0, buffer.Length);
149 }
150
151 public virtual void Write(byte[] buffer, int index, int count)
152 {
154 }
155
156 public virtual void Write(char ch)
157 {
158 if (!Rune.TryCreate(ch, out var result))
159 {
161 }
162 Span<byte> span = stackalloc byte[8];
163 if (_useFastUtf8)
164 {
165 int length = result.EncodeToUtf8(span);
166 OutStream.Write(span.Slice(0, length));
167 return;
168 }
169 byte[] array = null;
170 int maxByteCount = _encoding.GetMaxByteCount(1);
171 if (maxByteCount > span.Length)
172 {
173 array = ArrayPool<byte>.Shared.Rent(maxByteCount);
174 span = array;
175 }
176 int bytes = _encoding.GetBytes(MemoryMarshal.CreateReadOnlySpan(ref ch, 1), span);
177 OutStream.Write(span.Slice(0, bytes));
178 if (array != null)
179 {
181 }
182 }
183
184 public virtual void Write(char[] chars)
185 {
186 if (chars == null)
187 {
188 throw new ArgumentNullException("chars");
189 }
190 WriteCharsCommonWithoutLengthPrefix(chars, useThisWriteOverride: false);
191 }
192
193 public virtual void Write(char[] chars, int index, int count)
194 {
195 if (chars == null)
196 {
197 throw new ArgumentNullException("chars");
198 }
199 if (index < 0)
200 {
202 }
203 if (count < 0)
204 {
206 }
207 if (index > chars.Length - count)
208 {
210 }
211 WriteCharsCommonWithoutLengthPrefix(chars.AsSpan(index, count), useThisWriteOverride: false);
212 }
213
214 public virtual void Write(double value)
215 {
216 Span<byte> span = stackalloc byte[8];
218 OutStream.Write(span);
219 }
220
221 public virtual void Write(decimal value)
222 {
223 Span<byte> span = stackalloc byte[16];
224 decimal.GetBytes(in value, span);
225 OutStream.Write(span);
226 }
227
228 public virtual void Write(short value)
229 {
230 Span<byte> span = stackalloc byte[2];
232 OutStream.Write(span);
233 }
234
235 [CLSCompliant(false)]
236 public virtual void Write(ushort value)
237 {
238 Span<byte> span = stackalloc byte[2];
240 OutStream.Write(span);
241 }
242
243 public virtual void Write(int value)
244 {
245 Span<byte> span = stackalloc byte[4];
247 OutStream.Write(span);
248 }
249
250 [CLSCompliant(false)]
251 public virtual void Write(uint value)
252 {
253 Span<byte> span = stackalloc byte[4];
255 OutStream.Write(span);
256 }
257
258 public virtual void Write(long value)
259 {
260 Span<byte> span = stackalloc byte[8];
262 OutStream.Write(span);
263 }
264
265 [CLSCompliant(false)]
266 public virtual void Write(ulong value)
267 {
268 Span<byte> span = stackalloc byte[8];
270 OutStream.Write(span);
271 }
272
273 public virtual void Write(float value)
274 {
275 Span<byte> span = stackalloc byte[4];
277 OutStream.Write(span);
278 }
279
280 public virtual void Write(Half value)
281 {
282 Span<byte> span = stackalloc byte[2];
284 OutStream.Write(span);
285 }
286
287 public virtual void Write(string value)
288 {
289 if (value == null)
290 {
291 throw new ArgumentNullException("value");
292 }
293 if (_useFastUtf8)
294 {
295 if (value.Length <= 42)
296 {
297 Span<byte> span = stackalloc byte[128];
298 int bytes = _encoding.GetBytes(value, span.Slice(1));
299 span[0] = (byte)bytes;
300 OutStream.Write(span.Slice(0, bytes + 1));
301 return;
302 }
303 if (value.Length <= 21845)
304 {
305 byte[] array = ArrayPool<byte>.Shared.Rent(value.Length * 3);
306 int bytes2 = _encoding.GetBytes(value, array);
307 Write7BitEncodedInt(bytes2);
308 OutStream.Write(array, 0, bytes2);
310 return;
311 }
312 }
315 WriteCharsCommonWithoutLengthPrefix(value, useThisWriteOverride: false);
316 }
317
318 public virtual void Write(ReadOnlySpan<byte> buffer)
319 {
320 if (GetType() == typeof(BinaryWriter))
321 {
323 return;
324 }
325 byte[] array = ArrayPool<byte>.Shared.Rent(buffer.Length);
326 try
327 {
328 buffer.CopyTo(array);
329 Write(array, 0, buffer.Length);
330 }
331 finally
332 {
334 }
335 }
336
337 public virtual void Write(ReadOnlySpan<char> chars)
338 {
339 WriteCharsCommonWithoutLengthPrefix(chars, useThisWriteOverride: true);
340 }
341
342 private void WriteCharsCommonWithoutLengthPrefix(ReadOnlySpan<char> chars, bool useThisWriteOverride)
343 {
344 byte[] array;
345 if (chars.Length <= 65536)
346 {
347 int maxByteCount = _encoding.GetMaxByteCount(chars.Length);
348 if (maxByteCount <= 65536)
349 {
350 array = ArrayPool<byte>.Shared.Rent(maxByteCount);
352 WriteToOutStream(array, 0, bytes, useThisWriteOverride);
354 return;
355 }
356 }
357 array = ArrayPool<byte>.Shared.Rent(65536);
358 Encoder encoder = _encoding.GetEncoder();
359 bool completed;
360 do
361 {
362 encoder.Convert(chars, array, flush: true, out var charsUsed, out var bytesUsed, out completed);
363 if (bytesUsed != 0)
364 {
365 WriteToOutStream(array, 0, bytesUsed, useThisWriteOverride);
366 }
367 chars = chars.Slice(charsUsed);
368 }
369 while (!completed);
371 void WriteToOutStream(byte[] buffer, int offset, int count, bool useThisWriteOverride)
372 {
373 if (useThisWriteOverride)
374 {
376 }
377 else
378 {
380 }
381 }
382 }
383
384 public void Write7BitEncodedInt(int value)
385 {
386 uint num;
387 for (num = (uint)value; num > 127; num >>= 7)
388 {
389 Write((byte)(num | 0xFFFFFF80u));
390 }
391 Write((byte)num);
392 }
393
394 public void Write7BitEncodedInt64(long value)
395 {
396 ulong num;
397 for (num = (ulong)value; num > 127; num >>= 7)
398 {
399 Write((byte)((uint)(int)num | 0xFFFFFF80u));
400 }
401 Write((byte)num);
402 }
403}
static ArrayPool< T > Shared
Definition ArrayPool.cs:7
static void WriteUInt32LittleEndian(Span< byte > destination, uint value)
static void WriteInt32LittleEndian(Span< byte > destination, int value)
static void WriteUInt16LittleEndian(Span< byte > destination, ushort value)
static void WriteInt16LittleEndian(Span< byte > destination, short value)
static void WriteUInt64LittleEndian(Span< byte > destination, ulong value)
static void WriteHalfLittleEndian(Span< byte > destination, Half value)
static void WriteSingleLittleEndian(Span< byte > destination, float value)
static void WriteInt64LittleEndian(Span< byte > destination, long value)
static void WriteDoubleLittleEndian(Span< byte > destination, double value)
readonly bool _leaveOpen
virtual void Write(char ch)
virtual void Write(byte value)
void Write7BitEncodedInt64(long value)
virtual void Write(sbyte value)
virtual void Write(float value)
virtual void Write(byte[] buffer, int index, int count)
void WriteCharsCommonWithoutLengthPrefix(ReadOnlySpan< char > chars, bool useThisWriteOverride)
virtual void Write(short value)
virtual void Write(char[] chars, int index, int count)
virtual void Write(uint value)
virtual ValueTask DisposeAsync()
BinaryWriter(Stream output, Encoding encoding, bool leaveOpen)
virtual void Close()
virtual void Dispose(bool disposing)
virtual void Write(double value)
static readonly BinaryWriter Null
virtual Stream BaseStream
BinaryWriter(Stream output)
virtual long Seek(int offset, SeekOrigin origin)
virtual void Write(char[] chars)
virtual void Write(ushort value)
virtual void Write(int value)
void Write7BitEncodedInt(int value)
virtual void Write(long value)
virtual void Write(byte[] buffer)
virtual void Write(ReadOnlySpan< char > chars)
readonly bool _useFastUtf8
readonly Encoding _encoding
virtual void Write(ulong value)
virtual void Write(Half value)
virtual void Write(ReadOnlySpan< byte > buffer)
virtual void Write(bool value)
BinaryWriter(Stream output, Encoding encoding)
virtual void Write(decimal value)
virtual void Write(string value)
Task FlushAsync()
Definition Stream.cs:669
long Seek(long offset, SeekOrigin origin)
virtual void Close()
Definition Stream.cs:644
void Write(byte[] buffer, int offset, int count)
virtual void WriteByte(byte value)
Definition Stream.cs:1020
static readonly Stream Null
Definition Stream.cs:488
static string ArgumentOutOfRange_IndexCount
Definition SR.cs:80
static string Argument_StreamNotWritable
Definition SR.cs:878
static string Arg_SurrogatesNotAllowedAsSingleChar
Definition SR.cs:412
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
Definition SR.cs:7
virtual void Convert(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed)
Definition Encoder.cs:135
static Encoding UTF8
Definition Encoding.cs:526
virtual byte[] GetBytes(char[] chars)
Definition Encoding.cs:781
int GetMaxByteCount(int charCount)
virtual Encoder GetEncoder()
Definition Encoding.cs:1009
virtual int GetByteCount(char[] chars)
Definition Encoding.cs:713
Span< T > Slice(int start)
Definition Span.cs:271
int Length
Definition Span.cs:70
static bool TryCreate(char ch, out Rune result)
Definition Rune.cs:511
static ValueTask FromException(Exception exception)
Definition ValueTask.cs:190