Revert^2 "Move some image/9patch code to androidfw"
This reverts commit 917043bc2586743afda5a21386893fa8c787800b.
Reason for revert: Roll forward with fix
Test: Automatic
Bug: 296324826
Change-Id: I42a0b48c02fd497b2174c0c65f300265202f7ab1
diff --git a/tools/aapt2/io/BigBufferStream.cpp b/tools/aapt2/io/BigBufferStream.cpp
deleted file mode 100644
index 9704caa..0000000
--- a/tools/aapt2/io/BigBufferStream.cpp
+++ /dev/null
@@ -1,101 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "io/BigBufferStream.h"
-
-namespace aapt {
-namespace io {
-
-//
-// BigBufferInputStream
-//
-
-bool BigBufferInputStream::Next(const void** data, size_t* size) {
- if (iter_ == buffer_->end()) {
- return false;
- }
-
- if (offset_ == iter_->size) {
- ++iter_;
- if (iter_ == buffer_->end()) {
- return false;
- }
- offset_ = 0;
- }
-
- *data = iter_->buffer.get() + offset_;
- *size = iter_->size - offset_;
- bytes_read_ += iter_->size - offset_;
- offset_ = iter_->size;
- return true;
-}
-
-void BigBufferInputStream::BackUp(size_t count) {
- if (count > offset_) {
- bytes_read_ -= offset_;
- offset_ = 0;
- } else {
- offset_ -= count;
- bytes_read_ -= count;
- }
-}
-
-bool BigBufferInputStream::CanRewind() const {
- return true;
-}
-
-bool BigBufferInputStream::Rewind() {
- iter_ = buffer_->begin();
- offset_ = 0;
- bytes_read_ = 0;
- return true;
-}
-
-size_t BigBufferInputStream::ByteCount() const {
- return bytes_read_;
-}
-
-bool BigBufferInputStream::HadError() const {
- return false;
-}
-
-size_t BigBufferInputStream::TotalSize() const {
- return buffer_->size();
-}
-
-//
-// BigBufferOutputStream
-//
-
-bool BigBufferOutputStream::Next(void** data, size_t* size) {
- *data = buffer_->NextBlock(size);
- return true;
-}
-
-void BigBufferOutputStream::BackUp(size_t count) {
- buffer_->BackUp(count);
-}
-
-size_t BigBufferOutputStream::ByteCount() const {
- return buffer_->size();
-}
-
-bool BigBufferOutputStream::HadError() const {
- return false;
-}
-
-} // namespace io
-} // namespace aapt
diff --git a/tools/aapt2/io/BigBufferStream.h b/tools/aapt2/io/BigBufferStream.h
deleted file mode 100644
index 63a5e57..0000000
--- a/tools/aapt2/io/BigBufferStream.h
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef AAPT_IO_BIGBUFFERSTREAM_H
-#define AAPT_IO_BIGBUFFERSTREAM_H
-
-#include "androidfw/BigBuffer.h"
-#include "io/Io.h"
-
-namespace aapt {
-namespace io {
-
-class BigBufferInputStream : public KnownSizeInputStream {
- public:
- inline explicit BigBufferInputStream(const android::BigBuffer* buffer)
- : buffer_(buffer), iter_(buffer->begin()) {
- }
- virtual ~BigBufferInputStream() = default;
-
- bool Next(const void** data, size_t* size) override;
-
- void BackUp(size_t count) override;
-
- bool CanRewind() const override;
-
- bool Rewind() override;
-
- size_t ByteCount() const override;
-
- bool HadError() const override;
-
- size_t TotalSize() const override;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(BigBufferInputStream);
-
- const android::BigBuffer* buffer_;
- android::BigBuffer::const_iterator iter_;
- size_t offset_ = 0;
- size_t bytes_read_ = 0;
-};
-
-class BigBufferOutputStream : public OutputStream {
- public:
- inline explicit BigBufferOutputStream(android::BigBuffer* buffer) : buffer_(buffer) {
- }
- virtual ~BigBufferOutputStream() = default;
-
- bool Next(void** data, size_t* size) override;
-
- void BackUp(size_t count) override;
-
- size_t ByteCount() const override;
-
- bool HadError() const override;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(BigBufferOutputStream);
-
- android::BigBuffer* buffer_;
-};
-
-} // namespace io
-} // namespace aapt
-
-#endif // AAPT_IO_BIGBUFFERSTREAM_H
diff --git a/tools/aapt2/io/Data.h b/tools/aapt2/io/Data.h
index db91a77..29f523a 100644
--- a/tools/aapt2/io/Data.h
+++ b/tools/aapt2/io/Data.h
@@ -20,15 +20,14 @@
#include <memory>
#include "android-base/macros.h"
+#include "androidfw/Streams.h"
#include "utils/FileMap.h"
-#include "io/Io.h"
-
namespace aapt {
namespace io {
// Interface for a block of contiguous memory. An instance of this interface owns the data.
-class IData : public KnownSizeInputStream {
+class IData : public android::KnownSizeInputStream {
public:
virtual ~IData() = default;
diff --git a/tools/aapt2/io/File.cpp b/tools/aapt2/io/File.cpp
index b4f1ff3..4dfdb5b 100644
--- a/tools/aapt2/io/File.cpp
+++ b/tools/aapt2/io/File.cpp
@@ -39,7 +39,7 @@
return {};
}
-std::unique_ptr<io::InputStream> FileSegment::OpenInputStream() {
+std::unique_ptr<android::InputStream> FileSegment::OpenInputStream() {
return OpenAsData();
}
diff --git a/tools/aapt2/io/File.h b/tools/aapt2/io/File.h
index 673d1b7..248756b 100644
--- a/tools/aapt2/io/File.h
+++ b/tools/aapt2/io/File.h
@@ -43,7 +43,7 @@
// Returns nullptr on failure.
virtual std::unique_ptr<IData> OpenAsData() = 0;
- virtual std::unique_ptr<io::InputStream> OpenInputStream() = 0;
+ virtual std::unique_ptr<android::InputStream> OpenInputStream() = 0;
// Returns the source of this file. This is for presentation to the user and
// may not be a valid file system path (for example, it may contain a '@' sign to separate
@@ -78,7 +78,7 @@
: file_(file), offset_(offset), len_(len) {}
std::unique_ptr<IData> OpenAsData() override;
- std::unique_ptr<io::InputStream> OpenInputStream() override;
+ std::unique_ptr<android::InputStream> OpenInputStream() override;
const android::Source& GetSource() const override {
return file_->GetSource();
diff --git a/tools/aapt2/io/FileStream.cpp b/tools/aapt2/io/FileStream.cpp
deleted file mode 100644
index 27529bc..0000000
--- a/tools/aapt2/io/FileStream.cpp
+++ /dev/null
@@ -1,203 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "io/FileStream.h"
-
-#include <errno.h> // for errno
-#include <fcntl.h> // for O_RDONLY
-#include <unistd.h> // for read
-
-#include "android-base/errors.h"
-#include "android-base/file.h" // for O_BINARY
-#include "android-base/macros.h"
-#include "android-base/utf8.h"
-
-#if defined(_WIN32)
-// This is only needed for O_CLOEXEC.
-#include <windows.h>
-#define O_CLOEXEC O_NOINHERIT
-#endif
-
-using ::android::base::SystemErrorCodeToString;
-using ::android::base::unique_fd;
-
-namespace aapt {
-namespace io {
-
-FileInputStream::FileInputStream(const std::string& path, size_t buffer_capacity)
- : buffer_capacity_(buffer_capacity) {
- int mode = O_RDONLY | O_CLOEXEC | O_BINARY;
- fd_.reset(TEMP_FAILURE_RETRY(::android::base::utf8::open(path.c_str(), mode)));
- if (fd_ == -1) {
- error_ = SystemErrorCodeToString(errno);
- } else {
- buffer_.reset(new uint8_t[buffer_capacity_]);
- }
-}
-
-FileInputStream::FileInputStream(int fd, size_t buffer_capacity)
- : fd_(fd), buffer_capacity_(buffer_capacity) {
- if (fd_ < 0) {
- error_ = "Bad File Descriptor";
- } else {
- buffer_.reset(new uint8_t[buffer_capacity_]);
- }
-}
-
-bool FileInputStream::Next(const void** data, size_t* size) {
- if (HadError()) {
- return false;
- }
-
- // Deal with any remaining bytes after BackUp was called.
- if (buffer_offset_ != buffer_size_) {
- *data = buffer_.get() + buffer_offset_;
- *size = buffer_size_ - buffer_offset_;
- total_byte_count_ += buffer_size_ - buffer_offset_;
- buffer_offset_ = buffer_size_;
- return true;
- }
-
- ssize_t n = TEMP_FAILURE_RETRY(read(fd_, buffer_.get(), buffer_capacity_));
- if (n < 0) {
- error_ = SystemErrorCodeToString(errno);
- fd_.reset();
- buffer_.reset();
- return false;
- }
-
- buffer_size_ = static_cast<size_t>(n);
- buffer_offset_ = buffer_size_;
- total_byte_count_ += buffer_size_;
-
- *data = buffer_.get();
- *size = buffer_size_;
- return buffer_size_ != 0u;
-}
-
-void FileInputStream::BackUp(size_t count) {
- if (count > buffer_offset_) {
- count = buffer_offset_;
- }
- buffer_offset_ -= count;
- total_byte_count_ -= count;
-}
-
-size_t FileInputStream::ByteCount() const {
- return total_byte_count_;
-}
-
-bool FileInputStream::HadError() const {
- return fd_ == -1;
-}
-
-std::string FileInputStream::GetError() const {
- return error_;
-}
-
-FileOutputStream::FileOutputStream(const std::string& path, size_t buffer_capacity)
- : buffer_capacity_(buffer_capacity) {
- int mode = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY;
- owned_fd_.reset(TEMP_FAILURE_RETRY(::android::base::utf8::open(path.c_str(), mode, 0666)));
- fd_ = owned_fd_.get();
- if (fd_ < 0) {
- error_ = SystemErrorCodeToString(errno);
- } else {
- buffer_.reset(new uint8_t[buffer_capacity_]);
- }
-}
-
-FileOutputStream::FileOutputStream(unique_fd fd, size_t buffer_capacity)
- : FileOutputStream(fd.get(), buffer_capacity) {
- owned_fd_ = std::move(fd);
-}
-
-FileOutputStream::FileOutputStream(int fd, size_t buffer_capacity)
- : fd_(fd), buffer_capacity_(buffer_capacity) {
- if (fd_ < 0) {
- error_ = "Bad File Descriptor";
- } else {
- buffer_.reset(new uint8_t[buffer_capacity_]);
- }
-}
-
-FileOutputStream::~FileOutputStream() {
- // Flush the buffer.
- Flush();
-}
-
-bool FileOutputStream::Next(void** data, size_t* size) {
- if (HadError()) {
- return false;
- }
-
- if (buffer_offset_ == buffer_capacity_) {
- if (!FlushImpl()) {
- return false;
- }
- }
-
- const size_t buffer_size = buffer_capacity_ - buffer_offset_;
- *data = buffer_.get() + buffer_offset_;
- *size = buffer_size;
- total_byte_count_ += buffer_size;
- buffer_offset_ = buffer_capacity_;
- return true;
-}
-
-void FileOutputStream::BackUp(size_t count) {
- if (count > buffer_offset_) {
- count = buffer_offset_;
- }
- buffer_offset_ -= count;
- total_byte_count_ -= count;
-}
-
-size_t FileOutputStream::ByteCount() const {
- return total_byte_count_;
-}
-
-bool FileOutputStream::Flush() {
- if (!HadError()) {
- return FlushImpl();
- }
- return false;
-}
-
-bool FileOutputStream::FlushImpl() {
- ssize_t n = TEMP_FAILURE_RETRY(write(fd_, buffer_.get(), buffer_offset_));
- if (n < 0) {
- error_ = SystemErrorCodeToString(errno);
- owned_fd_.reset();
- fd_ = -1;
- buffer_.reset();
- return false;
- }
-
- buffer_offset_ = 0u;
- return true;
-}
-
-bool FileOutputStream::HadError() const {
- return fd_ == -1;
-}
-
-std::string FileOutputStream::GetError() const {
- return error_;
-}
-
-} // namespace io
-} // namespace aapt
diff --git a/tools/aapt2/io/FileStream.h b/tools/aapt2/io/FileStream.h
deleted file mode 100644
index 62d910f..0000000
--- a/tools/aapt2/io/FileStream.h
+++ /dev/null
@@ -1,107 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef AAPT_IO_FILESTREAM_H
-#define AAPT_IO_FILESTREAM_H
-
-#include "io/Io.h"
-
-#include <memory>
-#include <string>
-
-#include "android-base/macros.h"
-#include "android-base/unique_fd.h"
-
-namespace aapt {
-namespace io {
-
-constexpr size_t kDefaultBufferCapacity = 4096u;
-
-class FileInputStream : public InputStream {
- public:
- explicit FileInputStream(const std::string& path,
- size_t buffer_capacity = kDefaultBufferCapacity);
-
- // Take ownership of `fd`.
- explicit FileInputStream(int fd, size_t buffer_capacity = kDefaultBufferCapacity);
-
- bool Next(const void** data, size_t* size) override;
-
- void BackUp(size_t count) override;
-
- size_t ByteCount() const override;
-
- bool HadError() const override;
-
- std::string GetError() const override;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(FileInputStream);
-
- android::base::unique_fd fd_;
- std::string error_;
- std::unique_ptr<uint8_t[]> buffer_;
- size_t buffer_capacity_ = 0u;
- size_t buffer_offset_ = 0u;
- size_t buffer_size_ = 0u;
- size_t total_byte_count_ = 0u;
-};
-
-class FileOutputStream : public OutputStream {
- public:
- explicit FileOutputStream(const std::string& path,
- size_t buffer_capacity = kDefaultBufferCapacity);
-
- // Does not take ownership of `fd`.
- explicit FileOutputStream(int fd, size_t buffer_capacity = kDefaultBufferCapacity);
-
- // Takes ownership of `fd`.
- explicit FileOutputStream(android::base::unique_fd fd,
- size_t buffer_capacity = kDefaultBufferCapacity);
-
- ~FileOutputStream();
-
- bool Next(void** data, size_t* size) override;
-
- // Immediately flushes out the contents of the buffer to disk.
- bool Flush();
-
- void BackUp(size_t count) override;
-
- size_t ByteCount() const override;
-
- bool HadError() const override;
-
- std::string GetError() const override;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(FileOutputStream);
-
- bool FlushImpl();
-
- android::base::unique_fd owned_fd_;
- int fd_;
- std::string error_;
- std::unique_ptr<uint8_t[]> buffer_;
- size_t buffer_capacity_ = 0u;
- size_t buffer_offset_ = 0u;
- size_t total_byte_count_ = 0u;
-};
-
-} // namespace io
-} // namespace aapt
-
-#endif // AAPT_IO_FILESTREAM_H
diff --git a/tools/aapt2/io/FileStream_test.cpp b/tools/aapt2/io/FileStream_test.cpp
deleted file mode 100644
index cc9cd28..0000000
--- a/tools/aapt2/io/FileStream_test.cpp
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "io/FileStream.h"
-
-#include "android-base/file.h"
-#include "android-base/macros.h"
-
-#include "test/Test.h"
-
-using ::android::StringPiece;
-using ::testing::Eq;
-using ::testing::NotNull;
-using ::testing::StrEq;
-
-namespace aapt {
-namespace io {
-
-TEST(FileInputStreamTest, NextAndBackup) {
- std::string input = "this is a cool string";
- TemporaryFile file;
- ASSERT_THAT(TEMP_FAILURE_RETRY(write(file.fd, input.c_str(), input.size())), Eq(21));
- lseek64(file.fd, 0, SEEK_SET);
-
- // Use a small buffer size so that we can call Next() a few times.
- FileInputStream in(file.release(), 10u);
- ASSERT_FALSE(in.HadError());
- EXPECT_THAT(in.ByteCount(), Eq(0u));
-
- const void* buffer;
- size_t size;
- ASSERT_TRUE(in.Next(&buffer, &size)) << in.GetError();
- ASSERT_THAT(size, Eq(10u));
- ASSERT_THAT(buffer, NotNull());
- EXPECT_THAT(in.ByteCount(), Eq(10u));
- EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("this is a "));
-
- ASSERT_TRUE(in.Next(&buffer, &size));
- ASSERT_THAT(size, Eq(10u));
- ASSERT_THAT(buffer, NotNull());
- EXPECT_THAT(in.ByteCount(), Eq(20u));
- EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("cool strin"));
-
- in.BackUp(5u);
- EXPECT_THAT(in.ByteCount(), Eq(15u));
-
- ASSERT_TRUE(in.Next(&buffer, &size));
- ASSERT_THAT(size, Eq(5u));
- ASSERT_THAT(buffer, NotNull());
- ASSERT_THAT(in.ByteCount(), Eq(20u));
- EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("strin"));
-
- // Backup 1 more than possible. Should clamp.
- in.BackUp(11u);
- EXPECT_THAT(in.ByteCount(), Eq(10u));
-
- ASSERT_TRUE(in.Next(&buffer, &size));
- ASSERT_THAT(size, Eq(10u));
- ASSERT_THAT(buffer, NotNull());
- ASSERT_THAT(in.ByteCount(), Eq(20u));
- EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("cool strin"));
-
- ASSERT_TRUE(in.Next(&buffer, &size));
- ASSERT_THAT(size, Eq(1u));
- ASSERT_THAT(buffer, NotNull());
- ASSERT_THAT(in.ByteCount(), Eq(21u));
- EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("g"));
-
- EXPECT_FALSE(in.Next(&buffer, &size));
- EXPECT_FALSE(in.HadError());
-}
-
-TEST(FileOutputStreamTest, NextAndBackup) {
- const std::string input = "this is a cool string";
-
- TemporaryFile file;
-
- FileOutputStream out(file.fd, 10u);
- ASSERT_FALSE(out.HadError());
- EXPECT_THAT(out.ByteCount(), Eq(0u));
-
- void* buffer;
- size_t size;
- ASSERT_TRUE(out.Next(&buffer, &size));
- ASSERT_THAT(size, Eq(10u));
- ASSERT_THAT(buffer, NotNull());
- EXPECT_THAT(out.ByteCount(), Eq(10u));
- memcpy(reinterpret_cast<char*>(buffer), input.c_str(), size);
-
- ASSERT_TRUE(out.Next(&buffer, &size));
- ASSERT_THAT(size, Eq(10u));
- ASSERT_THAT(buffer, NotNull());
- EXPECT_THAT(out.ByteCount(), Eq(20u));
- memcpy(reinterpret_cast<char*>(buffer), input.c_str() + 10u, size);
-
- ASSERT_TRUE(out.Next(&buffer, &size));
- ASSERT_THAT(size, Eq(10u));
- ASSERT_THAT(buffer, NotNull());
- EXPECT_THAT(out.ByteCount(), Eq(30u));
- reinterpret_cast<char*>(buffer)[0] = input[20u];
- out.BackUp(size - 1);
- EXPECT_THAT(out.ByteCount(), Eq(21u));
-
- ASSERT_TRUE(out.Flush());
-
- lseek64(file.fd, 0, SEEK_SET);
-
- std::string actual;
- ASSERT_TRUE(android::base::ReadFdToString(file.fd, &actual));
- EXPECT_THAT(actual, StrEq(input));
-}
-
-} // namespace io
-} // namespace aapt
diff --git a/tools/aapt2/io/FileSystem.cpp b/tools/aapt2/io/FileSystem.cpp
index 6a692e4..03fabcc 100644
--- a/tools/aapt2/io/FileSystem.cpp
+++ b/tools/aapt2/io/FileSystem.cpp
@@ -22,9 +22,9 @@
#include <sys/stat.h>
#include "android-base/errors.h"
+#include "androidfw/FileStream.h"
#include "androidfw/Source.h"
#include "androidfw/StringPiece.h"
-#include "io/FileStream.h"
#include "util/Files.h"
#include "util/Util.h"
#include "utils/FileMap.h"
@@ -49,8 +49,8 @@
return {};
}
-std::unique_ptr<io::InputStream> RegularFile::OpenInputStream() {
- return util::make_unique<FileInputStream>(source_.path);
+std::unique_ptr<android::InputStream> RegularFile::OpenInputStream() {
+ return util::make_unique<android::FileInputStream>(source_.path);
}
const android::Source& RegularFile::GetSource() const {
diff --git a/tools/aapt2/io/FileSystem.h b/tools/aapt2/io/FileSystem.h
index f975196..d6ecfeb 100644
--- a/tools/aapt2/io/FileSystem.h
+++ b/tools/aapt2/io/FileSystem.h
@@ -30,7 +30,7 @@
explicit RegularFile(const android::Source& source);
std::unique_ptr<IData> OpenAsData() override;
- std::unique_ptr<io::InputStream> OpenInputStream() override;
+ std::unique_ptr<android::InputStream> OpenInputStream() override;
const android::Source& GetSource() const override;
bool GetModificationTime(struct tm* buf) const override;
diff --git a/tools/aapt2/io/Io.h b/tools/aapt2/io/Io.h
deleted file mode 100644
index e1df23a6..0000000
--- a/tools/aapt2/io/Io.h
+++ /dev/null
@@ -1,99 +0,0 @@
-/*
- * Copyright (C) 2016 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef AAPT_IO_IO_H
-#define AAPT_IO_IO_H
-
-#include <string>
-
-namespace aapt {
-namespace io {
-
-// InputStream interface that mimics protobuf's ZeroCopyInputStream,
-// with added error handling methods to better report issues.
-class InputStream {
- public:
- virtual ~InputStream() = default;
-
- // Returns a chunk of data for reading. data and size must not be nullptr.
- // Returns true so long as there is more data to read, returns false if an error occurred
- // or no data remains. If an error occurred, check HadError().
- // The stream owns the buffer returned from this method and the buffer is invalidated
- // anytime another mutable method is called.
- virtual bool Next(const void** data, size_t* size) = 0;
-
- // Backup count bytes, where count is smaller or equal to the size of the last buffer returned
- // from Next().
- // Useful when the last block returned from Next() wasn't fully read.
- virtual void BackUp(size_t count) = 0;
-
- // Returns true if this InputStream can rewind. If so, Rewind() can be called.
- virtual bool CanRewind() const { return false; };
-
- // Rewinds the stream to the beginning so it can be read again.
- // Returns true if the rewind succeeded.
- // This does nothing if CanRewind() returns false.
- virtual bool Rewind() { return false; }
-
- // Returns the number of bytes that have been read from the stream.
- virtual size_t ByteCount() const = 0;
-
- // Returns an error message if HadError() returned true.
- virtual std::string GetError() const { return {}; }
-
- // Returns true if an error occurred. Errors are permanent.
- virtual bool HadError() const = 0;
-};
-
-// A sub-InputStream interface that knows the total size of its stream.
-class KnownSizeInputStream : public InputStream {
- public:
- virtual size_t TotalSize() const = 0;
-};
-
-// OutputStream interface that mimics protobuf's ZeroCopyOutputStream,
-// with added error handling methods to better report issues.
-class OutputStream {
- public:
- virtual ~OutputStream() = default;
-
- // Returns a buffer to which data can be written to. The data written to this buffer will
- // eventually be written to the stream. Call BackUp() if the data written doesn't occupy the
- // entire buffer.
- // Return false if there was an error.
- // The stream owns the buffer returned from this method and the buffer is invalidated
- // anytime another mutable method is called.
- virtual bool Next(void** data, size_t* size) = 0;
-
- // Backup count bytes, where count is smaller or equal to the size of the last buffer returned
- // from Next().
- // Useful for when the last block returned from Next() wasn't fully written to.
- virtual void BackUp(size_t count) = 0;
-
- // Returns the number of bytes that have been written to the stream.
- virtual size_t ByteCount() const = 0;
-
- // Returns an error message if HadError() returned true.
- virtual std::string GetError() const { return {}; }
-
- // Returns true if an error occurred. Errors are permanent.
- virtual bool HadError() const = 0;
-};
-
-} // namespace io
-} // namespace aapt
-
-#endif /* AAPT_IO_IO_H */
diff --git a/tools/aapt2/io/StringStream.cpp b/tools/aapt2/io/StringStream.cpp
index 9c49788..bb3911b 100644
--- a/tools/aapt2/io/StringStream.cpp
+++ b/tools/aapt2/io/StringStream.cpp
@@ -51,6 +51,23 @@
return str_.size();
}
+bool StringInputStream::ReadFullyAtOffset(void* data, size_t byte_count, off64_t offset) {
+ if (byte_count == 0) {
+ return true;
+ }
+ if (offset < 0) {
+ return false;
+ }
+ if (offset > std::numeric_limits<off64_t>::max() - byte_count) {
+ return false;
+ }
+ if (offset + byte_count > str_.size()) {
+ return false;
+ }
+ memcpy(data, str_.data() + offset, byte_count);
+ return true;
+}
+
StringOutputStream::StringOutputStream(std::string* str, size_t buffer_capacity)
: str_(str),
buffer_capacity_(buffer_capacity),
diff --git a/tools/aapt2/io/StringStream.h b/tools/aapt2/io/StringStream.h
index f7bdecca..7e1abe5 100644
--- a/tools/aapt2/io/StringStream.h
+++ b/tools/aapt2/io/StringStream.h
@@ -17,17 +17,16 @@
#ifndef AAPT_IO_STRINGSTREAM_H
#define AAPT_IO_STRINGSTREAM_H
-#include "io/Io.h"
-
#include <memory>
#include "android-base/macros.h"
+#include "androidfw/Streams.h"
#include "androidfw/StringPiece.h"
namespace aapt {
namespace io {
-class StringInputStream : public KnownSizeInputStream {
+class StringInputStream : public android::KnownSizeInputStream {
public:
explicit StringInputStream(android::StringPiece str);
@@ -47,6 +46,8 @@
size_t TotalSize() const override;
+ bool ReadFullyAtOffset(void* data, size_t byte_count, off64_t offset) override;
+
private:
DISALLOW_COPY_AND_ASSIGN(StringInputStream);
@@ -54,7 +55,7 @@
size_t offset_;
};
-class StringOutputStream : public OutputStream {
+class StringOutputStream : public android::OutputStream {
public:
explicit StringOutputStream(std::string* str, size_t buffer_capacity = 4096u);
diff --git a/tools/aapt2/io/Util.cpp b/tools/aapt2/io/Util.cpp
index 79d8d52..9616e47 100644
--- a/tools/aapt2/io/Util.cpp
+++ b/tools/aapt2/io/Util.cpp
@@ -26,8 +26,9 @@
namespace aapt {
namespace io {
-bool CopyInputStreamToArchive(IAaptContext* context, InputStream* in, std::string_view out_path,
- uint32_t compression_flags, IArchiveWriter* writer) {
+bool CopyInputStreamToArchive(IAaptContext* context, android::InputStream* in,
+ std::string_view out_path, uint32_t compression_flags,
+ IArchiveWriter* writer) {
TRACE_CALL();
if (context->IsVerbose()) {
context->GetDiagnostics()->Note(android::DiagMessage()
@@ -91,7 +92,7 @@
return false;
}
-bool Copy(OutputStream* out, InputStream* in) {
+bool Copy(android::OutputStream* out, android::InputStream* in) {
TRACE_CALL();
const void* in_buffer;
size_t in_len;
@@ -110,7 +111,7 @@
return !in->HadError();
}
-bool Copy(OutputStream* out, StringPiece in) {
+bool Copy(android::OutputStream* out, StringPiece in) {
const char* in_buffer = in.data();
size_t in_len = in.size();
while (in_len != 0) {
@@ -129,7 +130,7 @@
return true;
}
-bool Copy(ZeroCopyOutputStream* out, InputStream* in) {
+bool Copy(ZeroCopyOutputStream* out, android::InputStream* in) {
OutputStreamAdaptor adaptor(out);
return Copy(&adaptor, in);
}
diff --git a/tools/aapt2/io/Util.h b/tools/aapt2/io/Util.h
index 685f522..25aa8f8 100644
--- a/tools/aapt2/io/Util.h
+++ b/tools/aapt2/io/Util.h
@@ -19,18 +19,19 @@
#include <string_view>
+#include "androidfw/Streams.h"
#include "format/Archive.h"
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/message.h"
#include "io/File.h"
-#include "io/Io.h"
#include "process/IResourceTableConsumer.h"
namespace aapt {
namespace io {
-bool CopyInputStreamToArchive(IAaptContext* context, InputStream* in, std::string_view out_path,
- uint32_t compression_flags, IArchiveWriter* writer);
+bool CopyInputStreamToArchive(IAaptContext* context, android::InputStream* in,
+ std::string_view out_path, uint32_t compression_flags,
+ IArchiveWriter* writer);
bool CopyFileToArchive(IAaptContext* context, IFile* file, std::string_view out_path,
uint32_t compression_flags, IArchiveWriter* writer);
@@ -44,11 +45,11 @@
// Copies the data from in to out. Returns false if there was an error.
// If there was an error, check the individual streams' HadError/GetError methods.
-bool Copy(OutputStream* out, InputStream* in);
-bool Copy(OutputStream* out, android::StringPiece in);
-bool Copy(::google::protobuf::io::ZeroCopyOutputStream* out, InputStream* in);
+bool Copy(android::OutputStream* out, android::InputStream* in);
+bool Copy(android::OutputStream* out, android::StringPiece in);
+bool Copy(::google::protobuf::io::ZeroCopyOutputStream* out, android::InputStream* in);
-class OutputStreamAdaptor : public io::OutputStream {
+class OutputStreamAdaptor : public android::OutputStream {
public:
explicit OutputStreamAdaptor(::google::protobuf::io::ZeroCopyOutputStream* out) : out_(out) {
}
@@ -84,7 +85,7 @@
class ZeroCopyInputAdaptor : public ::google::protobuf::io::ZeroCopyInputStream {
public:
- explicit ZeroCopyInputAdaptor(io::InputStream* in) : in_(in) {
+ explicit ZeroCopyInputAdaptor(android::InputStream* in) : in_(in) {
}
bool Next(const void** data, int* size) override {
@@ -119,12 +120,13 @@
private:
DISALLOW_COPY_AND_ASSIGN(ZeroCopyInputAdaptor);
- io::InputStream* in_;
+ android::InputStream* in_;
};
class ProtoInputStreamReader {
public:
- explicit ProtoInputStreamReader(io::InputStream* in) : in_(in) { }
+ explicit ProtoInputStreamReader(android::InputStream* in) : in_(in) {
+ }
/** Deserializes a Message proto from the current position in the input stream.*/
template <typename T> bool ReadMessage(T *message) {
@@ -135,7 +137,7 @@
}
private:
- io::InputStream* in_;
+ android::InputStream* in_;
};
} // namespace io
diff --git a/tools/aapt2/io/ZipArchive.cpp b/tools/aapt2/io/ZipArchive.cpp
index cb5bbe9..e44d61e 100644
--- a/tools/aapt2/io/ZipArchive.cpp
+++ b/tools/aapt2/io/ZipArchive.cpp
@@ -63,7 +63,7 @@
}
}
-std::unique_ptr<io::InputStream> ZipFile::OpenInputStream() {
+std::unique_ptr<android::InputStream> ZipFile::OpenInputStream() {
return OpenAsData();
}
diff --git a/tools/aapt2/io/ZipArchive.h b/tools/aapt2/io/ZipArchive.h
index ac125d0..a53c4a2 100644
--- a/tools/aapt2/io/ZipArchive.h
+++ b/tools/aapt2/io/ZipArchive.h
@@ -35,7 +35,7 @@
ZipFile(::ZipArchiveHandle handle, const ::ZipEntry& entry, const android::Source& source);
std::unique_ptr<IData> OpenAsData() override;
- std::unique_ptr<io::InputStream> OpenInputStream() override;
+ std::unique_ptr<android::InputStream> OpenInputStream() override;
const android::Source& GetSource() const override;
bool WasCompressed() override;
bool GetModificationTime(struct tm* buf) const override;