UNIT BUFFERED;
                         InterFace
CONST
   bufSize 			: LONGINT = 32768;
{FReadBuf}
   RBBuf			: ^BYTE = NIL;
   RBread    		: ^BYTE = NIL;
   RBcount          : word = 0;
   RBsize           : word = 0;
   RBFirst			: boolean = true;
   RBLast			: boolean = false;
{FReadBuf}
{FWriteBuf}
   WBBuf			: ^BYTE = NIL;
   WBwrt            : ^BYTE = NIL;
   WBcount          : word = 0;       
   WBFirst          : boolean = true; 
   WBLast           : boolean = false;
{FWritebuf}

Function	FReadBuf(var F:FILE;var val:byte):boolean;
Procedure	FWriteBuf(var F:FILE;val:byte);

                       Implementation

Function FReadBuf(var F:FILE;var val:byte):boolean;
begin
  if RBFirst then
   begin
    RBfirst:=false;
    GetMem(RBBuf,BUFSize);
    RBcount:=0;
    RBSize:=0;
   end;
  if RBLast then
   begin
    RBLast:=false;
    FreeMem(RBBuf,BufSize);
    RBFirst:=true;
    exit;
   end;
  If RBcount=RBsize then
   begin
     BlockRead(F,RBBuf^,BufSize,RBsize);
    if Rbsize=0 then
      begin
        FReadBuf:=false;
        exit;
      end;
     RBcount:=0;
   end;
  RBread:=PTR(SeG(RBBUF^),Ofs(RBbuf^)+RBcount);
  inc(RBcount);
  val:=RBread^;
  FReadBuf:=true;
end; {FReadBuf}
{-----------------------------------------}
Procedure FWriteBuf(var F:FILE;val:byte);
begin
  if WBFirst then
   begin
    WBfirst:=false;
    GetMem(WBBuf,BUFSize);
    WBcount:=0;
   end;
  if WBLast then
   begin
    WBLast:=false;
    BlockWrite(F,WBBuf^,WBcount);
    FreeMem(WBBuf,BufSize);
    WBFirst:=true;
    exit;
   end;
  WBwrt:=PTR(SeG(WBBUF^),Ofs(WBbuf^)+WBcount);
  inc(WBcount);
  WBwrt^:=val;
  If WBcount=BUFsize then
   begin
     BlockWrite(F,WBBuf^,WBcount);
     WBcount:=0;
   end;
end;{FWriteBuf}
{-----------------------------------------}
end.