Fixed a bug in file reading

PiperOrigin-RevId: 674498290
This commit is contained in:
Guogang Li
2024-09-13 18:15:35 -07:00
committed by Copybara-Service
parent 2f4eb8649a
commit f7e54916c0
2 changed files with 23 additions and 18 deletions
@@ -14,14 +14,15 @@
#include "internal/platform/implementation/windows/file.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <ios>
#include <memory>
#include <string>
#include "absl/memory/memory.h"
#include "absl/strings/string_view.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/windows/utils.h"
#include "internal/platform/logging.h"
@@ -30,13 +31,12 @@ namespace nearby {
namespace windows {
// InputFile
std::unique_ptr<IOFile> IOFile::CreateInputFile(
const absl::string_view file_path, size_t size) {
std::unique_ptr<IOFile> IOFile::CreateInputFile(absl::string_view file_path,
size_t size) {
return absl::WrapUnique(new IOFile(file_path, size));
}
IOFile::IOFile(const absl::string_view file_path, size_t size)
: path_(file_path) {
IOFile::IOFile(absl::string_view file_path, size_t size) : path_(file_path) {
// Always open input file path as wide string on Windows platform.
std::wstring wide_path = string_to_wstring(std::string(file_path));
file_.open(wide_path, std::ios::binary | std::ios::in | std::ios::ate);
@@ -46,17 +46,17 @@ IOFile::IOFile(const absl::string_view file_path, size_t size)
// Unsure why it consistently returns -1 when the file size exceeds 2GB. If
// obtaining the file size through tellg fails, use the size provided
// in the parameters.
total_size_ = size;
total_size_ = size;
}
file_.seekg(0);
}
std::unique_ptr<IOFile> IOFile::CreateOutputFile(const absl::string_view path) {
std::unique_ptr<IOFile> IOFile::CreateOutputFile(absl::string_view path) {
return std::unique_ptr<IOFile>(new IOFile(path));
}
IOFile::IOFile(const absl::string_view file_path)
IOFile::IOFile(absl::string_view file_path)
: file_(), path_(file_path), total_size_(0) {
// Always open input file path as wide string on Windows platform.
std::wstring wide_path = string_to_wstring(path_);
@@ -73,18 +73,20 @@ ExceptionOr<ByteArray> IOFile::Read(std::int64_t size) {
return ExceptionOr<ByteArray>{Exception::kIo};
}
if (file_.peek() == EOF) {
if (file_.eof()) {
return ExceptionOr<ByteArray>{ByteArray{}};
}
ByteArray bytes(size);
std::unique_ptr<char[]> read_bytes{new char[size]};
file_.read(read_bytes.get(), static_cast<ptrdiff_t>(size));
if (buffer_.size() < size) {
buffer_.resize(size);
}
file_.read(buffer_.data(), static_cast<ptrdiff_t>(size));
auto num_bytes_read = file_.gcount();
if (num_bytes_read == 0) {
return ExceptionOr<ByteArray>{Exception::kIo};
}
return ExceptionOr<ByteArray>(ByteArray(read_bytes.get(), num_bytes_read));
return ExceptionOr<ByteArray>(ByteArray(buffer_.data(), num_bytes_read));
} catch (...) {
NEARBY_LOGS(ERROR) << "Fail to read";
return ExceptionOr<ByteArray>{Exception::kIo};
@@ -15,12 +15,14 @@
#ifndef PLATFORM_IMPL_WINDOWS_FILE_H_
#define PLATFORM_IMPL_WINDOWS_FILE_H_
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <memory>
#include <string>
#include "absl/strings/string_view.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/input_file.h"
#include "internal/platform/implementation/output_file.h"
@@ -30,10 +32,10 @@ namespace windows {
class IOFile final : public api::InputFile, public api::OutputFile {
public:
static std::unique_ptr<IOFile> CreateInputFile(
const absl::string_view file_path, size_t size);
static std::unique_ptr<IOFile> CreateInputFile(absl::string_view file_path,
size_t size);
static std::unique_ptr<IOFile> CreateOutputFile(const absl::string_view path);
static std::unique_ptr<IOFile> CreateOutputFile(absl::string_view path);
ExceptionOr<ByteArray> Read(std::int64_t size) override;
@@ -46,11 +48,12 @@ class IOFile final : public api::InputFile, public api::OutputFile {
Exception Flush() override;
private:
explicit IOFile(const absl::string_view file_path, size_t size);
explicit IOFile(const absl::string_view file_path);
explicit IOFile(absl::string_view file_path, size_t size);
explicit IOFile(absl::string_view file_path);
std::fstream file_;
std::string path_;
std::string buffer_;
std::int64_t total_size_;
};