По мотивам - http://programmingmindstream.blogspot.ru/2014/03/blog-post_9768.html
Понятно, что CopyTo ВЫВОДИТСЯ из ОСТАЛЬНЫХ методов IStream. (хотя он у меня и написан в контексте "неймспейса" IStream, но понимающие люди увидят, что его легко можно "вынести за скобки")
Ссылка на код - https://sourceforge.net/p/rumtmarc/code-0/HEAD/tree/trunk/Blogger/RealWork/L3/l3BaseStream.pas
Сам код:
Понятно, что CopyTo ВЫВОДИТСЯ из ОСТАЛЬНЫХ методов IStream. (хотя он у меня и написан в контексте "неймспейса" IStream, но понимающие люди увидят, что его легко можно "вынести за скобки")
Ссылка на код - https://sourceforge.net/p/rumtmarc/code-0/HEAD/tree/trunk/Blogger/RealWork/L3/l3BaseStream.pas
Сам код:
function Tl3Stream.IStreamCopyTo(stm: IStream; cb: Large; out cbRead: Large;
out cbWritten: Large): HResult;
const
MaxBufSize = 1024 * 1024; // 1mb
var
Buffer : Pointer;
BufSize : Integer;
l_IntPartSize : Long;
l_NeedToRead : Long;
l_ReadedPart : Long;
BytesRead,
BytesWritten,
W : Large;
begin
{$IfDef nsTest}
Assert(g_IStreamCopyTo_Guard <= 0);
{$EndIf nsTest}
Result := S_OK;
BytesRead := 0;
BytesWritten := 0;
try
if (cb > MaxBufSize) then
BufSize := MaxBufSize
else
BufSize := Integer(cb);
l3System.GetLocalMem(Buffer, BufSize);
try
while cb > 0 do
begin
if cb > MaxInt then
l_IntPartSize := MaxInt
else
l_IntPartSize := cb;
while (l_IntPartSize > 0) do begin
if (l_IntPartSize > BufSize) then l_NeedToRead := BufSize else l_NeedToRead := l_IntPartSize;
l_ReadedPart := Self.Read(Buffer^, l_NeedToRead);
if (l_ReadedPart = 0) then Exit;
Inc(BytesRead, l_ReadedPart);
W := 0;
Result := stm.Write(Buffer, l_ReadedPart, @W);
Inc(BytesWritten, W);
if (Result = S_OK) and (Integer(W) <> l_ReadedPart) then Result := E_FAIL;
if Result <> S_OK then Exit;
Dec(l_IntPartSize, l_ReadedPart);
end;
Dec(cb, l_IntPartSize);
end;
finally
l3System.FreeLocalMem(Buffer);
if (@cbWritten <> nil) then cbWritten := BytesWritten;
if (@cbRead <> nil) then cbRead := BytesRead;
end;//try..finally
except
Result := E_UNEXPECTED;
end;//try..finally
end;
Комментариев нет:
Отправить комментарий