nsISupports
Last changed in Gecko 17.0 (Firefox 17.0 / Thunderbird 17.0 / SeaMonkey 2.14)An input stream may be "blocking" or "non-blocking" (see the isNonBlocking()
method). A blocking input stream may suspend the calling thread in order to satisfy a call to close()
, available()
, read()
, or readSegments()
. A non-blocking input stream, on the other hand, must not block the calling thread of execution.
unsigned long available(); Deprecated since Gecko 17.0 |
unsigned long long available(); |
void close(); |
boolean isNonBlocking(); |
unsigned long read(in charPtr aBuf, in unsigned long aCount); Native code only! |
unsigned long readSegments(in nsWriteSegmentFun aWriter, in voidPtr aClosure, in unsigned long aCount); Native code only! |
Determine number of bytes available in the stream.
In addition to the number of bytes available in the stream, this method also informs the caller of the current status of the stream. A stream that is closed will throw an exception when this method is called. That enables the caller to know the condition of the stream before attempting to read from it. If a stream is at end-of-file, but not closed, then this method returns 0 bytes available.
Note: Some nsIInputStream
implementations automatically close()
when end-of-file is reached; others do not.
unsigned long available();
None.
Number of bytes currently available in the stream, or PR_UINT32_MAX
if the size of the stream exceeds PR_UINT32_MAX
.
<other-error>
NS_BASE_STREAM_CLOSED
Close the stream. This method causes subsequent calls to read()
and readSegments()
to return 0 bytes read to indicate end-of-file.
void close();
None.
boolean isNonBlocking();
None.
true
if stream is non-blocking.
This method copies data from the stream into a buffer.
unsigned long read( in charPtr aBuf, in unsigned long aCount );
aBuf
aCount
This method returns the number of bytes copied from the stream (may be less than aCount). It returns 0 to indicate end-of-file.
<other-error>
NS_BASE_STREAM_WOULD_BLOCK
isNonBlocking()
returns true.This method provides direct access to the stream's internal buffer.
nsIInputStream
is not required to implement this method. In some contexts, readSegments
may be guaranteed to be implemented, but in general it is not. This method serves as an optimization.unsigned long readSegments( in nsWriteSegmentFun aWriter, in voidPtr aClosure, in unsigned long aCount );
aWriter
nsWriteSegmentFun
for more details on this function.aClosure
aCount
The number of bytes read from the stream (may be less than aCount). It returns 0 to indicate end-of-file.
<other-error>
NS_ERROR_NOT_IMPLEMENTED
NS_BASE_STREAM_WOULD_BLOCK
isNonBlocking()
returns true.read()
.nsresult ConsumeStream(nsIInputStream* aStream) { nsresult rv; uint32_t 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; }
readSegments()
.static NS_METHOD AppendSegment(nsIInputStream* aStream, void* aClosure, const char* aFromSegment, uint32_t aToOffset, uint32_t aCount, uint32_t* aWriteCount) { // aFromSegment now contains aCount bytes of data. nsACString* pBuf = (nsACString*) aClosure; pBuf->Append(aFromSegment, aCount); // Indicate that we have consumed all of aFromSegment *aWriteCount = aCount; return NS_OK; } // Copy the contents of aStream into aResultBuf as one contiguous // buffer. Use ReadSegments to avoid an intermediate buffer copy. nsresult CopyStream(nsIInputStream* aStream, nsACString& aResultBuf) { uint32_t numRead; return aStream->ReadSegments(AppendSegment, (void*) &aResultBuf, PR_UINT32_MAX, &numRead); }
This interface was frozen for Gecko 1.0. See bug 124465 for details. From Gecko 2.0 interfaces are no longer frozen.
nsIWriteSegmentFun
nsIScriptableInputStream
- If you need to read a stream from JavaScript.