Android Open Source - epgreader-android U R L Cache






From Project

Back to project page epgreader-android.

License

The source code is released under:

Apache License

If you think the Android project epgreader-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.jeffpalm.android.util.urls;
//from w w w. j  av a 2s . c o  m
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import android.content.Context;
import android.util.Log;

import com.jeffpalm.android.epg.app.BuildConfig;
import com.jeffpalm.android.util.Asserts;
import com.jeffpalm.android.util.Util;
import com.jeffpalm.android.util.urls.DatabaseHandler.CachedUrlResult;

public final class URLCache {

  private static final String TAG = "URLCache";

  final DatabaseHandler dbHandler;
  private final long cacheLifetimeMilis;
  private AtomicBoolean initted = new AtomicBoolean(false);

  public URLCache(Context context, long cacheLifetimeMilis) {
    this.dbHandler = new DatabaseHandler(context);
    this.cacheLifetimeMilis = cacheLifetimeMilis;
  }

  public void init() {
    if (!initted.getAndSet(true)) {
      dbHandler.open();
    }
  }

  /**
   * Blocking get of the input stream for the url.
   * 
   * @param url URL from which to read
   * @param force
   * @param timeout timeout in millis, ignored if null
   * @return input stream for the url
   * @throws IOException
   * @throws IllegalStateException
   */
  public InputStream openStream(final String url, boolean force, Integer timeout)
      throws IllegalStateException, IOException {
    IllegalStateException caughtIllegalStateException = null;
    IOException caughtIOException = null;
    try {
      return openStreamInternal(url, force, timeout);
    } catch (IllegalStateException e) {
      caughtIllegalStateException = e;
    } catch (IOException e) {
      caughtIOException = e;
    }
    // If an exception occured and we were forcing the download, get it from the
    // cache.
    if (force) {
      Log.d(TAG, "Could not download, so getting from the cache.");
      return getInputStreamFromCache(url);
    }
    // Rethrow the original exception if we couldn't get a value from the cache.
    if (caughtIllegalStateException != null) {
      throw caughtIllegalStateException;
    }
    if (caughtIOException != null) {
      throw caughtIOException;
    }
    throw new RuntimeException("Couldn't download or retrieve " + url);
  }

  public InputStream openStreamInternal(final String url, boolean force, Integer timeout)
      throws IllegalStateException, IOException {
    init();
    Log.d(TAG, String.format("Looking up %s", url));
    if (!force) {
      InputStream inputStream = getInputStreamFromCache(url);
      if (inputStream != null) {
        return inputStream;
      }
    } else {
      Log.d(TAG, "Forcing download");
    }
    Log.d(TAG, "Opening stream");
    return new WriteToDatabaseOnCloseInputStream(url, Util.openStream(url, timeout), dbHandler);
  }

  private InputStream getInputStreamFromCache(String url) {
    DatabaseHandler.CachedUrlResult urlResult = dbHandler.getUrlResult(url);
    if (urlResult != null) {
      long diff = DatabaseHandler.now() - urlResult.getTimeMillis();
      Log.d(TAG, "Cached result updated " + diff + "ms ago cacheLifetimeMilis="
          + cacheLifetimeMilis);
      if (diff < cacheLifetimeMilis) {
        Log.d(TAG, "Found a cached result");
        String body = urlResult.getBody();
        if (body != null) {
          return new StringInputStream(body);
        }
      } else {
        Log.d(TAG, "No cached result");
      }
    }
    return null;
  }

  public int getNumEntries() {
    init();
    return dbHandler.getNumEntries();
  }
}




Java Source Code List

com.example.android.imagedownloader.ImageDownloader.java
com.jeffpalm.android.epg.AbstractEPGItem.java
com.jeffpalm.android.epg.AbstractEPGNode.java
com.jeffpalm.android.epg.EPGContentTest.java
com.jeffpalm.android.epg.EPGContent.java
com.jeffpalm.android.epg.EPGIndexTest.java
com.jeffpalm.android.epg.EPGIndex.java
com.jeffpalm.android.epg.EPGItemTestCase.java
com.jeffpalm.android.epg.EPGItem.java
com.jeffpalm.android.epg.EPGLinkItemTest.java
com.jeffpalm.android.epg.EPGLinkItem.java
com.jeffpalm.android.epg.EPGNode.java
com.jeffpalm.android.epg.EPGParserTest.java
com.jeffpalm.android.epg.EPGParser.java
com.jeffpalm.android.epg.EPGSectionTest.java
com.jeffpalm.android.epg.EPGSection.java
com.jeffpalm.android.epg.EPGTestUtil.java
com.jeffpalm.android.epg.EPG.java
com.jeffpalm.android.epg.app.DefaultOnActivityCreatedNotifier.java
com.jeffpalm.android.epg.app.EPGApplication.java
com.jeffpalm.android.epg.app.EPGReaderConstants.java
com.jeffpalm.android.epg.app.EPGReaderFragment.java
com.jeffpalm.android.epg.app.EPGReaderItemListFragment.java
com.jeffpalm.android.epg.app.EPGReaderPreferencesActivity.java
com.jeffpalm.android.epg.app.EPGReaderSharedPrefs.java
com.jeffpalm.android.epg.app.EPGReaderUrlFinder.java
com.jeffpalm.android.epg.app.EPGReaderUtil.java
com.jeffpalm.android.epg.app.Feature.java
com.jeffpalm.android.epg.app.FeedLoader.java
com.jeffpalm.android.epg.app.HeaderFragment.java
com.jeffpalm.android.epg.app.ItemListActivity.java
com.jeffpalm.android.epg.app.ItemListAdapter.java
com.jeffpalm.android.epg.app.ItemListFragment.java
com.jeffpalm.android.epg.app.NoContentFragment.java
com.jeffpalm.android.epg.app.OnActivityCreatedNotifier.java
com.jeffpalm.android.epg.app.PhotoGalleryAdapter.java
com.jeffpalm.android.epg.app.PhotoGalleryFragment.java
com.jeffpalm.android.epg.app.PhotoItemFragment.java
com.jeffpalm.android.epg.app.VideoItemFragment.java
com.jeffpalm.android.epg.app.WebItemFragment.java
com.jeffpalm.android.tmz.model.AbstractTMZItem.java
com.jeffpalm.android.tmz.model.AbstractTMZNode.java
com.jeffpalm.android.tmz.model.DefaultTMZAdapter.java
com.jeffpalm.android.tmz.model.TMZAdapter.java
com.jeffpalm.android.tmz.model.TMZContentCache.java
com.jeffpalm.android.tmz.model.TMZContentTest.java
com.jeffpalm.android.tmz.model.TMZContent.java
com.jeffpalm.android.tmz.model.TMZFactoryHelper.java
com.jeffpalm.android.tmz.model.TMZFactoryHolder.java
com.jeffpalm.android.tmz.model.TMZFactoryTest.java
com.jeffpalm.android.tmz.model.TMZFactory.java
com.jeffpalm.android.tmz.model.TMZIndexTest.java
com.jeffpalm.android.tmz.model.TMZIndexWrapperTest.java
com.jeffpalm.android.tmz.model.TMZIndexWrapper.java
com.jeffpalm.android.tmz.model.TMZIndex.java
com.jeffpalm.android.tmz.model.TMZItemTestCase.java
com.jeffpalm.android.tmz.model.TMZItem.java
com.jeffpalm.android.tmz.model.TMZLinkItemTest.java
com.jeffpalm.android.tmz.model.TMZLinkItem.java
com.jeffpalm.android.tmz.model.TMZNode.java
com.jeffpalm.android.tmz.model.TMZSectionTest.java
com.jeffpalm.android.tmz.model.TMZSection.java
com.jeffpalm.android.tmz.model.TMZTestCase.java
com.jeffpalm.android.tmz.model.TMZTestUtil.java
com.jeffpalm.android.tmz.model.TMZTest.java
com.jeffpalm.android.tmz.model.TMZWrapperTest.java
com.jeffpalm.android.tmz.model.TMZWrapper.java
com.jeffpalm.android.tmz.model.TMZ.java
com.jeffpalm.android.util.Asserts.java
com.jeffpalm.android.util.ExpandableValueTest.java
com.jeffpalm.android.util.ExpandableValue.java
com.jeffpalm.android.util.ShiftSetTest.java
com.jeffpalm.android.util.ShiftSet.java
com.jeffpalm.android.util.TMZUrlFinderTest.java
com.jeffpalm.android.util.UtilTest.java
com.jeffpalm.android.util.Util.java
com.jeffpalm.android.util.urls.DatabaseHandler.java
com.jeffpalm.android.util.urls.DatabaseHelper.java
com.jeffpalm.android.util.urls.StringInputStream.java
com.jeffpalm.android.util.urls.URLCache.java
com.jeffpalm.android.util.urls.WriteToDatabaseOnCloseInputStream.java