-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReceiver.cs
70 lines (53 loc) · 1.78 KB
/
Receiver.cs
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
using System;
using System.IO;
using System.Text;
namespace Yove.Http;
internal class Receiver(int size, Stream stream)
{
private byte[] _buffer { get; } = new byte[size];
private byte[] _temporaryBuffer = new byte[1024];
private Stream _stream { get; } = stream;
private int _length { get; set; }
public int Position { get; set; }
public bool HasData
{
get { return (_length - Position) != 0; }
}
public string Get(bool readLine)
{
int currentPosition = 0;
while (true)
{
if (Position == _length)
{
Position = 0;
_length = _stream.Read(_buffer, 0, _buffer.Length);
if (_length == 0)
break;
}
byte symbol = _buffer[Position++];
_temporaryBuffer[currentPosition++] = symbol;
if ((!readLine && Encoding.ASCII.GetString(_temporaryBuffer, 0, currentPosition).EndsWith("\r\n\r\n")) ||
(readLine && symbol == (byte)'\n'))
{
break;
}
if (currentPosition == _temporaryBuffer.Length)
{
byte[] temporaryBufferX2 = new byte[_temporaryBuffer.Length * 2];
_temporaryBuffer.CopyTo(temporaryBufferX2, 0);
_temporaryBuffer = temporaryBufferX2;
}
}
return Encoding.ASCII.GetString(_temporaryBuffer, 0, currentPosition);
}
public int Read(byte[] buffer, int index, int length)
{
int currentLength = _length - Position;
if (currentLength > length)
currentLength = length;
Array.Copy(_buffer, Position, buffer, index, currentLength);
Position += currentLength;
return currentLength;
}
}