 Chromium Code Reviews
 Chromium Code Reviews Issue 9293029:
  net: Introduce SeekableIOBuffer and clean up HttpStreamParser.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 9293029:
  net: Introduce SeekableIOBuffer and clean up HttpStreamParser.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| Index: net/base/io_buffer.h | 
| diff --git a/net/base/io_buffer.h b/net/base/io_buffer.h | 
| index 3a04b6a969cfe3f5ee6da2aad1f15fff0514c4e1..0a9b4108ba270df0e4269acb19bbf99bb21f0467 100644 | 
| --- a/net/base/io_buffer.h | 
| +++ b/net/base/io_buffer.h | 
| @@ -154,6 +154,78 @@ class NET_EXPORT DrainableIOBuffer : public IOBuffer { | 
| int used_; | 
| }; | 
| +// Similar to DrainableIOBuffer(), but this version comes with its own | 
| +// storage, so you don't need to create a separate IOBuffer. DidAppend(), | 
| +// capacity(), and Clear() are also provided, so that the SeekableIOBuffer is | 
| +// reusable. Example: | 
| +// | 
| +// SeekableIOBuffer buf(1024); | 
| +// // capacity() == 1024. size() == BytesRemaining == BytesConsumed() == 0. | 
| +// // data() points to the beginning of the buffer. | 
| +// | 
| +// int bytes_read = some_reader->Read(buf, buf.capacity()); | 
| 
wtc
2012/02/02 20:22:57
The first argument should be buf.data().
In the g
 
satorux1
2012/02/02 22:02:27
Done.
 
wtc
2012/02/02 22:32:30
Please ignore this entire comment.  I forgot that
 
satorux1
2012/02/02 22:51:09
No worry, some Read() function takes const char* (
 | 
| +// buf.DidAppend(bytes_read); | 
| +// // size() == BytesRemaining() == bytes_read. data() is unaffected. | 
| +// | 
| +// while (buf.BytesRemaining() > 0) { | 
| +// int bytes_consumed = Read(buf, buf->BytesRemaining()); | 
| 
wtc
2012/02/02 20:22:57
We should call Write() instead of Read() here.
Th
 
satorux1
2012/02/02 22:02:27
Good catch. Fixed.
 
wtc
2012/02/02 22:32:30
I was wrong again.  Sorry!
 | 
| +// buf.DidConsume(bytes_consumed); | 
| +// } | 
| +// // BytesRemaining() == 0. BytesConsumed() == size(). | 
| +// // data() points to the end of the comsumed bytes (exclusive). | 
| +// | 
| +// // If you want to reuse the buffer, be sure to clear the buffer. | 
| +// buf.Clear(); | 
| +// // size() == BytesRemaining() == BytesConsumed() == 0. | 
| +// // data() points to the beginning of the buffer. | 
| +// | 
| +class NET_EXPORT SeekableIOBuffer : public IOBuffer { | 
| + public: | 
| + explicit SeekableIOBuffer(int size); | 
| 
wtc
2012/02/02 20:22:57
This parameter should be named 'capacity'.
 
satorux1
2012/02/02 22:02:27
Done.
 | 
| + | 
| + // DidConsume() changes the |data_| pointer so that |data_| always points | 
| + // to the first unconsumed byte. | 
| + void DidConsume(int bytes); | 
| + | 
| + // Returns the number of unconsumed bytes. | 
| + // GUARANTEES: 0 <= BytesRemaining() <= size(). | 
| + int BytesRemaining() const; | 
| + | 
| + // Returns the number of consumed bytes. | 
| + // GUARANTEES: BytesRemaining() + BytesConsumed == size(). | 
| + int BytesConsumed() const; | 
| 
wtc
2012/02/02 20:22:57
Do we need the BytesConsumed() method?
 
satorux1
2012/02/02 22:02:27
Removed. Right now, we don't need it. Removed it i
 | 
| + | 
| + // Seeks to an arbitrary point in the buffer. The notion of bytes consumed | 
| + // and remaining are updated appropriately. | 
| + // REQUIRES: 0 <= bytes <= size(). | 
| + void SetOffset(int bytes); | 
| + | 
| + // Marks that |bytes| have been appended. |bytes| is added to |size_|, but | 
| + // data() is unaffected. | 
| + // REQUIRES: 0 <= |bytes| + size() <= capacity(). | 
| + void DidAppend(int bytes); | 
| + | 
| + // Changes the logical size to 0, and the offset to 0. | 
| + void Clear(); | 
| + | 
| + // Returns the logical size of the buffer (i.e the number of bytes of data | 
| + // in the buffer). | 
| + // GUARANTEES: 0 <= size() <= capacity(). | 
| + int size() const { return size_; } | 
| + | 
| + // Returns the capacity of the buffer. The capacity is the size used when | 
| + // the object is created. | 
| + int capacity() const { return capacity_; }; | 
| + | 
| + private: | 
| + virtual ~SeekableIOBuffer(); | 
| + | 
| + char* begin_; | 
| 
wtc
2012/02/02 20:22:57
I suggest you name this member 'real_data_'.
 
satorux1
2012/02/02 22:02:27
Done.
 | 
| + int capacity_; | 
| + int size_; | 
| + int used_; | 
| +}; | 
| + | 
| // This version provides a resizable buffer and a changeable offset. | 
| class NET_EXPORT GrowableIOBuffer : public IOBuffer { | 
| 
wtc
2012/02/02 20:22:57
IMPORTANT: DrainableIOBuffer, SeekableIOBuffer, an
 
satorux1
2012/02/02 22:02:27
Agreed. Added some comments for DrainableIOBuffer
 | 
| public: |