OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. Use of this |
2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
3 // LICENSE file. | 3 // LICENSE file. |
4 | 4 |
5 // This file defines FileInputStream, a basic interface for reading files | 5 // This file defines FileStream, a basic interface for reading and writing files |
6 // synchronously or asynchronously with support for seeking to an offset. | 6 // synchronously or asynchronously with support for seeking to an offset. |
| 7 // Note that even when used asynchronously, only one operation is supported at |
| 8 // a time. |
7 | 9 |
8 #ifndef NET_BASE_FILE_INPUT_STREAM_H_ | 10 #ifndef NET_BASE_FILE_STREAM_H_ |
9 #define NET_BASE_FILE_INPUT_STREAM_H_ | 11 #define NET_BASE_FILE_STREAM_H_ |
10 | 12 |
| 13 #include "base/platform_file.h" |
11 #include "net/base/completion_callback.h" | 14 #include "net/base/completion_callback.h" |
12 | 15 |
13 #if defined(OS_WIN) | |
14 typedef void* HANDLE; | |
15 #endif | |
16 | |
17 namespace net { | 16 namespace net { |
18 | 17 |
19 // TODO(darin): Move this to a more generic location. | 18 // TODO(darin): Move this to a more generic location. |
20 // This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux. | 19 // This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux. |
21 enum Whence { | 20 enum Whence { |
22 FROM_BEGIN = 0, | 21 FROM_BEGIN = 0, |
23 FROM_CURRENT = 1, | 22 FROM_CURRENT = 1, |
24 FROM_END = 2 | 23 FROM_END = 2 |
25 }; | 24 }; |
26 | 25 |
27 class FileInputStream { | 26 class FileStream { |
28 public: | 27 public: |
29 FileInputStream(); | 28 FileStream(); |
30 ~FileInputStream(); | 29 ~FileStream(); |
31 | 30 |
32 // Call this method to close the FileInputStream. It is OK to call Close | 31 // Call this method to close the FileStream. It is OK to call Close |
33 // multiple times. Redundant calls are ignored. | 32 // multiple times. Redundant calls are ignored. |
| 33 // Note that if there are any pending async operations, they'll be aborted. |
34 void Close(); | 34 void Close(); |
35 | 35 |
36 // Call this method to open the FileInputStream. The remaining methods | 36 // Call this method to open the FileStream. The remaining methods |
37 // cannot be used unless this method returns OK. If the file cannot be | 37 // cannot be used unless this method returns OK. If the file cannot be |
38 // opened then an error code is returned. | 38 // opened then an error code is returned. |
39 // NOTE: The underlying file is opened with non-exclusive access. | 39 // open_flags is a bitfield of base::PlatformFileFlags |
40 int Open(const std::wstring& path, bool asynchronous_mode); | 40 int Open(const std::wstring& path, int open_flags); |
41 | 41 |
42 // Returns true if Open succeeded and Close has not been called. | 42 // Returns true if Open succeeded and Close has not been called. |
43 bool IsOpen() const; | 43 bool IsOpen() const; |
44 | 44 |
45 // Adjust the position from where data is read. Upon success, the stream | 45 // Adjust the position from where data is read. Upon success, the stream |
46 // position relative to the start of the file is returned. Otherwise, an | 46 // position relative to the start of the file is returned. Otherwise, an |
47 // error code is returned. It is not valid to call Seek while a Read call | 47 // error code is returned. It is not valid to call Seek while a Read call |
48 // has a pending completion. | 48 // has a pending completion. |
49 int64 Seek(Whence whence, int64 offset); | 49 int64 Seek(Whence whence, int64 offset); |
50 | 50 |
51 // Returns the number of bytes available to read from the current stream | 51 // Returns the number of bytes available to read from the current stream |
52 // position until the end of the file. Otherwise, an error code is returned. | 52 // position until the end of the file. Otherwise, an error code is returned. |
53 int64 Available(); | 53 int64 Available(); |
54 | 54 |
55 // Call this method to read data from the current stream position. Up to | 55 // Call this method to read data from the current stream position. Up to |
56 // buf_len bytes will be copied into buf. (In other words, partial reads are | 56 // buf_len bytes will be copied into buf. (In other words, partial reads are |
57 // allowed.) Returns the number of bytes copied, 0 if at end-of-file, or an | 57 // allowed.) Returns the number of bytes copied, 0 if at end-of-file, or an |
58 // error code if the operation could not be performed. | 58 // error code if the operation could not be performed. |
59 // | 59 // |
60 // If opened with |asynchronous_mode| set to true, then a non-null callback | 60 // If opened with PLATFORM_FILE_ASYNC, then a non-null callback |
61 // must be passed to this method. In asynchronous mode, if the read could | 61 // must be passed to this method. In asynchronous mode, if the read could |
62 // not complete synchronously, then ERR_IO_PENDING is returned, and the | 62 // not complete synchronously, then ERR_IO_PENDING is returned, and the |
63 // callback will be notified on the current thread (via the MessageLoop) when | 63 // callback will be notified on the current thread (via the MessageLoop) when |
64 // the read has completed. | 64 // the read has completed. |
65 // | 65 // |
66 // In the case of an asychronous read, the memory pointed to by |buf| must | 66 // In the case of an asychronous read, the memory pointed to by |buf| must |
67 // remain valid until the callback is notified. However, it is valid to | 67 // remain valid until the callback is notified. However, it is valid to |
68 // destroy or close the file stream while there is an asynchronous read in | 68 // destroy or close the file stream while there is an asynchronous read in |
69 // progress. That will cancel the read and allow the buffer to be freed. | 69 // progress. That will cancel the read and allow the buffer to be freed. |
70 // | 70 // |
| 71 // This method should not be called if the stream was opened WRITE_ONLY. |
71 int Read(char* buf, int buf_len, CompletionCallback* callback); | 72 int Read(char* buf, int buf_len, CompletionCallback* callback); |
72 | 73 |
| 74 // Call this method to write data at the current stream position. Up to |
| 75 // buf_len bytes will be written from buf. (In other words, partial writes are |
| 76 // allowed.) Returns the number of bytes written, or an error code if the |
| 77 // operation could not be performed. |
| 78 // |
| 79 // If opened with PLATFORM_FILE_ASYNC, then a non-null callback |
| 80 // must be passed to this method. In asynchronous mode, if the write could |
| 81 // not complete synchronously, then ERR_IO_PENDING is returned, and the |
| 82 // callback will be notified on the current thread (via the MessageLoop) when |
| 83 // the write has completed. |
| 84 // |
| 85 // In the case of an asychronous write, the memory pointed to by |buf| must |
| 86 // remain valid until the callback is notified. However, it is valid to |
| 87 // destroy or close the file stream while there is an asynchronous write in |
| 88 // progress. That will cancel the write and allow the buffer to be freed. |
| 89 // |
| 90 // This method should not be called if the stream was opened READ_ONLY. |
| 91 int Write(const char* buf, int buf_len, CompletionCallback* callback); |
| 92 |
73 private: | 93 private: |
74 class AsyncContext; | 94 class AsyncContext; |
75 friend class AsyncContext; | 95 friend class AsyncContext; |
76 | 96 |
77 // This member is used to support asynchronous reads. It is non-null when | 97 // This member is used to support asynchronous reads. It is non-null when |
78 // the FileInputStream was opened with asynchronous_mode set to true. | 98 // the FileStream was opened with PLATFORM_FILE_ASYNC. |
79 scoped_ptr<AsyncContext> async_context_; | 99 scoped_ptr<AsyncContext> async_context_; |
80 | 100 |
81 #if defined(OS_WIN) | 101 base::PlatformFile file_; |
82 HANDLE handle_; | 102 int open_flags_; |
83 #elif defined(OS_POSIX) | |
84 int fd_; | |
85 #endif | |
86 | 103 |
87 DISALLOW_COPY_AND_ASSIGN(FileInputStream); | 104 DISALLOW_COPY_AND_ASSIGN(FileStream); |
88 }; | 105 }; |
89 | 106 |
90 } // namespace net | 107 } // namespace net |
91 | 108 |
92 #endif // NET_BASE_FILE_INPUT_STREAM_H_ | 109 #endif // NET_BASE_FILE_STREAM_H_ |
OLD | NEW |