Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "MidiIoWrapper" |
| 19 | #include <utils/Log.h> |
| 20 | #include <utils/RefBase.h> |
| 21 | |
| 22 | #include <sys/stat.h> |
| 23 | #include <fcntl.h> |
| 24 | |
| 25 | #include "media/MidiIoWrapper.h" |
| 26 | |
| 27 | static int readAt(void *handle, void *buffer, int pos, int size) { |
| 28 | return ((android::MidiIoWrapper*)handle)->readAt(buffer, pos, size); |
| 29 | } |
| 30 | static int size(void *handle) { |
| 31 | return ((android::MidiIoWrapper*)handle)->size(); |
| 32 | } |
| 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | MidiIoWrapper::MidiIoWrapper(const char *path) { |
| 37 | ALOGV("MidiIoWrapper(%s)", path); |
| 38 | mFd = open(path, O_RDONLY | O_LARGEFILE); |
| 39 | mBase = 0; |
| 40 | mLength = lseek(mFd, 0, SEEK_END); |
| 41 | } |
| 42 | |
| 43 | MidiIoWrapper::MidiIoWrapper(int fd, off64_t offset, int64_t size) { |
| 44 | ALOGV("MidiIoWrapper(fd=%d)", fd); |
| 45 | mFd = dup(fd); |
| 46 | mBase = offset; |
| 47 | mLength = size; |
| 48 | } |
| 49 | |
| 50 | MidiIoWrapper::~MidiIoWrapper() { |
| 51 | ALOGV("~MidiIoWrapper"); |
| 52 | close(mFd); |
| 53 | } |
| 54 | |
| 55 | int MidiIoWrapper::readAt(void *buffer, int offset, int size) { |
| 56 | ALOGV("readAt(%p, %d, %d)", buffer, offset, size); |
| 57 | lseek(mFd, mBase + offset, SEEK_SET); |
| 58 | if (offset + size > mLength) { |
| 59 | size = mLength - offset; |
| 60 | } |
| 61 | return read(mFd, buffer, size); |
| 62 | } |
| 63 | |
| 64 | int MidiIoWrapper::size() { |
| 65 | ALOGV("size() = %d", mLength); |
| 66 | return mLength; |
| 67 | } |
| 68 | |
| 69 | EAS_FILE_LOCATOR MidiIoWrapper::getLocator() { |
| 70 | mEasFile.handle = this; |
| 71 | mEasFile.readAt = ::readAt; |
| 72 | mEasFile.size = ::size; |
| 73 | return &mEasFile; |
| 74 | } |
| 75 | |
| 76 | } // namespace android |