NS_IMETHODIMP nsUrlClassifierStreamUpdater::OnDataAvailable(nsIRequest *request, nsISupports* context, nsIInputStream *aIStream, uint64_t aSourceOffset, uint32_t aLength) { if (!mDBService) return NS_ERROR_NOT_INITIALIZED; LOG(("OnDataAvailable (%d bytes)", aLength)); nsresult rv; // Copy the data into a nsCString nsCString chunk; rv = NS_ConsumeStream(aIStream, aLength, chunk); NS_ENSURE_SUCCESS(rv, rv); //LOG(("Chunk (%d): %s\n\n", chunk.Length(), chunk.get())); rv = mDBService->UpdateStream(chunk); NS_ENSURE_SUCCESS(rv, rv); return NS_OK; }
NS_IMETHODIMP nsJSONListener::OnDataAvailable(nsIRequest *aRequest, nsISupports *aContext, nsIInputStream *aStream, uint64_t aOffset, uint32_t aLength) { nsresult rv = NS_OK; if (mNeedsConverter && mSniffBuffer.Length() < 4) { uint32_t readCount = (aLength < 4) ? aLength : 4; rv = NS_ConsumeStream(aStream, readCount, mSniffBuffer); NS_ENSURE_SUCCESS(rv, rv); if (mSniffBuffer.Length() < 4) return NS_OK; } char buffer[JSON_STREAM_BUFSIZE]; unsigned long bytesRemaining = aLength - mSniffBuffer.Length(); while (bytesRemaining) { unsigned int bytesRead; rv = aStream->Read(buffer, std::min((unsigned long)sizeof(buffer), bytesRemaining), &bytesRead); NS_ENSURE_SUCCESS(rv, rv); rv = ProcessBytes(buffer, bytesRead); NS_ENSURE_SUCCESS(rv, rv); bytesRemaining -= bytesRead; } return rv; }
static bool test_consume_stream() { const char kData[] = "Get your facts first, and then you can distort them as much as you " "please."; nsCOMPtr<nsIInputStream> input; nsCOMPtr<nsIOutputStream> output; NS_NewPipe(getter_AddRefs(input), getter_AddRefs(output), 10, PR_UINT32_MAX); if (!input || !output) return PR_FALSE; PRUint32 n = 0; output->Write(kData, sizeof(kData) - 1, &n); if (n != (sizeof(kData) - 1)) return PR_FALSE; output = nsnull; // close output nsCString buf; if (NS_FAILED(NS_ConsumeStream(input, PR_UINT32_MAX, buf))) return PR_FALSE; if (!buf.Equals(kData)) return PR_FALSE; return PR_TRUE; }
// Synchronously consume the given input stream and validate the resulting data // against the given string of expected values. void ConsumeAndValidateStream(nsIInputStream* aStream, const nsACString& aExpectedData) { nsAutoCString outputData; nsresult rv = NS_ConsumeStream(aStream, UINT32_MAX, outputData); ASSERT_TRUE(NS_SUCCEEDED(rv)); ASSERT_EQ(aExpectedData.Length(), outputData.Length()); ASSERT_TRUE(aExpectedData.Equals(outputData)); }
NS_IMETHODIMP nsStreamCipher::UpdateFromStream(nsIInputStream *aStream, PRInt32 aLen) { if (!mContext) return NS_ERROR_NOT_INITIALIZED; nsCString inputString; nsresult rv = NS_ConsumeStream(aStream, aLen, inputString); NS_ENSURE_SUCCESS(rv, rv); return UpdateFromString(inputString); }
NS_IMETHODIMP FaviconLoadListener::OnDataAvailable(nsIRequest *aRequest, nsISupports *aContext, nsIInputStream *aInputStream, PRUint32 aOffset, PRUint32 aCount) { nsCString buffer; nsresult rv = NS_ConsumeStream(aInputStream, aCount, buffer); if (rv != NS_BASE_STREAM_WOULD_BLOCK && NS_FAILED(rv)) return rv; mData.Append(buffer); return NS_OK; }
TEST(CloneInputStream, CloneMultiplexStreamPartial) { nsCOMPtr<nsIMultiplexInputStream> stream = do_CreateInstance("@mozilla.org/io/multiplex-input-stream;1"); ASSERT_TRUE(stream); nsTArray<char> inputData; testing::CreateData(1024, inputData); for (uint32_t i = 0; i < 2; ++i) { nsCString inputString(inputData.Elements(), inputData.Length()); nsCOMPtr<nsIInputStream> base; nsresult rv = NS_NewCStringInputStream(getter_AddRefs(base), inputString); ASSERT_TRUE(NS_SUCCEEDED(rv)); rv = stream->AppendStream(base); ASSERT_TRUE(NS_SUCCEEDED(rv)); } // Fail when first stream read, but second hasn't been started. nsAutoPtr<char> buffer(new char[1024]); uint32_t read; nsresult rv = stream->Read(buffer, 1024, &read); ASSERT_TRUE(NS_SUCCEEDED(rv)); nsCOMPtr<nsIInputStream> clone; rv = NS_CloneInputStream(stream, getter_AddRefs(clone)); ASSERT_TRUE(NS_FAILED(rv)); // Fail after beginning read of second stream. rv = stream->Read(buffer, 512, &read); ASSERT_TRUE(NS_SUCCEEDED(rv) && read == 512); rv = NS_CloneInputStream(stream, getter_AddRefs(clone)); ASSERT_TRUE(NS_FAILED(rv)); // Fail at the end. nsAutoCString consumed; rv = NS_ConsumeStream(stream, UINT32_MAX, consumed); ASSERT_TRUE(NS_SUCCEEDED(rv)); rv = NS_CloneInputStream(stream, getter_AddRefs(clone)); ASSERT_TRUE(NS_FAILED(rv)); }
NS_IMETHODIMP nsUrlClassifierHashCompleterRequest::OnDataAvailable(nsIRequest *request, nsISupports *context, nsIInputStream *stream, PRUint32 sourceOffset, PRUint32 length) { LOG(("nsUrlClassifierHashCompleter::OnDataAvailable [%p]", this)); if (mShuttingDown) return NS_ERROR_ABORT; nsCAutoString piece; nsresult rv = NS_ConsumeStream(stream, length, piece); NS_ENSURE_SUCCESS(rv, rv); mResponse.Append(piece); return NS_OK; }
// nsFaviconService::OptimizeFaviconImage // // Given a blob of data (a image file already read into a buffer), optimize // its size by recompressing it as a 16x16 PNG. nsresult nsFaviconService::OptimizeFaviconImage(const PRUint8* aData, PRUint32 aDataLen, const nsACString& aMimeType, nsACString& aNewData, nsACString& aNewMimeType) { nsresult rv; nsCOMPtr<imgITools> imgtool = do_CreateInstance("@mozilla.org/image/tools;1"); nsCOMPtr<nsIInputStream> stream; rv = NS_NewByteInputStream(getter_AddRefs(stream), reinterpret_cast<const char*>(aData), aDataLen, NS_ASSIGNMENT_DEPEND); NS_ENSURE_SUCCESS(rv, rv); // decode image nsCOMPtr<imgIContainer> container; rv = imgtool->DecodeImageData(stream, aMimeType, getter_AddRefs(container)); NS_ENSURE_SUCCESS(rv, rv); aNewMimeType.AssignLiteral("image/png"); // scale and recompress nsCOMPtr<nsIInputStream> iconStream; rv = imgtool->EncodeScaledImage(container, aNewMimeType, 16, 16, getter_AddRefs(iconStream)); NS_ENSURE_SUCCESS(rv, rv); // Read the stream into a new buffer. rv = NS_ConsumeStream(iconStream, PR_UINT32_MAX, aNewData); NS_ENSURE_SUCCESS(rv, rv); return NS_OK; }
NS_IMETHODIMP DataStorage::Reader::Run() { nsresult rv; // Concurrent operations on nsIFile objects are not guaranteed to be safe, // so we clone the file while holding the lock and then release the lock. // At that point, we can safely operate on the clone. nsCOMPtr<nsIFile> file; { MutexAutoLock lock(mDataStorage->mMutex); // If we don't have a profile, bail. if (!mDataStorage->mBackingFile) { return NS_OK; } rv = mDataStorage->mBackingFile->Clone(getter_AddRefs(file)); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } } nsCOMPtr<nsIInputStream> fileInputStream; rv = NS_NewLocalFileInputStream(getter_AddRefs(fileInputStream), file); // If we failed for some reason other than the file doesn't exist, bail. if (NS_WARN_IF(NS_FAILED(rv) && rv != NS_ERROR_FILE_TARGET_DOES_NOT_EXIST && // on Unix rv != NS_ERROR_FILE_NOT_FOUND)) { // on Windows return rv; } // If there is a file with data in it, read it. If there isn't, // we'll essentially fall through to notifying that we're good to go. nsCString data; if (fileInputStream) { // Limit to 2MB of data, but only store sMaxDataEntries entries. rv = NS_ConsumeStream(fileInputStream, 1u << 21, data); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } } // Atomically parse the data and insert the entries read. // Don't clear existing entries - they may have been inserted between when // this read was kicked-off and when it was run. { MutexAutoLock lock(mDataStorage->mMutex); // The backing file consists of a list of // <key>\t<score>\t<last accessed time>\t<value>\n // The final \n is not optional; if it is not present the line is assumed // to be corrupt. int32_t currentIndex = 0; int32_t newlineIndex = 0; do { newlineIndex = data.FindChar('\n', currentIndex); // If there are no more newlines or the data table has too many // entries, we are done. if (newlineIndex < 0 || mDataStorage->mPersistentDataTable.Count() >= sMaxDataEntries) { break; } nsDependentCSubstring line(data, currentIndex, newlineIndex - currentIndex); currentIndex = newlineIndex + 1; nsCString key; Entry entry; nsresult parseRV = ParseLine(line, key, entry); if (NS_SUCCEEDED(parseRV)) { // It could be the case that a newer entry was added before // we got around to reading the file. Don't overwrite new entries. Entry newerEntry; bool present = mDataStorage->mPersistentDataTable.Get(key, &newerEntry); if (!present) { mDataStorage->mPersistentDataTable.Put(key, entry); } } } while (true); } return NS_OK; }