| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/chunked_upload_data_stream.h" | 5 #include "net/base/chunked_upload_data_stream.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 | 11 |
| 12 namespace net { | 12 namespace net { |
| 13 | 13 |
| 14 ChunkedUploadDataStream::ChunkedUploadDataStream(int64 identifier) | 14 ChunkedUploadDataStream::ChunkedUploadDataStream(int64 identifier) |
| 15 : UploadDataStream(true, identifier), | 15 : UploadDataStream(true, identifier), |
| 16 read_index_(0), | 16 read_index_(0), |
| 17 read_offset_(0), | 17 read_offset_(0), |
| 18 all_data_appended_(false), | 18 all_data_appended_(false), |
| 19 read_buffer_len_(0) { | 19 read_buffer_len_(0) { |
| 20 } | 20 } |
| 21 | 21 |
| 22 ChunkedUploadDataStream::~ChunkedUploadDataStream() { | 22 ChunkedUploadDataStream::~ChunkedUploadDataStream() { |
| 23 } | 23 } |
| 24 | 24 |
| 25 void ChunkedUploadDataStream::AppendData( | 25 void ChunkedUploadDataStream::AppendData(const char* data, |
| 26 const char* data, int data_len, bool is_done) { | 26 int data_len, |
| 27 bool is_done) { |
| 27 DCHECK(!all_data_appended_); | 28 DCHECK(!all_data_appended_); |
| 28 DCHECK(data_len > 0 || is_done); | 29 DCHECK(data_len > 0 || is_done); |
| 29 if (data_len > 0) { | 30 if (data_len > 0) { |
| 30 DCHECK(data); | 31 DCHECK(data); |
| 31 upload_data_.push_back(new std::vector<char>(data, data + data_len)); | 32 upload_data_.push_back(new std::vector<char>(data, data + data_len)); |
| 32 } | 33 } |
| 33 all_data_appended_ = is_done; | 34 all_data_appended_ = is_done; |
| 34 | 35 |
| 35 if (!read_buffer_.get()) | 36 if (!read_buffer_.get()) |
| 36 return; | 37 return; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 read_buffer_len_ = 0; | 69 read_buffer_len_ = 0; |
| 69 read_index_ = 0; | 70 read_index_ = 0; |
| 70 read_offset_ = 0; | 71 read_offset_ = 0; |
| 71 } | 72 } |
| 72 | 73 |
| 73 int ChunkedUploadDataStream::ReadChunk(IOBuffer* buf, int buf_len) { | 74 int ChunkedUploadDataStream::ReadChunk(IOBuffer* buf, int buf_len) { |
| 74 // Copy as much data as possible from |upload_data_| to |buf|. | 75 // Copy as much data as possible from |upload_data_| to |buf|. |
| 75 int bytes_read = 0; | 76 int bytes_read = 0; |
| 76 while (read_index_ < upload_data_.size() && bytes_read < buf_len) { | 77 while (read_index_ < upload_data_.size() && bytes_read < buf_len) { |
| 77 std::vector<char>* data = upload_data_[read_index_]; | 78 std::vector<char>* data = upload_data_[read_index_]; |
| 78 size_t bytes_to_read = | 79 size_t bytes_to_read = std::min(static_cast<size_t>(buf_len - bytes_read), |
| 79 std::min(static_cast<size_t>(buf_len - bytes_read), | 80 data->size() - read_offset_); |
| 80 data->size() - read_offset_); | 81 memcpy(buf->data() + bytes_read, vector_as_array(data) + read_offset_, |
| 81 memcpy(buf->data() + bytes_read, | |
| 82 vector_as_array(data) + read_offset_, | |
| 83 bytes_to_read); | 82 bytes_to_read); |
| 84 bytes_read += bytes_to_read; | 83 bytes_read += bytes_to_read; |
| 85 read_offset_ += bytes_to_read; | 84 read_offset_ += bytes_to_read; |
| 86 if (read_offset_ == data->size()) { | 85 if (read_offset_ == data->size()) { |
| 87 read_index_++; | 86 read_index_++; |
| 88 read_offset_ = 0; | 87 read_offset_ = 0; |
| 89 } | 88 } |
| 90 } | 89 } |
| 91 DCHECK_LE(bytes_read, buf_len); | 90 DCHECK_LE(bytes_read, buf_len); |
| 92 | 91 |
| 93 // If no data was written, and not all data has been appended, return | 92 // If no data was written, and not all data has been appended, return |
| 94 // ERR_IO_PENDING. The read will be completed in the next call to AppendData. | 93 // ERR_IO_PENDING. The read will be completed in the next call to AppendData. |
| 95 if (bytes_read == 0 && !all_data_appended_) | 94 if (bytes_read == 0 && !all_data_appended_) |
| 96 return ERR_IO_PENDING; | 95 return ERR_IO_PENDING; |
| 97 | 96 |
| 98 if (read_index_ == upload_data_.size() && all_data_appended_) | 97 if (read_index_ == upload_data_.size() && all_data_appended_) |
| 99 SetIsFinalChunk(); | 98 SetIsFinalChunk(); |
| 100 return bytes_read; | 99 return bytes_read; |
| 101 } | 100 } |
| 102 | 101 |
| 103 } // namespace net | 102 } // namespace net |
| OLD | NEW |