This method provides direct access to the stream's internal buffer for writing.
[noscript] unsigned long writeSegments( in nsReadSegmentFun aReader, in voidPtr aClosure, in unsigned long aCount );
nsReadSegmentFun for more details on this function. A nsIOutputStream is not required to implement this method. In some contexts, writeSegments may be guaranteed to be implemented, but in general it is not. This method serves as an optimization.
// Copy data from a string to a stream
static NS_METHOD CopySegment(nsIInputStream* aStream,
void* aClosure,
char* aToSegment,
PRUint32 aFromOffset,
PRUint32 aCount,
PRUint32* aReadCount)
{
// aFromSegment now contains aCount bytes of data.
nsACString* pBuf = (nsACString*) aClosure;
const char* data;
PRUint32 len = NS_CStringGetData(&data);
data += aFromOffset;
len -= aFromOffset;
if (len > aCount)
len = aCount;
memcpy(aToSegment, data, len);
// Indicate that we have copied len bytes to the segment
*aReadCount = len;
return NS_OK;
}
// Write the contents of aSource into aStream, using WriteSegments
// to avoid intermediate buffer copies.
nsresult WriteStream(const nsACString& aSource, nsIInputStream* aStream)
{
PRUint32 num;
return aStream->WriteSegments(CopySegment, (void*) &aSource,
aSource.Length(), &num);
}
Page last modified 23:57, 23 Apr 2006 by Pmash