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 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 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 |
157 // This version provides a resizable buffer and a changeable offset. | 172 // This version provides a resizable buffer and a changeable offset. |
| 173 // |
| 174 // GrowableIOBuffer is useful when you read data progressively without |
| 175 // knowing the total size in advance. GrowableIOBuffer can be used as |
| 176 // follows: |
| 177 // |
| 178 // buf = new GrowableIOBuffer; |
| 179 // buf->SetCapacity(1024); // Initial capacity. |
| 180 // |
| 181 // while (!some_stream->IsEOF()) { |
| 182 // // Double the capacity if the remaining capacity is empty. |
| 183 // if (buf->RemainingCapacity() == 0) |
| 184 // buf->SetCapacity(buf->capacity() * 2); |
| 185 // int bytes_read = some_stream->Read(buf, buf->RemainingCapacity()); |
| 186 // buf->set_offset(buf->offset() + bytes_read); |
| 187 // } |
| 188 // |
158 class NET_EXPORT GrowableIOBuffer : public IOBuffer { | 189 class NET_EXPORT GrowableIOBuffer : public IOBuffer { |
159 public: | 190 public: |
160 GrowableIOBuffer(); | 191 GrowableIOBuffer(); |
161 | 192 |
162 // realloc memory to the specified capacity. | 193 // realloc memory to the specified capacity. |
163 void SetCapacity(int capacity); | 194 void SetCapacity(int capacity); |
164 int capacity() { return capacity_; } | 195 int capacity() { return capacity_; } |
165 | 196 |
166 // |offset| moves the |data_| pointer, allowing "seeking" in the data. | 197 // |offset| moves the |data_| pointer, allowing "seeking" in the data. |
167 void set_offset(int offset); | 198 void set_offset(int offset); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 public: | 236 public: |
206 explicit WrappedIOBuffer(const char* data); | 237 explicit WrappedIOBuffer(const char* data); |
207 | 238 |
208 protected: | 239 protected: |
209 virtual ~WrappedIOBuffer(); | 240 virtual ~WrappedIOBuffer(); |
210 }; | 241 }; |
211 | 242 |
212 } // namespace net | 243 } // namespace net |
213 | 244 |
214 #endif // NET_BASE_IO_BUFFER_H_ | 245 #endif // NET_BASE_IO_BUFFER_H_ |
OLD | NEW |