/** Commit the load. It should be ok to call repeatedly but only before tearDown is called. */
  private void commitLoad() {
    if (mCancelled) return;

    // Give the data to WebKit now
    PerfChecker checker = new PerfChecker();
    ByteArrayBuilder.Chunk c;
    while (true) {
      c = mDataBuilder.getFirstChunk();
      if (c == null) break;

      if (c.mLength != 0) {
        if (mCacheResult != null) {
          try {
            mCacheResult.outStream.write(c.mArray, 0, c.mLength);
          } catch (IOException e) {
            mCacheResult = null;
          }
        }
        nativeAddData(c.mArray, c.mLength);
      }
      mDataBuilder.releaseChunk(c);
      checker.responseAlert("res nativeAddData");
    }
  }
  /*
   * Perform the actual redirection. This involves setting up the new URL,
   * informing WebCore and then telling the Network to start loading again.
   */
  private void doRedirect() {
    // as cancel() can cancel the load before doRedirect() is
    // called through handleMessage, needs to check to see if we
    // are canceled before proceed
    if (mCancelled) {
      return;
    }

    String redirectTo = mHeaders.getLocation();
    if (redirectTo != null) {
      int nativeResponse = createNativeResponse();
      redirectTo = nativeRedirectedToUrl(mUrl, redirectTo, nativeResponse);
      // nativeRedirectedToUrl() may call cancel(), e.g. when redirect
      // from a https site to a http site, check mCancelled again
      if (mCancelled) {
        return;
      }
      if (redirectTo == null) {
        Log.d(LOGTAG, "Redirection failed for " + mHeaders.getLocation());
        cancel();
        return;
      } else if (!URLUtil.isNetworkUrl(redirectTo)) {
        final String text =
            mContext.getString(com.android.internal.R.string.open_permission_deny)
                + "\n"
                + redirectTo;
        nativeAddData(text.getBytes(), text.length());
        nativeFinished();
        clearNativeLoader();
        return;
      }

      if (mOriginalUrl == null) {
        mOriginalUrl = mUrl;
      }

      // Cache the redirect response
      if (mCacheResult != null) {
        if (getErrorID() == OK) {
          CacheManager.saveCacheFile(mUrl, mCacheResult);
        }
        mCacheResult = null;
      }

      setUrl(redirectTo);

      // Redirect may be in the cache
      if (mRequestHeaders == null) {
        mRequestHeaders = new HashMap<String, String>();
      }
      if (!checkCache(mRequestHeaders)) {
        // mRequestHandle can be null when the request was satisfied
        // by the cache, and the cache returned a redirect
        if (mRequestHandle != null) {
          mRequestHandle.setupRedirect(redirectTo, mStatusCode, mRequestHeaders);
        } else {
          String method = mMethod;

          if (method == null) {
            return;
          }

          Network network = Network.getInstance(getContext());
          network.requestURL(method, mRequestHeaders, mPostData, this, mIsHighPriority);
        }
      }
    } else {
      cancel();
    }

    if (Config.LOGV) {
      Log.v(LOGTAG, "LoadListener.onRedirect(): redirect to: " + redirectTo);
    }
  }