private int ReadWithoutValidation([In, Out] byte[] buffer, int offset, int size, bool abortOnError)
{
GlobalLog.Print("int ConnectStream::ReadWithoutValidation()");
GlobalLog.Print("(start)m_ReadBytes = "+m_ReadBytes);
// ********** WARNING - updating logic below should also be updated in BeginReadWithoutValidation *****************
//
// Figure out how much we should really read.
//
int bytesToRead = 0;
if (m_Chunked) {
if (!m_ChunkEofRecvd) {
// See if we have more left from a previous
// chunk.
if (m_ChunkSize != 0) {
bytesToRead = Math.Min(size, m_ChunkSize);
}
else {
// read size of next chunk
try {
bytesToRead = ReadChunkedSync(buffer, offset, size);
m_ChunkSize -= bytesToRead;
}
catch (Exception exception) {
if (abortOnError)
{
IOError(exception);
}
throw;
}
return bytesToRead;
}
}
}
else {
//
// Not doing chunked, so don't read more than is left.
//
if (m_ReadBytes != -1) {
bytesToRead = (int)Math.Min(m_ReadBytes, (long)size);
}
else {
bytesToRead = size;
}
}
// If we're not going to read anything, either because they're
// not asking for anything or there's nothing left, bail
// out now.
if (bytesToRead == 0 || this.Eof) {
return 0;
}
try {
bytesToRead = InternalRead(buffer, offset, bytesToRead);
}
catch (Exception exception) {
if (abortOnError)
{
IOError(exception);
}
throw;
}
GlobalLog.Print("bytesTransferred = "+bytesToRead);
int bytesTransferred = bytesToRead;
if (m_Chunked) {
m_ChunkSize -= bytesTransferred;
}
else {
bool doneReading = false;
if (bytesTransferred <= 0)
{
bytesTransferred = 0;
//
// We read 0 bytes from the connection, or got an error. This is OK if we're
// reading to end, it's an error otherwise.
//
if (m_ReadBytes != -1) {
// A Fatal error
if (abortOnError)
{
IOError(null, false); // request will be aborted but the user will see EOF on that stream read call
}
else
{
throw m_ErrorException; // CloseInternal will process this case as abnormal
}
}
else {
//
// We're reading to end, and we found the end, by reading 0 bytes
//
doneReading = true;
}
}
//
// Not chunking. Update our read bytes state and return what we've read.
//
if (m_ReadBytes != -1) {
m_ReadBytes -= bytesTransferred;
GlobalLog.Assert(m_ReadBytes >= 0, "ConnectStream: Attempting to read more bytes than available.|m_ReadBytes < 0");
GlobalLog.Print("m_ReadBytes = "+m_ReadBytes);
if (m_ReadBytes < 0)
throw new InternalException();
}
if (m_ReadBytes == 0 || doneReading) {
// We're all done reading, tell the connection that.
m_ReadBytes = 0;
//
// indicate to cache that read completed OK
//
CallDone();
}
}
GlobalLog.Print("bytesTransferred = "+bytesToRead);
GlobalLog.Print("(end)m_ReadBytes = "+m_ReadBytes);
// ********** WARNING - updating logic above should also be updated in BeginReadWithoutValidation and EndReadWithoutValidation *****************
return bytesTransferred;
}