In Indy 9 I had code to read data into a dynamically allocated char
buffer using ReadBuffer.
char* Buffer;
int Length;
...
ReadBuffer(Buffer, Length);
...
I have found that I need to do the following instead in Indy 10.
char* Buffer;
int Length;
...
TIdBytes byteBuf;
byteBuf.Length = Length;
IOHandler->ReadBytes(byteBuf, Length);
BytesToRaw(byteBuf, Buffer, Length);
...
My question is, is there a way to get Indy 10 to read directly into the
existing char buffer? I would like to avoid the extra allocations and
the buffer copy if possible.
TIA
Dennis Cote
> is there a way to get Indy 10 to read directly into the existing char
> buffer?
Indy does not support direct memory buffers anymore, by design. One thing
you can do, though, is derive a new class from TCustomMemoryStream that uses
the char* buffer as its underlying memory block. You could then use
ReadStream() to receive the data directly into the buffer. For example:
class TBufferStream : public TCustomMemoryStream
{
public:
__fastcall TBufferStream(void *Buffer, int Length)
: TCustomMemoryStream()
{
SetPointer(Buffer, Length);
}
int __fastcall Write(void *Buffer, int Count)
{
Count = min(Count, Size-Position);
if( Count > 0 )
{
memcpy(((LPBYTE)Memory) + Position, Buffer, Count);
Seek(Count, soFromCurrent);
return Count;
}
return 0;
}
};
char* Buffer;
int Length;
...
TBufferStream *strm = new TBufferStream(Buffer, Length);
IOHandler->ReadStream(strm, Length);
delete strm;
Gambit
> What you can also probably do, as I did using indy 9
> (sorry I do not use indy 10 yet) is get the socket handle
> similar to this...
You shouldn't be doing that. That completely bypasses Indy's ability to
manage and track the socket state.
Gambit