OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file defines FileStream, a basic interface for reading and writing 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 | 7 // Note that even when used asynchronously, only one operation is supported at |
8 // a time. | 8 // a time. |
9 | 9 |
10 #ifndef NET_BASE_FILE_STREAM_H_ | 10 #ifndef NET_BASE_FILE_STREAM_H_ |
(...skipping 22 matching lines...) Expand all Loading... |
33 FileStream(); | 33 FileStream(); |
34 | 34 |
35 // Construct a FileStream with an existing file handle and opening flags. | 35 // Construct a FileStream with an existing file handle and opening flags. |
36 // |file| is valid file handle. | 36 // |file| is valid file handle. |
37 // |flags| is a bitfield of base::PlatformFileFlags when the file handle was | 37 // |flags| is a bitfield of base::PlatformFileFlags when the file handle was |
38 // opened. | 38 // opened. |
39 // The already opened file will not be automatically closed when FileStream | 39 // The already opened file will not be automatically closed when FileStream |
40 // is destructed. | 40 // is destructed. |
41 FileStream(base::PlatformFile file, int flags); | 41 FileStream(base::PlatformFile file, int flags); |
42 | 42 |
43 ~FileStream(); | 43 virtual ~FileStream(); |
44 | 44 |
45 // Call this method to close the FileStream. It is OK to call Close | 45 // Call this method to close the FileStream. It is OK to call Close |
46 // multiple times. Redundant calls are ignored. | 46 // multiple times. Redundant calls are ignored. |
47 // Note that if there are any pending async operations, they'll be aborted. | 47 // Note that if there are any pending async operations, they'll be aborted. |
48 void Close(); | 48 virtual void Close(); |
49 | 49 |
50 // Call this method to open the FileStream. The remaining methods | 50 // Call this method to open the FileStream. The remaining methods |
51 // cannot be used unless this method returns OK. If the file cannot be | 51 // cannot be used unless this method returns OK. If the file cannot be |
52 // opened then an error code is returned. | 52 // opened then an error code is returned. |
53 // open_flags is a bitfield of base::PlatformFileFlags | 53 // open_flags is a bitfield of base::PlatformFileFlags |
54 int Open(const FilePath& path, int open_flags); | 54 virtual int Open(const FilePath& path, int open_flags); |
55 | 55 |
56 // Returns true if Open succeeded and Close has not been called. | 56 // Returns true if Open succeeded and Close has not been called. |
57 bool IsOpen() const; | 57 virtual bool IsOpen() const; |
58 | 58 |
59 // Adjust the position from where data is read. Upon success, the stream | 59 // Adjust the position from where data is read. Upon success, the stream |
60 // position relative to the start of the file is returned. Otherwise, an | 60 // position relative to the start of the file is returned. Otherwise, an |
61 // error code is returned. It is not valid to call Seek while a Read call | 61 // error code is returned. It is not valid to call Seek while a Read call |
62 // has a pending completion. | 62 // has a pending completion. |
63 int64 Seek(Whence whence, int64 offset); | 63 virtual int64 Seek(Whence whence, int64 offset); |
64 | 64 |
65 // Returns the number of bytes available to read from the current stream | 65 // Returns the number of bytes available to read from the current stream |
66 // position until the end of the file. Otherwise, an error code is returned. | 66 // position until the end of the file. Otherwise, an error code is returned. |
67 int64 Available(); | 67 virtual int64 Available(); |
68 | 68 |
69 // Call this method to read data from the current stream position. Up to | 69 // Call this method to read data from the current stream position. Up to |
70 // buf_len bytes will be copied into buf. (In other words, partial reads are | 70 // buf_len bytes will be copied into buf. (In other words, partial reads are |
71 // allowed.) Returns the number of bytes copied, 0 if at end-of-file, or an | 71 // allowed.) Returns the number of bytes copied, 0 if at end-of-file, or an |
72 // error code if the operation could not be performed. | 72 // error code if the operation could not be performed. |
73 // | 73 // |
74 // If opened with PLATFORM_FILE_ASYNC, then a non-null callback | 74 // If opened with PLATFORM_FILE_ASYNC, then a non-null callback |
75 // must be passed to this method. In asynchronous mode, if the read could | 75 // must be passed to this method. In asynchronous mode, if the read could |
76 // not complete synchronously, then ERR_IO_PENDING is returned, and the | 76 // not complete synchronously, then ERR_IO_PENDING is returned, and the |
77 // callback will be notified on the current thread (via the MessageLoop) when | 77 // callback will be notified on the current thread (via the MessageLoop) when |
78 // the read has completed. | 78 // the read has completed. |
79 // | 79 // |
80 // In the case of an asychronous read, the memory pointed to by |buf| must | 80 // In the case of an asychronous read, the memory pointed to by |buf| must |
81 // remain valid until the callback is notified. However, it is valid to | 81 // remain valid until the callback is notified. However, it is valid to |
82 // destroy or close the file stream while there is an asynchronous read in | 82 // destroy or close the file stream while there is an asynchronous read in |
83 // progress. That will cancel the read and allow the buffer to be freed. | 83 // progress. That will cancel the read and allow the buffer to be freed. |
84 // | 84 // |
85 // This method should not be called if the stream was opened WRITE_ONLY. | 85 // This method should not be called if the stream was opened WRITE_ONLY. |
86 // | 86 // |
87 // You can pass NULL as the callback for synchronous I/O. | 87 // You can pass NULL as the callback for synchronous I/O. |
88 int Read(char* buf, int buf_len, CompletionCallback* callback); | 88 virtual int Read(char* buf, int buf_len, CompletionCallback* callback); |
89 | 89 |
90 // Performs the same as Read, but ensures that exactly buf_len bytes | 90 // Performs the same as Read, but ensures that exactly buf_len bytes |
91 // are copied into buf. A partial read may occur, but only as a result of | 91 // are copied into buf. A partial read may occur, but only as a result of |
92 // end-of-file or fatal error. Returns the number of bytes copied into buf, | 92 // end-of-file or fatal error. Returns the number of bytes copied into buf, |
93 // 0 if at end-of-file and no bytes have been read into buf yet, | 93 // 0 if at end-of-file and no bytes have been read into buf yet, |
94 // or an error code if the operation could not be performed. | 94 // or an error code if the operation could not be performed. |
95 int ReadUntilComplete(char *buf, int buf_len); | 95 virtual int ReadUntilComplete(char *buf, int buf_len); |
96 | 96 |
97 // Call this method to write data at the current stream position. Up to | 97 // Call this method to write data at the current stream position. Up to |
98 // buf_len bytes will be written from buf. (In other words, partial writes are | 98 // buf_len bytes will be written from buf. (In other words, partial writes are |
99 // allowed.) Returns the number of bytes written, or an error code if the | 99 // allowed.) Returns the number of bytes written, or an error code if the |
100 // operation could not be performed. | 100 // operation could not be performed. |
101 // | 101 // |
102 // If opened with PLATFORM_FILE_ASYNC, then a non-null callback | 102 // If opened with PLATFORM_FILE_ASYNC, then a non-null callback |
103 // must be passed to this method. In asynchronous mode, if the write could | 103 // must be passed to this method. In asynchronous mode, if the write could |
104 // not complete synchronously, then ERR_IO_PENDING is returned, and the | 104 // not complete synchronously, then ERR_IO_PENDING is returned, and the |
105 // callback will be notified on the current thread (via the MessageLoop) when | 105 // callback will be notified on the current thread (via the MessageLoop) when |
106 // the write has completed. | 106 // the write has completed. |
107 // | 107 // |
108 // In the case of an asychronous write, the memory pointed to by |buf| must | 108 // In the case of an asychronous write, the memory pointed to by |buf| must |
109 // remain valid until the callback is notified. However, it is valid to | 109 // remain valid until the callback is notified. However, it is valid to |
110 // destroy or close the file stream while there is an asynchronous write in | 110 // destroy or close the file stream while there is an asynchronous write in |
111 // progress. That will cancel the write and allow the buffer to be freed. | 111 // progress. That will cancel the write and allow the buffer to be freed. |
112 // | 112 // |
113 // This method should not be called if the stream was opened READ_ONLY. | 113 // This method should not be called if the stream was opened READ_ONLY. |
114 // | 114 // |
115 // You can pass NULL as the callback for synchronous I/O. | 115 // You can pass NULL as the callback for synchronous I/O. |
116 int Write(const char* buf, int buf_len, CompletionCallback* callback); | 116 virtual int Write(const char* buf, int buf_len, CompletionCallback* callback); |
117 | 117 |
118 // Truncates the file to be |bytes| length. This is only valid for writable | 118 // Truncates the file to be |bytes| length. This is only valid for writable |
119 // files. After truncation the file stream is positioned at |bytes|. The new | 119 // files. After truncation the file stream is positioned at |bytes|. The new |
120 // position is retured, or a value < 0 on error. | 120 // position is retured, or a value < 0 on error. |
121 // WARNING: one may not truncate a file beyond its current length on any | 121 // WARNING: one may not truncate a file beyond its current length on any |
122 // platform with this call. | 122 // platform with this call. |
123 int64 Truncate(int64 bytes); | 123 virtual int64 Truncate(int64 bytes); |
124 | 124 |
125 // Forces out a filesystem sync on this file to make sure that the file was | 125 // Forces out a filesystem sync on this file to make sure that the file was |
126 // written out to disk and is not currently sitting in the buffer. This does | 126 // written out to disk and is not currently sitting in the buffer. This does |
127 // not have to be called, it just forces one to happen at the time of | 127 // not have to be called, it just forces one to happen at the time of |
128 // calling. | 128 // calling. |
129 // | 129 // |
130 /// Returns an error code if the operation could not be performed. | 130 /// Returns an error code if the operation could not be performed. |
131 // | 131 // |
132 // This method should not be called if the stream was opened READ_ONLY. | 132 // This method should not be called if the stream was opened READ_ONLY. |
133 int Flush(); | 133 virtual int Flush(); |
134 | 134 |
135 private: | 135 private: |
136 class AsyncContext; | 136 class AsyncContext; |
137 friend class AsyncContext; | 137 friend class AsyncContext; |
138 | 138 |
139 // This member is used to support asynchronous reads. It is non-null when | 139 // This member is used to support asynchronous reads. It is non-null when |
140 // the FileStream was opened with PLATFORM_FILE_ASYNC. | 140 // the FileStream was opened with PLATFORM_FILE_ASYNC. |
141 scoped_ptr<AsyncContext> async_context_; | 141 scoped_ptr<AsyncContext> async_context_; |
142 | 142 |
143 base::PlatformFile file_; | 143 base::PlatformFile file_; |
144 int open_flags_; | 144 int open_flags_; |
145 bool auto_closed_; | 145 bool auto_closed_; |
146 | 146 |
147 DISALLOW_COPY_AND_ASSIGN(FileStream); | 147 DISALLOW_COPY_AND_ASSIGN(FileStream); |
148 }; | 148 }; |
149 | 149 |
150 } // namespace net | 150 } // namespace net |
151 | 151 |
152 #endif // NET_BASE_FILE_STREAM_H_ | 152 #endif // NET_BASE_FILE_STREAM_H_ |
OLD | NEW |