@@ -98,9 +98,6 @@ public class PiterFMCachingDownloader {
9898 /** global object instance */
9999 public static final PiterFMCachingDownloader INSTANCE = new PiterFMCachingDownloader ();
100100
101- /** cache size */
102- private static final int READ_AHEAD_NUM = 10 ;
103-
104101 /**
105102 * a dummy non-null value for {@link CacheEntry#m_url} to ensure the entry
106103 * is not picked by {@link #getFile(String, TrackCalendar)}. null can't be
@@ -114,26 +111,14 @@ public class PiterFMCachingDownloader {
114111 private final Object lock = new Object ();
115112
116113 /** holds queued URLs */
117- private ArrayList <String > urlQueue = new ArrayList <String >(READ_AHEAD_NUM );
114+ private ArrayList <String > urlQueue = new ArrayList <String >();
118115
119116 /** holds Cache Entries */
120- private CacheEntry [] entries = new CacheEntry [ READ_AHEAD_NUM ] ;
117+ private ArrayList < CacheEntry > entries = new ArrayList < CacheEntry >() ;
121118
122119 /** current URL for download. If differs from thread's URL, then thread waits. Can be null. */
123120 private String currentUrl ;
124121
125- /** constant map of entries by path. See also {@link #byUrl(String)} */
126- private final HashMap <String , CacheEntry > byPath = new HashMap <String , CacheEntry >(READ_AHEAD_NUM );
127-
128- {
129- for (int i = 0 ; i < READ_AHEAD_NUM ; i ++) {
130- urlQueue .add (null );
131- CacheEntry entry = new CacheEntry (i );
132- entries [i ] = entry ;
133- byPath .put (entry .file .getAbsolutePath (), entry );
134- }
135- }
136-
137122 /**
138123 * Get and lock a file with the provided params. This function is
139124 * synchronous and may block forever, if 5 other files stay locked.
@@ -148,13 +133,20 @@ public String getFile(String channelId, TrackCalendar trackTime) throws Interrup
148133 String trackUrl ;
149134 // update the queue. Not queued entries may become victims, if not locked
150135 String prefix = CHANNEL_PREFIX + channelId + "/mp4/" ;
151- for (int i = 0 ;; i ++) {
136+ int cacheSize = Settings .getCacheSize ();
137+ urlQueue .clear ();
138+ urlQueue .ensureCapacity (cacheSize );
139+ for (int i = cacheSize - 1 ;; i --) {
152140 trackUrl = prefix + trackTime .asURLPart () + ".mp4" ;
153- urlQueue .set ( i , trackUrl );
154- if (i == READ_AHEAD_NUM - 1 )
141+ urlQueue .add ( trackUrl );
142+ if (i == 0 )
155143 break ;
156144 trackTime .nextTrackTime ();
157145 }
146+ entries .ensureCapacity (cacheSize );
147+ for (int i = entries .size (); i < cacheSize ; i ++) {
148+ entries .add (new CacheEntry (i ));
149+ }
158150 resumeOrCreateNextDownloadNoLock ();
159151 lock .notifyAll ();
160152
@@ -197,7 +189,8 @@ public void releaseFile(String path, boolean badFile) {
197189 Log .v (Tag , funcname + ",synchronized before, dummyNo:197" ); try {
198190 synchronized (lock ) {
199191 Log .v (Tag , funcname + ",synchronized in, dummyNo:199" );
200- CacheEntry entry = byPath .get (path );
192+ String sIndex = path .substring (path .lastIndexOf ('/' ) + 1 , path .length () - 4 );
193+ CacheEntry entry = entries .get (Integer .parseInt (sIndex ));
201194 assertNull (entry .m_fos );
202195 assertTrue (entry .omniCount > 0 );
203196 entry .omniCount --;
@@ -234,7 +227,10 @@ private void resumeOrCreateNextDownloadNoLock() {
234227
235228 // rank victim candidates. null > scheduled > complete unrefed
236229 entry = null ;
237- for (CacheEntry candidate : entries ) {
230+ // CacheEntry candidate : entries1
231+ int cacheSize = urlQueue .size ();
232+ for (int i = 0 ; i < cacheSize ; i ++) {
233+ CacheEntry candidate = entries .get (i );
238234 if (candidate .m_url == null ) {
239235 entry = candidate ; // best choice
240236 break ;
0 commit comments