Pastebin

Paste #2477: pascal write, TFileStream

< previous paste - next paste>

Pasted by zaher

Download View as text

procedure WriteFile(Filename: String; BufferSize: longint);
var
   //Buffer: Array of Byte;
   Buffer: PByte;
   BytesWritten, i, j: longint;
   StartTime, EndTime, TmpTime, TmpTime2, Elapsed: integer;
   WriteTime, WriteSpeed: Double;
   StatusLine : String;
   tmp: Word;
   doWrite: Boolean;
   fs: TFileStream;
begin
  Log('WriteFile("'+Filename+'", '+IntToStr(BufferSize)+')');
  StartTime := GetTickCount();
  BytesWritten:=0;
  //SetLength(Buffer, BufferSize);

  Buffer := AllocMem(SizeOf(Byte) * BufferSize);
  try
    // Initialize buffer with random bytes
    Randomize;
    Log('Initializing buffer with random bytes...');
    for j:=0 to BufferSize do
    begin
      Buffer[j] := Random(256);
    end;
    Log('Initialized buf[0..' + IntToStr(j) + ']');

    // Write the file
    Log('Opening "' + Filename + '" for writing...');
    Log('File opened...');
    fs:=TFileStream.Create(Filename, fmCreate);
    try
  //    Form1.ProgressBar1.Position:=0;
  //    Form1.ProgressBar1.Max:=Round((DiskFree(0) - BufferSize * 2 ) / 1024 / 1024);
      i := 0;
      While ( (doWrite = True ) and (DiskFree(0) > BufferSize * 2) ) do
      Begin
        Inc(i);
        If doWrite Then
        Begin
          TmpTime := GetTickCount64();
          fs.WriteBuffer(Buffer^, BufferSize);
          FlushFileBuffers(fs.Handle);
          BytesWritten:= BytesWritten + BufferSize;
          Elapsed := GetTickCount() - StartTime;

          try
            //Form1.StatusBar1.Panels[2].Text := 'Speed: ' + FormatFloat('0.00', (byteswritten / 1024/1024) / (elapsed / 1000)) + ' mb/s';
          Except
          end;

          tmptime2 := GetTickCount64();

          StatusLine:='';
          StatusLine:=StatusLine + ' I='           + IntToStr(i);
          StatusLine:=StatusLine + ' BytesWritten='+ IntToStr(BytesWritten);
          StatusLine:=StatusLine + ' CycleTime='   + IntToStr(tmptime2 - tmptime);
          Log(StatusLine);

          Application.ProcessMessages;
        end
        else
        begin
          Log('Aborting!');
        end;
        Application.ProcessMessages;
      end;
      Log('Done!');
    finally
      // Close the file handle
      fs.Free;
      Log('Closing file handle.');
      //Free;
    end;
    EndTime:=GetTickCount();
    WriteTime:=(endtime-starttime)/1000;
    WriteSpeed:=BytesWritten/1024/1024/writetime;
    if doWrite then
    else
        Log('Write aborted');
  finally
    Freemem(Buffer);
  end;
end;

New Paste


Do not write anything in this field if you're a human.

Go to most recent paste.