Bug 1754040 - Clean up common shared WriteSegments helper functions, r=necko-reviewers,xpcom-reviewers,mccr8

These helper methods are similar to the related ones for
nsIInputStream::ReadSegments, and can be used to implement
nsIOutputStream::Write and nsIOutputStream::WriteFrom in terms of
WriteSegments.

There were various places which used manual copies of these methods, which are
being unified.

Differential Revision: https://phabricator.services.mozilla.com/D138334
This commit is contained in:
Nika Layzell 2022-02-14 23:59:35 +00:00
Родитель a2de5b755d
Коммит de1e97c891
7 изменённых файлов: 28 добавлений и 31 удалений

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

@ -23,7 +23,7 @@ EncryptingOutputStreamBase::EncryptingOutputStreamBase(
NS_IMETHODIMP EncryptingOutputStreamBase::Write(const char* aBuf,
uint32_t aCount,
uint32_t* aResultOut) {
return WriteSegments(NS_CopySegmentToBuffer, const_cast<char*>(aBuf), aCount,
return WriteSegments(NS_CopyBufferToSegment, const_cast<char*>(aBuf), aCount,
aResultOut);
}

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

@ -49,7 +49,7 @@ MemoryOutputStream::Close() {
NS_IMETHODIMP
MemoryOutputStream::Write(const char* aBuf, uint32_t aCount,
uint32_t* _retval) {
return WriteSegments(NS_CopySegmentToBuffer, (char*)aBuf, aCount, _retval);
return WriteSegments(NS_CopyBufferToSegment, (char*)aBuf, aCount, _retval);
}
NS_IMETHODIMP

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

@ -1011,17 +1011,10 @@ nsBufferedOutputStream::Finish() {
return NS_OK;
}
static nsresult nsReadFromInputStream(nsIOutputStream* outStr, void* closure,
char* toRawSegment, uint32_t offset,
uint32_t count, uint32_t* readCount) {
nsIInputStream* fromStream = (nsIInputStream*)closure;
return fromStream->Read(toRawSegment, count, readCount);
}
NS_IMETHODIMP
nsBufferedOutputStream::WriteFrom(nsIInputStream* inStr, uint32_t count,
uint32_t* _retval) {
return WriteSegments(nsReadFromInputStream, inStr, count, _retval);
return WriteSegments(NS_CopyStreamToSegment, inStr, count, _retval);
}
NS_IMETHODIMP

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

@ -79,7 +79,7 @@ SnappyCompressOutputStream::Flush() {
NS_IMETHODIMP
SnappyCompressOutputStream::Write(const char* aBuf, uint32_t aCount,
uint32_t* aResultOut) {
return WriteSegments(NS_CopySegmentToBuffer, const_cast<char*>(aBuf), aCount,
return WriteSegments(NS_CopyBufferToSegment, const_cast<char*>(aBuf), aCount,
aResultOut);
}

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

@ -1719,19 +1719,10 @@ nsPipeOutputStream::WriteSegments(nsReadSegmentFun aReader, void* aClosure,
return rv;
}
static nsresult nsReadFromRawBuffer(nsIOutputStream* aOutStr, void* aClosure,
char* aToRawSegment, uint32_t aOffset,
uint32_t aCount, uint32_t* aReadCount) {
const char* fromBuf = (const char*)aClosure;
memcpy(aToRawSegment, &fromBuf[aOffset], aCount);
*aReadCount = aCount;
return NS_OK;
}
NS_IMETHODIMP
nsPipeOutputStream::Write(const char* aFromBuf, uint32_t aBufLen,
uint32_t* aWriteCount) {
return WriteSegments(nsReadFromRawBuffer, (void*)aFromBuf, aBufLen,
return WriteSegments(NS_CopyBufferToSegment, (void*)aFromBuf, aBufLen,
aWriteCount);
}
@ -1741,17 +1732,11 @@ nsPipeOutputStream::Flush(void) {
return NS_OK;
}
static nsresult nsReadFromInputStream(nsIOutputStream* aOutStr, void* aClosure,
char* aToRawSegment, uint32_t aOffset,
uint32_t aCount, uint32_t* aReadCount) {
nsIInputStream* fromStream = (nsIInputStream*)aClosure;
return fromStream->Read(aToRawSegment, aCount, aReadCount);
}
NS_IMETHODIMP
nsPipeOutputStream::WriteFrom(nsIInputStream* aFromStream, uint32_t aCount,
uint32_t* aWriteCount) {
return WriteSegments(nsReadFromInputStream, aFromStream, aCount, aWriteCount);
return WriteSegments(NS_CopyStreamToSegment, aFromStream, aCount,
aWriteCount);
}
NS_IMETHODIMP

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

@ -766,7 +766,7 @@ nsresult NS_CopySegmentToBuffer(nsIInputStream* aInStr, void* aClosure,
return NS_OK;
}
nsresult NS_CopySegmentToBuffer(nsIOutputStream* aOutStr, void* aClosure,
nsresult NS_CopyBufferToSegment(nsIOutputStream* aOutStr, void* aClosure,
char* aBuffer, uint32_t aOffset,
uint32_t aCount, uint32_t* aCountRead) {
const char* fromBuf = static_cast<const char*>(aClosure);
@ -775,6 +775,13 @@ nsresult NS_CopySegmentToBuffer(nsIOutputStream* aOutStr, void* aClosure,
return NS_OK;
}
nsresult NS_CopyStreamToSegment(nsIOutputStream* aOutputStream, void* aClosure,
char* aToSegment, uint32_t aFromOffset,
uint32_t aCount, uint32_t* aReadCount) {
nsIInputStream* fromStream = static_cast<nsIInputStream*>(aClosure);
return fromStream->Read(aToSegment, aCount, aReadCount);
}
nsresult NS_DiscardSegment(nsIInputStream* aInStr, void* aClosure,
const char* aBuffer, uint32_t aOffset,
uint32_t aCount, uint32_t* aCountWritten) {

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

@ -208,7 +208,19 @@ extern nsresult NS_CopySegmentToBuffer(nsIInputStream* aInputStream,
*
* @see nsIOutputStream.idl for a description of this function's parameters.
*/
extern nsresult NS_CopySegmentToBuffer(nsIOutputStream* aOutputStream,
extern nsresult NS_CopyBufferToSegment(nsIOutputStream* aOutputStream,
void* aClosure, char* aToSegment,
uint32_t aFromOffset, uint32_t aCount,
uint32_t* aReadCount);
/**
* This function is intended to be passed to nsIOutputStream::WriteSegments to
* copy data into the nsIOutputStream from a nsIInputStream passed as the
* aClosure parameter to the WriteSegments function.
*
* @see nsIOutputStream.idl for a description of this function's parameters.
*/
extern nsresult NS_CopyStreamToSegment(nsIOutputStream* aOutputStream,
void* aClosure, char* aToSegment,
uint32_t aFromOffset, uint32_t aCount,
uint32_t* aReadCount);