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  */
16 
17 #ifndef DATA_SOURCE_BASE_H_
18 
19 #define DATA_SOURCE_BASE_H_
20 
21 #include <sys/types.h>
22 #include <utils/Errors.h>
23 
24 namespace android {
25 
26 class String8;
27 
28 class DataSourceBase {
29 public:
30     enum Flags {
31         kWantsPrefetching      = 1,
32         kStreamedFromLocalHost = 2,
33         kIsCachingDataSource   = 4,
34         kIsHTTPBasedSource     = 8,
35         kIsLocalFileSource     = 16,
36     };
37 
DataSourceBase()38     DataSourceBase() {}
39 
40     virtual status_t initCheck() const = 0;
41 
42     // Returns the number of bytes read, or -1 on failure. It's not an error if
43     // this returns zero; it just means the given offset is equal to, or
44     // beyond, the end of the source.
45     virtual ssize_t readAt(off64_t offset, void *data, size_t size) = 0;
46 
47     // Convenience methods:
48     bool getUInt16(off64_t offset, uint16_t *x);
49     bool getUInt24(off64_t offset, uint32_t *x); // 3 byte int, returned as a 32-bit int
50     bool getUInt32(off64_t offset, uint32_t *x);
51     bool getUInt64(off64_t offset, uint64_t *x);
52 
53     // read either int<N> or int<2N> into a uint<2N>_t, size is the int size in bytes.
54     bool getUInt16Var(off64_t offset, uint16_t *x, size_t size);
55     bool getUInt32Var(off64_t offset, uint32_t *x, size_t size);
56     bool getUInt64Var(off64_t offset, uint64_t *x, size_t size);
57 
58     // May return ERROR_UNSUPPORTED.
59     virtual status_t getSize(off64_t *size);
60 
61     virtual bool getUri(char *uriString, size_t bufferSize);
62 
flags()63     virtual uint32_t flags() {
64         return 0;
65     }
66 
close()67     virtual void close() {};
68 
69 protected:
~DataSourceBase()70     virtual ~DataSourceBase() {}
71 
72 private:
73     DataSourceBase(const DataSourceBase &);
74     DataSourceBase &operator=(const DataSourceBase &);
75 };
76 
77 }  // namespace android
78 
79 #endif  // DATA_SOURCE_BASE_H_
80