Visit Mozilla.org

nsIInputStream:read

From MDC

« XPCOM API Reference

Contents

[edit] Summary

This method copies data from the stream into a buffer.

 [noscript] unsigned long read(
   in charPtr aBuf,
   in unsigned long aCount
 );

[edit] Parameters

aBuf
[in] The buffer into which the data from the stream is copied.
aCount
[in] The size of the buffer, or the maximum number of bytes to copy from the stream.

[edit] Return Values

This method returns the number of bytes copied from the stream. It returns 0 to indicate end-of-file.

[edit] Exceptions Thrown

NS_BASE_STREAM_WOULD_BLOCK
Indicates that reading from the input stream would block the calling thread for indeterminate amount of time. This exception may only be thrown if isNonBlocking returns true.

[edit] Example Code

 // Consume all data from an input stream...

 nsresult ConsumeStream(nsIInputStream* aStream)
 {
   nsresult rv;
   PRUint32 numRead;
   char buf[512];

   while (1)
   {
     rv = aStream->Read(buf, sizeof(buf), &numRead);
     if (NS_FAILED(rv))
     {
       printf("### error reading stream: %x\n", rv);
       break;
     }
     if (numRead == 0)
       break;

     // buf now contains numRead bytes of data
   }

   return rv;
 }

[edit] See Also

isNonBlocking