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 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
147 int size() const { return size_; } | 147 int size() const { return size_; } |
148 | 148 |
149 private: | 149 private: |
150 virtual ~DrainableIOBuffer(); | 150 virtual ~DrainableIOBuffer(); |
151 | 151 |
152 scoped_refptr<IOBuffer> base_; | 152 scoped_refptr<IOBuffer> base_; |
153 int size_; | 153 int size_; |
154 int used_; | 154 int used_; |
155 }; | 155 }; |
156 | 156 |
157 // Similar to DrainableIOBuffer(), but this version comes with its own | |
158 // storage, so you don't need to create a separate IOBuffer. DidAppend(), | |
159 // capacity(), and Clear() are also provided, so that the SeekableIOBuffer is | |
160 // reusable. Example: | |
161 // | |
162 // SeekableIOBuffer buf(1024); | |
163 // // capacity() == 1024. size() == BytesRemaining == BytesConsumed() == 0. | |
164 // // data() points to the beginning of the buffer. | |
165 // | |
166 // 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* (
| |
167 // buf.DidAppend(bytes_read); | |
168 // // size() == BytesRemaining() == bytes_read. data() is unaffected. | |
169 // | |
170 // while (buf.BytesRemaining() > 0) { | |
171 // 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!
| |
172 // buf.DidConsume(bytes_consumed); | |
173 // } | |
174 // // BytesRemaining() == 0. BytesConsumed() == size(). | |
175 // // data() points to the end of the comsumed bytes (exclusive). | |
176 // | |
177 // // If you want to reuse the buffer, be sure to clear the buffer. | |
178 // buf.Clear(); | |
179 // // size() == BytesRemaining() == BytesConsumed() == 0. | |
180 // // data() points to the beginning of the buffer. | |
181 // | |
182 class NET_EXPORT SeekableIOBuffer : public IOBuffer { | |
183 public: | |
184 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.
| |
185 | |
186 // DidConsume() changes the |data_| pointer so that |data_| always points | |
187 // to the first unconsumed byte. | |
188 void DidConsume(int bytes); | |
189 | |
190 // Returns the number of unconsumed bytes. | |
191 // GUARANTEES: 0 <= BytesRemaining() <= size(). | |
192 int BytesRemaining() const; | |
193 | |
194 // Returns the number of consumed bytes. | |
195 // GUARANTEES: BytesRemaining() + BytesConsumed == size(). | |
196 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
| |
197 | |
198 // Seeks to an arbitrary point in the buffer. The notion of bytes consumed | |
199 // and remaining are updated appropriately. | |
200 // REQUIRES: 0 <= bytes <= size(). | |
201 void SetOffset(int bytes); | |
202 | |
203 // Marks that |bytes| have been appended. |bytes| is added to |size_|, but | |
204 // data() is unaffected. | |
205 // REQUIRES: 0 <= |bytes| + size() <= capacity(). | |
206 void DidAppend(int bytes); | |
207 | |
208 // Changes the logical size to 0, and the offset to 0. | |
209 void Clear(); | |
210 | |
211 // Returns the logical size of the buffer (i.e the number of bytes of data | |
212 // in the buffer). | |
213 // GUARANTEES: 0 <= size() <= capacity(). | |
214 int size() const { return size_; } | |
215 | |
216 // Returns the capacity of the buffer. The capacity is the size used when | |
217 // the object is created. | |
218 int capacity() const { return capacity_; }; | |
219 | |
220 private: | |
221 virtual ~SeekableIOBuffer(); | |
222 | |
223 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.
| |
224 int capacity_; | |
225 int size_; | |
226 int used_; | |
227 }; | |
228 | |
157 // This version provides a resizable buffer and a changeable offset. | 229 // This version provides a resizable buffer and a changeable offset. |
158 class NET_EXPORT GrowableIOBuffer : public IOBuffer { | 230 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
| |
159 public: | 231 public: |
160 GrowableIOBuffer(); | 232 GrowableIOBuffer(); |
161 | 233 |
162 // realloc memory to the specified capacity. | 234 // realloc memory to the specified capacity. |
163 void SetCapacity(int capacity); | 235 void SetCapacity(int capacity); |
164 int capacity() { return capacity_; } | 236 int capacity() { return capacity_; } |
165 | 237 |
166 // |offset| moves the |data_| pointer, allowing "seeking" in the data. | 238 // |offset| moves the |data_| pointer, allowing "seeking" in the data. |
167 void set_offset(int offset); | 239 void set_offset(int offset); |
168 int offset() { return offset_; } | 240 int offset() { return offset_; } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
205 public: | 277 public: |
206 explicit WrappedIOBuffer(const char* data); | 278 explicit WrappedIOBuffer(const char* data); |
207 | 279 |
208 protected: | 280 protected: |
209 virtual ~WrappedIOBuffer(); | 281 virtual ~WrappedIOBuffer(); |
210 }; | 282 }; |
211 | 283 |
212 } // namespace net | 284 } // namespace net |
213 | 285 |
214 #endif // NET_BASE_IO_BUFFER_H_ | 286 #endif // NET_BASE_IO_BUFFER_H_ |
OLD | NEW |