5 type tMemoryStream
=object
10 procedure Seek(absolute:LongWord
);
11 procedure Skip(cnt
:Word);
12 procedure Read(var buf
; cnt
:Word);
13 function ReadByte
:byte;
14 function ReadWord(cnt
:byte): LongWord
;
17 function Tell
:LongWord
;
18 procedure Write(var buf
; cnt
:word);
19 procedure WriteByte(v
:byte);
20 procedure WriteWord(v
:LongWord
; cnt
:byte);
21 procedure Init(ibuf
:pointer; ilen
,isize
:LongWord
);
22 function WRBuf
:pointer;
23 function WRBufLen
:LongWord
;
24 procedure WREnd(used
:LongWord
);
25 function RDBuf
:pointer;
26 function RDBufLen
:LongWord
;
27 procedure RDEnd(used
:LongWord
);
30 type eInvalidMemStreamAccess
=class(Exception
)
36 procedure tMemoryStream
.Seek(absolute:LongWord
);
38 if absolute>size
then raise eInvalidMemStreamAccess
.Create('Seek out of bounds');
42 procedure tMemoryStream
.Skip(cnt
:Word);
47 procedure tMemoryStream
.Read(var buf
; cnt
:Word);
49 if (position
+cnt
)>length
then raise eInvalidMemStreamAccess
.Create('Read out of bounds');
50 Move((base
+position
)^,buf
,cnt
);
51 position
:=position
+cnt
;
54 function tMemoryStream
.ReadByte
:byte;
55 begin Read(result
, 1); end;
57 function tMemoryStream
.ReadWord(cnt
:byte): LongWord
;
58 {$IFDEF ENDIAN_LITTLE}
59 var tm
:packed array [0..3] of byte;
63 if (position
+cnt
)>length
then raise eInvalidMemStreamAccess
.Create('Read out of bounds');
64 for i
:=cnt
-1 downto 0 do begin
65 tm
[i
]:=byte((base
+position
)^);
72 ReadWord
:=LongWord(pointer(@tm
)^);
75 procedure tMemoryStream
.Rewind
;
76 begin position
:=0; end;
77 procedure tMemoryStream
.Append
;
78 begin position
:=length
; end;
79 function tMemoryStream
.Tell
:LongWord
;
80 begin Tell
:=position
; end;
82 procedure tMemoryStream
.Write(var buf
; cnt
:word);
84 if (position
+cnt
)>size
then raise eInvalidMemStreamAccess
.Create('Write out of bounds');
85 Move(buf
,(base
+position
)^,cnt
);
86 position
:=position
+cnt
;
87 if position
>length
then length
:=position
;
89 procedure tMemoryStream
.WriteByte(v
:byte);
90 begin Write(v
,1); end;
92 procedure tMemoryStream
.WriteWord(v
:LongWord
; cnt
:byte);
93 var tm
:packed array [0..3] of byte absolute v
;
96 {$IFDEF ENDIAN_LITTLE}
97 if (position
+cnt
)>size
then raise eInvalidMemStreamAccess
.Create('Write out of bounds');
98 for i
:=cnt
-1 downto 0 do begin
99 byte((base
+position
)^):=tm
[i
];
102 if position
>length
then length
:=position
;
104 Write(tm
[4-cnt
],cnt
);
108 procedure tMemoryStream
.Init(ibuf
:pointer; ilen
,isize
:LongWord
);
116 function tMemoryStream
.WRBuf
:pointer;
117 begin result
:=base
+position
end;
118 function tMemoryStream
.WRBufLen
:LongWord
;
119 begin result
:=size
-position
end;
120 procedure tMemoryStream
.WREnd(used
:LongWord
);
121 begin RDEnd(used
); if position
>length
then length
:=position
end;
122 function tMemoryStream
.RDBuf
:pointer;
123 begin result
:=base
+position
end;
124 function tMemoryStream
.RDBufLen
:LongWord
;
125 begin result
:=length
-position
end;
126 procedure tMemoryStream
.RDEnd(used
:LongWord
);
127 begin skip(used
) end;