Bug 1360807 - FileReaderSync must work with sync inputStream - part 1 - SyncRead should read as much as required, r=smaug

This commit is contained in:
Andrea Marchesini 2017-05-09 11:03:40 +02:00
Родитель 001b63cc33
Коммит 4e5948b3f2
2 изменённых файлов: 33 добавлений и 9 удалений

Просмотреть файл

@ -78,7 +78,7 @@ FileReaderSync::ReadAsArrayBuffer(JSContext* aCx,
}
uint32_t numRead;
aRv = Read(stream, bufferData.get(), blobSize, &numRead);
aRv = SyncRead(stream, bufferData.get(), blobSize, &numRead);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
@ -110,7 +110,7 @@ FileReaderSync::ReadAsBinaryString(Blob& aBlob,
uint32_t numRead;
do {
char readBuf[4096];
aRv = Read(stream, readBuf, sizeof(readBuf), &numRead);
aRv = SyncRead(stream, readBuf, sizeof(readBuf), &numRead);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
@ -145,7 +145,7 @@ FileReaderSync::ReadAsText(Blob& aBlob,
}
uint32_t numRead = 0;
aRv = Read(stream, sniffBuf.BeginWriting(), sniffBuf.Length(), &numRead);
aRv = SyncRead(stream, sniffBuf.BeginWriting(), sniffBuf.Length(), &numRead);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
@ -361,8 +361,8 @@ NS_INTERFACE_MAP_END
} // anonymous
nsresult
FileReaderSync::Read(nsIInputStream* aStream, char* aBuffer, uint32_t aBufferSize,
uint32_t* aRead)
FileReaderSync::SyncRead(nsIInputStream* aStream, char* aBuffer,
uint32_t aBufferSize, uint32_t* aRead)
{
MOZ_ASSERT(aStream);
MOZ_ASSERT(aBuffer);
@ -370,10 +370,34 @@ FileReaderSync::Read(nsIInputStream* aStream, char* aBuffer, uint32_t aBufferSiz
// Let's try to read, directly.
nsresult rv = aStream->Read(aBuffer, aBufferSize, aRead);
if (NS_SUCCEEDED(rv) || rv != NS_BASE_STREAM_WOULD_BLOCK) {
// Nothing else to read.
if (rv == NS_BASE_STREAM_CLOSED ||
(NS_SUCCEEDED(rv) && *aRead == 0)) {
return NS_OK;
}
// An error.
if (NS_FAILED(rv) && rv != NS_BASE_STREAM_WOULD_BLOCK) {
return rv;
}
// All good.
if (NS_SUCCEEDED(rv)) {
// Not enough data, let's read recursively.
if (*aRead != aBufferSize) {
uint32_t byteRead = 0;
rv = SyncRead(aStream, aBuffer + *aRead, aBufferSize - *aRead, &byteRead);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
*aRead += byteRead;
}
return NS_OK;
}
// We need to proceed async.
nsCOMPtr<nsIAsyncInputStream> asyncStream = do_QueryInterface(aStream);
if (!asyncStream) {
@ -408,5 +432,5 @@ FileReaderSync::Read(nsIInputStream* aStream, char* aBuffer, uint32_t aBufferSiz
}
// Now, we can try to read again.
return Read(aStream, aBuffer, aBufferSize, aRead);
return SyncRead(aStream, aBuffer, aBufferSize, aRead);
}

Просмотреть файл

@ -32,8 +32,8 @@ private:
nsresult ConvertStream(nsIInputStream *aStream, const char *aCharset,
nsAString &aResult);
nsresult Read(nsIInputStream* aStream, char* aBuffer, uint32_t aBufferSize,
uint32_t* aRead);
nsresult SyncRead(nsIInputStream* aStream, char* aBuffer,
uint32_t aBufferSize, uint32_t* aRead);
public:
static already_AddRefed<FileReaderSync>