Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef ART_SRC_FILE_H_ |
| 18 | #define ART_SRC_FILE_H_ |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | class File { |
| 26 | public: |
| 27 | virtual ~File() { } |
| 28 | |
| 29 | virtual int64_t Read(void* buffer, int64_t num_bytes) = 0; |
| 30 | virtual int64_t Write(const void* buffer, int64_t num_bytes) = 0; |
| 31 | |
| 32 | // ReadFully and WriteFully do attempt to transfer all of the bytes to/from |
| 33 | // the buffer. In the event of short accesses they will loop internally until |
| 34 | // the whole buffer has been transferred or an error occurs. If an error |
| 35 | // occurred the result will be set to false. |
| 36 | virtual bool ReadFully(void* buffer, int64_t num_bytes); |
| 37 | virtual bool WriteFully(const void* buffer, int64_t num_bytes); |
| 38 | bool WriteByte(uint8_t byte) { |
| 39 | return WriteFully(&byte, 1); |
| 40 | } |
| 41 | |
| 42 | // Get the length of the file. Returns a negative value if the length cannot |
| 43 | // be determined (e.g. not seekable device). |
Elliott Hughes | 2a2ff56 | 2012-01-06 18:07:59 -0800 | [diff] [blame] | 44 | virtual off_t Length() = 0; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 45 | |
| 46 | // Get the current position in the file. |
| 47 | // Returns a negative value if position cannot be determined. |
Elliott Hughes | 2a2ff56 | 2012-01-06 18:07:59 -0800 | [diff] [blame] | 48 | virtual off_t Position() = 0; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 49 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 50 | virtual int Fd() = 0; |
| 51 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 52 | const char* name() const { return name_; } |
| 53 | |
| 54 | protected: |
| 55 | explicit File(const char* name) : name_(name) { } |
| 56 | virtual void Close() = 0; |
| 57 | virtual bool IsClosed() = 0; |
| 58 | |
| 59 | private: |
| 60 | const char* name_; |
| 61 | }; |
| 62 | |
| 63 | } // namespace art |
| 64 | |
| 65 | #endif // ART_SRC_FILE_H_ |