OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "net/base/upload_data_stream.h" | 5 #include "net/base/upload_data_stream.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "net/base/file_stream.h" | 9 #include "net/base/file_stream.h" |
10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 | 12 |
13 namespace net { | 13 namespace net { |
14 | 14 |
| 15 UploadDataStream::~UploadDataStream() { |
| 16 } |
| 17 |
15 UploadDataStream* UploadDataStream::Create(UploadData* data, int* error_code) { | 18 UploadDataStream* UploadDataStream::Create(UploadData* data, int* error_code) { |
16 scoped_ptr<UploadDataStream> stream(new UploadDataStream(data)); | 19 scoped_ptr<UploadDataStream> stream(new UploadDataStream(data)); |
17 int rv = stream->FillBuf(); | 20 int rv = stream->FillBuf(); |
18 if (error_code) | 21 if (error_code) |
19 *error_code = rv; | 22 *error_code = rv; |
20 if (rv != OK) | 23 if (rv != OK) |
21 return NULL; | 24 return NULL; |
22 | 25 |
23 return stream.release(); | 26 return stream.release(); |
24 } | 27 } |
25 | 28 |
26 UploadDataStream::UploadDataStream(UploadData* data) | |
27 : data_(data), | |
28 buf_(new IOBuffer(kBufSize)), | |
29 buf_len_(0), | |
30 next_element_(data->elements()->begin()), | |
31 next_element_offset_(0), | |
32 next_element_remaining_(0), | |
33 total_size_(data->GetContentLength()), | |
34 current_position_(0), | |
35 eof_(false) { | |
36 } | |
37 | |
38 UploadDataStream::~UploadDataStream() { | |
39 } | |
40 | |
41 void UploadDataStream::DidConsume(size_t num_bytes) { | 29 void UploadDataStream::DidConsume(size_t num_bytes) { |
42 DCHECK_LE(num_bytes, buf_len_); | 30 DCHECK_LE(num_bytes, buf_len_); |
43 DCHECK(!eof_); | 31 DCHECK(!eof_); |
44 | 32 |
45 buf_len_ -= num_bytes; | 33 buf_len_ -= num_bytes; |
46 if (buf_len_) | 34 if (buf_len_) |
47 memmove(buf_->data(), buf_->data() + num_bytes, buf_len_); | 35 memmove(buf_->data(), buf_->data() + num_bytes, buf_len_); |
48 | 36 |
49 FillBuf(); | 37 FillBuf(); |
50 | 38 |
51 current_position_ += num_bytes; | 39 current_position_ += num_bytes; |
52 } | 40 } |
53 | 41 |
| 42 UploadDataStream::UploadDataStream(UploadData* data) |
| 43 : data_(data), |
| 44 buf_(new IOBuffer(kBufSize)), |
| 45 buf_len_(0), |
| 46 next_element_(data->elements()->begin()), |
| 47 next_element_offset_(0), |
| 48 next_element_remaining_(0), |
| 49 total_size_(data->GetContentLength()), |
| 50 current_position_(0), |
| 51 eof_(false) { |
| 52 } |
| 53 |
54 int UploadDataStream::FillBuf() { | 54 int UploadDataStream::FillBuf() { |
55 std::vector<UploadData::Element>::iterator end = | 55 std::vector<UploadData::Element>::iterator end = |
56 data_->elements()->end(); | 56 data_->elements()->end(); |
57 | 57 |
58 while (buf_len_ < kBufSize && next_element_ != end) { | 58 while (buf_len_ < kBufSize && next_element_ != end) { |
59 bool advance_to_next_element = false; | 59 bool advance_to_next_element = false; |
60 | 60 |
61 UploadData::Element& element = *next_element_; | 61 UploadData::Element& element = *next_element_; |
62 | 62 |
63 size_t size_remaining = kBufSize - buf_len_; | 63 size_t size_remaining = kBufSize - buf_len_; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 } | 126 } |
127 } | 127 } |
128 | 128 |
129 if (next_element_ == end && !buf_len_) | 129 if (next_element_ == end && !buf_len_) |
130 eof_ = true; | 130 eof_ = true; |
131 | 131 |
132 return OK; | 132 return OK; |
133 } | 133 } |
134 | 134 |
135 } // namespace net | 135 } // namespace net |
OLD | NEW |