modified for new streaming APIs.

This commit is contained in:
michaelp%netscape.com 1998-07-23 20:43:08 +00:00
Родитель 0f33513030
Коммит aa5160c9ae
4 изменённых файлов: 130 добавлений и 142 удалений

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

@ -134,11 +134,13 @@ nsNetlibStream::~nsNetlibStream()
}
void nsNetlibStream::Close()
nsresult nsNetlibStream::Close()
{
LockStream();
m_bIsClosed = PR_TRUE;
UnlockStream();
return NS_OK;
}
@ -178,9 +180,9 @@ PRInt32 nsBufferedStream::GetAvailableSpace(PRInt32 *aErrorCode)
PRInt32 size = 0;
if (m_bIsClosed) {
*aErrorCode = NS_INPUTSTREAM_EOF;
*aErrorCode = NS_BASE_STREAM_EOF;
} else {
*aErrorCode = 0;
*aErrorCode = NS_OK;
LockStream();
size = m_BufferLength - m_WriteOffset;
@ -190,25 +192,23 @@ PRInt32 nsBufferedStream::GetAvailableSpace(PRInt32 *aErrorCode)
}
PRInt32 nsBufferedStream::GetLength()
nsresult nsBufferedStream::GetLength(PRInt32 *aLength)
{
PRInt32 size;
LockStream();
size = m_WriteOffset;
*aLength = m_WriteOffset;
UnlockStream();
return size;
return NS_OK;
}
PRInt32 nsBufferedStream::Write(PRInt32 *aErrorCode,
const char *aBuf,
PRInt32 aOffset,
PRInt32 aLen)
nsresult nsBufferedStream::Write(const char *aBuf,
PRInt32 aOffset,
PRInt32 aLen,
PRInt32 *aWriteCount)
{
PRInt32 bytesWritten = 0;
PRInt32 bytesFree;
nsresult rv = NS_OK;
LockStream();
@ -217,13 +217,11 @@ PRInt32 nsBufferedStream::Write(PRInt32 *aErrorCode,
/* Check for initial error conditions... */
if (NULL == aBuf) {
*aErrorCode = NS_INPUTSTREAM_ILLEGAL_ARGS;
rv = NS_BASE_STREAM_ILLEGAL_ARGS;
goto done;
} else if (m_bIsClosed) {
*aErrorCode = NS_INPUTSTREAM_EOF;
rv = NS_BASE_STREAM_EOF;
goto done;
} else {
*aErrorCode = 0;
}
if (!m_bIsClosed && aBuf) {
@ -255,23 +253,23 @@ PRInt32 nsBufferedStream::Write(PRInt32 *aErrorCode,
memcpy(&m_Buffer[m_WriteOffset], aBuf, aLen);
m_WriteOffset += aLen;
bytesWritten = aLen;
*aWriteCount = aLen;
m_DataLength += aLen;
}
done:
UnlockStream();
return bytesWritten;
return rv;
}
PRInt32 nsBufferedStream::Read(PRInt32 *aErrorCode,
char *aBuf,
PRInt32 aOffset,
PRInt32 aCount)
nsresult nsBufferedStream::Read(char *aBuf,
PRInt32 aOffset,
PRInt32 aCount,
PRInt32 *aReadCount)
{
PRInt32 bytesRead = 0;
nsresult rv = NS_OK;
LockStream();
@ -280,13 +278,13 @@ PRInt32 nsBufferedStream::Read(PRInt32 *aErrorCode,
/* Check for initial error conditions... */
if (NULL == aBuf) {
*aErrorCode = NS_INPUTSTREAM_ILLEGAL_ARGS;
rv = NS_BASE_STREAM_ILLEGAL_ARGS;
*aReadCount = 0;
goto done;
} else if (m_bIsClosed && (0 == m_DataLength)) {
*aErrorCode = NS_INPUTSTREAM_EOF;
rv = NS_BASE_STREAM_EOF;
*aReadCount = 0;
goto done;
} else {
*aErrorCode = 0;
}
if (m_Buffer && m_DataLength) {
@ -303,21 +301,18 @@ PRInt32 nsBufferedStream::Read(PRInt32 *aErrorCode,
memcpy(aBuf, &m_Buffer[m_ReadOffset], aCount);
m_ReadOffset += aCount;
bytesRead = aCount;
*aReadCount = aCount;
m_DataLength -= aCount;
}
else
*aReadCount = 0;
done:
UnlockStream();
return bytesRead;
return rv;
}
nsAsyncStream::nsAsyncStream(PRInt32 buffer_size)
{
m_BufferLength = buffer_size;
@ -350,9 +345,9 @@ PRInt32 nsAsyncStream::GetAvailableSpace(PRInt32 *aErrorCode)
PRInt32 size = 0;
if (m_bIsClosed) {
*aErrorCode = NS_INPUTSTREAM_EOF;
*aErrorCode = NS_BASE_STREAM_EOF;
} else {
*aErrorCode = 0;
*aErrorCode = NS_OK;
LockStream();
size = m_BufferLength - m_DataLength;
@ -362,25 +357,23 @@ PRInt32 nsAsyncStream::GetAvailableSpace(PRInt32 *aErrorCode)
}
PRInt32 nsAsyncStream::GetLength()
nsresult nsAsyncStream::GetLength(PRInt32 *aLength)
{
PRInt32 size;
LockStream();
size = m_DataLength;
*aLength = m_DataLength;
UnlockStream();
return size;
return NS_OK;
}
PRInt32 nsAsyncStream::Write(PRInt32 *aErrorCode,
const char *aBuf,
PRInt32 aOffset,
PRInt32 aLen)
nsresult nsAsyncStream::Write(const char *aBuf,
PRInt32 aOffset,
PRInt32 aLen,
PRInt32 *aWriteCount)
{
PRInt32 bytesWritten = 0;
PRInt32 bytesFree;
nsresult rv = NS_OK;
LockStream();
@ -388,13 +381,11 @@ PRInt32 nsAsyncStream::Write(PRInt32 *aErrorCode,
/* Check for initial error conditions... */
if (NULL == aBuf) {
*aErrorCode = NS_INPUTSTREAM_ILLEGAL_ARGS;
rv = NS_BASE_STREAM_ILLEGAL_ARGS;
goto done;
} else if (m_bIsClosed) {
*aErrorCode = NS_INPUTSTREAM_EOF;
rv = NS_BASE_STREAM_EOF;
goto done;
} else {
*aErrorCode = 0;
}
if (!m_bIsClosed && aBuf) {
@ -425,23 +416,23 @@ PRInt32 nsAsyncStream::Write(PRInt32 *aErrorCode,
m_WriteOffset += aLen;
}
bytesWritten = aLen;
*aWriteCount = aLen;
m_DataLength += aLen;
}
done:
UnlockStream();
return bytesWritten;
return rv;
}
PRInt32 nsAsyncStream::Read(PRInt32 *aErrorCode,
char *aBuf,
PRInt32 aOffset,
PRInt32 aCount)
nsresult nsAsyncStream::Read(char *aBuf,
PRInt32 aOffset,
PRInt32 aCount,
PRInt32 *aReadCount)
{
PRInt32 bytesRead = 0;
nsresult rv = NS_OK;
LockStream();
@ -449,13 +440,13 @@ PRInt32 nsAsyncStream::Read(PRInt32 *aErrorCode,
/* Check for initial error conditions... */
if (NULL == aBuf) {
*aErrorCode = NS_INPUTSTREAM_ILLEGAL_ARGS;
rv = NS_BASE_STREAM_ILLEGAL_ARGS;
*aReadCount = 0;
goto done;
} else if (m_bIsClosed && (0 == m_DataLength)) {
*aErrorCode = NS_INPUTSTREAM_EOF;
rv = NS_BASE_STREAM_EOF;
*aReadCount = 0;
goto done;
} else {
*aErrorCode = 0;
}
if (m_Buffer && m_DataLength) {
@ -485,14 +476,16 @@ PRInt32 nsAsyncStream::Read(PRInt32 *aErrorCode,
m_ReadOffset += aCount;
}
bytesRead = aCount;
*aReadCount = aCount;
m_DataLength -= aCount;
}
else
*aReadCount = 0;
done:
UnlockStream();
return bytesRead;
return rv;
}
@ -535,9 +528,9 @@ PRInt32 nsBlockingStream::GetAvailableSpace(PRInt32 *aErrorCode)
PRInt32 size = 0;
if (m_bIsClosed) {
*aErrorCode = NS_INPUTSTREAM_EOF;
*aErrorCode = NS_BASE_STREAM_EOF;
} else {
*aErrorCode = 0;
*aErrorCode = NS_OK;
LockStream();
size = m_BufferLength - m_DataLength;
@ -547,25 +540,23 @@ PRInt32 nsBlockingStream::GetAvailableSpace(PRInt32 *aErrorCode)
}
PRInt32 nsBlockingStream::GetLength()
nsresult nsBlockingStream::GetLength(PRInt32 *aLength)
{
PRInt32 size;
LockStream();
size = m_DataLength;
*aLength = m_DataLength;
UnlockStream();
return size;
return NS_OK;
}
PRInt32 nsBlockingStream::Write(PRInt32 *aErrorCode,
const char *aBuf,
PRInt32 aOffset,
PRInt32 aLen)
nsresult nsBlockingStream::Write(const char *aBuf,
PRInt32 aOffset,
PRInt32 aLen,
PRInt32 *aWriteCount)
{
PRInt32 bytesWritten = 0;
PRInt32 bytesFree;
nsresult rv;
LockStream();
@ -573,13 +564,11 @@ PRInt32 nsBlockingStream::Write(PRInt32 *aErrorCode,
/* Check for initial error conditions... */
if (NULL == aBuf) {
*aErrorCode = NS_INPUTSTREAM_ILLEGAL_ARGS;
rv = NS_BASE_STREAM_ILLEGAL_ARGS;
goto done;
} else if (m_bIsClosed) {
*aErrorCode = NS_INPUTSTREAM_EOF;
rv = NS_BASE_STREAM_EOF;
goto done;
} else {
*aErrorCode = 0;
}
if (!m_bIsClosed && aBuf) {
@ -610,37 +599,37 @@ PRInt32 nsBlockingStream::Write(PRInt32 *aErrorCode,
m_WriteOffset += aLen;
}
bytesWritten = aLen;
*aWriteCount = aLen;
m_DataLength += aLen;
}
done:
UnlockStream();
return bytesWritten;
return rv;
}
PRInt32 nsBlockingStream::Read(PRInt32 *aErrorCode,
char *aBuf,
PRInt32 aOffset,
PRInt32 aCount)
nsresult nsBlockingStream::Read(char *aBuf,
PRInt32 aOffset,
PRInt32 aCount,
PRInt32 *aReadCount)
{
PRInt32 bytesRead = 0;
nsresult rv = NS_OK;
LockStream();
*aReadCount = 0;
NS_PRECONDITION((m_Buffer || m_bIsClosed), "m_Buffer is NULL!");
/* Check for initial error conditions... */
if (NULL == aBuf) {
*aErrorCode = NS_INPUTSTREAM_ILLEGAL_ARGS;
rv = NS_BASE_STREAM_ILLEGAL_ARGS;
goto done;
} else if (m_bIsClosed && (0 == m_DataLength)) {
*aErrorCode = NS_INPUTSTREAM_EOF;
rv = NS_BASE_STREAM_EOF;
goto done;
} else {
*aErrorCode = 0;
}
if (m_Buffer) {
@ -654,27 +643,27 @@ PRInt32 nsBlockingStream::Read(PRInt32 *aErrorCode,
UnlockStream();
do {
NET_PollSockets();
bytesRead += ReadBuffer(aBuf+bytesRead, aCount-bytesRead);
*aReadCount += ReadBuffer(aBuf + *aReadCount, aCount - *aReadCount);
/* XXX m_bIsClosed is checked outside of the lock! */
} while ((aCount > bytesRead) && !m_bIsClosed);
} while ((aCount > *aReadCount) && !m_bIsClosed);
LockStream();
/*
* It is possible that the stream was closed during
* NET_PollSockets(...)... In this case, return EOF if no data
* is available...
*/
if ((0 == bytesRead) && m_bIsClosed) {
*aErrorCode = NS_INPUTSTREAM_EOF;
if ((0 == *aReadCount) && m_bIsClosed) {
rv = NS_BASE_STREAM_EOF;
}
} else {
bytesRead = ReadBuffer(aBuf, aCount);
*aReadCount = ReadBuffer(aBuf, aCount);
}
}
done:
UnlockStream();
return bytesRead;
return rv;
}

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

@ -59,7 +59,7 @@ public:
virtual PRInt32 GetAvailableSpace(PRInt32 *aErrorCode) = 0;
/* From nsIBaseStream interface */
virtual void Close(void);
NS_IMETHOD Close(void);
protected:
virtual ~nsNetlibStream();
@ -85,20 +85,19 @@ public:
virtual PRInt32 GetAvailableSpace(PRInt32 *aErrorCode);
/* From nsIBaseStream interface */
virtual PRInt32 GetLength(void);
/* nsIInputStream interface */
virtual PRInt32 Read(PRInt32 *aErrorCode,
char *aBuf,
PRInt32 aOffset,
PRInt32 aCount);
NS_IMETHOD GetLength(PRInt32 *aLength);
NS_IMETHOD Read(char *aBuf,
PRInt32 aOffset,
PRInt32 aCount,
PRInt32 *aReadCount);
/* nsIOutputStream interface */
virtual PRInt32 Write(PRInt32 *aErrorCode,
const char *aBuf,
PRInt32 aOffset,
PRInt32 aLen);
NS_IMETHOD Write(const char *aBuf,
PRInt32 aOffset,
PRInt32 aLen,
PRInt32 *aWriteCount);
protected:
virtual ~nsBufferedStream();
@ -124,20 +123,19 @@ public:
virtual PRInt32 GetAvailableSpace(PRInt32 *aErrorCode);
/* From nsIBaseStream interface */
virtual PRInt32 GetLength(void);
/* nsIInputStream interface */
virtual PRInt32 Read(PRInt32 *aErrorCode,
char *aBuf,
PRInt32 aOffset,
PRInt32 aCount);
NS_IMETHOD GetLength(PRInt32 *aLength);
NS_IMETHOD Read(char *aBuf,
PRInt32 aOffset,
PRInt32 aCount,
PRInt32 *aReadLength);
/* nsIOutputStream interface */
virtual PRInt32 Write(PRInt32 *aErrorCode,
const char *aBuf,
PRInt32 aOffset,
PRInt32 aLen);
NS_IMETHOD Write(const char *aBuf,
PRInt32 aOffset,
PRInt32 aLen,
PRInt32 *aWriteLength);
protected:
virtual ~nsAsyncStream();
@ -162,20 +160,19 @@ public:
virtual PRInt32 GetAvailableSpace(PRInt32 *aErrorCode);
/* From nsIBaseStream interface */
virtual PRInt32 GetLength(void);
/* nsIInputStream interface */
virtual PRInt32 Read(PRInt32 *aErrorCode,
char *aBuf,
PRInt32 aOffset,
PRInt32 aCount);
NS_IMETHOD GetLength(PRInt32 *aLength);
NS_IMETHOD Read(char *aBuf,
PRInt32 aOffset,
PRInt32 aCount,
PRInt32 *aReadLength);
/* nsIOutputStream interface */
virtual PRInt32 Write(PRInt32 *aErrorCode,
const char *aBuf,
PRInt32 aOffset,
PRInt32 aLen);
NS_IMETHOD Write(const char *aBuf,
PRInt32 aOffset,
PRInt32 aLen,
PRInt32 *aWriteLength);
protected:
virtual ~nsBlockingStream();

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

@ -538,8 +538,8 @@ void stub_abort(NET_StreamClass *stream, int status)
int stub_put_block(NET_StreamClass *stream, const char *buffer, int32 length)
{
PRInt32 bytesWritten, errorCode;
nsresult rv = NS_OK;
PRInt32 bytesWritten;
nsresult errorCode;
nsConnectionInfo *pConn = GetConnectionInfoFromStream(stream);
TRACEMSG(("+++ stream put_block. Length = %d\n", length));
@ -550,18 +550,18 @@ int stub_put_block(NET_StreamClass *stream, const char *buffer, int32 length)
* is interrupted... In this case, Netlib will call put_block(...)
* with the string "Transfer Interrupted!"
*/
bytesWritten = pConn->pNetStream->Write(&errorCode, buffer, 0, length);
errorCode = pConn->pNetStream->Write(buffer, 0, length, &bytesWritten);
/* Abort the connection... */
if (NS_INPUTSTREAM_EOF == errorCode) {
if (NS_BASE_STREAM_EOF == errorCode) {
return -1;
}
if (pConn->pConsumer && (0 < bytesWritten)) {
rv = pConn->pConsumer->OnDataAvailable(pConn->pURL, pConn->pNetStream, bytesWritten);
errorCode = pConn->pConsumer->OnDataAvailable(pConn->pURL, pConn->pNetStream, bytesWritten);
}
return ((NS_OK == rv) && (bytesWritten == length));
return ((NS_OK == errorCode) && (bytesWritten == length));
}
unsigned int stub_is_write_ready(NET_StreamClass *stream)
@ -577,7 +577,7 @@ unsigned int stub_is_write_ready(NET_StreamClass *stream)
* If the InputStream has been closed... Return 1 byte available so
* Netlib will call put_block(...) one more time...
*/
if (NS_INPUTSTREAM_EOF == errorCode) {
if (NS_BASE_STREAM_EOF == errorCode) {
free_space = 1;
}

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

@ -127,13 +127,15 @@ NS_IMETHODIMP TestConsumer::OnDataAvailable(nsIURL* aURL, nsIInputStream *pIStre
}
do {
PRInt32 err;
nsresult err;
char buffer[80];
int i;
len = pIStream->Read(&err, buffer, 0, 80);
for (i=0; i<len; i++) {
putchar(buffer[i]);
err = pIStream->Read(buffer, 0, 80, &len);
if (err == NS_OK) {
for (i=0; i<len; i++) {
putchar(buffer[i]);
}
}
} while (len > 0);