Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
nsConverterInputStream Class Reference

#include <nsConverterInputStream.h>

Inheritance diagram for nsConverterInputStream:
Inheritance graph
[legend]
Collaboration diagram for nsConverterInputStream:
Collaboration graph
[legend]

List of all members.

Public Member Functions

NS_DECL_ISUPPORTS
NS_DECL_NSIUNICHARINPUTSTREAM
NS_DECL_NSIUNICHARLINEINPUTSTREAM
NS_DECL_NSICONVERTERINPUTSTREAM 
nsConverterInputStream ()
virtual ~nsConverterInputStream ()
void init (in nsIInputStream aStream, in string aCharset, in long aBufferSize, in PRUnichar aReplacementChar)
 Initialize this stream.
unsigned long read ([array, size_is(aCount)] in PRUnichar aBuf, in unsigned long aCount)
 Reads into a caller-provided character array.
unsigned long readSegments (in nsWriteUnicharSegmentFun aWriter, in voidPtr aClosure, in unsigned long aCount)
 Low-level read method that has access to the stream's underlying buffer.
unsigned long readString (in unsigned long aCount, out AString aString)
 Read into a string object.
void close ()
 Close the stream and free associated resources.
boolean readLine (out AString aLine)
 Read a single line from the stream, where a line is a possibly zero length sequence of characters terminated by a CR, LF, CRLF, LFCR, or eof.

Public Attributes

const PRUnichar DEFAULT_REPLACEMENT_CHARACTER = 0xFFFD
 Default replacement char value, U+FFFD REPLACEMENT CHARACTER.

Private Member Functions

PRUint32 Fill (nsresult *aErrorCode)

Private Attributes

nsCOMPtr< nsIUnicodeDecodermConverter
nsCOMPtr< nsIByteBuffermByteData
nsCOMPtr< nsIUnicharBuffermUnicharData
nsCOMPtr< nsIInputStreammInput
nsresult mLastErrorCode
PRUint32 mLeftOverBytes
PRUint32 mUnicharDataOffset
PRUint32 mUnicharDataLength
PRUnichar mReplacementChar
nsLineBuffer< PRUnichar > * mLineBuffer

Detailed Description

Definition at line 58 of file nsConverterInputStream.h.


Constructor & Destructor Documentation

NS_DECL_ISUPPORTS NS_DECL_NSIUNICHARINPUTSTREAM NS_DECL_NSIUNICHARLINEINPUTSTREAM NS_DECL_NSICONVERTERINPUTSTREAM nsConverterInputStream::nsConverterInputStream ( ) [inline]
virtual nsConverterInputStream::~nsConverterInputStream ( ) [inline, virtual]

Definition at line 75 of file nsConverterInputStream.h.

{ Close(); }

Member Function Documentation

Close the stream and free associated resources.

This also closes the underlying stream, if any.

PRUint32 nsConverterInputStream::Fill ( nsresult aErrorCode) [private]

Definition at line 191 of file nsConverterInputStream.cpp.

{
  if (nsnull == mInput) {
    // We already closed the stream!
    *aErrorCode = NS_BASE_STREAM_CLOSED;
    return 0;
  }

  if (NS_FAILED(mLastErrorCode)) {
    // We failed to completely convert last time, and error-recovery
    // is disabled.  We will fare no better this time, so...
    *aErrorCode = mLastErrorCode;
    return 0;
  }
  
  // We assume a many to one conversion and are using equal sizes for
  // the two buffers.  However if an error happens at the very start
  // of a byte buffer we may end up in a situation where n bytes lead
  // to n+1 unicode chars.  Thus we need to keep track of the leftover
  // bytes as we convert.
  
  PRInt32 nb = mByteData->Fill(aErrorCode, mInput, mLeftOverBytes);
#if defined(DEBUG_bzbarsky) && 0
  for (unsigned int foo = 0; foo < mByteData->GetLength(); ++foo) {
    fprintf(stderr, "%c", mByteData->GetBuffer()[foo]);
  }
  fprintf(stderr, "\n");
#endif
  if (nb <= 0 && mLeftOverBytes == 0) {
    // No more data 
    *aErrorCode = NS_OK;
    return 0;
  }

  NS_ASSERTION(PRUint32(nb) + mLeftOverBytes == mByteData->GetLength(),
               "mByteData is lying to us somewhere");
  
  // Now convert as much of the byte buffer to unicode as possible
  mUnicharDataOffset = 0;
  mUnicharDataLength = 0;
  PRUint32 srcConsumed = 0;
  do {
    PRInt32 srcLen = mByteData->GetLength() - srcConsumed;
    PRInt32 dstLen = mUnicharData->GetBufferSize() - mUnicharDataLength;
    *aErrorCode = mConverter->Convert(mByteData->GetBuffer()+srcConsumed,
                                      &srcLen,
                                      mUnicharData->GetBuffer()+mUnicharDataLength,
                                      &dstLen);
    mUnicharDataLength += dstLen;
    // XXX if srcLen is negative, we want to drop the _first_ byte in
    // the erroneous byte sequence and try again.  This is not quite
    // possible right now -- see bug 160784
    srcConsumed += srcLen;
    if (NS_FAILED(*aErrorCode) && mReplacementChar) {
      NS_ASSERTION(0 < mUnicharData->GetBufferSize() - mUnicharDataLength,
                   "Decoder returned an error but filled the output buffer! "
                   "Should not happen.");
      mUnicharData->GetBuffer()[mUnicharDataLength++] = mReplacementChar;
      ++srcConsumed;
      // XXX this is needed to make sure we don't underrun our buffer;
      // bug 160784 again
      srcConsumed = PR_MAX(srcConsumed, 0);
      mConverter->Reset();
    }
    NS_ASSERTION(srcConsumed <= mByteData->GetLength(),
                 "Whoa.  The converter should have returned NS_OK_UDEC_MOREINPUT before this point!");
  } while (mReplacementChar &&
           NS_FAILED(*aErrorCode));

  mLeftOverBytes = mByteData->GetLength() - srcConsumed;

  return mUnicharDataLength;
}

