1 /*
2  * Copyright (C) 2017 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 #pragma once
18 
19 #include <memory>
20 #include <string>
21 #include <unistd.h>
22 
23 #include "Streams.h"
24 #include "android-base/macros.h"
25 #include "android-base/unique_fd.h"
26 
27 namespace android {
28 
29 constexpr size_t kDefaultBufferCapacity = 4096u;
30 
31 class FileInputStream : public InputStream {
32  public:
33   explicit FileInputStream(const std::string& path,
34                            size_t buffer_capacity = kDefaultBufferCapacity);
35 
36   // Take ownership of `fd`.
37   explicit FileInputStream(int fd, size_t buffer_capacity = kDefaultBufferCapacity);
38 
39   // Take ownership of `fd`.
40   explicit FileInputStream(android::base::borrowed_fd fd,
41                            size_t buffer_capacity = kDefaultBufferCapacity);
42 
~FileInputStream()43   ~FileInputStream() {
44     if (should_close_ && (fd_ != -1)) {
45       close(fd_);
46     }
47   }
48 
49   bool Next(const void** data, size_t* size) override;
50 
51   void BackUp(size_t count) override;
52 
53   size_t ByteCount() const override;
54 
55   bool HadError() const override;
56 
57   std::string GetError() const override;
58 
59   bool ReadFullyAtOffset(void* data, size_t byte_count, off64_t offset) override;
60 
61  private:
62   DISALLOW_COPY_AND_ASSIGN(FileInputStream);
63 
64   int fd_ = -1;
65   std::string error_;
66   bool should_close_;
67   std::unique_ptr<uint8_t[]> buffer_;
68   size_t buffer_capacity_ = 0u;
69   size_t buffer_offset_ = 0u;
70   size_t buffer_size_ = 0u;
71   size_t total_byte_count_ = 0u;
72 };
73 
74 class FileOutputStream : public OutputStream {
75  public:
76   explicit FileOutputStream(const std::string& path,
77                             size_t buffer_capacity = kDefaultBufferCapacity);
78 
79   // Does not take ownership of `fd`.
80   explicit FileOutputStream(int fd, size_t buffer_capacity = kDefaultBufferCapacity);
81 
82   // Takes ownership of `fd`.
83   explicit FileOutputStream(android::base::unique_fd fd,
84                             size_t buffer_capacity = kDefaultBufferCapacity);
85 
86   ~FileOutputStream();
87 
88   bool Next(void** data, size_t* size) override;
89 
90   // Immediately flushes out the contents of the buffer to disk.
91   bool Flush();
92 
93   void BackUp(size_t count) override;
94 
95   size_t ByteCount() const override;
96 
97   bool HadError() const override;
98 
99   std::string GetError() const override;
100 
101  private:
102   DISALLOW_COPY_AND_ASSIGN(FileOutputStream);
103 
104   bool FlushImpl();
105 
106   android::base::unique_fd owned_fd_;
107   int fd_;
108   std::string error_;
109   std::unique_ptr<uint8_t[]> buffer_;
110   size_t buffer_capacity_ = 0u;
111   size_t buffer_offset_ = 0u;
112   size_t total_byte_count_ = 0u;
113 };
114 
115 }  // namespace android