public override int Read(byte[] buffer, int offset, int count)
{
int bytesRead = 0;
while (!_bFoundEnd && (count > 0))
{
// see if we need to start reading a new chunk
if (_bytesLeft == 0)
{
// this loop stops when the end of line is found
for (;;)
{
byte b = (byte)_inputStream.ReadByte();
// see if this is the end of the length
if (b == '\r')
{
// This had better be '\n'
if ((char)_inputStream.ReadByte() != '\n')
{
throw new RemotingException(
CoreChannel.GetResourceString(
"Remoting_Http_ChunkedEncodingError"));
}
else
{
break; // we've finished reading the length
}
}
else
{
int value = HttpChannelHelper.CharacterHexDigitToDecimal(b);
// make sure value is a hex-digit
if ((value < 0) || (value > 15))
{
throw new RemotingException(
CoreChannel.GetResourceString(
"Remoting_Http_ChunkedEncodingError"));
}
// update _bytesLeft value to account for new digit on the right
_bytesLeft = (_bytesLeft * 16) + value;
}
}
if (_bytesLeft == 0)
{
// read off trailing headers and end-line
String trailerHeader;
do
{
trailerHeader = _inputStream.ReadToEndOfLine();
} while (!(trailerHeader.Length == 0));
_bFoundEnd = true;
}
}
if (!_bFoundEnd)
{
int readCount = min(_bytesLeft, count);
int bytesReadThisTime = _inputStream.Read(buffer, offset, readCount);
if (bytesReadThisTime <= 0)
{
throw new RemotingException(
CoreChannel.GetResourceString(
"Remoting_Http_ChunkedEncodingError"));
}
_bytesLeft -= bytesReadThisTime;
count -= bytesReadThisTime;
offset += bytesReadThisTime;
bytesRead += bytesReadThisTime;
// see if the end of the chunk was found
if (_bytesLeft == 0)
{
// read off "\r\n"
char ch = (char)_inputStream.ReadByte();
if (ch != '\r')
{
throw new RemotingException(
CoreChannel.GetResourceString(
"Remoting_Http_ChunkedEncodingError"));
}
ch = (char)_inputStream.ReadByte();
if (ch != '\n')
{
throw new RemotingException(
CoreChannel.GetResourceString(
"Remoting_Http_ChunkedEncodingError"));
}
}
}
} // while (count > 0)
return(bytesRead);
} // Read