зеркало из https://github.com/mozilla/gecko-dev.git
Update the string stream interface a bit and simplify the implementation a
lot. Bug 212109, r=dougt, sr=darin
This commit is contained in:
Родитель
9cb380a7ff
Коммит
7b5865099a
|
@ -907,17 +907,12 @@ nsXMLHttpRequest::StreamReaderFunc(nsIInputStream* in,
|
|||
// We need to wrap the data in a new lightweight stream and pass that
|
||||
// to the parser, because calling ReadSegments() recursively on the same
|
||||
// stream is not supported.
|
||||
nsCOMPtr<nsISupports> supportsStream;
|
||||
rv = NS_NewByteInputStream(getter_AddRefs(supportsStream),fromRawSegment,count);
|
||||
nsCOMPtr<nsIInputStream> copyStream;
|
||||
rv = NS_NewByteInputStream(getter_AddRefs(copyStream), fromRawSegment, count);
|
||||
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
nsCOMPtr<nsIInputStream> copyStream(do_QueryInterface(supportsStream));
|
||||
if (copyStream) {
|
||||
rv = xmlHttpRequest->mXMLParserStreamListener->OnDataAvailable(xmlHttpRequest->mReadRequest,xmlHttpRequest->mContext,copyStream,toOffset,count);
|
||||
} else {
|
||||
NS_ERROR("NS_NewByteInputStream did not give out nsIInputStream!");
|
||||
rv = NS_ERROR_UNEXPECTED;
|
||||
}
|
||||
NS_ASSERTION(copyStream, "NS_NewByteInputStream lied");
|
||||
rv = xmlHttpRequest->mXMLParserStreamListener->OnDataAvailable(xmlHttpRequest->mReadRequest,xmlHttpRequest->mContext,copyStream,toOffset,count);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -496,12 +496,15 @@ NS_IMETHODIMP nsIconChannel::AsyncOpen(nsIStreamListener *aListener, nsISupports
|
|||
aListener->OnStartRequest(this, ctxt);
|
||||
|
||||
// turn our string into a stream...
|
||||
nsCOMPtr<nsISupports> streamSupports;
|
||||
NS_NewByteInputStream(getter_AddRefs(streamSupports), iconBuffer.get(), iconBuffer.Length());
|
||||
nsCOMPtr<nsIInputStream> inputStr;
|
||||
rv = NS_NewByteInputStream(getter_AddRefs(inputStr), iconBuffer.get(),
|
||||
iconBuffer.Length());
|
||||
|
||||
nsCOMPtr<nsIInputStream> inputStr (do_QueryInterface(streamSupports));
|
||||
aListener->OnDataAvailable(this, ctxt, inputStr, 0, iconBuffer.Length());
|
||||
aListener->OnStopRequest(this, ctxt, NS_OK);
|
||||
if (NS_SUCCEEDED(rv))
|
||||
{
|
||||
aListener->OnDataAvailable(this, ctxt, inputStr, 0, iconBuffer.Length());
|
||||
}
|
||||
aListener->OnStopRequest(this, ctxt, rv);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
|
|
@ -532,11 +532,13 @@ NS_IMETHODIMP nsIconChannel::AsyncOpen(nsIStreamListener *aListener, nsISupports
|
|||
aListener->OnStartRequest(this, ctxt);
|
||||
|
||||
// turn our string into a stream...and make the appropriate calls on our consumer
|
||||
nsCOMPtr<nsISupports> streamSupports;
|
||||
NS_NewByteInputStream(getter_AddRefs(streamSupports), iconBuffer.get(), iconBuffer.Length());
|
||||
nsCOMPtr<nsIInputStream> inputStr (do_QueryInterface(streamSupports));
|
||||
aListener->OnDataAvailable(this, ctxt, inputStr, 0, iconBuffer.Length());
|
||||
aListener->OnStopRequest(this, ctxt, NS_OK);
|
||||
nsCOMPtr<nsIInputStream> inputStr;
|
||||
rv = NS_NewByteInputStream(getter_AddRefs(inputStr), iconBuffer.get(),
|
||||
iconBuffer.Length());
|
||||
if (NS_SUCCEEDED(rv))
|
||||
aListener->OnDataAvailable(this, ctxt, inputStr, 0,
|
||||
iconBuffer.Length());
|
||||
aListener->OnStopRequest(this, ctxt, rv);
|
||||
} // if we have a mask buffer to apply
|
||||
|
||||
} // if we got color info
|
||||
|
|
|
@ -408,11 +408,14 @@ NS_IMETHODIMP nsIconChannel::AsyncOpen(nsIStreamListener *aListener, nsISupports
|
|||
aListener->OnStartRequest(this, ctxt);
|
||||
|
||||
// turn our string into a stream...and make the appropriate calls on our consumer
|
||||
nsCOMPtr<nsISupports> streamSupports;
|
||||
NS_NewByteInputStream(getter_AddRefs(streamSupports), iconBuffer.get(), iconBuffer.Length());
|
||||
nsCOMPtr<nsIInputStream> inputStr (do_QueryInterface(streamSupports));
|
||||
aListener->OnDataAvailable(this, ctxt, inputStr, 0, iconBuffer.Length());
|
||||
aListener->OnStopRequest(this, ctxt, NS_OK);
|
||||
nsCOMPtr<nsIInputStream> inputStr;
|
||||
rv = NS_NewByteInputStream(getter_AddRefs(inputStr),
|
||||
iconBuffer.get(),
|
||||
iconBuffer.Length());
|
||||
if (NS_SUCCEEDED(rv))
|
||||
aListener->OnDataAvailable(this, ctxt, inputStr, 0,
|
||||
iconBuffer.Length());
|
||||
aListener->OnStopRequest(this, ctxt, rv);
|
||||
} // if we have a mask buffer to apply
|
||||
} // if we got the color bit map
|
||||
|
||||
|
|
|
@ -195,12 +195,11 @@ nsHttpTransaction::Init(PRUint8 caps,
|
|||
// Create a string stream for the request header buf (the stream holds
|
||||
// a non-owning reference to the request header data, so we MUST keep
|
||||
// mReqHeaderBuf around).
|
||||
nsCOMPtr<nsISupports> sup;
|
||||
rv = NS_NewByteInputStream(getter_AddRefs(sup),
|
||||
nsCOMPtr<nsIInputStream> headers;
|
||||
rv = NS_NewByteInputStream(getter_AddRefs(headers),
|
||||
mReqHeaderBuf.get(),
|
||||
mReqHeaderBuf.Length());
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
nsCOMPtr<nsIInputStream> headers = do_QueryInterface(sup, &rv);
|
||||
|
||||
if (requestBody) {
|
||||
// wrap the headers and request body in a multiplexed input stream.
|
||||
|
|
|
@ -43,7 +43,7 @@
|
|||
* Based on original code from nsIStringStream.h
|
||||
*/
|
||||
|
||||
#include "nsISupports.idl"
|
||||
#include "nsIInputStream.idl"
|
||||
|
||||
/**
|
||||
* nsIStringInputStream
|
||||
|
@ -52,7 +52,7 @@
|
|||
* nsIInputStream implementation with a simple character array.
|
||||
*/
|
||||
[scriptable, uuid(450cd2d4-f0fd-424d-b365-b1251f80fd53)]
|
||||
interface nsIStringInputStream : nsISupports
|
||||
interface nsIStringInputStream : nsIInputStream
|
||||
{
|
||||
/**
|
||||
* SetData - assign data to the input stream (copied on assignment).
|
||||
|
@ -93,49 +93,39 @@ interface nsIStringInputStream : nsISupports
|
|||
|
||||
%{C++
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------------
|
||||
// C++ factory methods
|
||||
//----------------------------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include "nsIInputStream.h"
|
||||
#include "nsIOutputStream.h"
|
||||
#include "nsString.h"
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
extern "C" NS_COM nsresult NS_NewStringInputStream(
|
||||
nsIInputStream** aStreamResult,
|
||||
const nsAString& aStringToRead);
|
||||
// Factory method to get an nsInputStream from a string. Result will implement all the
|
||||
// file stream interfaces in nsIFileStream.h
|
||||
//-----------------------------------------------------------------------------
|
||||
// Factory method to get an nsInputStream from an nsAString. Result will
|
||||
// implement nsIStringInputStream and nsIRandomAccessStore
|
||||
extern "C" NS_COM nsresult
|
||||
NS_NewStringInputStream(nsIInputStream** aStreamResult,
|
||||
const nsAString& aStringToRead);
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
extern "C" NS_COM nsresult NS_NewCStringInputStream(
|
||||
nsIInputStream** aStreamResult,
|
||||
const nsACString& aStringToRead);
|
||||
// Factory method to get an nsInputStream from a cstring. Result will implement all the
|
||||
// file stream interfaces in nsIFileStream.h
|
||||
//-----------------------------------------------------------------------------
|
||||
// Factory method to get an nsInputStream from an nsACString. Result will
|
||||
// implement nsIStringInputStream and nsIRandomAccessStore
|
||||
extern "C" NS_COM nsresult
|
||||
NS_NewCStringInputStream(nsIInputStream** aStreamResult,
|
||||
const nsACString& aStringToRead);
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
extern "C" NS_COM nsresult NS_NewCharInputStream(
|
||||
nsISupports** aStreamResult,
|
||||
const char* aStringToRead);
|
||||
// Factory method to get an nsInputStream from a string. Result will implement all the
|
||||
// file stream interfaces in nsIFileStream.h
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
extern "C" NS_COM nsresult NS_NewCharOutputStream(
|
||||
nsISupports** aStreamResult,
|
||||
char** aStringToChange);
|
||||
// Factory method to get an nsOutputStream to a string. Result will implement all the
|
||||
// file stream interfaces in nsIFileStream.h
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
extern "C" NS_COM nsresult NS_NewByteInputStream(
|
||||
nsISupports** aStreamResult,
|
||||
const char* aStringToRead,
|
||||
PRInt32 aLength);
|
||||
// Factory method to get an nsInputStream from a string. Result will implement all the
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
// Factory method to get an nsInputStream from a string. Result will
|
||||
// implement nsIStringInputStream and nsIRandomAccessStore
|
||||
extern "C" NS_COM nsresult
|
||||
NS_NewCharInputStream(nsIInputStream** aStreamResult,
|
||||
const char* aStringToRead);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Factory method to get an nsInputStream from a byte buffer. Result will
|
||||
// implement nsIStringInputStream and nsIRandomAccessStore
|
||||
extern "C" NS_COM nsresult
|
||||
NS_NewByteInputStream(nsIInputStream** aStreamResult,
|
||||
const char* aStringToRead,
|
||||
PRInt32 aLength);
|
||||
%}
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
* davidm@netscape.com
|
||||
* sfraser@netscape.com
|
||||
* darin@netscape.com
|
||||
* bzbarsky@mit.edu
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
|
@ -68,87 +69,201 @@ static nsresult ns_file_convert_result(PRInt32 nativeErr)
|
|||
: NS_OK;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// nsIStringInputStream implementation
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
//========================================================================================
|
||||
class BasicStringImpl
|
||||
: public nsIOutputStream
|
||||
, public nsIInputStream
|
||||
, public nsIRandomAccessStore
|
||||
//========================================================================================
|
||||
class nsStringInputStream : public nsIStringInputStream
|
||||
, public nsIRandomAccessStore
|
||||
|
||||
{
|
||||
public:
|
||||
BasicStringImpl();
|
||||
virtual ~BasicStringImpl();
|
||||
|
||||
NS_DECL_NSISEEKABLESTREAM
|
||||
NS_IMETHOD GetAtEOF(PRBool* outAtEOF);
|
||||
NS_IMETHOD SetAtEOF(PRBool inAtEOF);
|
||||
NS_IMETHOD Available(PRUint32 *aLength);
|
||||
NS_IMETHOD Read(char* aBuf,
|
||||
PRUint32 aCount,
|
||||
PRUint32 *aReadCount);
|
||||
NS_IMETHOD ReadSegments(nsWriteSegmentFun writer, void * closure, PRUint32 count, PRUint32 *_retval) = 0;
|
||||
|
||||
// nsIOutputStream interface
|
||||
NS_IMETHOD Write(const char* aBuf,
|
||||
PRUint32 aCount,
|
||||
PRUint32 *aWriteCount);
|
||||
NS_IMETHOD WriteFrom(nsIInputStream *inStr, PRUint32 count, PRUint32 *_retval);
|
||||
NS_IMETHOD WriteSegments(nsReadSegmentFun reader, void * closure, PRUint32 count, PRUint32 *_retval);
|
||||
NS_IMETHOD IsNonBlocking(PRBool *aNonBlocking);
|
||||
|
||||
public:
|
||||
|
||||
// nsISupports interface
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
NS_IMETHOD Close() { return NS_OK; }
|
||||
|
||||
// nsIInputStream interface
|
||||
NS_IMETHOD Flush() { return NS_OK; }
|
||||
|
||||
public:
|
||||
nsStringInputStream()
|
||||
: mOffset(0)
|
||||
, mLastResult(NS_OK)
|
||||
, mEOF(PR_FALSE)
|
||||
, mOwned(PR_FALSE)
|
||||
, mConstString(nsnull)
|
||||
, mLength(0)
|
||||
{}
|
||||
|
||||
public:
|
||||
nsresult get_result() const { return mLastResult; }
|
||||
|
||||
protected:
|
||||
virtual ~nsStringInputStream()
|
||||
{
|
||||
if (mOwned)
|
||||
nsMemory::Free((char*)mConstString);
|
||||
}
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
NS_DECL_NSISTRINGINPUTSTREAM
|
||||
NS_DECL_NSIINPUTSTREAM
|
||||
|
||||
// nsIRandomAccessStore interface
|
||||
NS_IMETHOD GetAtEOF(PRBool* outAtEOF);
|
||||
NS_IMETHOD SetAtEOF(PRBool inAtEOF);
|
||||
|
||||
NS_DECL_NSISEEKABLESTREAM
|
||||
|
||||
virtual PRInt32 length() const = 0;
|
||||
virtual PRInt32 read(char* buf, PRUint32 count) = 0;
|
||||
virtual PRInt32 write(const char*, PRUint32);
|
||||
protected:
|
||||
PRInt32 LengthRemaining() const
|
||||
{
|
||||
return mLength - mOffset;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
PRUint32 mOffset;
|
||||
nsresult mLastResult;
|
||||
PRBool mEOF;
|
||||
|
||||
}; // class BasicStringImpl
|
||||
void Clear()
|
||||
{
|
||||
NS_ASSERTION(mConstString || !mOwned,
|
||||
"Can't have mOwned set and have a null string!")
|
||||
if (mOwned)
|
||||
nsMemory::Free((char*)mConstString);
|
||||
|
||||
// We're about to get a new string; clear the members that
|
||||
// would no longer have valid values.
|
||||
mOffset = 0;
|
||||
mLastResult = NS_OK;
|
||||
mEOF = PR_FALSE;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
BasicStringImpl::BasicStringImpl()
|
||||
//----------------------------------------------------------------------------------------
|
||||
: mOffset(0)
|
||||
, mLastResult(NS_OK)
|
||||
, mEOF(PR_FALSE)
|
||||
PRInt32 mOffset;
|
||||
nsresult mLastResult;
|
||||
PRPackedBool mEOF;
|
||||
PRPackedBool mOwned;
|
||||
const char* mConstString;
|
||||
PRInt32 mLength;
|
||||
};
|
||||
|
||||
NS_IMPL_THREADSAFE_ISUPPORTS4(nsStringInputStream,
|
||||
nsIStringInputStream,
|
||||
nsIInputStream,
|
||||
nsIRandomAccessStore,
|
||||
nsISeekableStream);
|
||||
|
||||
/////////
|
||||
// nsIStringInputStream implementation
|
||||
/////////
|
||||
NS_IMETHODIMP
|
||||
nsStringInputStream::SetData(const char *data, PRInt32 dataLen)
|
||||
{
|
||||
if (dataLen < 0)
|
||||
dataLen = strlen(data);
|
||||
|
||||
return AdoptData(nsCRT::strndup(data, dataLen), dataLen);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
BasicStringImpl::~BasicStringImpl()
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_IMETHODIMP
|
||||
nsStringInputStream::AdoptData(char *data, PRInt32 dataLen)
|
||||
{
|
||||
NS_ENSURE_ARG_POINTER(data);
|
||||
|
||||
if (dataLen < 0)
|
||||
dataLen = strlen(data);
|
||||
|
||||
Clear();
|
||||
|
||||
mConstString = (const char *) data;
|
||||
mLength = dataLen;
|
||||
mOwned = PR_TRUE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_IMETHODIMP BasicStringImpl::Seek(PRInt32 whence, PRInt32 offset)
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_IMETHODIMP
|
||||
nsStringInputStream::ShareData(const char *data, PRInt32 dataLen)
|
||||
{
|
||||
NS_ENSURE_ARG_POINTER(data);
|
||||
|
||||
if (dataLen < 0)
|
||||
dataLen = strlen(data);
|
||||
|
||||
Clear();
|
||||
|
||||
mConstString = data;
|
||||
mLength = dataLen;
|
||||
mOwned = PR_FALSE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/////////
|
||||
// nsIInputStream implementation
|
||||
/////////
|
||||
NS_IMETHODIMP nsStringInputStream::Close()
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP nsStringInputStream::Available(PRUint32 *aLength)
|
||||
{
|
||||
NS_PRECONDITION(aLength != nsnull, "null ptr");
|
||||
if (!aLength)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
*aLength = LengthRemaining();
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP nsStringInputStream::Read(char* aBuf, PRUint32 aCount,
|
||||
PRUint32 *aReadCount)
|
||||
{
|
||||
NS_PRECONDITION(aBuf != nsnull, "null ptr");
|
||||
if (!aBuf)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
NS_PRECONDITION(aReadCount != nsnull, "null ptr");
|
||||
if (!aReadCount)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
if (NS_FAILED(mLastResult))
|
||||
return mLastResult;
|
||||
|
||||
PRInt32 bytesRead;
|
||||
PRInt32 maxCount = mLength - mOffset;
|
||||
if ((PRInt32)aCount > maxCount)
|
||||
bytesRead = maxCount;
|
||||
else
|
||||
bytesRead = aCount;
|
||||
|
||||
memcpy(aBuf, mConstString + mOffset, bytesRead);
|
||||
mOffset += bytesRead;
|
||||
|
||||
*aReadCount = bytesRead;
|
||||
if (bytesRead < (PRInt32)aCount)
|
||||
SetAtEOF(PR_TRUE);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsStringInputStream::ReadSegments(nsWriteSegmentFun writer, void * closure,
|
||||
PRUint32 aCount, PRUint32 * result)
|
||||
{
|
||||
nsresult rv;
|
||||
PRInt32 maxCount = mLength - mOffset;
|
||||
if (maxCount == 0) {
|
||||
*result = 0;
|
||||
return NS_OK;
|
||||
}
|
||||
if ((PRInt32)aCount > maxCount)
|
||||
aCount = maxCount;
|
||||
rv = writer(this, closure, mConstString + mOffset,
|
||||
0, aCount, result);
|
||||
if (NS_SUCCEEDED(rv))
|
||||
mOffset += *result;
|
||||
// errors returned from the writer end here!
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsStringInputStream::IsNonBlocking(PRBool *aNonBlocking)
|
||||
{
|
||||
*aNonBlocking = PR_TRUE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
/////////
|
||||
// nsISeekableStream implementation
|
||||
/////////
|
||||
NS_IMETHODIMP nsStringInputStream::Seek(PRInt32 whence, PRInt32 offset)
|
||||
{
|
||||
mLastResult = NS_OK; // reset on a seek.
|
||||
mEOF = PR_FALSE; // reset on a seek.
|
||||
PRInt32 fileSize = length();
|
||||
PRInt32 fileSize = LengthRemaining();
|
||||
PRInt32 newPosition=-1;
|
||||
switch (whence)
|
||||
{
|
||||
|
@ -168,380 +283,148 @@ NS_IMETHODIMP BasicStringImpl::Seek(PRInt32 whence, PRInt32 offset)
|
|||
}
|
||||
mOffset = newPosition;
|
||||
return NS_OK;
|
||||
} // BasicStringImpl::Seek
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_IMETHODIMP BasicStringImpl::Tell(PRUint32* outWhere)
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
*outWhere = mOffset;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_IMETHODIMP BasicStringImpl::SetEOF()
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
NS_IMETHODIMP nsStringInputStream::Tell(PRUint32* outWhere)
|
||||
{
|
||||
NS_NOTYETIMPLEMENTED("BasicStringImpl::SetEOF");
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
*outWhere = mOffset;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_IMETHODIMP BasicStringImpl::GetAtEOF(PRBool* outAtEOF)
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_IMETHODIMP nsStringInputStream::SetEOF()
|
||||
{
|
||||
*outAtEOF = mEOF;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_IMETHODIMP BasicStringImpl::SetAtEOF(PRBool inAtEOF)
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
mEOF = inAtEOF;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_IMETHODIMP BasicStringImpl::Available(PRUint32 *aLength)
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
NS_PRECONDITION(aLength != nsnull, "null ptr");
|
||||
if (!aLength)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
*aLength = length();
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_IMETHODIMP BasicStringImpl::Read(char* aBuf, PRUint32 aCount, PRUint32 *aReadCount)
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
NS_PRECONDITION(aBuf != nsnull, "null ptr");
|
||||
if (!aBuf)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
NS_PRECONDITION(aReadCount != nsnull, "null ptr");
|
||||
if (!aReadCount)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
if (NS_FAILED(mLastResult))
|
||||
return mLastResult;
|
||||
PRInt32 bytesRead = read(aBuf, aCount);
|
||||
if (NS_FAILED(mLastResult))
|
||||
{
|
||||
*aReadCount = 0;
|
||||
return mLastResult;
|
||||
}
|
||||
*aReadCount = bytesRead;
|
||||
if (bytesRead < (PRInt32)aCount)
|
||||
SetAtEOF(PR_TRUE);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_IMETHODIMP BasicStringImpl::Write(const char* aBuf, PRUint32 aCount, PRUint32 *aWriteCount)
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
NS_PRECONDITION(aBuf != nsnull, "null ptr");
|
||||
NS_PRECONDITION(aWriteCount != nsnull, "null ptr");
|
||||
|
||||
if (NS_FAILED(mLastResult))
|
||||
return mLastResult;
|
||||
PRInt32 bytesWrit = write(aBuf, aCount);
|
||||
if (NS_FAILED(mLastResult))
|
||||
{
|
||||
*aWriteCount = 0;
|
||||
return mLastResult;
|
||||
}
|
||||
*aWriteCount = bytesWrit;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
BasicStringImpl::WriteFrom(nsIInputStream *inStr, PRUint32 count, PRUint32 *result)
|
||||
{
|
||||
NS_NOTREACHED("WriteFrom");
|
||||
NS_NOTYETIMPLEMENTED("nsStringInputStream::SetEOF");
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
BasicStringImpl::WriteSegments(nsReadSegmentFun reader, void * closure,
|
||||
PRUint32 count, PRUint32 *result)
|
||||
/////////
|
||||
// nsIRandomAccessStore implementation
|
||||
/////////
|
||||
NS_IMETHODIMP nsStringInputStream::GetAtEOF(PRBool* outAtEOF)
|
||||
{
|
||||
NS_NOTREACHED("WriteSegments");
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
BasicStringImpl::IsNonBlocking(PRBool *aNonBlocking)
|
||||
{
|
||||
*aNonBlocking = PR_TRUE;
|
||||
*outAtEOF = mEOF;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
PRInt32 BasicStringImpl::write(const char*, PRUint32)
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_IMETHODIMP nsStringInputStream::SetAtEOF(PRBool inAtEOF)
|
||||
{
|
||||
NS_ASSERTION(PR_FALSE, "Write to a const string");
|
||||
mLastResult = NS_FILE_RESULT(PR_ILLEGAL_ACCESS_ERROR);
|
||||
return -1;
|
||||
mEOF = inAtEOF;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
#ifdef XP_MAC
|
||||
#pragma mark -
|
||||
#endif
|
||||
|
||||
//========================================================================================
|
||||
class ConstCharImpl
|
||||
: public BasicStringImpl
|
||||
//========================================================================================
|
||||
// Factory method to get an nsInputStream from an nsAString. Result will
|
||||
// implement nsIStringInputStream and nsIRandomAccessStore
|
||||
extern "C" NS_COM nsresult
|
||||
NS_NewStringInputStream(nsIInputStream** aStreamResult,
|
||||
const nsAString& aStringToRead)
|
||||
{
|
||||
public:
|
||||
ConstCharImpl(const char* inString, PRInt32 inLength = -1)
|
||||
: mConstString(inString),
|
||||
mLength(inLength == -1 ?
|
||||
(inString ? strlen(inString) : 0) : inLength) {}
|
||||
|
||||
protected:
|
||||
NS_PRECONDITION(aStreamResult, "null out ptr");
|
||||
|
||||
char* data = ToNewCString(aStringToRead);
|
||||
if (!data)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
nsStringInputStream* stream = new nsStringInputStream();
|
||||
if (! stream) {
|
||||
nsMemory::Free(data);
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
nsresult rv = stream->AdoptData(data, aStringToRead.Length());
|
||||
if (NS_FAILED(rv)) {
|
||||
nsMemory::Free(data);
|
||||
delete stream;
|
||||
return rv;
|
||||
}
|
||||
|
||||
virtual PRInt32 length() const
|
||||
{
|
||||
return mLength - mOffset;
|
||||
}
|
||||
NS_ADDREF(*aStreamResult = stream);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
virtual PRInt32 read(char* buf, PRUint32 aCount)
|
||||
{
|
||||
PRInt32 maxCount = mLength - mOffset;
|
||||
if ((PRInt32)aCount > maxCount)
|
||||
aCount = maxCount;
|
||||
memcpy(buf, mConstString + mOffset, aCount);
|
||||
mOffset += aCount;
|
||||
return aCount;
|
||||
}
|
||||
|
||||
NS_IMETHOD ReadSegments(nsWriteSegmentFun writer, void * closure,
|
||||
PRUint32 aCount, PRUint32 *result) {
|
||||
nsresult rv;
|
||||
PRInt32 maxCount = mLength - mOffset;
|
||||
if (maxCount == 0) {
|
||||
*result = 0;
|
||||
return NS_OK;
|
||||
}
|
||||
if ((PRInt32)aCount > maxCount)
|
||||
aCount = maxCount;
|
||||
rv = writer(this, closure, mConstString + mOffset,
|
||||
0, aCount, result);
|
||||
if (NS_SUCCEEDED(rv))
|
||||
mOffset += *result;
|
||||
// errors returned from the writer end here!
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
const char* mConstString;
|
||||
size_t mLength;
|
||||
|
||||
}; // class ConstCharImpl
|
||||
|
||||
//========================================================================================
|
||||
class ConstStringImpl
|
||||
: public ConstCharImpl
|
||||
//========================================================================================
|
||||
// Factory method to get an nsInputStream from an nsAString. Result will
|
||||
// implement nsIStringInputStream and nsIRandomAccessStore
|
||||
extern "C" NS_COM nsresult
|
||||
NS_NewCStringInputStream(nsIInputStream** aStreamResult,
|
||||
const nsACString& aStringToRead)
|
||||
{
|
||||
public:
|
||||
ConstStringImpl(const nsAString& inString)
|
||||
: ConstCharImpl(ToNewCString(inString),
|
||||
inString.Length())
|
||||
{
|
||||
}
|
||||
NS_PRECONDITION(aStreamResult, "null out ptr");
|
||||
|
||||
ConstStringImpl(const nsACString& inString)
|
||||
: ConstCharImpl(ToNewCString(inString),
|
||||
inString.Length())
|
||||
{
|
||||
}
|
||||
char* data = ToNewCString(aStringToRead);
|
||||
if (!data)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
~ConstStringImpl()
|
||||
{
|
||||
Recycle((char*)mConstString);
|
||||
}
|
||||
nsStringInputStream* stream = new nsStringInputStream();
|
||||
if (! stream) {
|
||||
nsMemory::Free(data);
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
protected:
|
||||
nsresult rv = stream->AdoptData(data, aStringToRead.Length());
|
||||
if (NS_FAILED(rv)) {
|
||||
nsMemory::Free(data);
|
||||
delete stream;
|
||||
return rv;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
}; // class ConstStringImpl
|
||||
NS_ADDREF(*aStreamResult = stream);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
NS_IMPL_THREADSAFE_ADDREF(BasicStringImpl)
|
||||
NS_IMPL_THREADSAFE_RELEASE(BasicStringImpl)
|
||||
|
||||
NS_IMPL_QUERY_HEAD(BasicStringImpl)
|
||||
NS_IMPL_QUERY_BODY(nsISeekableStream)
|
||||
NS_IMPL_QUERY_BODY(nsIOutputStream)
|
||||
NS_IMPL_QUERY_BODY(nsIInputStream)
|
||||
NS_IMPL_QUERY_BODY(nsIRandomAccessStore)
|
||||
NS_IMPL_QUERY_TAIL(nsIOutputStream)
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
extern "C" NS_COM nsresult NS_NewStringInputStream(
|
||||
nsIInputStream** aStreamResult,
|
||||
const nsAString& aStringToRead)
|
||||
// Factory method to get an nsInputStream from a string. Result will implement all the
|
||||
// file stream interfaces in nsIFileStream.h
|
||||
//----------------------------------------------------------------------------------------
|
||||
// Factory method to get an nsInputStream from an nsAString. Result will
|
||||
// implement nsIStringInputStream and nsIRandomAccessStore
|
||||
extern "C" NS_COM nsresult
|
||||
NS_NewCharInputStream(nsIInputStream** aStreamResult,
|
||||
const char* aStringToRead)
|
||||
{
|
||||
NS_PRECONDITION(aStreamResult != nsnull, "null ptr");
|
||||
if (! aStreamResult)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
NS_PRECONDITION(aStreamResult, "null out ptr");
|
||||
|
||||
ConstStringImpl* stream = new ConstStringImpl(aStringToRead);
|
||||
nsStringInputStream* stream = new nsStringInputStream();
|
||||
if (! stream)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
NS_ADDREF(stream);
|
||||
*aStreamResult = NS_STATIC_CAST(nsIInputStream*,stream);
|
||||
nsresult rv = stream->ShareData(aStringToRead, -1);
|
||||
|
||||
if (NS_FAILED(rv)) {
|
||||
delete stream;
|
||||
return rv;
|
||||
}
|
||||
|
||||
NS_ADDREF(*aStreamResult = stream);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
extern "C" NS_COM nsresult NS_NewCStringInputStream(
|
||||
nsIInputStream** aStreamResult,
|
||||
const nsACString& aStringToRead)
|
||||
// Factory method to get an nsInputStream from a cstring. Result will implement all the
|
||||
// file stream interfaces in nsIFileStream.h
|
||||
//----------------------------------------------------------------------------------------
|
||||
// Factory method to get an nsInputStream from an nsAString. Result will
|
||||
// implement nsIStringInputStream and nsIRandomAccessStore
|
||||
extern "C" NS_COM nsresult
|
||||
NS_NewByteInputStream(nsIInputStream** aStreamResult,
|
||||
const char* aStringToRead,
|
||||
PRInt32 aLength)
|
||||
{
|
||||
NS_PRECONDITION(aStreamResult != nsnull, "null ptr");
|
||||
if (! aStreamResult)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
NS_PRECONDITION(aStreamResult, "null out ptr");
|
||||
|
||||
ConstStringImpl* stream = new ConstStringImpl(aStringToRead);
|
||||
nsStringInputStream* stream = new nsStringInputStream();
|
||||
if (! stream)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
NS_ADDREF(stream);
|
||||
*aStreamResult = NS_STATIC_CAST(nsIInputStream*,stream);
|
||||
nsresult rv = stream->ShareData(aStringToRead, aLength);
|
||||
|
||||
if (NS_FAILED(rv)) {
|
||||
delete stream;
|
||||
return rv;
|
||||
}
|
||||
|
||||
NS_ADDREF(*aStreamResult = stream);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
extern "C" NS_COM nsresult NS_NewCharInputStream(
|
||||
nsISupports** aStreamResult,
|
||||
const char* aStringToRead)
|
||||
// Factory method to get an nsInputStream from a string. Result will implement all the
|
||||
// file stream interfaces in nsIFileStream.h
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
NS_PRECONDITION(aStreamResult != nsnull, "null ptr");
|
||||
if (! aStreamResult)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
|
||||
ConstCharImpl* stream = new ConstCharImpl(aStringToRead);
|
||||
if (! stream)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
NS_ADDREF(stream);
|
||||
*aStreamResult = (nsISupports*)(void*)stream;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
extern "C" NS_COM nsresult NS_NewByteInputStream(
|
||||
nsISupports** aStreamResult,
|
||||
const char* aStringToRead,
|
||||
PRInt32 aLength)
|
||||
// Factory method to get an nsInputStream from a string. Result will implement all the
|
||||
// file stream interfaces in nsIFileStream.h
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
NS_PRECONDITION(aStreamResult != nsnull, "null ptr");
|
||||
if (! aStreamResult)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
|
||||
ConstCharImpl* stream = new ConstCharImpl(aStringToRead, aLength);
|
||||
if (! stream)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
NS_ADDREF(stream);
|
||||
*aStreamResult = (nsISupports*)(void*)stream;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
// nsIStringInputStream implementation
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
class nsStringInputStream : public ConstCharImpl
|
||||
, public nsIStringInputStream
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS_INHERITED
|
||||
NS_DECL_NSISTRINGINPUTSTREAM
|
||||
|
||||
nsStringInputStream() : ConstCharImpl(nsnull, -1), mOwned(PR_FALSE) {}
|
||||
virtual ~nsStringInputStream();
|
||||
|
||||
protected:
|
||||
PRBool mOwned;
|
||||
};
|
||||
|
||||
nsStringInputStream::~nsStringInputStream()
|
||||
{
|
||||
if (mOwned)
|
||||
nsMemory::Free((char *) mConstString);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsStringInputStream::SetData(const char *data, PRInt32 dataLen)
|
||||
{
|
||||
if (dataLen < 0)
|
||||
dataLen = strlen(data);
|
||||
|
||||
return AdoptData(nsCRT::strndup(data, dataLen), dataLen);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsStringInputStream::AdoptData(char *data, PRInt32 dataLen)
|
||||
{
|
||||
NS_ENSURE_ARG_POINTER(data);
|
||||
|
||||
if (dataLen < 0)
|
||||
dataLen = strlen(data);
|
||||
|
||||
mConstString = (const char *) data;
|
||||
mLength = dataLen;
|
||||
mOwned = PR_TRUE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsStringInputStream::ShareData(const char *data, PRInt32 dataLen)
|
||||
{
|
||||
NS_ENSURE_ARG_POINTER(data);
|
||||
|
||||
if (dataLen < 0)
|
||||
dataLen = strlen(data);
|
||||
|
||||
mConstString = data;
|
||||
mLength = dataLen;
|
||||
mOwned = PR_FALSE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMPL_ISUPPORTS_INHERITED1(nsStringInputStream,
|
||||
ConstCharImpl,
|
||||
nsIStringInputStream)
|
||||
|
||||
// factory method for constructing a nsStringInputStream object
|
||||
NS_METHOD
|
||||
nsStringInputStreamConstructor(nsISupports *outer, REFNSIID iid, void **result)
|
||||
{
|
||||
*result = nsnull;
|
||||
|
||||
if (nsnull != outer)
|
||||
if (outer)
|
||||
return NS_ERROR_NO_AGGREGATION;
|
||||
|
||||
nsStringInputStream *inst;
|
||||
|
|
|
@ -256,12 +256,11 @@ PRBool nsRandomAccessInputStream::readline(char* s, PRInt32 n)
|
|||
nsInputStringStream::nsInputStringStream(const char* stringToRead)
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
nsISupports* stream;
|
||||
if (NS_FAILED(NS_NewCharInputStream(&stream, stringToRead)))
|
||||
nsCOMPtr<nsIInputStream> stream;
|
||||
if (NS_FAILED(NS_NewCharInputStream(getter_AddRefs(stream), stringToRead)))
|
||||
return;
|
||||
mInputStream = do_QueryInterface(stream);
|
||||
mInputStream = stream;
|
||||
mStore = do_QueryInterface(stream);
|
||||
NS_RELEASE(stream);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
|
|
@ -256,12 +256,11 @@ PRBool nsRandomAccessInputStream::readline(char* s, PRInt32 n)
|
|||
nsInputStringStream::nsInputStringStream(const char* stringToRead)
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
nsISupports* stream;
|
||||
if (NS_FAILED(NS_NewCharInputStream(&stream, stringToRead)))
|
||||
nsCOMPtr<nsIInputStream> stream;
|
||||
if (NS_FAILED(NS_NewCharInputStream(getter_AddRefs(stream), stringToRead)))
|
||||
return;
|
||||
mInputStream = do_QueryInterface(stream);
|
||||
mInputStream = stream;
|
||||
mStore = do_QueryInterface(stream);
|
||||
NS_RELEASE(stream);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
|
Загрузка…
Ссылка в новой задаче