OLD | NEW |
| (Empty) |
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 | |
3 // LICENSE file. | |
4 | |
5 // This file defines FileInputStream, a basic interface for reading files | |
6 // synchronously or asynchronously with support for seeking to an offset. | |
7 | |
8 #ifndef NET_BASE_FILE_INPUT_STREAM_H_ | |
9 #define NET_BASE_FILE_INPUT_STREAM_H_ | |
10 | |
11 #include "net/base/completion_callback.h" | |
12 | |
13 #if defined(OS_WIN) | |
14 typedef void* HANDLE; | |
15 #endif | |
16 | |
17 namespace net { | |
18 | |
19 // TODO(darin): Move this to a more generic location. | |
20 // This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux. | |
21 enum Whence { | |
22 FROM_BEGIN = 0, | |
23 FROM_CURRENT = 1, | |
24 FROM_END = 2 | |
25 }; | |
26 | |
27 class FileInputStream { | |
28 public: | |
29 FileInputStream(); | |
30 ~FileInputStream(); | |
31 | |
32 // Call this method to close the FileInputStream. It is OK to call Close | |
33 // multiple times. Redundant calls are ignored. | |
34 void Close(); | |
35 | |
36 // Call this method to open the FileInputStream. The remaining methods | |
37 // cannot be used unless this method returns OK. If the file cannot be | |
38 // opened then an error code is returned. | |
39 // NOTE: The underlying file is opened with non-exclusive access. | |
40 int Open(const std::wstring& path, bool asynchronous_mode); | |
41 | |
42 // Returns true if Open succeeded and Close has not been called. | |
43 bool IsOpen() const; | |
44 | |
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 | |
47 // error code is returned. It is not valid to call Seek while a Read call | |
48 // has a pending completion. | |
49 int64 Seek(Whence whence, int64 offset); | |
50 | |
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. | |
53 int64 Available(); | |
54 | |
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 | |
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. | |
59 // | |
60 // If opened with |asynchronous_mode| set to true, then a non-null callback | |
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 | |
63 // callback will be notified on the current thread (via the MessageLoop) when | |
64 // the read has completed. | |
65 // | |
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 | |
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. | |
70 // | |
71 int Read(char* buf, int buf_len, CompletionCallback* callback); | |
72 | |
73 private: | |
74 class AsyncContext; | |
75 friend class AsyncContext; | |
76 | |
77 // This member is used to support asynchronous reads. It is non-null when | |
78 // the FileInputStream was opened with asynchronous_mode set to true. | |
79 scoped_ptr<AsyncContext> async_context_; | |
80 | |
81 #if defined(OS_WIN) | |
82 HANDLE handle_; | |
83 #elif defined(OS_POSIX) | |
84 int fd_; | |
85 #endif | |
86 | |
87 DISALLOW_COPY_AND_ASSIGN(FileInputStream); | |
88 }; | |
89 | |
90 } // namespace net | |
91 | |
92 #endif // NET_BASE_FILE_INPUT_STREAM_H_ | |
OLD | NEW |