示例#1
0
  public void run() {

    int read = -1,
        size = 0,
        max =
            Integer.parseInt(
                System.getSettings().getString("PREF_HTTP_MAX_BUFFER_SIZE", "10485760"));
    byte[] chunk = new byte[CHUNK_SIZE];

    try {
      while ((read = mReader.read(chunk, 0, CHUNK_SIZE)) > 0 && size < max) {
        mBuffer.append(chunk, read);

        size += read;
      }

      if (mBuffer.isEmpty() == false) {
        // do we have html ?
        if (mBuffer.indexOf(CONTENT_TEXT_HTML) != -1) {
          // split headers and body, then apply the filter
          String data = mBuffer.toString();
          String[] split = data.split(HEAD_SEPARATOR, 2);
          String headers = split[0], body = (split.length > 1 ? split[1] : ""), patched = "";

          body = mFilter.onHtmlReceived(body);

          // remove explicit content length, just in case the body changed after filtering
          for (String header : headers.split("\n")) {
            if (header.toLowerCase().contains("content-length") == false) patched += header + "\n";
          }

          headers = patched;
          mBuffer.setData((headers + HEAD_SEPARATOR + body).getBytes());
        }

        mWriter.write(mBuffer.getData());
        mWriter.flush();
      }
    } catch (OutOfMemoryError ome) {
      Log.e(TAG, ome.toString());
    } catch (Exception e) {
      System.errorLogging(TAG, e);
    } finally {
      try {
        mWriter.flush();
        mWriter.close();
        mReader.close();
      } catch (IOException e) {
        System.errorLogging(TAG, e);
      }
    }
  }