Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

nsIOutputStream

An interface describing a writable stream of data.
Inherits from: nsISupports Last changed in Gecko 1.0

An output stream may be "blocking" or "non-blocking" (see the IsNonBlocking() method). A blocking output stream may suspend the calling thread in order to satisfy a call to Close(), Flush(), write(), writeFrom(), or writeSegments(). A non-blocking output stream, on the other hand, must not block the calling thread of execution.

Note: Blocking output streams are often written to on a background thread to avoid locking up the main application thread. For this reason, it is generally the case that a blocking output stream should be implemented using thread-safe AddRef and Release.

Method overview

void close();
void flush();
boolean isNonBlocking();
unsigned long write(in string aBuf, in unsigned long aCount);
unsigned long writeFrom(in nsIInputStream aFromStream, in unsigned long aCount);
unsigned long writeSegments(in nsReadSegmentFun aReader, in voidPtr aClosure, in unsigned long aCount); Native code only!

Methods

close()

Close the stream. Forces the output stream to flush() any buffered data.

Note: This method may be called more than once, but subsequent calls are ignored.

void close();
Parameters

None.

Exceptions thrown
NS_BASE_STREAM_WOULD_BLOCK
Indicates that closing the output stream would block the calling thread for an indeterminate amount of time. This exception may only be thrown if isNonBlocking() returns true.

flush()

This method may be called to instruct the output stream to write out the contents of any internal buffers to a low-level data sink, such as a file on disk. However, the flush method may simply do nothing depending on the implementation of the output stream.

void flush();
Parameters

None.

Exceptions thrown
NS_BASE_STREAM_WOULD_BLOCK
Indicates that flushing the output stream would block the calling thread for an indeterminate amount of time. This exception may only be thrown if isNonBlocking() returns true.

isNonBlocking()

Note: Writing to a blocking output stream will block the calling thread until all given data can be consumed by the stream.

Note: A non-blocking output stream may implement nsIAsyncOutputStream to provide consumers with a way to wait for the stream to accept more data once its write() method is unable to accept any data without blocking.

boolean isNonBlocking();
Parameters

None.

Return value

true if stream is non-blocking.

Exceptions thrown
NS_BASE_STREAM_WOULD_BLOCK
A non-blocking stream may throw this exception when written to if space for the data is not immediately available.

write()

This method copies data from a buffer into the stream.

Note: Though this method is scriptable, JavaScript code must only pass an ASCII character string as the aBuf parameter. C++ code may however pass any arbitrary binary data, including data with embedded null bytes.

unsigned long write(
  in string aBuf,
  in unsigned long aCount
);
Parameters
aBuf
The buffer containing the data to be written.
aCount
The size of the buffer, or the maximum number of bytes to copy from the buffer.
Return value

This method returns the number of bytes copied from the buffer (may be less than aCount).

Exceptions thrown
NS_BASE_STREAM_WOULD_BLOCK
If writing to the output stream would block the calling thread (non-blocking mode only)

writeFrom()

This method copies data from an nsIInputStream to this nsIOutputStream.

A nsIOutputStream is not required to implement this method. In some contexts, writeFrom may be guaranteed to be implemented, but in general it is not. This method serves as an optimization.

Note: This method is defined by this interface in order to allow the output stream to efficiently copy the data from the input stream into its internal buffer (if any). If this method was provided as an external facility, a separate char* buffer would need to be used in order to call the output stream's other Write() method.

unsigned long writeFrom(
  in nsIInputStream aFromStream,
  in unsigned long aCount
);
Parameters
aFromStream
An nsIInputStream containing the data to be written.
aCount
The maximum number of bytes to write to the stream.
Return value

This method returns the number of bytes written to the stream (may be less than aCount).

Exceptions thrown
NS_BASE_STREAM_WOULD_BLOCK
Indicates that writing to the output stream would block the calling thread for an indeterminate amount of time. This exception may only be thrown if isNonBlocking() returns true.
NS_ERROR_NOT_IMPLEMENTED
Indicates that the stream does not implement this method. Typically, output streams that do not have an internal buffer will not implement this method since such an implementation would require an intermediate buffer unless aFromStream supported nsIInputStream.readSegments(), but that is not guaranteed.

Native code only!

writeSegments

Low-level write method that has access to the stream's underlying buffer. The reader function may be called multiple times for segmented buffers. This method is expected to keep calling the reader until either there is nothing left to write or the reader returns an error. This method should not call the reader with zero bytes to provide.

Note: 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.

unsigned long writeSegments(
  in nsReadSegmentFun aReader,
  in voidPtr aClosure,
  in unsigned long aCount
);
Parameters
aReader
A callback function that may be called multiple times. See nsReadSegmentFun for more details on this function.
aClosure
A parameter that is passed to aReader each time it is called.
aCount
The maximum number of bytes to write to the stream.
Return value

This method returns the number of bytes written to the stream (may be less than aCount).

Exceptions thrown
NS_BASE_STREAM_WOULD_BLOCK
Indicates that writing to the output stream would block the calling thread for an indeterminate amount of time. This exception may only be thrown if isNonBlocking() returns true.
NS_ERROR_NOT_IMPLEMENTED
Indicates that the stream does not have an internal buffer that can be written to directly.

Example

writeSegments() example
 // 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);
 }

Remarks

This interface was frozen for Gecko 1.0. See bug 124465 for details. From Gecko 2.0 interfaces are no longer frozen.

See also

Document Tags and Contributors

 Contributors to this page: Sheppy, darktrojan, trevorh, Ted_Mielczarek, Okome, Pmash
 Last updated by: Sheppy,