зеркало из https://github.com/mozilla/pjs.git
modified streaming APIs to be what the OJI guys want.
This commit is contained in:
Родитель
67b8719c02
Коммит
3f1cee6416
|
@ -31,7 +31,7 @@ public:
|
|||
virtual PRInt32 GetBufferSize() const;
|
||||
virtual char* GetBuffer() const;
|
||||
virtual PRBool Grow(PRInt32 aNewSize);
|
||||
virtual PRInt32 Fill(PRInt32* aErrorCode, nsIInputStream* aStream,
|
||||
virtual PRInt32 Fill(nsresult* aErrorCode, nsIInputStream* aStream,
|
||||
PRInt32 aKeep);
|
||||
|
||||
char* mBuffer;
|
||||
|
@ -94,14 +94,14 @@ PRBool ByteBufferImpl::Grow(PRInt32 aNewSize)
|
|||
return PR_FALSE;
|
||||
}
|
||||
|
||||
PRInt32 ByteBufferImpl::Fill(PRInt32* aErrorCode, nsIInputStream* aStream,
|
||||
PRInt32 ByteBufferImpl::Fill(nsresult* aErrorCode, nsIInputStream* 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_INPUTSTREAM_ILLEGAL_ARGS;
|
||||
*aErrorCode = NS_BASE_STREAM_ILLEGAL_ARGS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -113,10 +113,13 @@ PRInt32 ByteBufferImpl::Fill(PRInt32* aErrorCode, nsIInputStream* aStream,
|
|||
// Read in some new data
|
||||
mLength = aKeep;
|
||||
PRInt32 amount = mSpace - aKeep;
|
||||
PRInt32 nb = aStream->Read(aErrorCode, mBuffer, aKeep, amount);
|
||||
if (nb > 0) {
|
||||
PRInt32 nb;
|
||||
*aErrorCode = aStream->Read(mBuffer, aKeep, amount, &nb);
|
||||
if (NS_SUCCEEDED(*aErrorCode)) {
|
||||
mLength += nb;
|
||||
}
|
||||
else
|
||||
nb = 0;
|
||||
return nb;
|
||||
}
|
||||
|
||||
|
|
|
@ -31,12 +31,27 @@
|
|||
class nsIBaseStream : public nsISupports {
|
||||
public:
|
||||
|
||||
/** Return the number of bytes in the stream */
|
||||
virtual PRInt32 GetLength() = 0;
|
||||
|
||||
/** Close the stream. */
|
||||
virtual void Close() = 0;
|
||||
NS_IMETHOD
|
||||
Close(void) = 0;
|
||||
};
|
||||
|
||||
/** Error codes */
|
||||
//@{
|
||||
// XXX fix up the values so they are not total hacks... MMP
|
||||
/// End of file
|
||||
#define NS_BASE_STREAM_EOF 0x80001001
|
||||
/// Stream closed
|
||||
#define NS_BASE_STREAM_CLOSED 0x80001002
|
||||
/// Error from the operating system
|
||||
#define NS_BASE_STREAM_OSERROR 0x80001003
|
||||
/// Illegal arguments
|
||||
#define NS_BASE_STREAM_ILLEGAL_ARGS 0x80001004
|
||||
/// For unichar streams
|
||||
#define NS_BASE_STREAM_NO_CONVERTER 0x80001005
|
||||
/// For unichar streams
|
||||
#define NS_BASE_STREAM_BAD_CONVERSION 0x80001006
|
||||
//@}
|
||||
|
||||
|
||||
#endif /* nsInputStream_h___ */
|
||||
|
|
|
@ -44,7 +44,7 @@ public:
|
|||
|
||||
/** Fill the buffer with data from aStream. Don't grow the buffer, only
|
||||
* read until length of buffer equals buffer size. */
|
||||
virtual PRInt32 Fill(PRInt32* aErrorCode, nsIInputStream* aStream,
|
||||
virtual PRInt32 Fill(nsresult* aErrorCode, nsIInputStream* aStream,
|
||||
PRInt32 aKeep) = 0;
|
||||
};
|
||||
|
||||
|
|
|
@ -27,34 +27,28 @@
|
|||
/** Abstract byte input stream */
|
||||
class nsIInputStream : public nsIBaseStream {
|
||||
public:
|
||||
|
||||
/** Return the number of bytes in the stream
|
||||
* @param aLength out parameter to hold the length
|
||||
* of the stream. if an error occurs, the length
|
||||
* will be undefined
|
||||
* @return error status
|
||||
*/
|
||||
NS_IMETHOD
|
||||
GetLength(PRInt32 *aLength) = 0;
|
||||
|
||||
/** Read data from the stream.
|
||||
* @param aErrorCode the error code if an error occurs
|
||||
* @param aBuf the buffer into which the data is read
|
||||
* @param aOffset the start offset of the data
|
||||
* @param aCount the maximum number of bytes to read
|
||||
* @return number of bytes read or -1 if error
|
||||
* @param aReadCount out parameter to hold the number of
|
||||
* bytes read, eof if 0. if an error occurs, the
|
||||
* read count will be undefined
|
||||
* @return error status
|
||||
*/
|
||||
virtual PRInt32 Read(PRInt32* aErrorCode,
|
||||
char* aBuf,
|
||||
PRInt32 aOffset,
|
||||
PRInt32 aCount) = 0;
|
||||
NS_IMETHOD
|
||||
Read(char* aBuf, PRInt32 aOffset, PRInt32 aCount, PRInt32 *aReadCount) = 0;
|
||||
};
|
||||
|
||||
/** Error codes */
|
||||
//@{
|
||||
// XXX fix up the values to work with nsqresult
|
||||
/// End of file
|
||||
#define NS_INPUTSTREAM_EOF 1
|
||||
/// Stream closed
|
||||
#define NS_INPUTSTREAM_CLOSED 2
|
||||
/// Error from the operating system
|
||||
#define NS_INPUTSTREAM_OSERROR 3
|
||||
/// Illegal arguments
|
||||
#define NS_INPUTSTREAM_ILLEGAL_ARGS 4
|
||||
/// For unichar streams
|
||||
#define NS_INPUTSTREAM_NO_CONVERTER 5
|
||||
/// For unichar streams
|
||||
#define NS_INPUTSTREAM_BAD_CONVERSION 6
|
||||
//@}
|
||||
|
||||
#endif /* nsInputStream_h___ */
|
||||
|
|
|
@ -25,20 +25,21 @@
|
|||
{ 0x7f13b870, 0xe95f, 0x11d1, \
|
||||
{0xbe, 0xae, 0x00, 0x80, 0x5f, 0x8a, 0x66, 0xdc} }
|
||||
|
||||
/** Abstract byte input stream */
|
||||
/** Abstract byte output stream */
|
||||
class nsIOutputStream : public nsIBaseStream {
|
||||
public:
|
||||
|
||||
/** Write data into the stream.
|
||||
* @param aErrorCode the error code if an error occurs
|
||||
* @param aBuf the buffer into which the data is read
|
||||
* @param aOffset the start offset of the data
|
||||
* @param aCount the maximum number of bytes to read
|
||||
* @return number of bytes read or -1 if error
|
||||
* @param aWriteCount out parameter to hold the number of
|
||||
* bytes written. if an error occurs, the writecount
|
||||
* is undefined
|
||||
* @return error status
|
||||
*/
|
||||
virtual PRInt32 Write(PRInt32* aErrorCode,
|
||||
const char* aBuf,
|
||||
PRInt32 aOffset,
|
||||
PRInt32 aCount) = 0;
|
||||
NS_IMETHOD
|
||||
Write(const char* aBuf, PRInt32 aOffset, PRInt32 aCount, PRInt32 *aWriteCount) = 0;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@ public:
|
|||
virtual PRInt32 GetBufferSize() const = 0;
|
||||
virtual PRUnichar* GetBuffer() const = 0;
|
||||
virtual PRBool Grow(PRInt32 aNewSize) = 0;
|
||||
virtual PRInt32 Fill(PRInt32* aErrorCode, nsIUnicharInputStream* aStream,
|
||||
virtual PRInt32 Fill(nsresult* aErrorCode, nsIUnicharInputStream* aStream,
|
||||
PRInt32 aKeep) = 0;
|
||||
};
|
||||
|
||||
|
|
|
@ -42,11 +42,11 @@ enum nsCharSetID {
|
|||
*/
|
||||
class nsIUnicharInputStream : public nsISupports {
|
||||
public:
|
||||
virtual PRInt32 Read(PRInt32* aErrorCode,
|
||||
PRUnichar* aBuf,
|
||||
NS_IMETHOD Read(PRUnichar* aBuf,
|
||||
PRInt32 aOffset,
|
||||
PRInt32 aCount) = 0;
|
||||
virtual void Close() = 0;
|
||||
PRInt32 aCount,
|
||||
PRInt32 *aReadCount) = 0;
|
||||
NS_IMETHOD Close() = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -31,7 +31,7 @@ public:
|
|||
virtual PRInt32 GetBufferSize() const;
|
||||
virtual PRUnichar* GetBuffer() const;
|
||||
virtual PRBool Grow(PRInt32 aNewSize);
|
||||
virtual PRInt32 Fill(PRInt32* aErrorCode, nsIUnicharInputStream* aStream,
|
||||
virtual PRInt32 Fill(nsresult* aErrorCode, nsIUnicharInputStream* aStream,
|
||||
PRInt32 aKeep);
|
||||
|
||||
PRUnichar* mBuffer;
|
||||
|
@ -94,7 +94,7 @@ PRBool UnicharBufferImpl::Grow(PRInt32 aNewSize)
|
|||
return PR_FALSE;
|
||||
}
|
||||
|
||||
PRInt32 UnicharBufferImpl::Fill(PRInt32* aErrorCode,
|
||||
PRInt32 UnicharBufferImpl::Fill(nsresult* aErrorCode,
|
||||
nsIUnicharInputStream* aStream,
|
||||
PRInt32 aKeep)
|
||||
{
|
||||
|
@ -102,7 +102,7 @@ PRInt32 UnicharBufferImpl::Fill(PRInt32* aErrorCode,
|
|||
NS_PRECONDITION(PRUint32(aKeep) < PRUint32(mLength), "illegal keep count");
|
||||
if ((nsnull == aStream) || (PRUint32(aKeep) >= PRUint32(mLength))) {
|
||||
// whoops
|
||||
*aErrorCode = NS_INPUTSTREAM_ILLEGAL_ARGS;
|
||||
*aErrorCode = NS_BASE_STREAM_ILLEGAL_ARGS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -115,10 +115,13 @@ PRInt32 UnicharBufferImpl::Fill(PRInt32* aErrorCode,
|
|||
// Read in some new data
|
||||
mLength = aKeep;
|
||||
PRInt32 amount = mSpace - aKeep;
|
||||
PRInt32 nb = aStream->Read(aErrorCode, mBuffer, aKeep, amount);
|
||||
if (nb > 0) {
|
||||
PRInt32 nb;
|
||||
*aErrorCode = aStream->Read(mBuffer, aKeep, amount, &nb);
|
||||
if (NS_SUCCEEDED(*aErrorCode)) {
|
||||
mLength += nb;
|
||||
}
|
||||
else
|
||||
nb = 0;
|
||||
return nb;
|
||||
}
|
||||
|
||||
|
|
|
@ -36,11 +36,11 @@ public:
|
|||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
virtual PRInt32 Read(PRInt32* aErrorCode,
|
||||
PRUnichar* aBuf,
|
||||
NS_IMETHOD Read(PRUnichar* aBuf,
|
||||
PRInt32 aOffset,
|
||||
PRInt32 aCount);
|
||||
virtual void Close();
|
||||
PRInt32 aCount,
|
||||
PRInt32 *aReadCount);
|
||||
NS_IMETHOD Close();
|
||||
|
||||
nsString* mString;
|
||||
PRInt32 mPos;
|
||||
|
@ -62,12 +62,13 @@ StringUnicharInputStream::~StringUnicharInputStream()
|
|||
}
|
||||
}
|
||||
|
||||
PRInt32 StringUnicharInputStream::Read(PRInt32* aErrorCode,
|
||||
PRUnichar* aBuf,
|
||||
nsresult StringUnicharInputStream::Read(PRUnichar* aBuf,
|
||||
PRInt32 aOffset,
|
||||
PRInt32 aCount)
|
||||
PRInt32 aCount,
|
||||
PRInt32 *aReadCount)
|
||||
{
|
||||
if (mPos >= mLen) {
|
||||
*aReadCount = 0;
|
||||
return -1;
|
||||
}
|
||||
const PRUnichar* us = mString->GetUnicode();
|
||||
|
@ -77,15 +78,17 @@ PRInt32 StringUnicharInputStream::Read(PRInt32* aErrorCode,
|
|||
}
|
||||
nsCRT::memcpy(aBuf + aOffset, us + mPos, sizeof(PRUnichar) * amount);
|
||||
mPos += amount;
|
||||
return amount;
|
||||
*aReadCount = amount;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
void StringUnicharInputStream::Close()
|
||||
nsresult StringUnicharInputStream::Close()
|
||||
{
|
||||
mPos = mLen;
|
||||
if (nsnull != mString) {
|
||||
delete mString;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMPL_ISUPPORTS(StringUnicharInputStream, kIUnicharInputStreamIID);
|
||||
|
@ -163,7 +166,7 @@ NS_NewB2UConverter(nsIB2UConverter** aInstancePtrResult,
|
|||
return NS_ERROR_NO_AGGREGATION;
|
||||
}
|
||||
if (eCharSetID_IsoLatin1 != aCharSet) {
|
||||
return NS_INPUTSTREAM_NO_CONVERTER;
|
||||
return NS_BASE_STREAM_NO_CONVERTER;
|
||||
}
|
||||
IsoLatin1Converter* it = new IsoLatin1Converter();
|
||||
if (nsnull == it) {
|
||||
|
@ -182,14 +185,14 @@ public:
|
|||
~ConverterInputStream();
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
virtual PRInt32 Read(PRInt32* aErrorCode,
|
||||
PRUnichar* aBuf,
|
||||
NS_IMETHOD Read(PRUnichar* aBuf,
|
||||
PRInt32 aOffset,
|
||||
PRInt32 aCount);
|
||||
virtual void Close();
|
||||
PRInt32 aCount,
|
||||
PRInt32 *aReadCount);
|
||||
NS_IMETHOD Close();
|
||||
|
||||
protected:
|
||||
PRInt32 Fill(PRInt32* aErrorCode);
|
||||
PRInt32 Fill(nsresult * aErrorCode);
|
||||
|
||||
nsIInputStream* mInput;
|
||||
nsIB2UConverter* mConverter;
|
||||
|
@ -224,7 +227,7 @@ ConverterInputStream::~ConverterInputStream()
|
|||
Close();
|
||||
}
|
||||
|
||||
void ConverterInputStream::Close()
|
||||
nsresult ConverterInputStream::Close()
|
||||
{
|
||||
if (nsnull != mInput) {
|
||||
mInput->Release();
|
||||
|
@ -242,19 +245,23 @@ void ConverterInputStream::Close()
|
|||
mUnicharData->Release();
|
||||
mUnicharData = nsnull;
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
PRInt32 ConverterInputStream::Read(PRInt32* aErrorCode,
|
||||
PRUnichar* aBuf,
|
||||
nsresult ConverterInputStream::Read(PRUnichar* aBuf,
|
||||
PRInt32 aOffset,
|
||||
PRInt32 aCount)
|
||||
PRInt32 aCount,
|
||||
PRInt32 *aReadCount)
|
||||
{
|
||||
PRInt32 rv = mUnicharDataLength - mUnicharDataOffset;
|
||||
nsresult errorCode;
|
||||
if (0 == rv) {
|
||||
// Fill the unichar buffer
|
||||
rv = Fill(aErrorCode);
|
||||
rv = Fill(&errorCode);
|
||||
if (rv <= 0) {
|
||||
return rv;
|
||||
*aReadCount = 0;
|
||||
return errorCode;
|
||||
}
|
||||
}
|
||||
if (rv > aCount) {
|
||||
|
@ -263,14 +270,15 @@ PRInt32 ConverterInputStream::Read(PRInt32* aErrorCode,
|
|||
nsCRT::memcpy(aBuf + aOffset, mUnicharData->GetBuffer() + mUnicharDataOffset,
|
||||
rv * sizeof(PRUnichar));
|
||||
mUnicharDataOffset += rv;
|
||||
return rv;
|
||||
*aReadCount = rv;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
PRInt32 ConverterInputStream::Fill(PRInt32* aErrorCode)
|
||||
PRInt32 ConverterInputStream::Fill(nsresult * aErrorCode)
|
||||
{
|
||||
if (nsnull == mInput) {
|
||||
// We already closed the stream!
|
||||
*aErrorCode = NS_INPUTSTREAM_CLOSED;
|
||||
*aErrorCode = NS_BASE_STREAM_CLOSED;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
|
@ -107,4 +107,10 @@ typedef PRUcs2 PRUnichar;
|
|||
#define NS_GFX NS_IMPORT
|
||||
#endif
|
||||
|
||||
#ifdef _IMPL_NS_PLUGIN
|
||||
#define NS_PLUGIN NS_EXPORT
|
||||
#else
|
||||
#define NS_PLUGIN NS_IMPORT
|
||||
#endif
|
||||
|
||||
#endif /* nscore_h___ */
|
||||
|
|
|
@ -75,9 +75,10 @@ int main(int argc, char** argv)
|
|||
PRInt32 count = 0;
|
||||
for (;;) {
|
||||
PRUnichar buf[1000];
|
||||
PRInt32 nb = uin->Read(&ec, buf, 0, 1000);
|
||||
if (nb <= 0) {
|
||||
if (nb < 0) {
|
||||
PRInt32 nb;
|
||||
ec = uin->Read(buf, 0, 1000, &nb);
|
||||
if (ec < 0) {
|
||||
if (ec != NS_BASE_STREAM_EOF) {
|
||||
printf("i/o error: %d\n", ec);
|
||||
}
|
||||
break;
|
||||
|
|
|
@ -31,7 +31,7 @@ public:
|
|||
virtual PRInt32 GetBufferSize() const;
|
||||
virtual char* GetBuffer() const;
|
||||
virtual PRBool Grow(PRInt32 aNewSize);
|
||||
virtual PRInt32 Fill(PRInt32* aErrorCode, nsIInputStream* aStream,
|
||||
virtual PRInt32 Fill(nsresult* aErrorCode, nsIInputStream* aStream,
|
||||
PRInt32 aKeep);
|
||||
|
||||
char* mBuffer;
|
||||
|
@ -94,14 +94,14 @@ PRBool ByteBufferImpl::Grow(PRInt32 aNewSize)
|
|||
return PR_FALSE;
|
||||
}
|
||||
|
||||
PRInt32 ByteBufferImpl::Fill(PRInt32* aErrorCode, nsIInputStream* aStream,
|
||||
PRInt32 ByteBufferImpl::Fill(nsresult* aErrorCode, nsIInputStream* 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_INPUTSTREAM_ILLEGAL_ARGS;
|
||||
*aErrorCode = NS_BASE_STREAM_ILLEGAL_ARGS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -113,10 +113,13 @@ PRInt32 ByteBufferImpl::Fill(PRInt32* aErrorCode, nsIInputStream* aStream,
|
|||
// Read in some new data
|
||||
mLength = aKeep;
|
||||
PRInt32 amount = mSpace - aKeep;
|
||||
PRInt32 nb = aStream->Read(aErrorCode, mBuffer, aKeep, amount);
|
||||
if (nb > 0) {
|
||||
PRInt32 nb;
|
||||
*aErrorCode = aStream->Read(mBuffer, aKeep, amount, &nb);
|
||||
if (NS_SUCCEEDED(*aErrorCode)) {
|
||||
mLength += nb;
|
||||
}
|
||||
else
|
||||
nb = 0;
|
||||
return nb;
|
||||
}
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ public:
|
|||
|
||||
/** Fill the buffer with data from aStream. Don't grow the buffer, only
|
||||
* read until length of buffer equals buffer size. */
|
||||
virtual PRInt32 Fill(PRInt32* aErrorCode, nsIInputStream* aStream,
|
||||
virtual PRInt32 Fill(nsresult* aErrorCode, nsIInputStream* aStream,
|
||||
PRInt32 aKeep) = 0;
|
||||
};
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@ public:
|
|||
virtual PRInt32 GetBufferSize() const = 0;
|
||||
virtual PRUnichar* GetBuffer() const = 0;
|
||||
virtual PRBool Grow(PRInt32 aNewSize) = 0;
|
||||
virtual PRInt32 Fill(PRInt32* aErrorCode, nsIUnicharInputStream* aStream,
|
||||
virtual PRInt32 Fill(nsresult* aErrorCode, nsIUnicharInputStream* aStream,
|
||||
PRInt32 aKeep) = 0;
|
||||
};
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ public:
|
|||
virtual PRInt32 GetBufferSize() const;
|
||||
virtual PRUnichar* GetBuffer() const;
|
||||
virtual PRBool Grow(PRInt32 aNewSize);
|
||||
virtual PRInt32 Fill(PRInt32* aErrorCode, nsIUnicharInputStream* aStream,
|
||||
virtual PRInt32 Fill(nsresult* aErrorCode, nsIUnicharInputStream* aStream,
|
||||
PRInt32 aKeep);
|
||||
|
||||
PRUnichar* mBuffer;
|
||||
|
@ -94,7 +94,7 @@ PRBool UnicharBufferImpl::Grow(PRInt32 aNewSize)
|
|||
return PR_FALSE;
|
||||
}
|
||||
|
||||
PRInt32 UnicharBufferImpl::Fill(PRInt32* aErrorCode,
|
||||
PRInt32 UnicharBufferImpl::Fill(nsresult* aErrorCode,
|
||||
nsIUnicharInputStream* aStream,
|
||||
PRInt32 aKeep)
|
||||
{
|
||||
|
@ -102,7 +102,7 @@ PRInt32 UnicharBufferImpl::Fill(PRInt32* aErrorCode,
|
|||
NS_PRECONDITION(PRUint32(aKeep) < PRUint32(mLength), "illegal keep count");
|
||||
if ((nsnull == aStream) || (PRUint32(aKeep) >= PRUint32(mLength))) {
|
||||
// whoops
|
||||
*aErrorCode = NS_INPUTSTREAM_ILLEGAL_ARGS;
|
||||
*aErrorCode = NS_BASE_STREAM_ILLEGAL_ARGS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -115,10 +115,13 @@ PRInt32 UnicharBufferImpl::Fill(PRInt32* aErrorCode,
|
|||
// Read in some new data
|
||||
mLength = aKeep;
|
||||
PRInt32 amount = mSpace - aKeep;
|
||||
PRInt32 nb = aStream->Read(aErrorCode, mBuffer, aKeep, amount);
|
||||
if (nb > 0) {
|
||||
PRInt32 nb;
|
||||
*aErrorCode = aStream->Read(mBuffer, aKeep, amount, &nb);
|
||||
if (NS_SUCCEEDED(*aErrorCode)) {
|
||||
mLength += nb;
|
||||
}
|
||||
else
|
||||
nb = 0;
|
||||
return nb;
|
||||
}
|
||||
|
||||
|
|
|
@ -31,12 +31,27 @@
|
|||
class nsIBaseStream : public nsISupports {
|
||||
public:
|
||||
|
||||
/** Return the number of bytes in the stream */
|
||||
virtual PRInt32 GetLength() = 0;
|
||||
|
||||
/** Close the stream. */
|
||||
virtual void Close() = 0;
|
||||
NS_IMETHOD
|
||||
Close(void) = 0;
|
||||
};
|
||||
|
||||
/** Error codes */
|
||||
//@{
|
||||
// XXX fix up the values so they are not total hacks... MMP
|
||||
/// End of file
|
||||
#define NS_BASE_STREAM_EOF 0x80001001
|
||||
/// Stream closed
|
||||
#define NS_BASE_STREAM_CLOSED 0x80001002
|
||||
/// Error from the operating system
|
||||
#define NS_BASE_STREAM_OSERROR 0x80001003
|
||||
/// Illegal arguments
|
||||
#define NS_BASE_STREAM_ILLEGAL_ARGS 0x80001004
|
||||
/// For unichar streams
|
||||
#define NS_BASE_STREAM_NO_CONVERTER 0x80001005
|
||||
/// For unichar streams
|
||||
#define NS_BASE_STREAM_BAD_CONVERSION 0x80001006
|
||||
//@}
|
||||
|
||||
|
||||
#endif /* nsInputStream_h___ */
|
||||
|
|
|
@ -27,34 +27,28 @@
|
|||
/** Abstract byte input stream */
|
||||
class nsIInputStream : public nsIBaseStream {
|
||||
public:
|
||||
|
||||
/** Return the number of bytes in the stream
|
||||
* @param aLength out parameter to hold the length
|
||||
* of the stream. if an error occurs, the length
|
||||
* will be undefined
|
||||
* @return error status
|
||||
*/
|
||||
NS_IMETHOD
|
||||
GetLength(PRInt32 *aLength) = 0;
|
||||
|
||||
/** Read data from the stream.
|
||||
* @param aErrorCode the error code if an error occurs
|
||||
* @param aBuf the buffer into which the data is read
|
||||
* @param aOffset the start offset of the data
|
||||
* @param aCount the maximum number of bytes to read
|
||||
* @return number of bytes read or -1 if error
|
||||
* @param aReadCount out parameter to hold the number of
|
||||
* bytes read, eof if 0. if an error occurs, the
|
||||
* read count will be undefined
|
||||
* @return error status
|
||||
*/
|
||||
virtual PRInt32 Read(PRInt32* aErrorCode,
|
||||
char* aBuf,
|
||||
PRInt32 aOffset,
|
||||
PRInt32 aCount) = 0;
|
||||
NS_IMETHOD
|
||||
Read(char* aBuf, PRInt32 aOffset, PRInt32 aCount, PRInt32 *aReadCount) = 0;
|
||||
};
|
||||
|
||||
/** Error codes */
|
||||
//@{
|
||||
// XXX fix up the values to work with nsqresult
|
||||
/// End of file
|
||||
#define NS_INPUTSTREAM_EOF 1
|
||||
/// Stream closed
|
||||
#define NS_INPUTSTREAM_CLOSED 2
|
||||
/// Error from the operating system
|
||||
#define NS_INPUTSTREAM_OSERROR 3
|
||||
/// Illegal arguments
|
||||
#define NS_INPUTSTREAM_ILLEGAL_ARGS 4
|
||||
/// For unichar streams
|
||||
#define NS_INPUTSTREAM_NO_CONVERTER 5
|
||||
/// For unichar streams
|
||||
#define NS_INPUTSTREAM_BAD_CONVERSION 6
|
||||
//@}
|
||||
|
||||
#endif /* nsInputStream_h___ */
|
||||
|
|
|
@ -25,20 +25,21 @@
|
|||
{ 0x7f13b870, 0xe95f, 0x11d1, \
|
||||
{0xbe, 0xae, 0x00, 0x80, 0x5f, 0x8a, 0x66, 0xdc} }
|
||||
|
||||
/** Abstract byte input stream */
|
||||
/** Abstract byte output stream */
|
||||
class nsIOutputStream : public nsIBaseStream {
|
||||
public:
|
||||
|
||||
/** Write data into the stream.
|
||||
* @param aErrorCode the error code if an error occurs
|
||||
* @param aBuf the buffer into which the data is read
|
||||
* @param aOffset the start offset of the data
|
||||
* @param aCount the maximum number of bytes to read
|
||||
* @return number of bytes read or -1 if error
|
||||
* @param aWriteCount out parameter to hold the number of
|
||||
* bytes written. if an error occurs, the writecount
|
||||
* is undefined
|
||||
* @return error status
|
||||
*/
|
||||
virtual PRInt32 Write(PRInt32* aErrorCode,
|
||||
const char* aBuf,
|
||||
PRInt32 aOffset,
|
||||
PRInt32 aCount) = 0;
|
||||
NS_IMETHOD
|
||||
Write(const char* aBuf, PRInt32 aOffset, PRInt32 aCount, PRInt32 *aWriteCount) = 0;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -42,11 +42,11 @@ enum nsCharSetID {
|
|||
*/
|
||||
class nsIUnicharInputStream : public nsISupports {
|
||||
public:
|
||||
virtual PRInt32 Read(PRInt32* aErrorCode,
|
||||
PRUnichar* aBuf,
|
||||
NS_IMETHOD Read(PRUnichar* aBuf,
|
||||
PRInt32 aOffset,
|
||||
PRInt32 aCount) = 0;
|
||||
virtual void Close() = 0;
|
||||
PRInt32 aCount,
|
||||
PRInt32 *aReadCount) = 0;
|
||||
NS_IMETHOD Close() = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -36,11 +36,11 @@ public:
|
|||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
virtual PRInt32 Read(PRInt32* aErrorCode,
|
||||
PRUnichar* aBuf,
|
||||
NS_IMETHOD Read(PRUnichar* aBuf,
|
||||
PRInt32 aOffset,
|
||||
PRInt32 aCount);
|
||||
virtual void Close();
|
||||
PRInt32 aCount,
|
||||
PRInt32 *aReadCount);
|
||||
NS_IMETHOD Close();
|
||||
|
||||
nsString* mString;
|
||||
PRInt32 mPos;
|
||||
|
@ -62,12 +62,13 @@ StringUnicharInputStream::~StringUnicharInputStream()
|
|||
}
|
||||
}
|
||||
|
||||
PRInt32 StringUnicharInputStream::Read(PRInt32* aErrorCode,
|
||||
PRUnichar* aBuf,
|
||||
nsresult StringUnicharInputStream::Read(PRUnichar* aBuf,
|
||||
PRInt32 aOffset,
|
||||
PRInt32 aCount)
|
||||
PRInt32 aCount,
|
||||
PRInt32 *aReadCount)
|
||||
{
|
||||
if (mPos >= mLen) {
|
||||
*aReadCount = 0;
|
||||
return -1;
|
||||
}
|
||||
const PRUnichar* us = mString->GetUnicode();
|
||||
|
@ -77,15 +78,17 @@ PRInt32 StringUnicharInputStream::Read(PRInt32* aErrorCode,
|
|||
}
|
||||
nsCRT::memcpy(aBuf + aOffset, us + mPos, sizeof(PRUnichar) * amount);
|
||||
mPos += amount;
|
||||
return amount;
|
||||
*aReadCount = amount;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
void StringUnicharInputStream::Close()
|
||||
nsresult StringUnicharInputStream::Close()
|
||||
{
|
||||
mPos = mLen;
|
||||
if (nsnull != mString) {
|
||||
delete mString;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMPL_ISUPPORTS(StringUnicharInputStream, kIUnicharInputStreamIID);
|
||||
|
@ -163,7 +166,7 @@ NS_NewB2UConverter(nsIB2UConverter** aInstancePtrResult,
|
|||
return NS_ERROR_NO_AGGREGATION;
|
||||
}
|
||||
if (eCharSetID_IsoLatin1 != aCharSet) {
|
||||
return NS_INPUTSTREAM_NO_CONVERTER;
|
||||
return NS_BASE_STREAM_NO_CONVERTER;
|
||||
}
|
||||
IsoLatin1Converter* it = new IsoLatin1Converter();
|
||||
if (nsnull == it) {
|
||||
|
@ -182,14 +185,14 @@ public:
|
|||
~ConverterInputStream();
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
virtual PRInt32 Read(PRInt32* aErrorCode,
|
||||
PRUnichar* aBuf,
|
||||
NS_IMETHOD Read(PRUnichar* aBuf,
|
||||
PRInt32 aOffset,
|
||||
PRInt32 aCount);
|
||||
virtual void Close();
|
||||
PRInt32 aCount,
|
||||
PRInt32 *aReadCount);
|
||||
NS_IMETHOD Close();
|
||||
|
||||
protected:
|
||||
PRInt32 Fill(PRInt32* aErrorCode);
|
||||
PRInt32 Fill(nsresult * aErrorCode);
|
||||
|
||||
nsIInputStream* mInput;
|
||||
nsIB2UConverter* mConverter;
|
||||
|
@ -224,7 +227,7 @@ ConverterInputStream::~ConverterInputStream()
|
|||
Close();
|
||||
}
|
||||
|
||||
void ConverterInputStream::Close()
|
||||
nsresult ConverterInputStream::Close()
|
||||
{
|
||||
if (nsnull != mInput) {
|
||||
mInput->Release();
|
||||
|
@ -242,19 +245,23 @@ void ConverterInputStream::Close()
|
|||
mUnicharData->Release();
|
||||
mUnicharData = nsnull;
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
PRInt32 ConverterInputStream::Read(PRInt32* aErrorCode,
|
||||
PRUnichar* aBuf,
|
||||
nsresult ConverterInputStream::Read(PRUnichar* aBuf,
|
||||
PRInt32 aOffset,
|
||||
PRInt32 aCount)
|
||||
PRInt32 aCount,
|
||||
PRInt32 *aReadCount)
|
||||
{
|
||||
PRInt32 rv = mUnicharDataLength - mUnicharDataOffset;
|
||||
nsresult errorCode;
|
||||
if (0 == rv) {
|
||||
// Fill the unichar buffer
|
||||
rv = Fill(aErrorCode);
|
||||
rv = Fill(&errorCode);
|
||||
if (rv <= 0) {
|
||||
return rv;
|
||||
*aReadCount = 0;
|
||||
return errorCode;
|
||||
}
|
||||
}
|
||||
if (rv > aCount) {
|
||||
|
@ -263,14 +270,15 @@ PRInt32 ConverterInputStream::Read(PRInt32* aErrorCode,
|
|||
nsCRT::memcpy(aBuf + aOffset, mUnicharData->GetBuffer() + mUnicharDataOffset,
|
||||
rv * sizeof(PRUnichar));
|
||||
mUnicharDataOffset += rv;
|
||||
return rv;
|
||||
*aReadCount = rv;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
PRInt32 ConverterInputStream::Fill(PRInt32* aErrorCode)
|
||||
PRInt32 ConverterInputStream::Fill(nsresult * aErrorCode)
|
||||
{
|
||||
if (nsnull == mInput) {
|
||||
// We already closed the stream!
|
||||
*aErrorCode = NS_INPUTSTREAM_CLOSED;
|
||||
*aErrorCode = NS_BASE_STREAM_CLOSED;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
|
@ -75,9 +75,10 @@ int main(int argc, char** argv)
|
|||
PRInt32 count = 0;
|
||||
for (;;) {
|
||||
PRUnichar buf[1000];
|
||||
PRInt32 nb = uin->Read(&ec, buf, 0, 1000);
|
||||
if (nb <= 0) {
|
||||
if (nb < 0) {
|
||||
PRInt32 nb;
|
||||
ec = uin->Read(buf, 0, 1000, &nb);
|
||||
if (ec < 0) {
|
||||
if (ec != NS_BASE_STREAM_EOF) {
|
||||
printf("i/o error: %d\n", ec);
|
||||
}
|
||||
break;
|
||||
|
|
Загрузка…
Ссылка в новой задаче