anchor.diff:  begin
     inherited;
     Result := 0;
  -  if count > 0 then
  -  begin
  -    if IsOpen then
  -    begin
  -      if FBuffer.Position >= FBuffer.Size then
  +  if IsOpen then
  +  begin
  +    while count > 0 do begin
  +
  +      if FReadBuffer.Position >= FReadBuffer.Size then
         begin
  -        FBuffer.Clear;
  +        FReadBuffer.Clear;
           SetLength( tempbuf, FBufSize);
           nRead := FStream.Read( tempbuf, 0, FBufSize );
  -        if nRead > 0 then
  -        begin
  -          FBuffer.WriteBuffer( Pointer(@tempbuf[0])^, nRead );
  -          FBuffer.Position := 0;
  -        end;
  +        if nRead = 0 then Break; // avoid infinite loop
  +
  +        FReadBuffer.WriteBuffer( Pointer(@tempbuf[0])^, nRead );
  +        FReadBuffer.Position := 0;
         end;
   
  -      if FBuffer.Position < FBuffer.Size then
  +      if FReadBuffer.Position < FReadBuffer.Size then
         begin
  -        Result := FBuffer.Read( Pointer(@buffer[offset])^, count );
  +        nRead  := Min( FReadBuffer.Size - FReadBuffer.Position, count);
  +        Inc( Result, FReadBuffer.Read( Pointer(@buffer[offset])^, nRead));
  +        Dec( count, nRead);
  +        Inc( offset, nRead);
         end;
       end;
     end;
  
positive.code: 
    if IsOpen then
    begin
      len := FBuffer.Size;
    end;
  
    SetLength( Result, len);
  
    if len > 0 then
    begin
      FBuffer.Position := 0;
      FBuffer.Read( Pointer(@Result[0])^, len );
    end;
  end;
  
  
negative.code: begin
    if IsOpen then
    begin
      len := FWriteBuffer.Size;
      if len > 0 then
      begin
        SetLength( buf, len );
        FWriteBuffer.Position := 0;
        FWriteBuffer.Read( Pointer(@buf[0])^, len );
        FStream.Write( buf, 0, len );
      end;
      FWriteBuffer.Clear;
    end;
  end;
  
  function TBufferedStreamImpl.IsOpen: Boolean;
  begin
    Result := (FWriteBuffer <> nil)
          and (FReadBuffer <> nil)
          and (FStream <> nil);
  end;
  
  procedure TBufferedStreamImpl.Open;
  