NMFileBuffer.pas
资源名称:FastNet.rar [点击查看]
上传用户:szzdds
上传日期:2013-09-18
资源大小:293k
文件大小:2k
源码类别:
Delphi控件源码
开发平台:
Delphi
- unit NMFileBuffer;
- interface
- uses
- Classes;
- type
- TNMFileBuffer = class( TObject )
- private
- FSource: TStream;
- FSourceSize: LongInt;
- FBuffer: PChar;
- FBufPos: PChar;
- FBufEnd: PChar;
- FBufSize: LongInt;
- protected
- public
- constructor Create( const aSource: TStream );
- destructor Destroy; override;
- function NextMemoryBuffer( const Ptr: PChar; const Counter: LongInt ): Boolean;
- property BufPos: PChar read FBufPos;
- property BufEnd: PChar read FBufEnd;
- property BufSize: LongInt read FBufSize;
- published
- end;
- implementation
- uses
- SysUtils;
- const
- MaxBufSize = $FFFD;
- constructor TNMFileBuffer.Create( const aSource: TStream );
- begin
- inherited Create;
- FBuffer := AllocMem( MaxBufSize + 2 );
- FSource := aSource;
- FSourceSize := FSource.Size;
- FSource.Position := 0;
- NextMemoryBuffer( FBufPos, 0 );
- end;
- destructor TNMFileBuffer.Destroy;
- begin
- FreeMem( FBuffer );
- end;
- function TNMFileBuffer.NextMemoryBuffer( const Ptr: PChar; const Counter: LongInt ): Boolean;
- var
- BytesRead: LongInt;
- FillPos: PChar;
- begin
- if FSource.Position < FSourceSize then
- begin
- FBufPos := FBuffer + 1;
- FillPos := FBufPos;
- if Counter > 0 then
- begin
- System.Move( Ptr^, FillPos^, Counter );
- inc( FillPos, Counter );
- end;
- BytesRead := FSource.Read( FillPos^, MaxBufSize - Counter );
- FBufSize := MaxBufSize;
- if BytesRead < MaxBufSize - Counter then
- begin
- ( FillPos + BytesRead )^ := #0;
- FBufEnd := FillPos + BytesRead;
- FBufSize := BytesRead;
- end;
- Result := True;
- end
- else
- begin
- Result := False;
- end;
- end;
- end.