blob: 7e3d72b562c76de0ced138268b024410f18b3c3c [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Carlstromdb4d5402011-08-09 12:18:28 -070016
17#ifndef ART_SRC_FILE_H_
18#define ART_SRC_FILE_H_
19
20#include <stdint.h>
21#include <sys/types.h>
22
23namespace art {
24
25class 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 Hughes2a2ff562012-01-06 18:07:59 -080044 virtual off_t Length() = 0;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070045
46 // Get the current position in the file.
47 // Returns a negative value if position cannot be determined.
Elliott Hughes2a2ff562012-01-06 18:07:59 -080048 virtual off_t Position() = 0;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070049
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070050 virtual int Fd() = 0;
51
Brian Carlstromdb4d5402011-08-09 12:18:28 -070052 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_