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
This commit is contained in:
alecf%netscape.com 2003-03-06 19:54:51 +00:00
Родитель dcad0d8960
Коммит 5b7536c806
12 изменённых файлов: 150 добавлений и 58 удалений

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

@ -310,7 +310,7 @@ PRInt32 nsCSSScanner::Read(PRInt32& aErrorCode)
}
if (mOffset == mCount) {
mOffset = 0;
aErrorCode = mInput->Read(mBuffer, 0, BUFFER_SIZE, (PRUint32*)&mCount);
aErrorCode = mInput->Read(mBuffer, BUFFER_SIZE, (PRUint32*)&mCount);
if (NS_FAILED(aErrorCode) || mCount == 0) {
mCount = 0;
return -1;

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

@ -609,7 +609,7 @@ nsExpatDriver::HandleExternalEntityRef(const PRUnichar *openEntityNames,
mInExternalDTD = PR_TRUE;
while (NS_SUCCEEDED(uniIn->Read(uniBuff, 0, 1024, &readCount)) && result) {
while (NS_SUCCEEDED(uniIn->Read(uniBuff, 1024, &readCount)) && result) {
if (readCount) {
// Pass the buffer to expat for parsing
result = XML_Parse(entParser, (char *)uniBuff, readCount * sizeof(PRUnichar), 0);

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

@ -95,7 +95,6 @@ nsConverterInputStream::Close()
NS_IMETHODIMP
nsConverterInputStream::Read(PRUnichar* aBuf,
PRUint32 aOffset,
PRUint32 aCount,
PRUint32 *aReadCount)
{
@ -112,13 +111,56 @@ nsConverterInputStream::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
nsConverterInputStream::ReadSegments(nsWriteUnicharSegmentFun aWriter,
void* aClosure,
PRUint32 aCount, PRUint32 *aReadCount)
{
NS_ASSERTION(mUnicharDataLength >= mUnicharDataOffset, "unsigned madness");
PRUint32 bytesToWrite = mUnicharDataLength - mUnicharDataOffset;
nsresult rv;
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;
}
PRUint32
nsConverterInputStream::Fill(nsresult * aErrorCode)
{

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

@ -58,12 +58,14 @@ class nsConverterInputStream : nsIConverterInputStream {
public:
NS_DECL_ISUPPORTS
NS_IMETHOD Read(PRUnichar* aBuf,
PRUint32 aOffset,
PRUint32 aCount,
PRUint32 *aReadCount);
NS_IMETHOD Close();
NS_IMETHOD Init(nsIInputStream* aStream, const PRUnichar *aCharset,
PRInt32 aBufferSize, PRBool aRecoverFromErrors);
NS_IMETHOD ReadSegments(nsWriteUnicharSegmentFun aWriter,
void* aClosure,
PRUint32 aCount, PRUint32* aReadCount);
nsConverterInputStream() :
mLastErrorCode(NS_OK),

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

@ -310,7 +310,7 @@ PRInt32 nsCSSScanner::Read(PRInt32& aErrorCode)
}
if (mOffset == mCount) {
mOffset = 0;
aErrorCode = mInput->Read(mBuffer, 0, BUFFER_SIZE, (PRUint32*)&mCount);
aErrorCode = mInput->Read(mBuffer, BUFFER_SIZE, (PRUint32*)&mCount);
if (NS_FAILED(aErrorCode) || mCount == 0) {
mCount = 0;
return -1;

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

@ -609,7 +609,7 @@ nsExpatDriver::HandleExternalEntityRef(const PRUnichar *openEntityNames,
mInExternalDTD = PR_TRUE;
while (NS_SUCCEEDED(uniIn->Read(uniBuff, 0, 1024, &readCount)) && result) {
while (NS_SUCCEEDED(uniIn->Read(uniBuff, 1024, &readCount)) && result) {
if (readCount) {
// Pass the buffer to expat for parsing
result = XML_Parse(entParser, (char *)uniBuff, readCount * sizeof(PRUnichar), 0);

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

@ -39,7 +39,6 @@
#include "nscore.h"
#include "nsISupports.h"
class nsIUnicharInputStream;
#define NS_IUNICHARBUFFER_IID \
{ 0x14cf6970, 0x93b5, 0x11d1, \
@ -55,8 +54,6 @@ public:
NS_IMETHOD_(PRInt32) GetBufferSize() const = 0;
NS_IMETHOD_(PRUnichar*) GetBuffer() const = 0;
NS_IMETHOD_(PRBool) Grow(PRInt32 aNewSize) = 0;
NS_IMETHOD_(PRInt32) Fill(nsresult* aErrorCode, nsIUnicharInputStream* aStream,
PRInt32 aKeep) = 0;
};
/// Factory method for nsIUnicharBuffer.

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

@ -369,7 +369,7 @@ nsPersistentProperties::Read()
PRUint32 nRead;
nsresult ret;
ret = mIn->Read(&c, 0, 1, &nRead);
ret = mIn->Read(&c, 1, &nRead);
if (ret == NS_OK && nRead == 1) {
return c;
}

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

@ -37,7 +37,6 @@
* ***** END LICENSE BLOCK ***** */
#include "nsUnicharBuffer.h"
#include "nsIUnicharInputStream.h"
#include "nsCRT.h"
#define MIN_BUFFER_SIZE 32
@ -122,40 +121,6 @@ UnicharBufferImpl::Grow(PRInt32 aNewSize)
return PR_FALSE;
}
NS_IMETHODIMP_(PRInt32)
UnicharBufferImpl::Fill(nsresult* aErrorCode,
nsIUnicharInputStream* aStream,
PRInt32 aKeep)
{
NS_PRECONDITION(nsnull != aStream, "null stream");
NS_PRECONDITION(PRUint32(aKeep) < PRUint32(mLength), "illegal keep count");
if ((nsnull == aStream) || (PRUint32(aKeep) >= PRUint32(mLength))) {
// whoops
*aErrorCode = NS_BASE_STREAM_ILLEGAL_ARGS;
return -1;
}
if (0 != aKeep) {
// Slide over kept data
memmove(mBuffer, mBuffer + (mLength - aKeep),
aKeep * sizeof(PRUnichar));
}
// Read in some new data
mLength = aKeep;
PRInt32 amount = mSpace - aKeep;
PRUint32 nb;
NS_ASSERTION(aKeep >= 0, "unsigned madness");
NS_ASSERTION(amount >= 0, "unsigned madness");
*aErrorCode = aStream->Read(mBuffer, (PRUint32)aKeep, (PRUint32)amount, &nb);
if (NS_SUCCEEDED(*aErrorCode)) {
mLength += nb;
}
else
nb = 0;
return nb;
}
NS_COM nsresult
NS_NewUnicharBuffer(nsIUnicharBuffer** aInstancePtrResult,
nsISupports* aOuter,

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

@ -54,8 +54,6 @@ public:
NS_IMETHOD_(PRInt32) GetBufferSize() const;
NS_IMETHOD_(PRUnichar*) GetBuffer() const;
NS_IMETHOD_(PRBool) Grow(PRInt32 aNewSize);
NS_IMETHOD_(PRInt32) Fill(nsresult* aErrorCode, nsIUnicharInputStream* aStream,
PRInt32 aKeep);
PRUnichar* mBuffer;
PRUint32 mSpace;

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

@ -41,6 +41,14 @@
#include "nscore.h"
class nsString;
class nsIUnicharInputStream;
typedef NS_CALLBACK(nsWriteUnicharSegmentFun)(nsIUnicharInputStream *aInStream,
void *aClosure,
const PRUnichar *aFromSegment,
PRUint32 aToOffset,
PRUint32 aCount,
PRUint32 *aWriteCount);
#define NS_IUNICHAR_INPUT_STREAM_IID \
{ 0x2d97fbf0, 0x93b5, 0x11d1, \
@ -49,15 +57,18 @@ class nsString;
/** Abstract unicode character input stream
* @see nsIInputStream
*/
class nsIUnicharInputStream : public nsISupports {
class NS_NO_VTABLE nsIUnicharInputStream : public nsISupports {
public:
NS_DEFINE_STATIC_IID_ACCESSOR(NS_IUNICHAR_INPUT_STREAM_IID)
NS_IMETHOD Read(PRUnichar* aBuf,
PRUint32 aOffset,
PRUint32 aCount,
PRUint32 *aReadCount) = 0;
NS_IMETHOD Close() = 0;
NS_IMETHOD ReadSegments(nsWriteUnicharSegmentFun aWriter,
void* aClosure,
PRUint32 aCount,
PRUint32 *aReadCount) = 0;
};
/**

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

@ -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) {