1 //
2 // Copyright (C) 2014 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef SHILL_PROTOBUF_LITE_STREAMS_H_
18 #define SHILL_PROTOBUF_LITE_STREAMS_H_
19 
20 #include <string>
21 
22 #include <base/files/scoped_file.h>
23 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
24 
25 // Some basic input/output streams are not implemented for protobuf-lite.
26 
27 namespace shill {
28 
29 // Attempts to create a |google::protobuf::io::CopyingInputStreamAdaptor| using
30 // a |shill::ProtobufLiteCopyingFileInputStream|. Returns a new instance on
31 // success. The caller owns the new instance, and must free it when done.
32 // Returns nullptr on failure.
33 google::protobuf::io::CopyingInputStreamAdaptor *
34 protobuf_lite_file_input_stream(const std::string& file_path);
35 
36 
37 class ProtobufLiteCopyingFileInputStream :
38     public google::protobuf::io::CopyingInputStream {
39  public:
40   // Takes ownership of |fd| and closes it when the object is deleted.
41   explicit ProtobufLiteCopyingFileInputStream(int fd);
42   ~ProtobufLiteCopyingFileInputStream() override;
43   int Read(void* buffer, int size) override;
44   int Skip(int count) override;
45  private:
46   int fd_;
47   base::ScopedFD scoped_fd_closer_;
48   bool previous_seek_failed_;
49 
50   DISALLOW_COPY_AND_ASSIGN(ProtobufLiteCopyingFileInputStream);
51 };
52 
53 }  // namespace shill
54 
55 #endif  // SHILL_PROTOBUF_LITE_STREAMS_H_
56