diff options
author | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-09 22:26:41 +0000 |
---|---|---|
committer | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-09 22:26:41 +0000 |
commit | 92aad5227a8e7b00f7fbc3e409037bf23cccda0c (patch) | |
tree | 13ec2aa09ed2e376a6c78524c3489f24d8e2d9a4 | |
parent | af416dfd64790147f1b25c634b4054907e54ba12 (diff) | |
download | chromium_src-92aad5227a8e7b00f7fbc3e409037bf23cccda0c.zip chromium_src-92aad5227a8e7b00f7fbc3e409037bf23cccda0c.tar.gz chromium_src-92aad5227a8e7b00f7fbc3e409037bf23cccda0c.tar.bz2 |
Add constructor to net::FileStream to create it with a base::PlatformFile handle.
Constructor added to both _win and _posix implementations and provided a unit test.
Review URL: http://codereview.chromium.org/20137
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9428 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | net/base/file_stream.h | 7 | ||||
-rw-r--r-- | net/base/file_stream_posix.cc | 6 | ||||
-rw-r--r-- | net/base/file_stream_unittest.cc | 39 | ||||
-rw-r--r-- | net/base/file_stream_win.cc | 11 |
4 files changed, 63 insertions, 0 deletions
diff --git a/net/base/file_stream.h b/net/base/file_stream.h index 67f93ed..20466b4 100644 --- a/net/base/file_stream.h +++ b/net/base/file_stream.h @@ -27,6 +27,13 @@ enum Whence { class FileStream { public: FileStream(); + + // Construct a FileStream with an existing file handle and opening flags. + // |file| is valid file handle. + // |flags| is a bitfield of base::PlatformFileFlags when the file handle was + // opened. + FileStream(base::PlatformFile file, int flags); + ~FileStream(); // Call this method to close the FileStream. It is OK to call Close diff --git a/net/base/file_stream_posix.cc b/net/base/file_stream_posix.cc index abef9c2..7b248a4 100644 --- a/net/base/file_stream_posix.cc +++ b/net/base/file_stream_posix.cc @@ -47,6 +47,12 @@ FileStream::FileStream() : file_(base::kInvalidPlatformFileValue) { DCHECK(!IsOpen()); } +FileStream::FileStream(base::PlatformFile file, int flags) + : file_(file), open_flags_(flags) { + // TODO(hclam): initialize the aync_context_ if the file handle + // is opened as an asynchronous file handle. +} + FileStream::~FileStream() { Close(); } diff --git a/net/base/file_stream_unittest.cc b/net/base/file_stream_unittest.cc index abee897..f96d57f 100644 --- a/net/base/file_stream_unittest.cc +++ b/net/base/file_stream_unittest.cc @@ -4,6 +4,7 @@ #include "base/file_util.h" #include "base/path_service.h" +#include "base/platform_file.h" #include "net/base/file_stream.h" #include "net/base/net_errors.h" #include "net/base/test_completion_callback.h" @@ -39,6 +40,44 @@ TEST_F(FileStreamTest, BasicOpenClose) { EXPECT_EQ(net::OK, rv); } +// Test the use of FileStream with a file handle provided at construction. +TEST_F(FileStreamTest, UseFileHandle) { + bool created = false; + + // 1. Test reading with a file handle. + ASSERT_EQ(kTestDataSize, + file_util::WriteFile(temp_file_path(), kTestData, kTestDataSize)); + int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ; + base::PlatformFile file = base::CreatePlatformFile( + temp_file_path().ToWStringHack(), flags, &created); + + // Seek to the beginning of the file and read. + net::FileStream read_stream(file, flags); + ASSERT_EQ(0, read_stream.Seek(net::FROM_BEGIN, 0)); + ASSERT_EQ(kTestDataSize, read_stream.Available()); + // Read into buffer and compare. + char buffer[kTestDataSize]; + ASSERT_EQ(kTestDataSize, read_stream.Read(buffer, kTestDataSize, NULL)); + ASSERT_EQ(0, memcmp(kTestData, buffer, kTestDataSize)); + read_stream.Close(); + + // 2. Test writing with a file handle. + file_util::Delete(temp_file_path(), false); + flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE; + file = base::CreatePlatformFile(temp_file_path().ToWStringHack(), + flags, &created); + + net::FileStream write_stream(file, flags); + ASSERT_EQ(0, write_stream.Seek(net::FROM_BEGIN, 0)); + ASSERT_EQ(kTestDataSize, write_stream.Write(kTestData, kTestDataSize, NULL)); + write_stream.Close(); + + // Read into buffer and compare to make sure the handle worked fine. + ASSERT_EQ(kTestDataSize, + file_util::ReadFile(temp_file_path(), buffer, kTestDataSize)); + ASSERT_EQ(0, memcmp(kTestData, buffer, kTestDataSize)); +} + TEST_F(FileStreamTest, UseClosedStream) { net::FileStream stream; diff --git a/net/base/file_stream_win.cc b/net/base/file_stream_win.cc index d70b398..ca492a2 100644 --- a/net/base/file_stream_win.cc +++ b/net/base/file_stream_win.cc @@ -110,6 +110,17 @@ void FileStream::AsyncContext::OnIOCompleted( FileStream::FileStream() : file_(INVALID_HANDLE_VALUE) { } +FileStream::FileStream(base::PlatformFile file, int flags) + : file_(file), open_flags_(flags) { + // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to + // make sure we will perform asynchronous File IO to it. + if (flags & base::PLATFORM_FILE_ASYNC) { + async_context_.reset(new AsyncContext(this)); + MessageLoopForIO::current()->RegisterIOHandler(file_, + async_context_.get()); + } +} + FileStream::~FileStream() { Close(); } |