Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BaseWriter.cs
Go to the documentation of this file.
2using System.IO;
5
6namespace System.Net.Mime;
7
8internal abstract class BaseWriter
9{
10 private static readonly AsyncCallback s_onWrite = OnWrite;
11
12 protected static readonly byte[] s_crlf = new byte[2] { 13, 10 };
13
14 protected readonly BufferBuilder _bufferBuilder;
15
16 protected readonly Stream _stream;
17
18 private readonly EventHandler _onCloseHandler;
19
20 private readonly bool _shouldEncodeLeadingDots;
21
22 private readonly int _lineLength;
23
25
26 protected bool _isInContent;
27
28 protected BaseWriter(Stream stream, bool shouldEncodeLeadingDots)
29 {
30 if (stream == null)
31 {
32 throw new ArgumentNullException("stream");
33 }
35 _shouldEncodeLeadingDots = shouldEncodeLeadingDots;
38 _lineLength = 76;
39 }
40
41 internal abstract void WriteHeaders(NameValueCollection headers, bool allowUnicode);
42
43 internal void WriteHeader(string name, string value, bool allowUnicode)
44 {
45 if (name == null)
46 {
47 throw new ArgumentNullException("name");
48 }
49 if (value == null)
50 {
51 throw new ArgumentNullException("value");
52 }
53 if (_isInContent)
54 {
56 }
60 WriteAndFold(value, name.Length + 2, allowUnicode);
62 }
63
64 private void WriteAndFold(string value, int charsAlreadyOnLine, bool allowUnicode)
65 {
66 int num = 0;
67 int num2 = 0;
68 for (int i = 0; i < value.Length; i++)
69 {
71 {
72 i += 2;
73 _bufferBuilder.Append(value, num2, i - num2, allowUnicode);
74 num2 = i;
75 num = i;
76 charsAlreadyOnLine = 0;
77 }
78 else if (i - num2 > _lineLength - charsAlreadyOnLine && num != num2)
79 {
80 _bufferBuilder.Append(value, num2, num - num2, allowUnicode);
82 num2 = num;
83 charsAlreadyOnLine = 0;
84 }
85 else if (value[i] == ' ' || value[i] == '\t')
86 {
87 num = i;
88 }
89 }
90 if (value.Length - num2 > 0)
91 {
92 _bufferBuilder.Append(value, num2, value.Length - num2, allowUnicode);
93 }
94 }
95
97 {
98 return GetContentStream(null);
99 }
100
113
114 internal IAsyncResult BeginGetContentStream(AsyncCallback callback, object state)
115 {
116 MultiAsyncResult multiAsyncResult = new MultiAsyncResult(this, callback, state);
117 Stream contentStream = GetContentStream(multiAsyncResult);
118 if (!(multiAsyncResult.Result is Exception))
119 {
120 multiAsyncResult.Result = contentStream;
121 }
122 multiAsyncResult.CompleteSequence();
123 return multiAsyncResult;
124 }
125
127 {
128 object obj = MultiAsyncResult.End(result);
129 if (obj is Exception source)
130 {
132 }
133 return (Stream)obj;
134 }
135
136 protected void Flush(MultiAsyncResult multiResult)
137 {
138 if (_bufferBuilder.Length <= 0)
139 {
140 return;
141 }
142 if (multiResult != null)
143 {
144 multiResult.Enter();
146 if (asyncResult.CompletedSynchronously)
147 {
149 multiResult.Leave();
150 }
151 }
152 else
153 {
155 }
157 }
158
159 protected static void OnWrite(IAsyncResult result)
160 {
161 if (!result.CompletedSynchronously)
162 {
163 MultiAsyncResult multiAsyncResult = (MultiAsyncResult)result.AsyncState;
164 BaseWriter baseWriter = (BaseWriter)multiAsyncResult.Context;
165 try
166 {
167 baseWriter._stream.EndWrite(result);
168 multiAsyncResult.Leave();
169 }
170 catch (Exception result2)
171 {
172 multiAsyncResult.Leave(result2);
173 }
174 }
175 }
176
177 internal abstract void Close();
178
179 protected abstract void OnClose(object sender, EventArgs args);
180
181 protected virtual void CheckBoundary()
182 {
183 }
184}
virtual void EndWrite(IAsyncResult asyncResult)
Definition Stream.cs:889
void Write(byte[] buffer, int offset, int count)
virtual IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
Definition Stream.cs:813
void WriteHeaders(NameValueCollection headers, bool allowUnicode)
Stream EndGetContentStream(IAsyncResult result)
readonly bool _shouldEncodeLeadingDots
Definition BaseWriter.cs:20
readonly Stream _stream
Definition BaseWriter.cs:16
virtual void CheckBoundary()
Stream GetContentStream(MultiAsyncResult multiResult)
IAsyncResult BeginGetContentStream(AsyncCallback callback, object state)
void WriteHeader(string name, string value, bool allowUnicode)
Definition BaseWriter.cs:43
BaseWriter(Stream stream, bool shouldEncodeLeadingDots)
Definition BaseWriter.cs:28
void WriteAndFold(string value, int charsAlreadyOnLine, bool allowUnicode)
Definition BaseWriter.cs:64
static readonly byte[] s_crlf
Definition BaseWriter.cs:12
void OnClose(object sender, EventArgs args)
static void OnWrite(IAsyncResult result)
readonly BufferBuilder _bufferBuilder
Definition BaseWriter.cs:14
readonly EventHandler _onCloseHandler
Definition BaseWriter.cs:18
static readonly AsyncCallback s_onWrite
Definition BaseWriter.cs:10
void Flush(MultiAsyncResult multiResult)
static bool IsFWSAt(string data, int index)
static object End(IAsyncResult result)
static string MailWriterIsInContent
Definition SR.cs:40
Definition SR.cs:7