[Cronet] Allow multiple src-dir arguments for jar_src.py.
[chromium-blink-merge.git] / net / base / upload_bytes_element_reader.cc
blobfd74dd80e55a3fb0f6dff4ba6ac7c1dba90d0e37
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/base/upload_bytes_element_reader.h"
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9 #include "net/base/io_buffer.h"
10 #include "net/base/net_errors.h"
12 namespace net {
14 UploadBytesElementReader::UploadBytesElementReader(const char* bytes,
15 uint64_t length)
16 : bytes_(bytes), length_(length), offset_(0) {
19 UploadBytesElementReader::~UploadBytesElementReader() {
22 const UploadBytesElementReader*
23 UploadBytesElementReader::AsBytesReader() const {
24 return this;
27 int UploadBytesElementReader::Init(const CompletionCallback& callback) {
28 offset_ = 0;
29 return OK;
32 uint64_t UploadBytesElementReader::GetContentLength() const {
33 return length_;
36 uint64_t UploadBytesElementReader::BytesRemaining() const {
37 return length_ - offset_;
40 bool UploadBytesElementReader::IsInMemory() const {
41 return true;
44 int UploadBytesElementReader::Read(IOBuffer* buf,
45 int buf_length,
46 const CompletionCallback& callback) {
47 DCHECK_LT(0, buf_length);
49 const int num_bytes_to_read = static_cast<int>(
50 std::min(BytesRemaining(), static_cast<uint64_t>(buf_length)));
52 // Check if we have anything to copy first, because we are getting
53 // the address of an element in |bytes_| and that will throw an
54 // exception if |bytes_| is an empty vector.
55 if (num_bytes_to_read > 0)
56 memcpy(buf->data(), bytes_ + offset_, num_bytes_to_read);
58 offset_ += num_bytes_to_read;
59 return num_bytes_to_read;
63 UploadOwnedBytesElementReader::UploadOwnedBytesElementReader(
64 std::vector<char>* data)
65 : UploadBytesElementReader(vector_as_array(data), data->size()) {
66 data_.swap(*data);
69 UploadOwnedBytesElementReader::~UploadOwnedBytesElementReader() {}
71 UploadOwnedBytesElementReader*
72 UploadOwnedBytesElementReader::CreateWithString(const std::string& string) {
73 std::vector<char> data(string.begin(), string.end());
74 return new UploadOwnedBytesElementReader(&data);
77 } // namespace net