Re: probably not an Indy question

Giganews Newsgroups
Subject: Re: probably not an Indy question
Posted by:  Remy Lebeau (TeamB) (no.spam@no.spam.com)
Date: Wed, 29 Dec 2004

"xdiscent" <xdisce…@yahoo.com> wrote in message
news:6B3BA746D1B9E240xdisce…@yahoo.com...

> How do I temporarily write data to disk while the
> download aint finished yet?

You could just use a TFileStream instead of a TMemoryStream.  By default,
file writing is buffered by the OS and written to disk periodically.

If you do not want to rely on the OS, however, then you will have to write
your own TStream class that explicitally flushes its internal data to disk
periodically. For example:

    #define MEGABYTE 1048576

    class TMyFileStream : public TFileStream
    {
    private:
        int FNextFlushSize;
    public:
        __fastcall TMyFileStream(const AnsiString FileName, Word Mode)
            : TFileStream(FileName, Mode), FNextFlushSize(MEGABYTE)
        {
        }

        virtual int __fastcall Write(const void *Buffer, int Count)
        {
            int Result = TFileStream::Write(Buffer, Count);
            if( Result > 0 )
            {
                int iSize = Size;
                if( iSize >= FNextFlushSize )
                {
                    ::FlushFileBuffers((HANDLE)Handle);
                    FNextFlushSize = ((iSize + MEGABYTE) - (iSize %
MEGABYTE));
                }
            }
            return Result;
        }
    };

    #include <memory>

    std::auto_ptr<TIdHTTP> IdHttpClient(new TIdHTTP(NULL));
    std::auto_ptr<TMyFileStream> FStream(new TMyFileStream(cFileName,
fmCreate));
    IdHttpClient->Get(strUrl, FStream.get());

Gambit

Replies

None

In response to

probably not an Indy question posted by xdiscent on Thu, 30 Dec 2004