• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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.cts;
18 
19 import android.content.res.AssetFileDescriptor;
20 import android.media.cts.MediaPlayerTestBase.Monitor;
21 import android.media.MediaDataSource;
22 import android.util.Log;
23 
24 import java.io.FileInputStream;
25 import java.io.InputStream;
26 import java.io.IOException;
27 
28 /**
29  * A MediaDataSource that reads from a byte array for use in tests.
30  */
31 public class TestMediaDataSource extends MediaDataSource {
32     private static final String TAG = "TestMediaDataSource";
33 
34     private byte[] mData;
35 
36     private boolean mThrowFromReadAt;
37     private boolean mThrowFromGetSize;
38     private Integer mReturnFromReadAt;
39     private Long mReturnFromGetSize;
40     private boolean mIsClosed;
41 
42     // Read an asset fd into a new byte array data source. Closes afd.
fromAssetFd(AssetFileDescriptor afd)43     public static TestMediaDataSource fromAssetFd(AssetFileDescriptor afd) throws IOException {
44         try {
45             InputStream in = afd.createInputStream();
46             final int size = (int) afd.getDeclaredLength();
47             byte[] data = new byte[(int) size];
48             int writeIndex = 0;
49             int numRead = 0;
50             do {
51                 numRead = in.read(data, writeIndex, size - writeIndex);
52                 writeIndex += numRead;
53             } while (numRead >= 0);
54             return new TestMediaDataSource(data);
55         } finally {
56             afd.close();
57         }
58     }
59 
TestMediaDataSource(byte[] data)60     public TestMediaDataSource(byte[] data) {
61         mData = data;
62     }
63 
64     @Override
readAt(long position, byte[] buffer, int offset, int size)65     public synchronized int readAt(long position, byte[] buffer, int offset, int size)
66             throws IOException {
67         if (mThrowFromReadAt) {
68             throw new IOException("Test exception from readAt()");
69         }
70         if (mReturnFromReadAt != null) {
71             return mReturnFromReadAt;
72         }
73 
74         // Clamp reads past the end of the source.
75         if (position >= mData.length) {
76             return -1; // -1 indicates EOF
77         }
78         if (position + size > mData.length) {
79             size -= (position + size) - mData.length;
80         }
81         System.arraycopy(mData, (int)position, buffer, offset, size);
82         return size;
83     }
84 
85     @Override
getSize()86     public synchronized long getSize() throws IOException {
87         if (mThrowFromGetSize) {
88             throw new IOException("Test exception from getSize()");
89         }
90         if (mReturnFromGetSize != null) {
91             return mReturnFromGetSize;
92         }
93 
94         Log.v(TAG, "getSize: " + mData.length);
95         return mData.length;
96     }
97 
98     // Note: it's fine to keep using this data source after closing it.
99     @Override
close()100     public synchronized void close() {
101         Log.v(TAG, "close()");
102         mIsClosed = true;
103     }
104 
105     // Whether close() has been called.
isClosed()106     public synchronized boolean isClosed() {
107         return mIsClosed;
108     }
109 
throwFromReadAt()110     public void throwFromReadAt() {
111         mThrowFromReadAt = true;
112     }
113 
throwFromGetSize()114     public void throwFromGetSize() {
115         mThrowFromGetSize = true;
116     }
117 
returnFromReadAt(int numRead)118     public void returnFromReadAt(int numRead) {
119         mReturnFromReadAt = numRead;
120     }
121 
returnFromGetSize(long size)122     public void returnFromGetSize(long size) {
123         mReturnFromGetSize = size;
124     }
125 }
126 
127