Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Member Functions
UTF8InputStream Class Reference
Inheritance diagram for UTF8InputStream:
Inheritance graph
Collaboration diagram for UTF8InputStream:
Collaboration graph

List of all members.

Public Member Functions

 UTF8InputStream ()
nsresult Init (nsIInputStream *aStream, PRUint32 aBufSize)
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.

Protected Member Functions

PRInt32 Fill (nsresult *aErrorCode)

Static Protected Member Functions

static void CountValidUTF8Bytes (const char *aBuf, PRUint32 aMaxBytes, PRUint32 &aValidUTF8bytes, PRUint32 &aValidUCS2bytes)

Protected Attributes

nsCOMPtr< nsIInputStreammInput
nsCOMPtr< nsIByteBuffermByteData
nsCOMPtr< nsIUnicharBuffermUnicharData
PRUint32 mByteDataOffset
PRUint32 mUnicharDataOffset
PRUint32 mUnicharDataLength

Private Member Functions

 ~UTF8InputStream ()

Detailed Description

Definition at line 195 of file nsUnicharInputStream.cpp.

Constructor & Destructor Documentation

Definition at line 246 of file nsUnicharInputStream.cpp.


Member Function Documentation

Close the stream and free associated resources.

This also closes the underlying stream, if any.

void UTF8InputStream::CountValidUTF8Bytes ( const char *  aBuf,
PRUint32  aMaxBytes,
PRUint32 aValidUTF8bytes,
PRUint32 aValidUCS2bytes 
) [static, protected]

Definition at line 406 of file nsUnicharInputStream.cpp.

  const char *c = aBuffer;
  const char *end = aBuffer + aMaxBytes;
  const char *lastchar = c;     // pre-initialize in case of 0-length buffer
  PRUint32 ucs2bytes = 0;
  while (c < end && *c) {
    lastchar = c;
    if (UTF8traits::isASCII(*c))
    else if (UTF8traits::is2byte(*c))
      c += 2;
    else if (UTF8traits::is3byte(*c))
      c += 3;
    else if (UTF8traits::is4byte(*c))
      c += 4;
    else if (UTF8traits::is5byte(*c))
      c += 5;
    else if (UTF8traits::is6byte(*c))
      c += 6;
    else {
      NS_WARNING("Unrecognized UTF8 string in UTF8InputStream::CountValidUTF8Bytes()");
      break; // Otherwise we go into an infinite loop.  But what happens now?
  if (c > end) {
    c = lastchar;

  aValidUTF8bytes = c - aBuffer;
  aValidUCS2chars = ucs2bytes;

Here is the call graph for this function:

Here is the caller graph for this function:

PRInt32 UTF8InputStream::Fill ( nsresult aErrorCode) [protected]

Definition at line 357 of file nsUnicharInputStream.cpp.

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

  NS_ASSERTION(mByteData->GetLength() >= mByteDataOffset, "unsigned madness");
  PRUint32 remainder = mByteData->GetLength() - mByteDataOffset;
  mByteDataOffset = remainder;
  PRInt32 nb = mByteData->Fill(aErrorCode, mInput, remainder);
  if (nb <= 0) {
    // Because we assume a many to one conversion, the lingering data
    // in the byte buffer must be a partial conversion
    // fragment. Because we know that we have recieved no more new
    // data to add to it, we can't convert it. Therefore, we discard
    // it.
    return nb;
  NS_ASSERTION(remainder + nb == mByteData->GetLength(), "bad nb");

  // Now convert as much of the byte buffer to unicode as possible
  PRUint32 srcLen, dstLen;
  CountValidUTF8Bytes(mByteData->GetBuffer(),remainder + nb, srcLen, dstLen);

  // the number of UCS2 characters should always be <= the number of
  // UTF8 chars
  NS_ASSERTION( (remainder+nb >= srcLen), "cannot be longer than out buffer");
  NS_ASSERTION(PRInt32(dstLen) <= mUnicharData->GetBufferSize(),
               "Ouch. I would overflow my buffer if I wasn't so careful.");
  if (PRInt32(dstLen) > mUnicharData->GetBufferSize()) return 0;
  ConvertUTF8toUTF16 converter(mUnicharData->GetBuffer());
  nsASingleFragmentCString::const_char_iterator start = mByteData->GetBuffer();
  nsASingleFragmentCString::const_char_iterator end = mByteData->GetBuffer() + srcLen;
  copy_string(start, end, converter);
  NS_ASSERTION(converter.Length() == dstLen, "length mismatch");
  mUnicharDataOffset = 0;
  mUnicharDataLength = converter.Length();
  mByteDataOffset = srcLen;
  return mUnicharDataLength;

Here is the call graph for this function:

nsresult UTF8InputStream::Init ( nsIInputStream aStream,
PRUint32  aBufSize 

Definition at line 228 of file nsUnicharInputStream.cpp.

  if (aBufferSize == 0) {
    aBufferSize = 8192;

  nsresult rv = NS_NewByteBuffer(getter_AddRefs(mByteData), nsnull, aBufferSize);
  if (NS_FAILED(rv)) return rv;
  rv = NS_NewUnicharBuffer(getter_AddRefs(mUnicharData), nsnull, aBufferSize);
  if (NS_FAILED(rv)) return rv;

  mInput = aStream;

  return NS_OK;

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned long nsIUnicharInputStream::read ( [array, size_is(aCount)] in PRUnichar  aBuf,
in unsigned long  aCount 
) [inherited]

Reads into a caller-provided character array.

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.
To read more than 2^32 characters, call this method multiple times.
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.

aWriterthe "consumer" of the data to be read
aClosureopaque parameter passed to writer
aCountthe maximum number of characters to be read
number of characters read (may be less than aCount)
0 if reached end of file (or if aWriter refused to consume data)
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.

aCountThe number of characters that should be read
The number of characters that were read.

Member Data Documentation

Definition at line 212 of file nsUnicharInputStream.cpp.

Definition at line 215 of file nsUnicharInputStream.cpp.

Definition at line 211 of file nsUnicharInputStream.cpp.

Definition at line 213 of file nsUnicharInputStream.cpp.

Definition at line 217 of file nsUnicharInputStream.cpp.

Definition at line 216 of file nsUnicharInputStream.cpp.

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