content: /*
   * Copyright (c) 2013. wyouflf (wyouflf@gmail.com)
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package com.lidroid.xutils.http.callback;
  
  import android.text.TextUtils;
  import com.lidroid.xutils.util.IOUtils;
  import org.apache.http.HttpEntity;
  
  import java.io.*;
  
  public class FileDownloadHandler {
  
      public File handleEntity(HttpEntity entity,
                               RequestCallBackHandler callBackHandler, //这个就是HttpHandler
                               String target,
                               boolean isResume,
                               String responseFileName) throws IOException {
          if (entity == null || TextUtils.isEmpty(target)) {
              return null;
          }
  
          File targetFile = new File(target);
  
          if (!targetFile.exists()) {
              File dir = targetFile.getParentFile();
              if (dir.exists() || dir.mkdirs()) {
                  targetFile.createNewFile();
              }
          }
  
          long current = 0;
          BufferedInputStream bis = null;
          BufferedOutputStream bos = null;
          try {
              FileOutputStream fileOutputStream = null;
              if (isResume) {
                  current = targetFile.length();
                  fileOutputStream = new FileOutputStream(target, true); //以追加的方式
              } else {
                  fileOutputStream = new FileOutputStream(target);
              }
              long total = entity.getContentLength() + current;
              bis = new BufferedInputStream(entity.getContent());
              bos = new BufferedOutputStream(fileOutputStream);
  
              if (callBackHandler != null && !callBackHandler.updateProgress(total, current, true)) {
                  return targetFile;
              }
  
              byte[] tmp = new byte[4096];
              int len;
              while ((len = bis.read(tmp)) != -1) {
                  bos.write(tmp, 0, len);
                  current += len;
                  if (callBackHandler != null) {
                  	//回调， 当停止时这里直接返回，中断下载
                      if (!callBackHandler.updateProgress(total, current, false)) { 
                          return targetFile;
                      }
                  }
              }
              bos.flush();
              if (callBackHandler != null) {
                  callBackHandler.updateProgress(total, current, true);
              }
          } finally {
              IOUtils.closeQuietly(bis);
              IOUtils.closeQuietly(bos);
          }
  
        //如果是自动命名的时候
          if (targetFile.exists() && !TextUtils.isEmpty(responseFileName)) {  
              File newFile = new File(targetFile.getParent(), responseFileName);
              while (newFile.exists()) {
                  newFile = new File(targetFile.getParent(), System.currentTimeMillis() + responseFileName);
              }
              return targetFile.renameTo(newFile) ? newFile : targetFile;
          } else {
              return targetFile;
          }
      }
  
  }
  