OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/download/download_buffer.h" | |
6 | |
7 #include "net/base/io_buffer.h" | |
8 | |
9 namespace content { | |
10 | |
11 net::IOBuffer* AssembleData(const ContentVector& contents, size_t* num_bytes) { | |
12 if (num_bytes) | |
13 *num_bytes = 0; | |
14 | |
15 size_t data_len; | |
16 // Determine how large a buffer we need. | |
17 size_t assembly_buffer_length = 0; | |
18 for (size_t i = 0; i < contents.size(); ++i) { | |
19 data_len = contents[i].second; | |
20 assembly_buffer_length += data_len; | |
21 } | |
22 | |
23 // 0-length IOBuffers are not allowed. | |
24 if (assembly_buffer_length == 0) | |
25 return NULL; | |
26 | |
27 net::IOBuffer* assembly_buffer = new net::IOBuffer(assembly_buffer_length); | |
28 | |
29 // Copy the data into |assembly_buffer|. | |
30 size_t bytes_copied = 0; | |
31 for (size_t i = 0; i < contents.size(); ++i) { | |
32 net::IOBuffer* data = contents[i].first; | |
33 data_len = contents[i].second; | |
34 DCHECK(data != NULL); | |
35 DCHECK_LE(bytes_copied + data_len, assembly_buffer_length); | |
36 memcpy(assembly_buffer->data() + bytes_copied, data->data(), data_len); | |
37 bytes_copied += data_len; | |
38 } | |
39 DCHECK_EQ(assembly_buffer_length, bytes_copied); | |
40 | |
41 if (num_bytes) | |
42 *num_bytes = assembly_buffer_length; | |
43 | |
44 return assembly_buffer; | |
45 } | |
46 | |
47 DownloadBuffer::DownloadBuffer() { | |
48 } | |
49 | |
50 DownloadBuffer::~DownloadBuffer() { | |
51 } | |
52 | |
53 size_t DownloadBuffer::AddData(net::IOBuffer* io_buffer, size_t byte_count) { | |
54 base::AutoLock auto_lock(lock_); | |
55 contents_.push_back(std::make_pair(io_buffer, byte_count)); | |
56 return contents_.size(); | |
57 } | |
58 | |
59 ContentVector* DownloadBuffer::ReleaseContents() { | |
60 base::AutoLock auto_lock(lock_); | |
61 ContentVector* other_contents = new ContentVector; | |
62 other_contents->swap(contents_); | |
63 return other_contents; | |
64 } | |
65 | |
66 size_t DownloadBuffer::size() const { | |
67 base::AutoLock auto_lock(lock_); | |
68 return contents_.size(); | |
69 } | |
70 | |
71 } // namespace content | |
OLD | NEW |