| 1 |
{%MainUnit castledownload.pas} |
| 2 |
{ |
| 3 |
Copyright 2013-2020 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
{ Open a proper stream to read a file, fast (with buffering) and with seeking. |
| 20 |
This gives you a stream most comfortable for reading (buffering means |
| 21 |
that you can read small, comfortable pieces of it; seeking means |
| 22 |
you can jump freely to various file positions, back and forward). |
| 23 |
|
| 24 |
On different OSes or even compilers this may require a little different |
| 25 |
stream, so it's safest to just use this function. For example, |
| 26 |
traditional Classes.TFileStream doesn't do buffering. Although under Linux, |
| 27 |
the buffering of file handles is done at kernel level (so everything |
| 28 |
works fast), on Windows the slowdown is noticeable. |
| 29 |
This function will always create |
| 30 |
proper stream descendant, eventually wrapping some standard stream |
| 31 |
in a buffered stream with full seeking capability. |
| 32 |
|
| 33 |
@deprecated Instead of this, use @link(Download) with [soForceMemoryStream]. } |
| 34 |
function CreateReadFileStream(const Url: String): TStream; deprecated 'use Download(Url, [soForceMemoryStream])'; |
| 35 |
|
| 36 |
{ Save the contents of given Stream to an Url. } |
| 37 |
procedure StreamSaveToFile(Stream: TStream; const Url: String); |
| 38 |
|
| 39 |
{$endif} |
| 40 |
|
| 41 |
{$ifdef read_implementation} |
| 42 |
|
| 43 |
function CreateReadFileStream(const Url: String): TStream; |
| 44 |
begin |
| 45 |
Result := Download(Url, [soForceMemoryStream]); |
| 46 |
end; |
| 47 |
|
| 48 |
procedure StreamSaveToFile(Stream: TStream; const Url: String); |
| 49 |
const |
| 50 |
BufSize = 100000; |
| 51 |
var |
| 52 |
S : TStream; |
| 53 |
Buffer: Pointer; |
| 54 |
ReadCount: Integer; |
| 55 |
begin |
| 56 |
{ optimized implementation for TMemoryStream } |
| 57 |
if Stream is TMemoryStream then |
| 58 |
begin |
| 59 |
TMemoryStream(Stream).SaveToFile(UriToFilenameSafe(Url)); |
| 60 |
Exit; |
| 61 |
end; |
| 62 |
|
| 63 |
Buffer := GetMem(BufSize); |
| 64 |
try |
| 65 |
S := UrlSaveStream(Url); |
| 66 |
try |
| 67 |
repeat |
| 68 |
ReadCount := Stream.Read(Buffer^, BufSize); |
| 69 |
if ReadCount = 0 then |
| 70 |
Break else |
| 71 |
S.WriteBuffer(Buffer^, ReadCount); |
| 72 |
until false; |
| 73 |
finally |
| 74 |
S.free; |
| 75 |
end; |
| 76 |
finally FreeMem(Buffer) end; |
| 77 |
end; |
| 78 |
|
| 79 |
{$endif} |