gecko-dev/network/module/nsNetStream.cpp

751 строка
18 KiB
C++
Исходник Обычный вид История

1998-05-23 03:38:40 +04:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "nsNetStream.h"
#include "net.h"
#include "mktrace.h"
#if defined(XP_PC)
#include <windows.h> // Needed for Interlocked APIs defined in nsISupports.h
#endif /* XP_PC */
1998-05-23 03:38:40 +04:00
/* XXX: Declare NET_PollSockets(...) for the blocking stream hack... */
extern "C" {
1998-06-02 04:23:17 +04:00
XP_Bool NET_PollSockets(void);
}
1998-05-23 03:38:40 +04:00
NS_DEFINE_IID(kIInputStreamIID, NS_IINPUTSTREAM_IID);
NS_DEFINE_IID(kIConnectionInfoIID, NS_ICONNECTIONINFO_IID);
1998-05-23 03:38:40 +04:00
#define BUFFER_BLOCK_SIZE 8192
nsConnectionInfo::nsConnectionInfo(nsIURL *aURL,
nsNetlibStream *aStream,
nsIStreamListener *aNotify)
{
NS_INIT_REFCNT();
mStatus = nsConnectionActive;
1998-05-23 03:38:40 +04:00
pURL = aURL;
pNetStream = aStream;
pConsumer = aNotify;
NS_IF_ADDREF(pURL);
NS_IF_ADDREF(pNetStream);
NS_IF_ADDREF(pConsumer);
1998-05-23 03:38:40 +04:00
NS_VERIFY_THREADSAFE_INTERFACE(pURL);
NS_VERIFY_THREADSAFE_INTERFACE(pNetStream);
NS_VERIFY_THREADSAFE_INTERFACE(pConsumer);
1998-05-23 03:38:40 +04:00
}
NS_IMPL_THREADSAFE_ISUPPORTS(nsConnectionInfo, kIConnectionInfoIID);
1998-05-23 03:38:40 +04:00
nsConnectionInfo::~nsConnectionInfo()
{
TRACEMSG(("nsConnectionInfo is being destroyed...\n"));
NS_IF_RELEASE(pURL);
NS_IF_RELEASE(pNetStream);
NS_IF_RELEASE(pConsumer);
1998-05-23 03:38:40 +04:00
}
NS_IMETHODIMP
nsConnectionInfo::GetURL(nsIURL **aURL)
{
*aURL = pURL;
NS_IF_ADDREF(pURL);
return NS_OK;
}
NS_IMETHODIMP
nsConnectionInfo::GetInputStream(nsIInputStream **aStream)
{
*aStream = (nsIInputStream *)pNetStream;
NS_IF_ADDREF(pNetStream);
return NS_OK;
}
1998-05-23 03:38:40 +04:00
NS_IMETHODIMP
nsConnectionInfo::GetOutputStream(nsIOutputStream **aStream)
{
*aStream = (nsIOutputStream *)pNetStream;
NS_IF_ADDREF(pNetStream);
return NS_OK;
}
NS_IMETHODIMP
nsConnectionInfo::GetConsumer(nsIStreamListener **aConsumer)
{
*aConsumer = pConsumer;
NS_IF_ADDREF(pConsumer);
return NS_OK;
}
1998-05-23 03:38:40 +04:00
nsNetlibStream::nsNetlibStream(void)
{
NS_INIT_REFCNT();
m_Lock = PR_NewMonitor();
m_bIsClosed = PR_FALSE;
}
NS_IMPL_THREADSAFE_ADDREF(nsNetlibStream)
NS_IMPL_THREADSAFE_RELEASE(nsNetlibStream)
1998-05-23 03:38:40 +04:00
nsresult nsNetlibStream::QueryInterface(const nsIID &aIID, void** aInstancePtr)
{
if (NULL == aInstancePtr) {
return NS_ERROR_NULL_POINTER;
}
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
static NS_DEFINE_IID(kIInputStreamIID, NS_IINPUTSTREAM_IID);
static NS_DEFINE_IID(kIOutputStreamIID, NS_IOUTPUTSTREAM_IID);
if (aIID.Equals(kIInputStreamIID)) {
*aInstancePtr = (void*) ((nsIInputStream*)this);
AddRef();
return NS_OK;
}
if (aIID.Equals(kIOutputStreamIID)) {
*aInstancePtr = (void*) ((nsIOutputStream*)this);
AddRef();
return NS_OK;
}
if (aIID.Equals(kISupportsIID)) {
*aInstancePtr = ((nsISupports *)(nsIInputStream *)this);
AddRef();
return NS_OK;
}
#if defined(NS_DEBUG)
/*
* Check for the debug-only interface indicating thread-safety
*/
static NS_DEFINE_IID(kIsThreadsafeIID, NS_ISTHREADSAFE_IID);
if (aIID.Equals(kIsThreadsafeIID)) {
return NS_OK;
}
#endif /* NS_DEBUG */
1998-05-23 03:38:40 +04:00
return NS_NOINTERFACE;
}
nsNetlibStream::~nsNetlibStream()
{
if (m_Lock) {
PR_DestroyMonitor(m_Lock);
m_Lock = NULL;
}
}
1998-07-24 00:43:08 +04:00
nsresult nsNetlibStream::Close()
1998-05-23 03:38:40 +04:00
{
LockStream();
m_bIsClosed = PR_TRUE;
UnlockStream();
1998-07-24 00:43:08 +04:00
return NS_OK;
1998-05-23 03:38:40 +04:00
}
nsBufferedStream::nsBufferedStream(void)
{
m_BufferLength = BUFFER_BLOCK_SIZE;
m_Buffer = new char[m_BufferLength];
/* If the allocation failed, mark the stream as closed... */
if (NULL == m_Buffer) {
m_bIsClosed = PR_TRUE;
m_BufferLength = 0;
}
m_DataLength = 0;
m_ReadOffset = m_WriteOffset = 0;
}
nsBufferedStream::~nsBufferedStream()
{
TRACEMSG(("nsBufferedStream is being destroyed...\n"));
if (m_Buffer) {
free(m_Buffer);
m_Buffer = NULL;
}
}
nsresult nsBufferedStream::GetAvailableSpace(PRUint32 *aCount)
1998-05-23 03:38:40 +04:00
{
nsresult err;
PRUint32 size = 0;
1998-05-23 03:38:40 +04:00
if (m_bIsClosed) {
err = NS_BASE_STREAM_EOF;
1998-05-23 03:38:40 +04:00
} else {
err = NS_OK;
1998-05-23 03:38:40 +04:00
LockStream();
NS_ASSERTION(m_BufferLength >= m_WriteOffset, "unsigned madness");
1998-05-23 03:38:40 +04:00
size = m_BufferLength - m_WriteOffset;
UnlockStream();
}
*aCount = size;
return err;
1998-05-23 03:38:40 +04:00
}
nsresult nsBufferedStream::GetLength(PRUint32 *aLength)
1998-05-23 03:38:40 +04:00
{
LockStream();
*aLength = m_DataLength;
1998-05-23 03:38:40 +04:00
UnlockStream();
1998-07-24 00:43:08 +04:00
return NS_OK;
1998-05-23 03:38:40 +04:00
}
1998-07-24 00:43:08 +04:00
nsresult nsBufferedStream::Write(const char *aBuf,
PRUint32 aLen,
PRUint32 *aWriteCount)
1998-05-23 03:38:40 +04:00
{
PRUint32 bytesFree;
1998-07-24 00:43:08 +04:00
nsresult rv = NS_OK;
1998-05-23 03:38:40 +04:00
LockStream();
NS_PRECONDITION((m_Buffer || m_bIsClosed), "m_Buffer is NULL!");
NS_PRECONDITION((m_WriteOffset >= m_ReadOffset), "Read past the end of buffer.");
/* Check for initial error conditions... */
if (NULL == aBuf) {
1998-07-24 00:43:08 +04:00
rv = NS_BASE_STREAM_ILLEGAL_ARGS;
1998-05-23 03:38:40 +04:00
goto done;
} else if (m_bIsClosed) {
1998-07-24 00:43:08 +04:00
rv = NS_BASE_STREAM_EOF;
1998-05-23 03:38:40 +04:00
goto done;
}
if (!m_bIsClosed && aBuf) {
/* Grow the buffer if necessary */
NS_ASSERTION(m_BufferLength >= m_WriteOffset, "unsigned madness");
1998-05-23 03:38:40 +04:00
bytesFree = m_BufferLength - m_WriteOffset;
if (aLen > bytesFree) {
char *newBuffer;
m_BufferLength += (((aLen - bytesFree) / BUFFER_BLOCK_SIZE)+1) * BUFFER_BLOCK_SIZE;
newBuffer = (char *)realloc(m_Buffer, m_BufferLength);
/* If the allocation failed, close the stream and free the buffer... */
if (NULL == newBuffer) {
m_bIsClosed = PR_TRUE;
free(m_Buffer);
m_Buffer = NULL;
m_BufferLength = 0;
goto done;
} else {
m_Buffer = newBuffer;
}
}
memcpy(&m_Buffer[m_WriteOffset], aBuf, aLen);
m_WriteOffset += aLen;
1998-07-24 00:43:08 +04:00
*aWriteCount = aLen;
1998-05-23 03:38:40 +04:00
m_DataLength += aLen;
}
done:
UnlockStream();
1998-07-24 00:43:08 +04:00
return rv;
1998-05-23 03:38:40 +04:00
}
1998-07-24 00:43:08 +04:00
nsresult nsBufferedStream::Read(char *aBuf,
PRUint32 aCount,
PRUint32 *aReadCount)
1998-05-23 03:38:40 +04:00
{
1998-07-24 00:43:08 +04:00
nsresult rv = NS_OK;
1998-05-23 03:38:40 +04:00
LockStream();
NS_PRECONDITION((m_Buffer || m_bIsClosed), "m_Buffer is NULL!");
NS_PRECONDITION((m_WriteOffset >= m_ReadOffset), "Read past the end of buffer.");
/* Check for initial error conditions... */
if (NULL == aBuf) {
1998-07-24 00:43:08 +04:00
rv = NS_BASE_STREAM_ILLEGAL_ARGS;
*aReadCount = 0;
1998-05-23 03:38:40 +04:00
goto done;
} else if (m_bIsClosed && (0 == m_DataLength)) {
1998-07-24 00:43:08 +04:00
rv = NS_BASE_STREAM_EOF;
*aReadCount = 0;
1998-05-23 03:38:40 +04:00
goto done;
}
if (m_Buffer && m_DataLength) {
/* Do not read more data than there is available... */
if (aCount > m_DataLength) {
aCount = m_DataLength;
}
memcpy(aBuf, &m_Buffer[m_ReadOffset], aCount);
m_ReadOffset += aCount;
1998-07-24 00:43:08 +04:00
*aReadCount = aCount;
1998-05-23 03:38:40 +04:00
m_DataLength -= aCount;
}
1998-07-24 00:43:08 +04:00
else
*aReadCount = 0;
1998-05-23 03:38:40 +04:00
done:
UnlockStream();
1998-07-24 00:43:08 +04:00
return rv;
1998-05-23 03:38:40 +04:00
}
nsAsyncStream::nsAsyncStream(PRUint32 buffer_size)
1998-05-23 03:38:40 +04:00
{
m_BufferLength = buffer_size;
m_Buffer = (char *)malloc(m_BufferLength);
/* If the allocation failed, mark the stream as closed... */
if (NULL == m_Buffer) {
m_bIsClosed = PR_TRUE;
m_BufferLength = 0;
}
m_DataLength = 0;
m_ReadOffset = m_WriteOffset = 0;
}
nsAsyncStream::~nsAsyncStream()
{
TRACEMSG(("nsAsyncStream is being destroyed...\n"));
if (m_Buffer) {
free(m_Buffer);
m_Buffer = NULL;
}
}
nsresult nsAsyncStream::GetAvailableSpace(PRUint32 *aCount)
1998-05-23 03:38:40 +04:00
{
nsresult err;
1998-05-23 03:38:40 +04:00
PRInt32 size = 0;
if (m_bIsClosed) {
err = NS_BASE_STREAM_EOF;
1998-05-23 03:38:40 +04:00
} else {
err = NS_OK;
1998-05-23 03:38:40 +04:00
LockStream();
size = m_BufferLength - m_DataLength;
UnlockStream();
}
*aCount = size;
return err;
1998-05-23 03:38:40 +04:00
}
nsresult nsAsyncStream::GetLength(PRUint32 *aLength)
1998-05-23 03:38:40 +04:00
{
LockStream();
1998-07-24 00:43:08 +04:00
*aLength = m_DataLength;
1998-05-23 03:38:40 +04:00
UnlockStream();
1998-07-24 00:43:08 +04:00
return NS_OK;
1998-05-23 03:38:40 +04:00
}
1998-07-24 00:43:08 +04:00
nsresult nsAsyncStream::Write(const char *aBuf,
PRUint32 aLen,
PRUint32 *aWriteCount)
1998-05-23 03:38:40 +04:00
{
PRUint32 bytesFree;
1998-07-24 00:43:08 +04:00
nsresult rv = NS_OK;
1998-05-23 03:38:40 +04:00
LockStream();
NS_PRECONDITION((m_Buffer || m_bIsClosed), "m_Buffer is NULL!");
/* Check for initial error conditions... */
if (NULL == aBuf) {
1998-07-24 00:43:08 +04:00
rv = NS_BASE_STREAM_ILLEGAL_ARGS;
1998-05-23 03:38:40 +04:00
goto done;
} else if (m_bIsClosed) {
1998-07-24 00:43:08 +04:00
rv = NS_BASE_STREAM_EOF;
1998-05-23 03:38:40 +04:00
goto done;
}
if (!m_bIsClosed && aBuf) {
/* Do not store more data than there is space for... */
NS_ASSERTION(m_BufferLength >= m_DataLength, "unsigned madness");
1998-05-23 03:38:40 +04:00
bytesFree = m_BufferLength - m_DataLength;
if (aLen > bytesFree) {
aLen = bytesFree;
}
/* Storing the data will cause m_WriteOffset to wrap */
if (m_WriteOffset + aLen > m_BufferLength) {
PRInt32 delta;
/* Store the first chunk through the end of the buffer */
delta = m_BufferLength - m_WriteOffset;
memcpy(&m_Buffer[m_WriteOffset], aBuf, delta);
/* Store the second chunk from the beginning of the buffer */
m_WriteOffset = aLen-delta;
memcpy(m_Buffer, &aBuf[delta], m_WriteOffset);
} else {
memcpy(&m_Buffer[m_WriteOffset], aBuf, aLen);
m_WriteOffset += aLen;
}
1998-07-24 00:43:08 +04:00
*aWriteCount = aLen;
1998-05-23 03:38:40 +04:00
m_DataLength += aLen;
}
done:
UnlockStream();
1998-07-24 00:43:08 +04:00
return rv;
1998-05-23 03:38:40 +04:00
}
1998-07-24 00:43:08 +04:00
nsresult nsAsyncStream::Read(char *aBuf,
PRUint32 aCount,
PRUint32 *aReadCount)
1998-05-23 03:38:40 +04:00
{
1998-07-24 00:43:08 +04:00
nsresult rv = NS_OK;
1998-05-23 03:38:40 +04:00
LockStream();
NS_PRECONDITION((m_Buffer || m_bIsClosed), "m_Buffer is NULL!");
/* Check for initial error conditions... */
if (NULL == aBuf) {
1998-07-24 00:43:08 +04:00
rv = NS_BASE_STREAM_ILLEGAL_ARGS;
*aReadCount = 0;
1998-05-23 03:38:40 +04:00
goto done;
} else if (m_bIsClosed && (0 == m_DataLength)) {
1998-07-24 00:43:08 +04:00
rv = NS_BASE_STREAM_EOF;
*aReadCount = 0;
1998-05-23 03:38:40 +04:00
goto done;
}
if (m_Buffer && m_DataLength) {
/* Do not read more data than there is available... */
if (aCount > m_DataLength) {
aCount = m_DataLength;
}
/* Reading the data will cause m_ReadOffset to wrap */
if (m_ReadOffset + aCount > m_BufferLength) {
PRInt32 delta;
/* Read the first chunk through the end of the buffer */
delta = m_BufferLength - m_ReadOffset;
memcpy(aBuf, &m_Buffer[m_ReadOffset], delta);
/* Read the second chunk from the beginning of the buffer */
m_ReadOffset = aCount-delta;
memcpy(&aBuf[delta], m_Buffer, m_ReadOffset);
} else {
memcpy(aBuf, &m_Buffer[m_ReadOffset], aCount);
m_ReadOffset += aCount;
}
1998-07-24 00:43:08 +04:00
*aReadCount = aCount;
1998-05-23 03:38:40 +04:00
m_DataLength -= aCount;
}
1998-07-24 00:43:08 +04:00
else
*aReadCount = 0;
1998-05-23 03:38:40 +04:00
done:
UnlockStream();
1998-07-24 00:43:08 +04:00
return rv;
1998-05-23 03:38:40 +04:00
}
nsBlockingStream::nsBlockingStream()
{
m_BufferLength = BUFFER_BLOCK_SIZE;
m_Buffer = (char *)malloc(m_BufferLength);
/* If the allocation failed, mark the stream as closed... */
if (NULL == m_Buffer) {
m_bIsClosed = PR_TRUE;
m_BufferLength = 0;
}
m_DataLength = 0;
m_ReadOffset = m_WriteOffset = 0;
}
nsBlockingStream::~nsBlockingStream()
{
TRACEMSG(("nsBlockingStream is being destroyed...\n"));
if (m_Buffer) {
free(m_Buffer);
m_Buffer = NULL;
}
}
nsresult nsBlockingStream::Close()
{
LockStream();
m_bIsClosed = PR_TRUE;
#if defined(NETLIB_THREAD)
/* Wake up any waiting threads... */
PR_Notify(m_Lock);
#endif /* NETLIB_THREAD */
UnlockStream();
return NS_OK;
}
nsresult nsBlockingStream::GetAvailableSpace(PRUint32 *aCount)
1998-05-23 03:38:40 +04:00
{
nsresult err;
1998-05-23 03:38:40 +04:00
PRInt32 size = 0;
if (m_bIsClosed) {
err = NS_BASE_STREAM_EOF;
1998-05-23 03:38:40 +04:00
} else {
err = NS_OK;
1998-05-23 03:38:40 +04:00
LockStream();
size = m_BufferLength - m_DataLength;
UnlockStream();
}
*aCount = size;
return err;
1998-05-23 03:38:40 +04:00
}
nsresult nsBlockingStream::GetLength(PRUint32 *aLength)
1998-05-23 03:38:40 +04:00
{
LockStream();
1998-07-24 00:43:08 +04:00
*aLength = m_DataLength;
1998-05-23 03:38:40 +04:00
UnlockStream();
1998-07-24 00:43:08 +04:00
return NS_OK;
1998-05-23 03:38:40 +04:00
}
1998-07-24 00:43:08 +04:00
nsresult nsBlockingStream::Write(const char *aBuf,
PRUint32 aLen,
PRUint32 *aWriteCount)
1998-05-23 03:38:40 +04:00
{
PRUint32 bytesFree;
nsresult rv = NS_OK;
1998-05-23 03:38:40 +04:00
LockStream();
NS_PRECONDITION((m_Buffer || m_bIsClosed), "m_Buffer is NULL!");
/* Check for initial error conditions... */
if (NULL == aBuf) {
1998-07-24 00:43:08 +04:00
rv = NS_BASE_STREAM_ILLEGAL_ARGS;
1998-05-23 03:38:40 +04:00
goto done;
} else if (m_bIsClosed) {
1998-07-24 00:43:08 +04:00
rv = NS_BASE_STREAM_EOF;
1998-05-23 03:38:40 +04:00
goto done;
}
if (!m_bIsClosed && aBuf) {
/* Do not store more data than there is space for... */
NS_ASSERTION(m_BufferLength >= m_DataLength, "unsigned madness");
1998-05-23 03:38:40 +04:00
bytesFree = m_BufferLength - m_DataLength;
if (aLen > bytesFree) {
aLen = bytesFree;
}
/* Storing the data will cause m_WriteOffset to wrap */
if (m_WriteOffset + aLen > m_BufferLength) {
PRUint32 delta;
1998-05-23 03:38:40 +04:00
/* Store the first chunk through the end of the buffer */
NS_ASSERTION(m_BufferLength >= m_WriteOffset, "unsigned madness");
1998-05-23 03:38:40 +04:00
delta = m_BufferLength - m_WriteOffset;
memcpy(&m_Buffer[m_WriteOffset], aBuf, delta);
/* Store the second chunk from the beginning of the buffer */
m_WriteOffset = aLen-delta;
memcpy(m_Buffer, &aBuf[delta], m_WriteOffset);
} else {
memcpy(&m_Buffer[m_WriteOffset], aBuf, aLen);
m_WriteOffset += aLen;
}
1998-07-24 00:43:08 +04:00
*aWriteCount = aLen;
1998-05-23 03:38:40 +04:00
m_DataLength += aLen;
#if defined(NETLIB_THREAD)
/* Wake up any waiting threads... */
PR_Notify(m_Lock);
#endif /* NETLIB_THREAD */
1998-05-23 03:38:40 +04:00
}
done:
UnlockStream();
1998-07-24 00:43:08 +04:00
return rv;
1998-05-23 03:38:40 +04:00
}
1998-07-24 00:43:08 +04:00
nsresult nsBlockingStream::Read(char *aBuf,
PRUint32 aCount,
PRUint32 *aReadCount)
1998-05-23 03:38:40 +04:00
{
1998-07-24 00:43:08 +04:00
nsresult rv = NS_OK;
1998-05-23 03:38:40 +04:00
LockStream();
1998-07-24 00:43:08 +04:00
*aReadCount = 0;
1998-05-23 03:38:40 +04:00
NS_PRECONDITION((m_Buffer || m_bIsClosed), "m_Buffer is NULL!");
/* Check for initial error conditions... */
if (NULL == aBuf) {
1998-07-24 00:43:08 +04:00
rv = NS_BASE_STREAM_ILLEGAL_ARGS;
1998-05-23 03:38:40 +04:00
goto done;
} else if (m_bIsClosed && (0 == m_DataLength)) {
1998-07-24 00:43:08 +04:00
rv = NS_BASE_STREAM_EOF;
1998-05-23 03:38:40 +04:00
goto done;
}
if (m_Buffer) {
/*
* There is either enough data, or some data left in the stream
* after it has been closed... So, read it and return.
*/
if ((aCount <= m_DataLength) || m_bIsClosed) {
*aReadCount = ReadBuffer(aBuf, aCount);
}
1998-05-23 03:38:40 +04:00
/* Not enough data is available... Must block. */
else {
#if !defined(NETLIB_THREAD)
1998-05-23 03:38:40 +04:00
UnlockStream();
do {
NET_PollSockets();
1998-07-24 00:43:08 +04:00
*aReadCount += ReadBuffer(aBuf + *aReadCount, aCount - *aReadCount);
1998-05-23 03:38:40 +04:00
/* XXX m_bIsClosed is checked outside of the lock! */
1998-07-24 00:43:08 +04:00
} while ((aCount > *aReadCount) && !m_bIsClosed);
1998-05-23 03:38:40 +04:00
LockStream();
#else
do {
PR_Wait(m_Lock, PR_INTERVAL_NO_TIMEOUT);
*aReadCount += ReadBuffer(aBuf + *aReadCount, aCount - *aReadCount);
} while ((aCount > *aReadCount) && !m_bIsClosed);
#endif /* NETLIB_THREAD */
1998-05-23 03:38:40 +04:00
/*
* It is possible that the stream was closed during
* NET_PollSockets(...)... In this case, return EOF if no data
* is available...
*/
1998-07-24 00:43:08 +04:00
if ((0 == *aReadCount) && m_bIsClosed) {
rv = NS_BASE_STREAM_EOF;
1998-05-23 03:38:40 +04:00
}
}
1998-05-23 03:38:40 +04:00
}
done:
UnlockStream();
1998-07-24 00:43:08 +04:00
return rv;
1998-05-23 03:38:40 +04:00
}
PRInt32 nsBlockingStream::ReadBuffer(char *aBuf, PRUint32 aCount)
1998-05-23 03:38:40 +04:00
{
if (aCount == 0) return 0; /* avoid calling LockStream, memcpy, etc. */
PRUint32 bytesRead = 0;
1998-05-23 03:38:40 +04:00
LockStream();
/* Do not read more data than there is available... */
if (aCount > m_DataLength) {
aCount = m_DataLength;
}
/* Reading the data will cause m_ReadOffset to wrap */
if (m_ReadOffset + aCount > m_BufferLength) {
PRInt32 delta;
/* Read the first chunk through the end of the buffer */
delta = m_BufferLength - m_ReadOffset;
memcpy(aBuf, &m_Buffer[m_ReadOffset], delta);
/* Read the second chunk from the beginning of the buffer */
m_ReadOffset = aCount-delta;
memcpy(&aBuf[delta], m_Buffer, m_ReadOffset);
} else {
memcpy(aBuf, &m_Buffer[m_ReadOffset], aCount);
m_ReadOffset += aCount;
}
bytesRead = aCount;
m_DataLength -= aCount;
UnlockStream();
return bytesRead;
}