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
; experimental
;
17 function Tell
:LongWord
;
18 procedure Write(var buf
; cnt
:word);
19 procedure WriteByte(v
:byte);
20 procedure WriteWord(v
:LongWord
; cnt
:byte); experimental
;
21 procedure Init(ibuf
:pointer; ilen
,isize
:LongWord
);
24 type eInvalidMemStreamAccess
=class(Exception
)
30 procedure tMemoryStream
.Seek(absolute:LongWord
);
32 if absolute>=size
then raise eInvalidMemStreamAccess
.Create('Seek out of bounds');
33 position
:=data
+absolute;
36 procedure tMemoryStream
.Skip(cnt
:Word);
38 if cnt
>=size
then raise eInvalidMemStreamAccess
.Create('Seek out of bounds');
39 position
:=position
+cnt
;
42 procedure tMemoryStream
.Read(var buf
; cnt
:Word);
44 if (position
+cnt
)>=(data
+length
) then raise eInvalidMemStreamAccess
.Create('Read out of bounds');
45 Move(position
^,buf
,cnt
);
46 position
:=position
+cnt
;
49 function tMemoryStream
.ReadByte
:byte;
50 begin Read(result
, 1); end;
52 function tMemoryStream
.ReadWord(cnt
:byte): LongWord
;
53 {$IFDEF ENDIAN_LITTLE}
54 var tm
:packed array [0..3] of byte;
57 if (position
+cnt
)>=(data
+length
) then raise eInvalidMemStreamAccess
.Create('Read out of bounds');
58 for i
:=cnt
-1 downto 0 do begin
59 tm
[i
]:=byte((position
)^);
66 ReadWord
:=LongWord(pointer(@tm
)^);
69 procedure tMemoryStream
.Rewind
;
70 begin position
:=data
; end;
71 procedure tMemoryStream
.Append
;
72 begin position
:=data
+length
; end;
73 function tMemoryStream
.Tell
:LongWord
;
74 begin Tell
:=position
-data
; end;
75 procedure tMemoryStream
.Write(var buf
; cnt
:word);
77 if (position
+cnt
)>=(data
+size
) then raise eInvalidMemStreamAccess
.Create('Write out of bounds');
78 Move(buf
,position
^,cnt
);
79 length
:=position
-data
;
81 procedure tMemoryStream
.WriteByte(v
:byte);
82 begin Write(v
,1); end;
84 procedure tMemoryStream
.WriteWord(v
:LongWord
; cnt
:byte);
85 var tm
:packed array [0..3] of byte absolute v
;
88 {$IFDEF ENDIAN_LITTLE}
89 if (position
+cnt
)>=(data
+size
) then raise eInvalidMemStreamAccess
.Create('Write out of bounds');
90 for i
:=cnt
-1 to 0 do begin
91 byte(position
^):=tm
[i
];
97 length
:=position
-data
;
100 procedure tMemoryStream
.Init(ibuf
:pointer; ilen
,isize
:LongWord
);