1 /* 2 * Copyright 2019 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 package android.media; 18 19 import android.os.ParcelFileDescriptor; 20 import android.system.ErrnoException; 21 import android.system.Os; 22 import android.system.OsConstants; 23 import android.util.Log; 24 25 import java.io.FileDescriptor; 26 import java.io.IOException; 27 28 /** 29 * A DataSourceCallback that is backed by a ParcelFileDescriptor. 30 */ 31 class ProxyDataSourceCallback extends DataSourceCallback { 32 private static final String TAG = "TestDataSourceCallback"; 33 34 ParcelFileDescriptor mPFD; 35 FileDescriptor mFD; 36 ProxyDataSourceCallback(ParcelFileDescriptor pfd)37 ProxyDataSourceCallback(ParcelFileDescriptor pfd) throws IOException { 38 mPFD = pfd.dup(); 39 mFD = mPFD.getFileDescriptor(); 40 } 41 42 @Override readAt(long position, byte[] buffer, int offset, int size)43 public synchronized int readAt(long position, byte[] buffer, int offset, int size) 44 throws IOException { 45 try { 46 Os.lseek(mFD, position, OsConstants.SEEK_SET); 47 int ret = Os.read(mFD, buffer, offset, size); 48 return (ret == 0) ? END_OF_STREAM : ret; 49 } catch (ErrnoException e) { 50 throw new IOException(e); 51 } 52 } 53 54 @Override getSize()55 public synchronized long getSize() throws IOException { 56 return mPFD.getStatSize(); 57 } 58 59 @Override close()60 public synchronized void close() { 61 try { 62 mPFD.close(); 63 } catch (IOException e) { 64 Log.e(TAG, "Failed to close the PFD.", e); 65 } 66 } 67 } 68 69