Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_BASE_IO_BUFFER_H_ | 5 #ifndef NET_BASE_IO_BUFFER_H_ |
| 6 #define NET_BASE_IO_BUFFER_H_ | 6 #define NET_BASE_IO_BUFFER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 int size() const { return string_data_.size(); } | 119 int size() const { return string_data_.size(); } |
| 120 | 120 |
| 121 private: | 121 private: |
| 122 virtual ~StringIOBuffer(); | 122 virtual ~StringIOBuffer(); |
| 123 | 123 |
| 124 std::string string_data_; | 124 std::string string_data_; |
| 125 }; | 125 }; |
| 126 | 126 |
| 127 // This version wraps an existing IOBuffer and provides convenient functions | 127 // This version wraps an existing IOBuffer and provides convenient functions |
| 128 // to progressively read all the data. | 128 // to progressively read all the data. |
| 129 // | |
| 130 // DrainableIOBuffer is useful when you have an IOBuffer that contains data | |
| 131 // to be written progressively, and Write() function takes an IOBuffer rather | |
| 132 // than const char*. DrainableIOBuffer can be used as follows: | |
| 133 // | |
| 134 // // payload is the IOBuffer containing the data to be written. | |
| 135 // buf = new DrainableIOBuffer(payload, payload_size); | |
| 136 // | |
| 137 // while (buf->BytesRemaining() > 0) { | |
| 138 // // Write() takes an IOBuffer. If it takes const char*, we could | |
| 139 // // simply use the regular IOBuffer like payload->data() + offset. | |
| 140 // int bytes_written = Write(buf, buf->BytesRemaining()); | |
| 141 // buf->DidConsume(bytes_written); | |
| 142 // } | |
| 143 // | |
| 129 class NET_EXPORT DrainableIOBuffer : public IOBuffer { | 144 class NET_EXPORT DrainableIOBuffer : public IOBuffer { |
| 130 public: | 145 public: |
| 131 DrainableIOBuffer(IOBuffer* base, int size); | 146 DrainableIOBuffer(IOBuffer* base, int size); |
| 132 | 147 |
| 133 // DidConsume() changes the |data_| pointer so that |data_| always points | 148 // DidConsume() changes the |data_| pointer so that |data_| always points |
| 134 // to the first unconsumed byte. | 149 // to the first unconsumed byte. |
| 135 void DidConsume(int bytes); | 150 void DidConsume(int bytes); |
| 136 | 151 |
| 137 // Returns the number of unconsumed bytes. | 152 // Returns the number of unconsumed bytes. |
| 138 int BytesRemaining() const; | 153 int BytesRemaining() const; |
| 139 | 154 |
| 140 // Returns the number of consumed bytes. | 155 // Returns the number of consumed bytes. |
| 141 int BytesConsumed() const; | 156 int BytesConsumed() const; |
| 142 | 157 |
| 143 // Seeks to an arbitrary point in the buffer. The notion of bytes consumed | 158 // Seeks to an arbitrary point in the buffer. The notion of bytes consumed |
| 144 // and remaining are updated appropriately. | 159 // and remaining are updated appropriately. |
| 145 void SetOffset(int bytes); | 160 void SetOffset(int bytes); |
| 146 | 161 |
| 147 int size() const { return size_; } | 162 int size() const { return size_; } |
| 148 | 163 |
| 149 private: | 164 private: |
| 150 virtual ~DrainableIOBuffer(); | 165 virtual ~DrainableIOBuffer(); |
| 151 | 166 |
| 152 scoped_refptr<IOBuffer> base_; | 167 scoped_refptr<IOBuffer> base_; |
| 153 int size_; | 168 int size_; |
| 154 int used_; | 169 int used_; |
| 155 }; | 170 }; |
| 156 | 171 |
| 172 // Similar to DrainableIOBuffer(), but this version comes with its own | |
| 173 // storage, so you don't need to create a separate IOBuffer. DidAppend(), | |
|
rvargas (doing something else)
2012/02/03 23:13:34
I would prefer not having references to bad patter
satorux1
2012/02/03 23:46:11
Good point. Reworked and simplified the comment pe
| |
| 174 // capacity(), and Clear() are also provided, so that the SeekableIOBuffer is | |
| 175 // reusable. | |
| 176 // | |
| 177 // SeekableIOBuffer is useful when you want to avoid repeated allocations of | |
| 178 // DrainableIOBuffer. Consider this case: | |
| 179 // | |
| 180 // scoped_refptr<IOBuffer> buf = new IOBufferWithSize(1024); | |
| 181 // while (!some_reader->IsEOF()) { | |
| 182 // int bytes_read = some_reader->Read(buf, buf->size()); | |
| 183 // scoped_refptr<DrainableIOBuffer> drainable(buf, num_bytes); // HERE | |
| 184 // while (!drainable->BytesRemaining()) { | |
| 185 // int bytes_written = Write(drainable, drainable->BytesRemaining()); | |
| 186 // drainable->DidConsume(bytes_written); | |
| 187 // } | |
| 188 // } | |
| 189 // | |
| 190 // As shown, DrainableIOBuffer is repeatedly allocated. With | |
| 191 // SeekableIOBuffer, the extra allocations can be eliminated: | |
| 192 // | |
| 193 // scoped_refptr<SeekableIOBuffer> seekable = new SeekableIOBuffer(1024); | |
| 194 // while (!some_reader->IsEOF()) { | |
| 195 // seekable->Clear(); // Clear before reuse. | |
| 196 // int bytes_read = some_reader->Read(seekable, seekable->capacity()); | |
| 197 // seekable->DidAppend(bytes_read); | |
| 198 // while (!seekable->BytesRemaining()) { | |
| 199 // int bytes_written = Write(seekable, seekable->BytesRemaining()); | |
| 200 // seekable->DidConsume(bytes_written); | |
| 201 // } | |
| 202 // } | |
| 203 // | |
| 204 // General example: | |
| 205 // | |
| 206 // scoped_refptr<SeekableIOBuffer> buf = new SeekableIOBuffer(1024); | |
| 207 // // capacity() == 1024. size() == BytesRemaining == BytesConsumed() == 0. | |
| 208 // // data() points to the beginning of the buffer. | |
| 209 // | |
| 210 // // Read() takes an IOBuffer. | |
| 211 // int bytes_read = some_reader->Read(buf, buf->capacity()); | |
| 212 // buf->DidAppend(bytes_read); | |
| 213 // // size() == BytesRemaining() == bytes_read. data() is unaffected. | |
| 214 // | |
| 215 // while (buf->BytesRemaining() > 0) { | |
| 216 // // Write() takes an IOBuffer. If it takes const char*, we could | |
| 217 /// // simply use the regular IOBuffer like buf->data() + offset. | |
| 218 // int bytes_written = Write(buf, buf->BytesRemaining()); | |
| 219 // buf->DidConsume(bytes_written); | |
| 220 // } | |
| 221 // // BytesRemaining() == 0. BytesConsumed() == size(). | |
| 222 // // data() points to the end of the comsumed bytes (exclusive). | |
| 223 // | |
| 224 // // If you want to reuse the buffer, be sure to clear the buffer. | |
| 225 // buf->Clear(); | |
| 226 // // size() == BytesRemaining() == BytesConsumed() == 0. | |
| 227 // // data() points to the beginning of the buffer. | |
| 228 // | |
| 229 class NET_EXPORT SeekableIOBuffer : public IOBuffer { | |
| 230 public: | |
| 231 explicit SeekableIOBuffer(int capacity); | |
| 232 | |
| 233 // DidConsume() changes the |data_| pointer so that |data_| always points | |
| 234 // to the first unconsumed byte. | |
| 235 void DidConsume(int bytes); | |
| 236 | |
| 237 // Returns the number of unconsumed bytes. | |
| 238 // GUARANTEES: 0 <= BytesRemaining() <= size(). | |
|
rvargas (doing something else)
2012/02/03 23:13:34
nit: we have always used comments that are in regu
satorux1
2012/02/03 23:46:11
Removed.
| |
| 239 int BytesRemaining() const; | |
| 240 | |
| 241 // Seeks to an arbitrary point in the buffer. The notion of bytes consumed | |
| 242 // and remaining are updated appropriately. | |
| 243 // REQUIRES: 0 <= bytes <= size(). | |
| 244 void SetOffset(int bytes); | |
| 245 | |
| 246 // Marks that |bytes| have been appended. |bytes| is added to |size_|, but | |
|
rvargas (doing something else)
2012/02/03 23:13:34
nit: "marks" is not that clear. Go either with "ca
satorux1
2012/02/03 23:46:11
Done.
| |
| 247 // data() is unaffected. | |
| 248 // REQUIRES: 0 <= |bytes| + size() <= capacity(). | |
| 249 void DidAppend(int bytes); | |
| 250 | |
| 251 // Changes the logical size to 0, and the offset to 0. | |
| 252 void Clear(); | |
| 253 | |
| 254 // Returns the logical size of the buffer (i.e the number of bytes of data | |
| 255 // in the buffer). | |
| 256 // GUARANTEES: 0 <= size() <= capacity(). | |
| 257 int size() const { return size_; } | |
| 258 | |
| 259 // Returns the capacity of the buffer. The capacity is the size used when | |
| 260 // the object is created. | |
| 261 int capacity() const { return capacity_; }; | |
| 262 | |
| 263 private: | |
| 264 virtual ~SeekableIOBuffer(); | |
| 265 | |
| 266 char* real_data_; | |
|
rvargas (doing something else)
2012/02/03 23:13:34
buffer_start_ ? data_start_ ?
satorux1
2012/02/03 23:46:11
it used to be begin_, but changed to real_data_ pe
| |
| 267 int capacity_; | |
| 268 int size_; | |
| 269 int used_; | |
| 270 }; | |
| 271 | |
| 157 // This version provides a resizable buffer and a changeable offset. | 272 // This version provides a resizable buffer and a changeable offset. |
| 273 // | |
| 274 // GrowableIOBuffer is useful when you read data progressively without | |
| 275 // knowing the total size in advance. GrowableIOBuffer can be used as | |
| 276 // follows: | |
| 277 // | |
| 278 // buf = new GrowableIOBuffer; | |
| 279 // buf->SetCapacity(1024); // Initial capacity. | |
| 280 // | |
| 281 // while (!some_stream->IsEOF()) { | |
| 282 // // Double the capacity if the remaining capacity is empty. | |
| 283 // if (buf->RemainingCapacity() == 0) | |
| 284 // buf->SetCapacity(buf->capacity() * 2); | |
| 285 // int bytes_read = some_stream->Read(buf, buf->RemainingCapacity()); | |
| 286 // some_stream->set_offset(buf->offset() + bytes_read); | |
|
rvargas (doing something else)
2012/02/03 23:13:34
This should be buf->set_offset(
satorux1
2012/02/03 23:46:11
Good catch. Done.
| |
| 287 // } | |
| 288 // | |
| 158 class NET_EXPORT GrowableIOBuffer : public IOBuffer { | 289 class NET_EXPORT GrowableIOBuffer : public IOBuffer { |
| 159 public: | 290 public: |
| 160 GrowableIOBuffer(); | 291 GrowableIOBuffer(); |
| 161 | 292 |
| 162 // realloc memory to the specified capacity. | 293 // realloc memory to the specified capacity. |
| 163 void SetCapacity(int capacity); | 294 void SetCapacity(int capacity); |
| 164 int capacity() { return capacity_; } | 295 int capacity() { return capacity_; } |
| 165 | 296 |
| 166 // |offset| moves the |data_| pointer, allowing "seeking" in the data. | 297 // |offset| moves the |data_| pointer, allowing "seeking" in the data. |
| 167 void set_offset(int offset); | 298 void set_offset(int offset); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 205 public: | 336 public: |
| 206 explicit WrappedIOBuffer(const char* data); | 337 explicit WrappedIOBuffer(const char* data); |
| 207 | 338 |
| 208 protected: | 339 protected: |
| 209 virtual ~WrappedIOBuffer(); | 340 virtual ~WrappedIOBuffer(); |
| 210 }; | 341 }; |
| 211 | 342 |
| 212 } // namespace net | 343 } // namespace net |
| 213 | 344 |
| 214 #endif // NET_BASE_IO_BUFFER_H_ | 345 #endif // NET_BASE_IO_BUFFER_H_ |
| OLD | NEW |