com.mycomm.YesHttp.core.FileDownLoadResponse Maven / Gradle / Ivy
/*
* Copyright 2018 jw362j.
*
* 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.mycomm.YesHttp.core;
import com.mycomm.YesHttp.comm.MyConstant;
import com.mycomm.YesHttp.comm.SystemUtil;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
*
* @author jw362j
*/
public abstract class FileDownLoadResponse implements Response.Listener {
private final String folderDir;
private Request.YesLog myLog;
private Response.DownLoadUpLoadListener downLoadListener;
private String theDownLoadFileName;
public FileDownLoadResponse(String folderDir, Request.YesLog log, Response.DownLoadUpLoadListener loadListener) {
this(folderDir, log, loadListener, null);
}
public FileDownLoadResponse(String folderDir, Request.YesLog log, Response.DownLoadUpLoadListener loadListener, String simpleName) {
this.theDownLoadFileName = simpleName;
this.downLoadListener = loadListener;
this.folderDir = folderDir;
this.myLog = log;
File dirTmp = new File(this.folderDir);
if (!dirTmp.exists()) {
dirTmp.mkdirs();
}
if (!folderDir.endsWith(File.separator)) {
folderDir = folderDir + File.separator;
}
}
public FileDownLoadResponse(String folderDir, Request.YesLog log) {
this(folderDir, log, null);
}
public FileDownLoadResponse(String folderDir) {
this(folderDir, null);
}
public void onResponse(InputStream response, long contentLength,Request currentRequest) {
if (myLog != null) {
myLog.LogMe("FileDownLoadResponse contentLength:" + contentLength);
}
FileOutputStream fos = null;
try {
byte[] buffer = new byte[1024];
int len = 0;
long savedLength = 0;
if (theDownLoadFileName == null || "".equals(theDownLoadFileName)) {
theDownLoadFileName = SystemUtil.getMD5(currentRequest.getmUrl().getBytes());
}
fos = new FileOutputStream(new File(folderDir, theDownLoadFileName));
float rate_;
float lastRate = 0f;
float rateDiffer;
while ((len = response.read(buffer)) > 0) {
savedLength += len;
fos.write(buffer, 0, len);
if (downLoadListener != null) {
rate_ = ((float) savedLength / (float) contentLength);
rateDiffer = rate_ - lastRate;
if (rateDiffer >= MyConstant.refRate) {
downLoadListener.onProgressing(rate_);
lastRate = rate_;
}
}
}
if (myLog != null) {
myLog.LogMe("saved size when done:" + savedLength);
}
fos.flush();
} catch (FileNotFoundException ex) {
if (myLog != null) {
myLog.LogMe("FileDownLoadResponse A,error:" + ex.getMessage());
}
} catch (IOException ex) {
myLog.LogMe("FileDownLoadResponse B,error:" + ex.getMessage());
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException ex) {
myLog.LogMe("FileDownLoadResponse C,error:" + ex.getMessage());
}
}
responseMe(theDownLoadFileName);
}
public abstract void responseMe(String newFileNameSimple);
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy