blob: 2f7a4b3479caaab920b3567ff090e132a7eebbdf [file] [log] [blame]
Adam Lesinski00451162017-10-03 07:44:08 -07001/*
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#include "io/FileStream.h"
18
19#include <errno.h> // for errno
20#include <fcntl.h> // for O_RDONLY
21#include <unistd.h> // for read
22
23#include "android-base/errors.h"
24#include "android-base/file.h" // for O_BINARY
25#include "android-base/macros.h"
26#include "android-base/utf8.h"
27
28using ::android::base::SystemErrorCodeToString;
29
30namespace aapt {
31namespace io {
32
33FileInputStream::FileInputStream(const std::string& path, size_t buffer_capacity)
34 : FileInputStream(::android::base::utf8::open(path.c_str(), O_RDONLY | O_BINARY),
35 buffer_capacity) {
36}
37
38FileInputStream::FileInputStream(int fd, size_t buffer_capacity)
39 : fd_(fd),
40 buffer_capacity_(buffer_capacity),
41 buffer_offset_(0u),
42 buffer_size_(0u),
43 total_byte_count_(0u) {
44 if (fd_ == -1) {
45 error_ = SystemErrorCodeToString(errno);
46 } else {
47 buffer_.reset(new uint8_t[buffer_capacity_]);
48 }
49}
50
51bool FileInputStream::Next(const void** data, size_t* size) {
52 if (HadError()) {
53 return false;
54 }
55
56 // Deal with any remaining bytes after BackUp was called.
57 if (buffer_offset_ != buffer_size_) {
58 *data = buffer_.get() + buffer_offset_;
59 *size = buffer_size_ - buffer_offset_;
60 total_byte_count_ += buffer_size_ - buffer_offset_;
61 buffer_offset_ = buffer_size_;
62 return true;
63 }
64
65 ssize_t n = TEMP_FAILURE_RETRY(read(fd_, buffer_.get(), buffer_capacity_));
66 if (n < 0) {
67 error_ = SystemErrorCodeToString(errno);
68 fd_.reset();
69 buffer_.reset();
70 return false;
71 }
72
73 buffer_size_ = static_cast<size_t>(n);
74 buffer_offset_ = buffer_size_;
75 total_byte_count_ += buffer_size_;
76
77 *data = buffer_.get();
78 *size = buffer_size_;
79 return buffer_size_ != 0u;
80}
81
82void FileInputStream::BackUp(size_t count) {
83 if (count > buffer_offset_) {
84 count = buffer_offset_;
85 }
86 buffer_offset_ -= count;
87 total_byte_count_ -= count;
88}
89
90size_t FileInputStream::ByteCount() const {
91 return total_byte_count_;
92}
93
94bool FileInputStream::HadError() const {
95 return fd_ == -1;
96}
97
98std::string FileInputStream::GetError() const {
99 return error_;
100}
101
102FileOutputStream::FileOutputStream(const std::string& path, int mode, size_t buffer_capacity)
103 : FileOutputStream(::android::base::utf8::open(path.c_str(), mode), buffer_capacity) {
104}
105
106FileOutputStream::FileOutputStream(int fd, size_t buffer_capacity)
107 : fd_(fd), buffer_capacity_(buffer_capacity), buffer_offset_(0u), total_byte_count_(0u) {
108 if (fd_ == -1) {
109 error_ = SystemErrorCodeToString(errno);
110 } else {
111 buffer_.reset(new uint8_t[buffer_capacity_]);
112 }
113}
114
115FileOutputStream::~FileOutputStream() {
116 // Flush the buffer.
117 Flush();
118}
119
120bool FileOutputStream::Next(void** data, size_t* size) {
121 if (fd_ == -1 || HadError()) {
122 return false;
123 }
124
125 if (buffer_offset_ == buffer_capacity_) {
126 if (!FlushImpl()) {
127 return false;
128 }
129 }
130
131 const size_t buffer_size = buffer_capacity_ - buffer_offset_;
132 *data = buffer_.get() + buffer_offset_;
133 *size = buffer_size;
134 total_byte_count_ += buffer_size;
135 buffer_offset_ = buffer_capacity_;
136 return true;
137}
138
139void FileOutputStream::BackUp(size_t count) {
140 if (count > buffer_offset_) {
141 count = buffer_offset_;
142 }
143 buffer_offset_ -= count;
144 total_byte_count_ -= count;
145}
146
147size_t FileOutputStream::ByteCount() const {
148 return total_byte_count_;
149}
150
151bool FileOutputStream::Flush() {
152 if (!HadError()) {
153 return FlushImpl();
154 }
155 return false;
156}
157
158bool FileOutputStream::FlushImpl() {
159 ssize_t n = TEMP_FAILURE_RETRY(write(fd_, buffer_.get(), buffer_offset_));
160 if (n < 0) {
161 error_ = SystemErrorCodeToString(errno);
162 fd_.reset();
163 buffer_.reset();
164 return false;
165 }
166
167 buffer_offset_ = 0u;
168 return true;
169}
170
171bool FileOutputStream::HadError() const {
172 return fd_ == -1;
173}
174
175std::string FileOutputStream::GetError() const {
176 return error_;
177}
178
179} // namespace io
180} // namespace aapt