forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteBuffer.cs
More file actions
300 lines (257 loc) · 9.84 KB
/
WriteBuffer.cs
File metadata and controls
300 lines (257 loc) · 9.84 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#region License
// The PostgreSQL License
//
// Copyright (C) 2016 The Npgsql Development Team
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#endregion
using System;
using System.Diagnostics.Contracts;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using AsyncRewriter;
namespace Npgsql
{
internal partial class WriteBuffer
{
#region Fields and Properties
internal Stream Underlying { get; set; }
/// <summary>
/// The total byte length of the buffer.
/// </summary>
internal int Size { get; }
/// <summary>
/// During copy operations, the buffer's usable size is smaller than its total size because of the CopyData
/// message header. This distinction is important since some type handlers check how much space is left
/// in the buffer in their decision making.
/// </summary>
internal int UsableSize
{
get { return _usableSize; }
set
{
Contract.Requires(value <= Size);
_usableSize = value;
}
}
int _usableSize;
internal Encoding TextEncoding { get; }
internal int WritePosition { get { return _writePosition; } set { _writePosition = value; } }
internal int WriteSpaceLeft => Size - _writePosition;
internal long TotalBytesFlushed { get; private set; }
internal readonly byte[] _buf;
readonly Encoder _textEncoder;
int _writePosition;
BitConverterUnion _bitConverterUnion;
/// <summary>
/// The minimum buffer size possible.
/// </summary>
internal const int MinimumBufferSize = ReadBuffer.MinimumBufferSize;
internal const int DefaultBufferSize = ReadBuffer.DefaultBufferSize;
#endregion
#region Constructors
internal WriteBuffer(Stream underlying, int size, Encoding textEncoding)
{
if (size < MinimumBufferSize) {
throw new ArgumentOutOfRangeException(nameof(size), size, "Buffer size must be at least " + MinimumBufferSize);
}
Contract.EndContractBlock();
Underlying = underlying;
Size = size;
UsableSize = Size;
_buf = new byte[Size];
TextEncoding = textEncoding;
_textEncoder = TextEncoding.GetEncoder();
}
#endregion
#region I/O
[RewriteAsync]
public void Flush()
{
if (_writePosition != 0)
{
Underlying.Write(_buf, 0, _writePosition);
Underlying.Flush();
TotalBytesFlushed += _writePosition;
_writePosition = 0;
}
}
#endregion
#region Write Simple
public void WriteByte(byte b)
{
Contract.Requires(WriteSpaceLeft >= sizeof(byte));
_buf[_writePosition++] = b;
}
public void WriteInt16(int i)
{
Contract.Requires(WriteSpaceLeft >= sizeof(short));
_buf[_writePosition++] = (byte)(i >> 8);
_buf[_writePosition++] = (byte)i;
}
public void WriteUInt16(int i)
{
Contract.Requires(WriteSpaceLeft >= sizeof(ushort));
_buf[_writePosition++] = (byte)(i >> 8);
_buf[_writePosition++] = (byte)i;
}
public void WriteInt32(int i)
{
Contract.Requires(WriteSpaceLeft >= sizeof(int));
var pos = _writePosition;
_buf[pos++] = (byte)(i >> 24);
_buf[pos++] = (byte)(i >> 16);
_buf[pos++] = (byte)(i >> 8);
_buf[pos++] = (byte)i;
_writePosition = pos;
}
internal void WriteUInt32(uint i)
{
Contract.Requires(WriteSpaceLeft >= sizeof(uint));
var pos = _writePosition;
_buf[pos++] = (byte)(i >> 24);
_buf[pos++] = (byte)(i >> 16);
_buf[pos++] = (byte)(i >> 8);
_buf[pos++] = (byte)i;
_writePosition = pos;
}
public void WriteInt64(long i)
{
Contract.Requires(WriteSpaceLeft >= sizeof(long));
var pos = _writePosition;
_buf[pos++] = (byte)(i >> 56);
_buf[pos++] = (byte)(i >> 48);
_buf[pos++] = (byte)(i >> 40);
_buf[pos++] = (byte)(i >> 32);
_buf[pos++] = (byte)(i >> 24);
_buf[pos++] = (byte)(i >> 16);
_buf[pos++] = (byte)(i >> 8);
_buf[pos++] = (byte)i;
_writePosition = pos;
}
public void WriteSingle(float f)
{
Contract.Requires(WriteSpaceLeft >= sizeof(float));
_bitConverterUnion.float4 = f;
var pos = _writePosition;
if (BitConverter.IsLittleEndian)
{
_buf[pos++] = _bitConverterUnion.b3;
_buf[pos++] = _bitConverterUnion.b2;
_buf[pos++] = _bitConverterUnion.b1;
_buf[pos++] = _bitConverterUnion.b0;
}
else
{
_buf[pos++] = _bitConverterUnion.b0;
_buf[pos++] = _bitConverterUnion.b1;
_buf[pos++] = _bitConverterUnion.b2;
_buf[pos++] = _bitConverterUnion.b3;
}
_writePosition = pos;
}
public void WriteDouble(double d)
{
Contract.Requires(WriteSpaceLeft >= sizeof(double));
_bitConverterUnion.float8 = d;
var pos = _writePosition;
if (BitConverter.IsLittleEndian)
{
_buf[pos++] = _bitConverterUnion.b7;
_buf[pos++] = _bitConverterUnion.b6;
_buf[pos++] = _bitConverterUnion.b5;
_buf[pos++] = _bitConverterUnion.b4;
_buf[pos++] = _bitConverterUnion.b3;
_buf[pos++] = _bitConverterUnion.b2;
_buf[pos++] = _bitConverterUnion.b1;
_buf[pos++] = _bitConverterUnion.b0;
}
else
{
_buf[pos++] = _bitConverterUnion.b0;
_buf[pos++] = _bitConverterUnion.b1;
_buf[pos++] = _bitConverterUnion.b2;
_buf[pos++] = _bitConverterUnion.b3;
_buf[pos++] = _bitConverterUnion.b4;
_buf[pos++] = _bitConverterUnion.b5;
_buf[pos++] = _bitConverterUnion.b6;
_buf[pos++] = _bitConverterUnion.b7;
}
_writePosition = pos;
}
internal void WriteString(string s, int len = 0)
{
Contract.Requires(TextEncoding.GetByteCount(s) <= WriteSpaceLeft);
WritePosition += TextEncoding.GetBytes(s, 0, len == 0 ? s.Length : len, _buf, WritePosition);
}
internal void WriteChars(char[] chars, int len = 0)
{
Contract.Requires(TextEncoding.GetByteCount(chars) <= WriteSpaceLeft);
WritePosition += TextEncoding.GetBytes(chars, 0, len == 0 ? chars.Length : len, _buf, WritePosition);
}
public void WriteBytes(byte[] buf, int offset, int count)
{
Contract.Requires(count <= WriteSpaceLeft);
Buffer.BlockCopy(buf, offset, _buf, WritePosition, count);
WritePosition += count;
}
public void WriteBytesNullTerminated(byte[] buf)
{
Contract.Requires(WriteSpaceLeft >= buf.Length + 1);
WriteBytes(buf, 0, buf.Length);
WriteByte(0);
}
#endregion
#region Write Complex
internal void WriteStringChunked(char[] chars, int charIndex, int charCount,
bool flush, out int charsUsed, out bool completed)
{
int bytesUsed;
_textEncoder.Convert(chars, charIndex, charCount, _buf, WritePosition, WriteSpaceLeft,
flush, out charsUsed, out bytesUsed, out completed);
WritePosition += bytesUsed;
}
#endregion
#region Misc
internal void Clear()
{
WritePosition = 0;
}
internal void ResetTotalBytesFlushed()
{
TotalBytesFlushed = 0;
}
[StructLayout(LayoutKind.Explicit, Size = 8)]
struct BitConverterUnion
{
[FieldOffset(0)] public readonly byte b0;
[FieldOffset(1)] public readonly byte b1;
[FieldOffset(2)] public readonly byte b2;
[FieldOffset(3)] public readonly byte b3;
[FieldOffset(4)] public readonly byte b4;
[FieldOffset(5)] public readonly byte b5;
[FieldOffset(6)] public readonly byte b6;
[FieldOffset(7)] public readonly byte b7;
[FieldOffset(0)] public float float4;
[FieldOffset(0)] public double float8;
}
#endregion
}
}