fix for bug 191416 - clean up nsIUnicharInputStream to make it more like nsIInputStream:
- hide Fill() (or remove it where not necessary) - add ReadSegments() so that we can do reads without copying - remove the extra offset parameter to Read() r=dougt, sr=darin git-svn-id: svn://10.0.0.236/trunk@139039 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
@@ -57,9 +57,11 @@ public:
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
NS_IMETHOD Read(PRUnichar* aBuf,
|
||||
PRUint32 aOffset,
|
||||
PRUint32 aCount,
|
||||
PRUint32 *aReadCount);
|
||||
NS_IMETHOD ReadSegments(nsWriteUnicharSegmentFun aWriter,
|
||||
void* aClosure,
|
||||
PRUint32 aCount, PRUint32* aReadCount);
|
||||
NS_IMETHOD Close();
|
||||
|
||||
nsString* mString;
|
||||
@@ -81,10 +83,10 @@ StringUnicharInputStream::~StringUnicharInputStream()
|
||||
}
|
||||
}
|
||||
|
||||
nsresult StringUnicharInputStream::Read(PRUnichar* aBuf,
|
||||
PRUint32 aOffset,
|
||||
PRUint32 aCount,
|
||||
PRUint32 *aReadCount)
|
||||
NS_IMETHODIMP
|
||||
StringUnicharInputStream::Read(PRUnichar* aBuf,
|
||||
PRUint32 aCount,
|
||||
PRUint32 *aReadCount)
|
||||
{
|
||||
if (mPos >= mLen) {
|
||||
*aReadCount = 0;
|
||||
@@ -102,6 +104,36 @@ nsresult StringUnicharInputStream::Read(PRUnichar* aBuf,
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
StringUnicharInputStream::ReadSegments(nsWriteUnicharSegmentFun aWriter,
|
||||
void* aClosure,
|
||||
PRUint32 aCount, PRUint32 *aReadCount)
|
||||
{
|
||||
PRUint32 bytesWritten;
|
||||
PRUint32 totalBytesWritten = 0;
|
||||
|
||||
nsresult rv;
|
||||
aCount = PR_MIN(mString->Length() - mPos, aCount);
|
||||
|
||||
while (aCount) {
|
||||
rv = aWriter(this, aClosure, mString->get() + mPos,
|
||||
totalBytesWritten, aCount, &bytesWritten);
|
||||
|
||||
if (NS_FAILED(rv)) {
|
||||
// don't propagate errors to the caller
|
||||
break;
|
||||
}
|
||||
|
||||
aCount -= bytesWritten;
|
||||
totalBytesWritten += bytesWritten;
|
||||
mPos += bytesWritten;
|
||||
}
|
||||
|
||||
*aReadCount = totalBytesWritten;
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult StringUnicharInputStream::Close()
|
||||
{
|
||||
mPos = mLen;
|
||||
@@ -143,9 +175,12 @@ public:
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_IMETHOD Read(PRUnichar* aBuf,
|
||||
PRUint32 aOffset,
|
||||
PRUint32 aCount,
|
||||
PRUint32 *aReadCount);
|
||||
NS_IMETHOD ReadSegments(nsWriteUnicharSegmentFun aWriter,
|
||||
void* aClosure,
|
||||
PRUint32 aCount,
|
||||
PRUint32 *aReadCount);
|
||||
NS_IMETHOD Close();
|
||||
|
||||
protected:
|
||||
@@ -203,7 +238,6 @@ nsresult UTF8InputStream::Close()
|
||||
}
|
||||
|
||||
nsresult UTF8InputStream::Read(PRUnichar* aBuf,
|
||||
PRUint32 aOffset,
|
||||
PRUint32 aCount,
|
||||
PRUint32 *aReadCount)
|
||||
{
|
||||
@@ -221,13 +255,56 @@ nsresult UTF8InputStream::Read(PRUnichar* aBuf,
|
||||
if (rv > aCount) {
|
||||
rv = aCount;
|
||||
}
|
||||
memcpy(aBuf + aOffset, mUnicharData->GetBuffer() + mUnicharDataOffset,
|
||||
memcpy(aBuf, mUnicharData->GetBuffer() + mUnicharDataOffset,
|
||||
rv * sizeof(PRUnichar));
|
||||
mUnicharDataOffset += rv;
|
||||
*aReadCount = rv;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
UTF8InputStream::ReadSegments(nsWriteUnicharSegmentFun aWriter,
|
||||
void* aClosure,
|
||||
PRUint32 aCount, PRUint32 *aReadCount)
|
||||
{
|
||||
NS_ASSERTION(mUnicharDataLength >= mUnicharDataOffset, "unsigned madness");
|
||||
PRUint32 bytesToWrite = mUnicharDataLength - mUnicharDataOffset;
|
||||
nsresult rv = NS_OK;
|
||||
if (0 == bytesToWrite) {
|
||||
// Fill the unichar buffer
|
||||
bytesToWrite = Fill(&rv);
|
||||
if (bytesToWrite <= 0) {
|
||||
*aReadCount = 0;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
if (bytesToWrite > aCount)
|
||||
bytesToWrite = aCount;
|
||||
|
||||
PRUint32 bytesWritten;
|
||||
PRUint32 totalBytesWritten = 0;
|
||||
|
||||
while (bytesToWrite) {
|
||||
rv = aWriter(this, aClosure,
|
||||
mUnicharData->GetBuffer() + mUnicharDataOffset,
|
||||
totalBytesWritten, bytesToWrite, &bytesWritten);
|
||||
|
||||
if (NS_FAILED(rv)) {
|
||||
// don't propagate errors to the caller
|
||||
break;
|
||||
}
|
||||
|
||||
bytesToWrite -= bytesWritten;
|
||||
totalBytesWritten += bytesWritten;
|
||||
mUnicharDataOffset += bytesWritten;
|
||||
}
|
||||
|
||||
*aReadCount = totalBytesWritten;
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
PRInt32 UTF8InputStream::Fill(nsresult * aErrorCode)
|
||||
{
|
||||
if (nsnull == mInput) {
|
||||
|
||||
Reference in New Issue
Block a user