зеркало из https://github.com/mozilla/gecko-dev.git
Bug 638031 Allow string streams to share their string buffer r=bsmedberg f=bz
This commit is contained in:
Родитель
a94e57306e
Коммит
6ac5d55d53
|
@ -84,38 +84,36 @@ public:
|
||||||
NS_DECL_NSIIPCSERIALIZABLE
|
NS_DECL_NSIIPCSERIALIZABLE
|
||||||
|
|
||||||
nsStringInputStream()
|
nsStringInputStream()
|
||||||
: mData(nsnull)
|
{
|
||||||
, mOffset(0)
|
Clear();
|
||||||
, mLength(0)
|
}
|
||||||
, mOwned(false)
|
|
||||||
{}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
~nsStringInputStream()
|
~nsStringInputStream()
|
||||||
|
{}
|
||||||
|
|
||||||
|
PRUint32 Length() const
|
||||||
{
|
{
|
||||||
if (mData)
|
return mData.Length();
|
||||||
Clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PRInt32 LengthRemaining() const
|
PRUint32 LengthRemaining() const
|
||||||
{
|
{
|
||||||
return mLength - mOffset;
|
return Length() - mOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Clear()
|
void Clear()
|
||||||
{
|
{
|
||||||
NS_ASSERTION(mData || !mOwned, "bad state");
|
mData.SetIsVoid(true);
|
||||||
if (mOwned)
|
|
||||||
NS_Free(const_cast<char*>(mData));
|
|
||||||
|
|
||||||
// We're about to get a new string; reset the offset.
|
|
||||||
mOffset = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* mData;
|
bool Closed()
|
||||||
PRUint32 mOffset;
|
{
|
||||||
PRUint32 mLength;
|
return mData.IsVoid();
|
||||||
bool mOwned;
|
}
|
||||||
|
|
||||||
|
nsDependentCSubstring mData;
|
||||||
|
PRUint32 mOffset;
|
||||||
};
|
};
|
||||||
|
|
||||||
// This class needs to support threadsafe refcounting since people often
|
// This class needs to support threadsafe refcounting since people often
|
||||||
|
@ -155,18 +153,18 @@ nsStringInputStream::GetData(nsACString &data)
|
||||||
// The stream doesn't have any data when it is closed. We could fake it
|
// The stream doesn't have any data when it is closed. We could fake it
|
||||||
// and return an empty string here, but it seems better to keep this return
|
// and return an empty string here, but it seems better to keep this return
|
||||||
// value consistent with the behavior of the other 'getter' methods.
|
// value consistent with the behavior of the other 'getter' methods.
|
||||||
NS_ENSURE_TRUE(mData, NS_BASE_STREAM_CLOSED);
|
NS_ENSURE_TRUE(!Closed(), NS_BASE_STREAM_CLOSED);
|
||||||
|
|
||||||
data.Assign(mData, mLength);
|
data.Assign(mData);
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
nsStringInputStream::SetData(const nsACString &data)
|
nsStringInputStream::SetData(const nsACString &data)
|
||||||
{
|
{
|
||||||
nsACString::const_iterator iter;
|
mData.Assign(data);
|
||||||
data.BeginReading(iter);
|
mOffset = 0;
|
||||||
return SetData(iter.get(), iter.size_forward());
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
|
@ -184,34 +182,17 @@ NS_IMETHODIMP
|
||||||
nsStringInputStream::SetData(const char *data, PRInt32 dataLen)
|
nsStringInputStream::SetData(const char *data, PRInt32 dataLen)
|
||||||
{
|
{
|
||||||
NS_ENSURE_ARG_POINTER(data);
|
NS_ENSURE_ARG_POINTER(data);
|
||||||
|
mData.Assign(data, dataLen);
|
||||||
if (dataLen < 0)
|
mOffset = 0;
|
||||||
dataLen = strlen(data);
|
return NS_OK;
|
||||||
|
|
||||||
// NOTE: We do not use nsCRT::strndup here because that does not handle
|
|
||||||
// null bytes in the middle of the given data.
|
|
||||||
|
|
||||||
char *copy = static_cast<char *>(NS_Alloc(dataLen));
|
|
||||||
if (!copy)
|
|
||||||
return NS_ERROR_OUT_OF_MEMORY;
|
|
||||||
memcpy(copy, data, dataLen);
|
|
||||||
|
|
||||||
return AdoptData(copy, dataLen);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
nsStringInputStream::AdoptData(char *data, PRInt32 dataLen)
|
nsStringInputStream::AdoptData(char *data, PRInt32 dataLen)
|
||||||
{
|
{
|
||||||
NS_ENSURE_ARG_POINTER(data);
|
NS_ENSURE_ARG_POINTER(data);
|
||||||
|
mData.Adopt(data, dataLen);
|
||||||
if (dataLen < 0)
|
mOffset = 0;
|
||||||
dataLen = strlen(data);
|
|
||||||
|
|
||||||
Clear();
|
|
||||||
|
|
||||||
mData = data;
|
|
||||||
mLength = dataLen;
|
|
||||||
mOwned = true;
|
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -223,11 +204,8 @@ nsStringInputStream::ShareData(const char *data, PRInt32 dataLen)
|
||||||
if (dataLen < 0)
|
if (dataLen < 0)
|
||||||
dataLen = strlen(data);
|
dataLen = strlen(data);
|
||||||
|
|
||||||
Clear();
|
mData.Rebind(data, data + dataLen);
|
||||||
|
mOffset = 0;
|
||||||
mData = data;
|
|
||||||
mLength = dataLen;
|
|
||||||
mOwned = false;
|
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -239,9 +217,6 @@ NS_IMETHODIMP
|
||||||
nsStringInputStream::Close()
|
nsStringInputStream::Close()
|
||||||
{
|
{
|
||||||
Clear();
|
Clear();
|
||||||
mData = nsnull;
|
|
||||||
mLength = 0;
|
|
||||||
mOwned = false;
|
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -250,7 +225,7 @@ nsStringInputStream::Available(PRUint32 *aLength)
|
||||||
{
|
{
|
||||||
NS_ASSERTION(aLength, "null ptr");
|
NS_ASSERTION(aLength, "null ptr");
|
||||||
|
|
||||||
if (!mData)
|
if (Closed())
|
||||||
return NS_BASE_STREAM_CLOSED;
|
return NS_BASE_STREAM_CLOSED;
|
||||||
|
|
||||||
*aLength = LengthRemaining();
|
*aLength = LengthRemaining();
|
||||||
|
@ -269,7 +244,10 @@ nsStringInputStream::ReadSegments(nsWriteSegmentFun writer, void *closure,
|
||||||
PRUint32 aCount, PRUint32 *result)
|
PRUint32 aCount, PRUint32 *result)
|
||||||
{
|
{
|
||||||
NS_ASSERTION(result, "null ptr");
|
NS_ASSERTION(result, "null ptr");
|
||||||
NS_ASSERTION(mLength >= mOffset, "bad stream state");
|
NS_ASSERTION(Length() >= mOffset, "bad stream state");
|
||||||
|
|
||||||
|
if (Closed())
|
||||||
|
return NS_BASE_STREAM_CLOSED;
|
||||||
|
|
||||||
// We may be at end-of-file
|
// We may be at end-of-file
|
||||||
PRUint32 maxCount = LengthRemaining();
|
PRUint32 maxCount = LengthRemaining();
|
||||||
|
@ -277,11 +255,10 @@ nsStringInputStream::ReadSegments(nsWriteSegmentFun writer, void *closure,
|
||||||
*result = 0;
|
*result = 0;
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
NS_ASSERTION(mData, "must have data if maxCount != 0");
|
|
||||||
|
|
||||||
if (aCount > maxCount)
|
if (aCount > maxCount)
|
||||||
aCount = maxCount;
|
aCount = maxCount;
|
||||||
nsresult rv = writer(this, closure, mData + mOffset, 0, aCount, result);
|
nsresult rv = writer(this, closure, mData.BeginReading() + mOffset, 0, aCount, result);
|
||||||
if (NS_SUCCEEDED(rv)) {
|
if (NS_SUCCEEDED(rv)) {
|
||||||
NS_ASSERTION(*result <= aCount,
|
NS_ASSERTION(*result <= aCount,
|
||||||
"writer should not write more than we asked it to write");
|
"writer should not write more than we asked it to write");
|
||||||
|
@ -306,7 +283,7 @@ nsStringInputStream::IsNonBlocking(bool *aNonBlocking)
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
nsStringInputStream::Seek(PRInt32 whence, PRInt64 offset)
|
nsStringInputStream::Seek(PRInt32 whence, PRInt64 offset)
|
||||||
{
|
{
|
||||||
if (!mData)
|
if (Closed())
|
||||||
return NS_BASE_STREAM_CLOSED;
|
return NS_BASE_STREAM_CLOSED;
|
||||||
|
|
||||||
// Compute new stream position. The given offset may be a negative value.
|
// Compute new stream position. The given offset may be a negative value.
|
||||||
|
@ -316,29 +293,27 @@ nsStringInputStream::Seek(PRInt32 whence, PRInt64 offset)
|
||||||
case NS_SEEK_SET:
|
case NS_SEEK_SET:
|
||||||
break;
|
break;
|
||||||
case NS_SEEK_CUR:
|
case NS_SEEK_CUR:
|
||||||
newPos += (PRInt32) mOffset;
|
newPos += mOffset;
|
||||||
break;
|
break;
|
||||||
case NS_SEEK_END:
|
case NS_SEEK_END:
|
||||||
newPos += (PRInt32) mLength;
|
newPos += Length();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
NS_ERROR("invalid whence");
|
NS_ERROR("invalid whence");
|
||||||
return NS_ERROR_INVALID_ARG;
|
return NS_ERROR_INVALID_ARG;
|
||||||
}
|
}
|
||||||
|
|
||||||
// mLength is never larger than PR_INT32_MAX due to the way it is assigned.
|
|
||||||
|
|
||||||
NS_ENSURE_ARG(newPos >= 0);
|
NS_ENSURE_ARG(newPos >= 0);
|
||||||
NS_ENSURE_ARG(newPos <= (PRInt32) mLength);
|
NS_ENSURE_ARG(newPos <= Length());
|
||||||
|
|
||||||
mOffset = (PRInt32) newPos;
|
mOffset = (PRUint32)newPos;
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
nsStringInputStream::Tell(PRInt64* outWhere)
|
nsStringInputStream::Tell(PRInt64* outWhere)
|
||||||
{
|
{
|
||||||
if (!mData)
|
if (Closed())
|
||||||
return NS_BASE_STREAM_CLOSED;
|
return NS_BASE_STREAM_CLOSED;
|
||||||
|
|
||||||
*outWhere = mOffset;
|
*outWhere = mOffset;
|
||||||
|
@ -348,10 +323,10 @@ nsStringInputStream::Tell(PRInt64* outWhere)
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
nsStringInputStream::SetEOF()
|
nsStringInputStream::SetEOF()
|
||||||
{
|
{
|
||||||
if (!mData)
|
if (Closed())
|
||||||
return NS_BASE_STREAM_CLOSED;
|
return NS_BASE_STREAM_CLOSED;
|
||||||
|
|
||||||
mLength = mOffset;
|
mOffset = Length();
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -364,12 +339,12 @@ nsStringInputStream::Read(const IPC::Message *aMsg, void **aIter)
|
||||||
{
|
{
|
||||||
using IPC::ReadParam;
|
using IPC::ReadParam;
|
||||||
|
|
||||||
nsCAutoString value;
|
nsCString value;
|
||||||
|
|
||||||
if (!ReadParam(aMsg, aIter, &value))
|
if (!ReadParam(aMsg, aIter, &value))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
nsresult rv = SetData(value.get(), value.Length());
|
nsresult rv = SetData(value);
|
||||||
if (NS_FAILED(rv))
|
if (NS_FAILED(rv))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -381,10 +356,7 @@ nsStringInputStream::Write(IPC::Message *aMsg)
|
||||||
{
|
{
|
||||||
using IPC::WriteParam;
|
using IPC::WriteParam;
|
||||||
|
|
||||||
nsCAutoString value;
|
WriteParam(aMsg, static_cast<const nsCString&>(PromiseFlatCString(mData)));
|
||||||
GetData(value);
|
|
||||||
|
|
||||||
WriteParam(aMsg, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nsresult
|
nsresult
|
||||||
|
@ -429,27 +401,26 @@ nsresult
|
||||||
NS_NewStringInputStream(nsIInputStream** aStreamResult,
|
NS_NewStringInputStream(nsIInputStream** aStreamResult,
|
||||||
const nsAString& aStringToRead)
|
const nsAString& aStringToRead)
|
||||||
{
|
{
|
||||||
char* data = ToNewCString(aStringToRead); // truncates high-order bytes
|
NS_LossyConvertUTF16toASCII data(aStringToRead); // truncates high-order bytes
|
||||||
if (!data)
|
return NS_NewCStringInputStream(aStreamResult, data);
|
||||||
return NS_ERROR_OUT_OF_MEMORY;
|
|
||||||
|
|
||||||
nsresult rv = NS_NewByteInputStream(aStreamResult, data,
|
|
||||||
aStringToRead.Length(),
|
|
||||||
NS_ASSIGNMENT_ADOPT);
|
|
||||||
if (NS_FAILED(rv))
|
|
||||||
NS_Free(data);
|
|
||||||
return rv;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nsresult
|
nsresult
|
||||||
NS_NewCStringInputStream(nsIInputStream** aStreamResult,
|
NS_NewCStringInputStream(nsIInputStream** aStreamResult,
|
||||||
const nsACString& aStringToRead)
|
const nsACString& aStringToRead)
|
||||||
{
|
{
|
||||||
nsACString::const_iterator data;
|
NS_PRECONDITION(aStreamResult, "null out ptr");
|
||||||
aStringToRead.BeginReading(data);
|
|
||||||
|
|
||||||
return NS_NewByteInputStream(aStreamResult, data.get(), data.size_forward(),
|
nsStringInputStream* stream = new nsStringInputStream();
|
||||||
NS_ASSIGNMENT_COPY);
|
if (! stream)
|
||||||
|
return NS_ERROR_OUT_OF_MEMORY;
|
||||||
|
|
||||||
|
NS_ADDREF(stream);
|
||||||
|
|
||||||
|
stream->SetData(aStringToRead);
|
||||||
|
|
||||||
|
*aStreamResult = stream;
|
||||||
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
// factory method for constructing a nsStringInputStream object
|
// factory method for constructing a nsStringInputStream object
|
||||||
|
|
Загрузка…
Ссылка в новой задаче