unit UByteStream;
interface
uses
Sysutils, Classes;
type
PByteStream = ^TByteStream;
TByteStream =
Class(TObject)
private
FFile:
File Of Byte;
FFileName:
String;
public
constructor Create(AFileName:
String; Mode: Word);
destructor Destroy;
override;
function ReadByte: Byte;
function Read(
var Buffer:
array of byte; BufSize: Longint): Longint;
procedure WriteByte(b: Byte);
function Write(
var Buffer:
array of byte; BufSize: Longint): Longint;
function Position: Longint;
function Size: Longint;
procedure Seek(APos: Longint; Flag: Word);
End;
implementation
{ TByteStream }
constructor TByteStream.Create(AFileName:
String; Mode: Word);
var IResult: Integer;
begin
inherited Create;
System.Assign(FFile, FFileName);
{$I-}
if (Mode=fmOpen)
or (Mode=fmOpenRead)
then
begin
System.Reset(FFile);
IResult := System.IOResult;
if IResult <> 0
then fail;
end
else
if Mode = fmCreate
then
begin
System.Rewrite(FFile);
IResult := IOResult;
if IResult <> 0
then fail;
end;
{$I+}
end;
destructor TByteStream.Destroy;
begin
System.Close(FFile);
inherited Done;
end;
function TByteStream.ReadByte: Byte;
var b: Byte;
begin
System.
Read(FFile, b);
Result := b;
end;
function TByteStream.
Read(
var Buffer:
array of byte; BufSize: Longint): Longint;
var Count,Current,BufStart,BufEnd,BufPos: Word;
begin
BufStart := Low(Buffer);
BufEnd := High(Buffer);
BufPos := BufStart;
Current := System.FilePos(FFile);
Count := Current;
while Count < Current + BufSize
do
begin
System.
Read(FFile, Buffer[BufPos]);
inc(BufPos);
inc(Count);
end;
Result := Count - Current;
end;
procedure TByteStream.WriteByte(b: Byte);
begin
System.
Write(FFile, b);
end;
function TByteStream.
Write(
var Buffer:
array of byte; BufSize: Longint): Longint;
var Count,Current,BufStart,BufEnd,BufPos: Word; x:
array[0..1023]
of byte
absolute Buffer;
begin
BufStart := Low(Buffer);
BufEnd := High(Buffer);
BufPos := BufStart;
Current := System.FilePos(FFile);
Count := Current;
while Count < Current + BufSize
do
begin
System.
Write(FFile, Buffer[BufPos]);
inc(BufPos);
inc(Count);
end;
Result := Count - Current;
end;
function TByteStream.Position: Longint;
begin
Result := System.FilePos(FFile);
end;
function TByteStream.Size: Longint;
begin
Result := System.FileSize(FFile);
end;
procedure TByteStream.Seek(APos: Longint; Flag: Word);
begin
case Flag
of
soFromBeginning: System.Seek(FFile, APos);
soFromCurrentPos: ;
soFromEnd: ;
end;
end;
end.