summaryrefslogtreecommitdiffstats
path: root/remoting/base/multiple_array_input_stream.h
blob: a8482481aca67246f011a9ddf98f8f484e5efb59 (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
// Copyright (c) 2010 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.

// MultipleArrayInputStream implements ZeroCopyInputStream to be used by
// protobuf to decode bytes into a protocol buffer message.
//
// This input stream is made of multiple IOBuffers received from the network.
// This object retains the IOBuffers added to it.
//
// Internally, we wrap each added IOBuffer in a DrainableIOBuffer. This allows
// us to track how much data has been consumed from each IOBuffer.

#ifndef REMOTING_BASE_MULTIPLE_ARRAY_INPUT_STREAM_H_
#define REMOTING_BASE_MULTIPLE_ARRAY_INPUT_STREAM_H_

#include <vector>

#include "base/basictypes.h"
#include "base/ref_counted.h"
#include "google/protobuf/io/zero_copy_stream.h"

namespace net {
class DrainableIOBuffer;
class IOBuffer;
}  // namespace net

namespace remoting {

class MultipleArrayInputStream :
      public google::protobuf::io::ZeroCopyInputStream {
 public:
  MultipleArrayInputStream();
  virtual ~MultipleArrayInputStream();

  // Add a buffer to the list. |buffer| is retained by this object.
  void AddBuffer(net::IOBuffer* buffer, int size);

  // google::protobuf::io::ZeroCopyInputStream interface.
  virtual bool Next(const void** data, int* size);
  virtual void BackUp(int count);
  virtual bool Skip(int count);
  virtual int64 ByteCount() const;

 private:
  std::vector<scoped_refptr<net::DrainableIOBuffer> > buffers_;

  size_t current_buffer_;
  int position_;
  int last_returned_size_;

  DISALLOW_COPY_AND_ASSIGN(MultipleArrayInputStream);
};

}  // namespace remoting

#endif  // REMOTING_BASE_MULTIPLE_ARRAY_INPUT_STREAM_H_