summaryrefslogtreecommitdiffstats
path: root/content/browser/download/download_buffer.cc
blob: c8d5c8019a08aab1bbffa13dc93a0653735ce355 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/download/download_buffer.h"

#include "net/base/io_buffer.h"

namespace content {

net::IOBuffer* AssembleData(const ContentVector& contents, size_t* num_bytes) {
  if (num_bytes)
    *num_bytes = 0;

  size_t data_len;
  // Determine how large a buffer we need.
  size_t assembly_buffer_length = 0;
  for (size_t i = 0; i < contents.size(); ++i) {
    data_len = contents[i].second;
    assembly_buffer_length += data_len;
  }

  // 0-length IOBuffers are not allowed.
  if (assembly_buffer_length == 0)
    return NULL;

  net::IOBuffer* assembly_buffer = new net::IOBuffer(assembly_buffer_length);

  // Copy the data into |assembly_buffer|.
  size_t bytes_copied = 0;
  for (size_t i = 0; i < contents.size(); ++i) {
    net::IOBuffer* data = contents[i].first;
    data_len = contents[i].second;
    DCHECK(data != NULL);
    DCHECK_LE(bytes_copied + data_len, assembly_buffer_length);
    memcpy(assembly_buffer->data() + bytes_copied, data->data(), data_len);
    bytes_copied += data_len;
  }
  DCHECK_EQ(assembly_buffer_length, bytes_copied);

  if (num_bytes)
    *num_bytes = assembly_buffer_length;

  return assembly_buffer;
}

DownloadBuffer::DownloadBuffer() {
}

DownloadBuffer::~DownloadBuffer() {
}

size_t DownloadBuffer::AddData(net::IOBuffer* io_buffer, size_t byte_count) {
  base::AutoLock auto_lock(lock_);
  contents_.push_back(std::make_pair(io_buffer, byte_count));
  return contents_.size();
}

ContentVector* DownloadBuffer::ReleaseContents() {
  base::AutoLock auto_lock(lock_);
  ContentVector* other_contents = new ContentVector;
  other_contents->swap(contents_);
  return other_contents;
}

size_t DownloadBuffer::size() const {
  base::AutoLock auto_lock(lock_);
  return contents_.size();
}

}  // namespace content