Refactored a few for RequestStream.cs

This commit is contained in:
sta 2015-04-08 18:02:05 +09:00
parent a25a1ea652
commit 6847e632ea

View File

@ -46,30 +46,30 @@ namespace WebSocketSharp.Net
{ {
#region Private Fields #region Private Fields
private byte [] _buffer; private long _bodyLeft;
private byte[] _buffer;
private int _count;
private bool _disposed; private bool _disposed;
private int _length;
private int _offset; private int _offset;
private long _remainingBody;
private Stream _stream; private Stream _stream;
#endregion #endregion
#region Internal Constructors #region Internal Constructors
internal RequestStream (Stream stream, byte [] buffer, int offset, int length) internal RequestStream (Stream stream, byte[] buffer, int offset, int count)
: this (stream, buffer, offset, length, -1) : this (stream, buffer, offset, count, -1)
{ {
} }
internal RequestStream ( internal RequestStream (
Stream stream, byte [] buffer, int offset, int length, long contentlength) Stream stream, byte[] buffer, int offset, int count, long contentLength)
{ {
_stream = stream; _stream = stream;
_buffer = buffer; _buffer = buffer;
_offset = offset; _offset = offset;
_length = length; _count = count;
_remainingBody = contentlength; _bodyLeft = contentLength;
} }
#endregion #endregion
@ -115,50 +115,43 @@ namespace WebSocketSharp.Net
#region Private Methods #region Private Methods
// Returns 0 if we can keep reading from the base stream, // Returns 0 if we can keep reading from the base stream,
// > 0 if we read something from the buffer. // > 0 if we read something from the buffer,
// -1 if we had a content length set and we finished reading that many bytes. // -1 if we had a content length set and we finished reading that many bytes.
private int fillFromBuffer (byte [] buffer, int offset, int count) private int fillFromBuffer (byte[] buffer, int offset, int count)
{ {
if (buffer == null) if (buffer == null)
throw new ArgumentNullException ("buffer"); throw new ArgumentNullException ("buffer");
if (offset < 0) if (offset < 0)
throw new ArgumentOutOfRangeException ("offset", "Less than zero."); throw new ArgumentOutOfRangeException ("offset", "A negative value.");
if (count < 0) if (count < 0)
throw new ArgumentOutOfRangeException ("count", "Less than zero."); throw new ArgumentOutOfRangeException ("count", "A negative value.");
var len = buffer.Length; var len = buffer.Length;
if (offset > len) if (offset + count > len)
throw new ArgumentException ("'offset' is greater than 'buffer' size."); throw new ArgumentException (
"The sum of 'offset' and 'count' is greater than 'buffer' length.");
if (offset > len - count) if (_bodyLeft == 0)
throw new ArgumentException ("Reading would overrun 'buffer'.");
if (_remainingBody == 0)
return -1; return -1;
if (_length == 0) if (_count == 0 || count == 0)
return 0; return 0;
var size = _length < count ? _length : count; if (count > _count)
if (_remainingBody > 0 && _remainingBody < size) count = _count;
size = (int) _remainingBody;
var remainingBuffer = _buffer.Length - _offset; if (_bodyLeft > 0 && count > _bodyLeft)
if (remainingBuffer < size) count = (int) _bodyLeft;
size = remainingBuffer;
if (size == 0) Buffer.BlockCopy (_buffer, _offset, buffer, offset, count);
return 0; _offset += count;
_count -= count;
if (_bodyLeft > 0)
_bodyLeft -= count;
Buffer.BlockCopy (_buffer, _offset, buffer, offset, size); return count;
_offset += size;
_length -= size;
if (_remainingBody > 0)
_remainingBody -= size;
return size;
} }
#endregion #endregion
@ -166,7 +159,7 @@ namespace WebSocketSharp.Net
#region Public Methods #region Public Methods
public override IAsyncResult BeginRead ( public override IAsyncResult BeginRead (
byte [] buffer, int offset, int count, AsyncCallback callback, object state) byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{ {
if (_disposed) if (_disposed)
throw new ObjectDisposedException (GetType ().ToString ()); throw new ObjectDisposedException (GetType ().ToString ());
@ -184,14 +177,14 @@ namespace WebSocketSharp.Net
} }
// Avoid reading past the end of the request to allow for HTTP pipelining. // Avoid reading past the end of the request to allow for HTTP pipelining.
if (_remainingBody >= 0 && _remainingBody < count) if (_bodyLeft >= 0 && count > _bodyLeft)
count = (int) _remainingBody; count = (int) _bodyLeft;
return _stream.BeginRead (buffer, offset, count, callback, state); return _stream.BeginRead (buffer, offset, count, callback, state);
} }
public override IAsyncResult BeginWrite ( public override IAsyncResult BeginWrite (
byte [] buffer, int offset, int count, AsyncCallback callback, object state) byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{ {
throw new NotSupportedException (); throw new NotSupportedException ();
} }
@ -219,8 +212,8 @@ namespace WebSocketSharp.Net
// Close on exception? // Close on exception?
var nread = _stream.EndRead (asyncResult); var nread = _stream.EndRead (asyncResult);
if (nread > 0 && _remainingBody > 0) if (nread > 0 && _bodyLeft > 0)
_remainingBody -= nread; _bodyLeft -= nread;
return nread; return nread;
} }
@ -234,13 +227,12 @@ namespace WebSocketSharp.Net
{ {
} }
public override int Read (byte [] buffer, int offset, int count) public override int Read (byte[] buffer, int offset, int count)
{ {
if (_disposed) if (_disposed)
throw new ObjectDisposedException (GetType ().ToString ()); throw new ObjectDisposedException (GetType ().ToString ());
// Call fillFromBuffer to check for buffer boundaries even when // Call the fillFromBuffer method to check for buffer boundaries even when _bodyLeft is 0.
// _remainingBody is 0.
var nread = fillFromBuffer (buffer, offset, count); var nread = fillFromBuffer (buffer, offset, count);
if (nread == -1) // No more bytes available (Content-Length). if (nread == -1) // No more bytes available (Content-Length).
return 0; return 0;
@ -249,8 +241,8 @@ namespace WebSocketSharp.Net
return nread; return nread;
nread = _stream.Read (buffer, offset, count); nread = _stream.Read (buffer, offset, count);
if (nread > 0 && _remainingBody > 0) if (nread > 0 && _bodyLeft > 0)
_remainingBody -= nread; _bodyLeft -= nread;
return nread; return nread;
} }
@ -265,7 +257,7 @@ namespace WebSocketSharp.Net
throw new NotSupportedException (); throw new NotSupportedException ();
} }
public override void Write (byte [] buffer, int offset, int count) public override void Write (byte[] buffer, int offset, int count)
{ {
throw new NotSupportedException (); throw new NotSupportedException ();
} }