blob: 41d7982d1f82d8c35c6f4b82fc9c8d7d7a744f91 [file] [log] [blame]
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -07001// Copyright (c) 2014 The Chromium OS 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#ifndef SHILL_PROTOBUF_LITE_STREAMS_H_
6#define SHILL_PROTOBUF_LITE_STREAMS_H_
7
8#include <string>
9
Ben Chan6fbf64f2014-05-21 18:07:01 -070010#include <base/files/scoped_file.h>
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -070011#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
12
13// Some basic input/output streams are not implemented for protobuf-lite.
14
15namespace shill {
16
17// Attempts to create a |google::protobuf::io::CopyingInputStreamAdaptor| using
18// a |shill::ProtobufLiteCopyingFileInputStream|. Returns a new instance on
19// success. The caller owns the new instance, and must free it when done.
Ben Chancc225ef2014-09-30 13:26:51 -070020// Returns nullptr on failure.
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -070021google::protobuf::io::CopyingInputStreamAdaptor *
22protobuf_lite_file_input_stream(const std::string &file_path);
23
24
25class ProtobufLiteCopyingFileInputStream :
26 public google::protobuf::io::CopyingInputStream {
27 public:
28 // Takes ownership of |fd| and closes it when the object is deleted.
29 explicit ProtobufLiteCopyingFileInputStream(int fd);
Ben Chan5ea763b2014-08-13 11:07:54 -070030 ~ProtobufLiteCopyingFileInputStream() override;
Yunlian Jiang6acd9662015-01-30 08:36:10 -080031 int Read(void *buffer, int size) override;
32 int Skip(int count) override;
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -070033 private:
34 int fd_;
Ben Chan6fbf64f2014-05-21 18:07:01 -070035 base::ScopedFD scoped_fd_closer_;
Prathmesh Prabhu28b4a3b2014-03-28 11:52:09 -070036 bool previous_seek_failed_;
37
38 DISALLOW_COPY_AND_ASSIGN(ProtobufLiteCopyingFileInputStream);
39};
40
41} // namespace shill
42
43#endif // SHILL_PROTOBUF_LITE_STREAMS_H_