-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUCSTREAM.PAS
138 lines (118 loc) · 3.21 KB
/
UCSTREAM.PAS
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
{
ucstream Unit
2022 LRT
}
unit
ucstream;
interface
uses
consts, utils, uclasses, types, locale, math,
uobject, uexc, ustream;
type
PCachedStream = ^TCachedStream;
TCachedStream = object (TStream)
public
constructor initWithStream(stream: PStream; cacheSize: word);
destructor done; virtual;
function read(buffer: pointer; count: word): word; virtual;
procedure write(buffer: pointer; count: word); virtual;
procedure seek(position: longint); virtual;
function getPosition: longint; virtual;
function isEOF: boolean; virtual;
function getSize: longint; virtual;
function isReadOnly: boolean; virtual;
function getClassName: string; virtual;
function getClassId: word; virtual;
private
_stream: PStream;
_sourceSize: longint;
_cache: PByte;
_cacheSize: word;
_from: longint;
_count: word;
_relativePos: longint;
end;
implementation
{ TCachedStream public }
constructor TCachedStream.initWithStream(stream: PStream; cacheSize: word);
begin
inherited init;
_stream := stream;
_stream^.retain;
_sourceSize := _stream^.getSize;
_cacheSize := cacheSize;
getMem(_cache, _cacheSize);
_count := 0;
seek(0);
end;
destructor TCachedStream.done;
begin
freeMem(_cache, _cacheSize);
_stream^.release;
inherited done;
end;
function TCachedStream.read(buffer: pointer; count: word): word;
var
remaining: word;
src, dst: pbyte;
chunkSize: word;
begin
remaining := count;
dst := buffer;
while not isEOF and (remaining > 0) do
begin
if _relativePos = _cacheSize then seek(_from + _cacheSize);
src := _cache;
inc(src, _relativePos);
chunkSize := minword(remaining, _count - _relativePos);
Move(src^, dst^, chunkSize);
inc(dst, chunkSize);
dec(remaining, chunkSize);
inc(_relativePos, chunkSize);
end;
read := count - remaining;
end;
procedure TCachedStream.write(buffer: pointer; count: word);
begin
iassert(false, @self, 0, S_ERR_UNSUPPORTED_ACTION);
end;
procedure TCachedStream.seek(position: longint);
begin
if (position >= _from) and (position <= (_from + _count - 1)) then
begin
_relativePos := position - _from;
end else
begin
_stream^.seek(position);
_count := _stream^.read(_cache, _cacheSize);
_from := position;
_relativePos := 0;
end;
end;
function TCachedStream.getPosition: longint;
begin
getPosition := _from + _relativePos;
end;
function TCachedStream.isEOF: boolean;
begin
isEOF := (_from + _relativePos) = _sourceSize;
end;
function TCachedStream.getSize: longint;
begin
getSize := _sourceSize;
end;
function TCachedStream.isReadOnly: boolean;
begin
isReadOnly := true;
end;
function TCachedStream.getClassName: string;
begin
getClassName := 'TCachedStream';
end;
function TCachedStream.getClassId: word;
begin
getClassId := C_CLASS_ID_CachedStream;
end;
{ TCachedStream private }
{ Other }
end.