Here is the call graph for this function:

void nsIConverterInputStream::init ( in nsIInputStream  aStream,
in string  aCharset,
in long  aBufferSize,
in PRUnichar  aReplacementChar 
) [inherited]

Initialize this stream.

Parameters:
aStreamThe underlying stream to read from.
aCharsetThe character encoding to use for converting the bytes of the stream. A null charset will be interpreted as UTF-8.
aBufferSizeHow many bytes to buffer.
aReplacementCharThe character to replace unknown byte sequences in the stream with. The standard replacement character is U+FFFD. A value of 0x0000 will cause an exception to be thrown if unknown byte sequences are encountered in the stream.
unsigned long nsIUnicharInputStream::read ( [array, size_is(aCount)] in PRUnichar  aBuf,
in unsigned long  aCount 
) [inherited]

Reads into a caller-provided character array.

Returns:
The number of characters that were successfully read. May be less than aCount, even if there is more data in the input stream. A return value of 0 means EOF.
Note:
To read more than 2^32 characters, call this method multiple times.
boolean nsIUnicharLineInputStream::readLine ( out AString  aLine) [inherited]

Read a single line from the stream, where a line is a possibly zero length sequence of characters terminated by a CR, LF, CRLF, LFCR, or eof.

The line terminator is not returned.

Return values:
falseEnd of file. This line is the last line of the file (aLine is valid).
trueThe file contains further lines.
Note:
Do not mix readLine with other read functions. Doing so can cause various problems and is not supported.
unsigned long nsIUnicharInputStream::readSegments ( in nsWriteUnicharSegmentFun  aWriter,
in voidPtr  aClosure,
in unsigned long  aCount 
) [inherited]

Low-level read method that has access to the stream's underlying buffer.

The writer function may be called multiple times for segmented buffers. ReadSegments is expected to keep calling the writer until either there is nothing left to read or the writer returns an error. ReadSegments should not call the writer with zero characters to consume.

Parameters:
aWriterthe "consumer" of the data to be read
aClosureopaque parameter passed to writer
aCountthe maximum number of characters to be read
Returns:
number of characters read (may be less than aCount)
0 if reached end of file (or if aWriter refused to consume data)
Exceptions:
NS_BASE_STREAM_WOULD_BLOCKif reading from the input stream would block the calling thread (non-blocking mode only)
<other-error>on failure

NOTE: this function may be unimplemented if a stream has no underlying buffer

unsigned long nsIUnicharInputStream::readString ( in unsigned long  aCount,
out AString  aString 
) [inherited]

Read into a string object.

Parameters:
aCountThe number of characters that should be read
Returns:
The number of characters that were read.

Member Data Documentation

Default replacement char value, U+FFFD REPLACEMENT CHARACTER.

Definition at line 52 of file nsIConverterInputStream.idl.

Definition at line 83 of file nsConverterInputStream.h.

Definition at line 82 of file nsConverterInputStream.h.

Definition at line 85 of file nsConverterInputStream.h.

Definition at line 87 of file nsConverterInputStream.h.

Definition at line 88 of file nsConverterInputStream.h.

Definition at line 93 of file nsConverterInputStream.h.

Definition at line 91 of file nsConverterInputStream.h.

Definition at line 84 of file nsConverterInputStream.h.

Definition at line 90 of file nsConverterInputStream.h.

Definition at line 89 of file nsConverterInputStream.h.


The documentation for this class was generated from the following files: