FileDocCategorySizeDatePackage
FileLoader.javaAPI DocAndroid 1.5 API4943Wed May 06 22:41:56 BST 2009android.webkit

FileLoader

public class FileLoader extends StreamLoader
This class is a concrete implementation of StreamLoader that uses a file or asset as the source for the stream.

Fields Summary
private String
mPath
private android.content.Context
mContext
private boolean
mIsAsset
private boolean
mAllowFileAccess
Constructors Summary
FileLoader(String url, LoadListener loadListener, android.content.Context context, boolean asset, boolean allowFileAccess)
Construct a FileLoader with the file URL specified as the content source.

param
url Full file url pointing to content to be loaded
param
loadListener LoadListener to pass the content to
param
context Context to use to access the asset.
param
asset true if url points to an asset.
param
allowFileAccess true if this WebView is allowed to access files on the file system.

        super(loadListener);
        mIsAsset = asset;
        mContext = context;
        mAllowFileAccess = allowFileAccess;

        // clean the Url
        int index = url.indexOf('?");
        if (mIsAsset) {
            mPath = index > 0 ? URLUtil.stripAnchor(
                    url.substring(URLUtil.ASSET_BASE.length(), index)) :
                    URLUtil.stripAnchor(url.substring(
                            URLUtil.ASSET_BASE.length()));
        } else {
            mPath = index > 0 ? URLUtil.stripAnchor(
                    url.substring(URLUtil.FILE_BASE.length(), index)) :
                    URLUtil.stripAnchor(url.substring(
                            URLUtil.FILE_BASE.length()));
        }
    
Methods Summary
protected voidbuildHeaders(android.net.http.Headers headers)

        // do nothing.
    
public static voidrequestUrl(java.lang.String url, LoadListener loadListener, android.content.Context context, boolean asset, boolean allowFileAccess)
Construct a FileLoader and instruct it to start loading.

param
url Full file url pointing to content to be loaded
param
loadListener LoadListener to pass the content to
param
context Context to use to access the asset.
param
asset true if url points to an asset.
param
allowFileAccess true if this FileLoader can load files from the file system.

        FileLoader loader = new FileLoader(url, loadListener, context, asset,
                allowFileAccess);
        loader.load();
    
protected booleansetupStreamAndSendStatus()

        try {
            if (mIsAsset) {
                try {
                    mDataStream = mContext.getAssets().open(mPath);
                } catch (java.io.FileNotFoundException ex) {
                    // try the rest files included in the package
                    mDataStream = mContext.getAssets().openNonAsset(mPath);
                }
            } else {
                if (!mAllowFileAccess) {
                    mHandler.error(EventHandler.FILE_ERROR,
                            mContext.getString(R.string.httpErrorFileNotFound));
                    return false;
                }

                mDataStream = new FileInputStream(mPath);
                mContentLength = (new File(mPath)).length();
            }
            mHandler.status(1, 1, 0, "OK");

        } catch (java.io.FileNotFoundException ex) {
            mHandler.error(
                    EventHandler.FILE_NOT_FOUND_ERROR,
                    mContext.getString(R.string.httpErrorFileNotFound) +
                    " " + ex.getMessage());
            return false;

        } catch (java.io.IOException ex) {
            mHandler.error(EventHandler.FILE_ERROR,
                           mContext.getString(R.string.httpErrorFileNotFound) +
                           " " + ex.getMessage());
            return false;
        }
        return true;