-
Notifications
You must be signed in to change notification settings - Fork 474
/
Filter.cs
148 lines (126 loc) · 3.89 KB
/
Filter.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
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
using System;
using System.IO;
namespace SharpCompress.Compressors.Filters;
internal abstract class Filter : Stream
{
protected bool _isEncoder;
protected Stream _baseStream;
private readonly byte[] _tail;
private readonly byte[] _window;
private int _transformed;
private int _read;
private bool _endReached;
private bool _isDisposed;
protected Filter(bool isEncoder, Stream baseStream, int lookahead)
{
_isEncoder = isEncoder;
_baseStream = baseStream;
_tail = new byte[lookahead - 1];
_window = new byte[_tail.Length * 2];
}
protected override void Dispose(bool disposing)
{
if (_isDisposed)
{
return;
}
_isDisposed = true;
base.Dispose(disposing);
_baseStream.Dispose();
}
public override bool CanRead => !_isEncoder;
public override bool CanSeek => false;
public override bool CanWrite => _isEncoder;
public override void Flush() { }
public override long Length => _baseStream.Length;
public override long Position
{
get => _baseStream.Position;
set => throw new NotSupportedException();
}
public override int Read(byte[] buffer, int offset, int count)
{
var size = 0;
if (_transformed > 0)
{
var copySize = _transformed;
if (copySize > count)
{
copySize = count;
}
Buffer.BlockCopy(_tail, 0, buffer, offset, copySize);
_transformed -= copySize;
_read -= copySize;
offset += copySize;
count -= copySize;
size += copySize;
Buffer.BlockCopy(_tail, copySize, _tail, 0, _read);
}
if (count == 0)
{
return size;
}
var inSize = _read;
if (inSize > count)
{
inSize = count;
}
Buffer.BlockCopy(_tail, 0, buffer, offset, inSize);
_read -= inSize;
Buffer.BlockCopy(_tail, inSize, _tail, 0, _read);
while (!_endReached && inSize < count)
{
var baseRead = _baseStream.Read(buffer, offset + inSize, count - inSize);
inSize += baseRead;
if (baseRead == 0)
{
_endReached = true;
}
}
while (!_endReached && _read < _tail.Length)
{
var baseRead = _baseStream.Read(_tail, _read, _tail.Length - _read);
_read += baseRead;
if (baseRead == 0)
{
_endReached = true;
}
}
if (inSize > _tail.Length)
{
_transformed = Transform(buffer, offset, inSize);
offset += _transformed;
count -= _transformed;
size += _transformed;
inSize -= _transformed;
_transformed = 0;
}
if (count == 0)
{
return size;
}
Buffer.BlockCopy(buffer, offset, _window, 0, inSize);
Buffer.BlockCopy(_tail, 0, _window, inSize, _read);
if (inSize + _read > _tail.Length)
{
_transformed = Transform(_window, 0, inSize + _read);
}
else
{
_transformed = inSize + _read;
}
Buffer.BlockCopy(_window, 0, buffer, offset, inSize);
Buffer.BlockCopy(_window, inSize, _tail, 0, _read);
size += inSize;
_transformed -= inSize;
return size;
}
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count)
{
Transform(buffer, offset, count);
_baseStream.Write(buffer, offset, count);
}
protected abstract int Transform(byte[] buffer, int offset, int count);
}