ImageConsumer::OnDataAvailable(nsIURI* aURL, nsIInputStream *pIStream, PRUint32 length) #endif
ImageConsumer::OnDataAvailable(nsIChannel* channel, nsISupports* aContext, nsIInputStream *pIStream, PRUint32 offset, PRUint32 length)
ImageConsumer::OnDataAvailable(nsIURI* aURL, nsIInputStream *pIStream, PRUint32 length)#endif{  PRUint32 max_read=0;  PRUint32 bytes_read = 0;  ilINetReader *reader = mURL->GetReader();  if (mInterrupted || mStatus != 0) {    mStatus = MK_INTERRUPTED;    reader->StreamAbort(mStatus);    NS_RELEASE(reader);    return NS_ERROR_ABORT;  }    nsresult err = 0;  PRUint32 nb;  do {    err = reader->WriteReady(&max_read); //max read is most decoder can handle    if(NS_FAILED(err))   //note length tells how much we already have.        break;    if(max_read < 0){            max_read = 128;    }    if (max_read > (length-bytes_read)) {      max_read = length-bytes_read;    }    if (max_read > IMAGE_BUF_SIZE) {      max_read = IMAGE_BUF_SIZE;    }        // make sure there's enough data available to decode the image.    // put test into WriteReady    if (mFirstRead && length < 4)      break;    err = pIStream->Read(mBuffer,                         max_read, &nb);    if (err == NS_BASE_STREAM_WOULD_BLOCK) {      NS_ASSERTION(nb == 0, "Data will be lost.");      err = NS_OK;      break;    }    if (NS_FAILED(err) || nb == 0) {      NS_ASSERTION(nb == 0, "Data will be lost.");      break;    }    bytes_read += nb;    if (mFirstRead == PR_TRUE) {                  err = reader->FirstWrite((const unsigned char *)mBuffer, nb);      mFirstRead = PR_FALSE; //? move after err chk?      /*        * If FirstWrite(...) fails then the image type       * cannot be determined and the il_container        * stream functions have not been initialized!       */      if (NS_FAILED(err)) {        mStatus = MK_IMAGE_LOSSAGE;        mInterrupted = PR_TRUE;	    NS_RELEASE(reader);        return NS_ERROR_ABORT;      }    }    err = reader->Write((const unsigned char *)mBuffer, (int32)nb);    if(NS_FAILED(err)){      mStatus = MK_IMAGE_LOSSAGE;      mInterrupted = PR_TRUE;	    NS_RELEASE(reader);      return NS_ERROR_ABORT;    }  } while(bytes_read < length);  if (NS_FAILED(err)) {    mStatus = MK_IMAGE_LOSSAGE;    mInterrupted = PR_TRUE;  }  if (bytes_read < length) {    // If we haven't emptied the stream, hold onto it, because    // we will need to read from it subsequently and we don't    // know if we'll get a OnDataAvailable call again.    //    // Addref the new stream before releasing the old one,    // in case it is the same stream!    NS_ADDREF(pIStream);    NS_IF_RELEASE(mStream);    mStream = pIStream;  } else {    NS_IF_RELEASE(mStream);  }  NS_RELEASE(reader);  return NS_OK;}