blob: 33536021458e3ede6d0d155be6bdbbfb04f6dd7b [file] [log] [blame]
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001// Copyright 2009 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_FILE_H_
4#define ART_SRC_FILE_H_
5
6#include <stdint.h>
7#include <sys/types.h>
8
9namespace art {
10
11class File {
12 public:
13 virtual ~File() { }
14
15 virtual int64_t Read(void* buffer, int64_t num_bytes) = 0;
16 virtual int64_t Write(const void* buffer, int64_t num_bytes) = 0;
17
18 // ReadFully and WriteFully do attempt to transfer all of the bytes to/from
19 // the buffer. In the event of short accesses they will loop internally until
20 // the whole buffer has been transferred or an error occurs. If an error
21 // occurred the result will be set to false.
22 virtual bool ReadFully(void* buffer, int64_t num_bytes);
23 virtual bool WriteFully(const void* buffer, int64_t num_bytes);
24 bool WriteByte(uint8_t byte) {
25 return WriteFully(&byte, 1);
26 }
27
28 // Get the length of the file. Returns a negative value if the length cannot
29 // be determined (e.g. not seekable device).
30 virtual off_t Length() = 0;
31
32 // Get the current position in the file.
33 // Returns a negative value if position cannot be determined.
34 virtual off_t Position() = 0;
35
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070036 virtual int Fd() = 0;
37
Brian Carlstromdb4d5402011-08-09 12:18:28 -070038 const char* name() const { return name_; }
39
40 protected:
41 explicit File(const char* name) : name_(name) { }
42 virtual void Close() = 0;
43 virtual bool IsClosed() = 0;
44
45 private:
46 const char* name_;
47};
48
49} // namespace art
50
51#endif // ART_SRC_FILE_H_