📄 sitefilefetch.java
字号:
/**
* SiteFileFetch.java
*
* There are five classes:
* SiteFileFetch.java: for whole file fetch and control the threads of FileSplitterFetch
* FileSplitterFetch.java: for fetch partials of given file
* FileAccess.java: for file storage
* SiteInfoBean.java: for information storage
* Utility.java: tools such as log
*
*/
package NetFox;
import java.io.*;
import javax.microedition.io.file.*;
import javax.microedition.io.*;
import FileUtils;
public class SiteFileFetch extends Thread {
SiteInfoBean siteInfoBean = null;
long[] m_nOrgStartPos;
long[] nStartPos;
long[] nEndPos;
FileSplitterFetch[] fileSplitterFetch;
long nFileLength;
boolean bFirst = true; // 是否第一次取文件
boolean bStop = false; // 停止标志
FileConnection tmpFile; // 文件下载的临时信息
FileSelector fSel = null;
public SiteFileFetch(SiteInfoBean bean, FileSelector fSel) throws IOException {
this.fSel = fSel;
bean.printBean();
siteInfoBean = bean;
String tmpFn = bean.getSFilePath() + bean.getSFileName() + ".info";
Utility.log("check file " + tmpFn + "\n");
tmpFile = Utility.createFileConn(tmpFn);
boolean bGotPos = false;
if( tmpFile.exists() )
{
Utility.log("info file exists, read pos\n");
bGotPos = read_nPos();
}
if( !bGotPos )
{
Utility.log("info file does NOT exist, init new one\n");
tmpFile.create();
m_nOrgStartPos = new long[bean.getNSplitter()];
nStartPos = new long[bean.getNSplitter()];
nEndPos = new long[bean.getNSplitter()];
}
else
bFirst = false;
}
public void run() {
Utility.log("SiteFileFetch runs\n");
// 获得文件长度
// 分割文件
// 实例FileSplitterFetch
// 启动FileSplitterFetch线程
// 等待子线程返回
try
{
if( bFirst )
{
nFileLength = getFileSize();
if (nFileLength == -1)
{
Utility.log("File Length is not known!\n");
return;
}
else if (nFileLength == -2)
{
Utility.log("File is not access!\n");
return;
}
else
{
for (int i = 0; i < nStartPos.length; i++)
{
m_nOrgStartPos[i] = (long)(i * (nFileLength / nStartPos.length));
nStartPos[i] = m_nOrgStartPos[i];
}
for(int i = 0;i < nEndPos.length - 1; i++)
{
nEndPos[i] = nStartPos[i + 1];
}
nEndPos[nEndPos.length - 1] = nFileLength;
}
}
// start download thread
fileSplitterFetch = new FileSplitterFetch[nStartPos.length];
for (int i = 0; i < nStartPos.length; i++)
{
if( nStartPos[i] < nEndPos[i] )
{
fileSplitterFetch[i] = new FileSplitterFetch(siteInfoBean.getSSiteHost(),
siteInfoBean.getSSiteURL(),
siteInfoBean.getSFilePath() + siteInfoBean.getSFileName() + i,
m_nOrgStartPos[i],
nStartPos[i],
nEndPos[i],
i);
Utility.log("Thread " + i + ": nOrgStartPos = " + m_nOrgStartPos[i] + ", nStartPos = " + nStartPos[i] + ", nEndPos = " + nEndPos[i]);
fileSplitterFetch[i].start();
}
}
// 等待子线程结束
// int count = 0;
// 是否结束while循环
boolean breakWhile = false;
while (!bStop)
{
write_nPos();
Utility.sleep(Utility.SLEEP);
breakWhile = true;
for( int i = 0; i < nStartPos.length; i++ )
{
if( !fileSplitterFetch[i].bDownOver )
{
breakWhile = false;
break;
}
}
if(breakWhile)
break;
}
if( bStop )
{
// user stop, we need to write pos too
write_nPos();
tmpFile.close();
checkProgress(fileSplitterFetch);
Utility.log("User stopped download.");
}
else
{
Utility.log("Download finished." + FileUtils.getDefaultRoot());
String[] asFile = new String[fileSplitterFetch.length];
for( int i=0; i<asFile.length; i++ )
{
asFile[i] = siteInfoBean.getSFileName() + i;
}
FileUtils.mergeFiles(null, siteInfoBean.getSFileName(), null, asFile, 0);
Utility.log("Merge finished.");
for( int i=0; i<asFile.length; i++ )
{
FileUtils.deleteFile(null, asFile[i]);
}
Utility.log("Delete file finished.");
tmpFile.delete();
fSel.notifyProgress(true, 0, 0);
Utility.log("File download is finished.");
}
} catch(Exception e) {
Utility.log(e.getMessage());
}
}
/**
* Check download progress and notify FileSelector
*
*/
private int checkProgress(FileSplitterFetch[] fSplitter) {
int transferred = 0;
int left = 0;
for (int i = 0; i < nStartPos.length; i++) {
if (fSplitter[i].bStop) {
transferred += fSplitter[i].nStartPos;
left += fSplitter[i].nEndPos - fSplitter[i].nStartPos;
}
}
Utility.log("In checkProgress, transferred:" + transferred
+ " left:" + left);
transferred /= 1000;
left /= 1000;
fSel.notifyProgress(false, transferred, left);
return 0;
}
/**
* Get download file length
*/
public long getFileSize() {
long nFileLength = -1;
try {
HttpConnection httpConn = Utility.createHttpConn(Utility.DOWNLOAD_HOST, Utility.FILE_NAME);
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty(Utility.PROP_USER_AGENT, Utility.MOBILE_UA);
int responseCode = httpConn.getResponseCode();
if (responseCode >= 400) {
Utility.log("responseCode=" + responseCode);
return -2; // -2 represent access is error
}
nFileLength = httpConn.getLength();
Utility.log("getLength =" + nFileLength);
/*
String sHeader;
for (int i = 1 ; ; i++) {
sHeader = httpConn.getHeaderFieldKey(i);
if (sHeader != null)
{
//Utility.log(sHeader);
if (sHeader.equalsIgnoreCase("content-length"))
{
nFileLength = Integer.parseInt(httpConn.getHeaderField(sHeader));
Utility.log("content-length= " + nFileLength);
break;
}
else
Utility.log(sHeader + "=" + httpConn.getHeaderField(sHeader));
} else
break;
}
*/
httpConn.close();
} catch(IOException e) {
Utility.log(e.getMessage());
} catch(Exception e) {
Utility.log(e.getMessage());
}
Utility.log("file length === " + nFileLength);
return nFileLength;
}
/**
* Store the download file offset
*/
private void write_nPos() {
DataOutputStream output = null;
try
{
output = tmpFile.openDataOutputStream();
output.writeInt(nStartPos.length);
for (int i = 0; i < nStartPos.length; i++)
{
output.writeLong(fileSplitterFetch[i].m_nOrgStartPos);
output.writeLong(fileSplitterFetch[i].nStartPos);
output.writeLong(fileSplitterFetch[i].nEndPos);
}
output.close();
} catch (IOException e) {
Utility.log(e.getMessage());
} catch (Exception e) {
Utility.log(e.getMessage());
}
}
/**
* Read the download file offset
*
*/
private boolean read_nPos() {
DataInputStream input = null;
try
{
input = tmpFile.openDataInputStream();
int nCount = input.readInt();
m_nOrgStartPos = new long[nCount];
nStartPos = new long[nCount];
nEndPos = new long[nCount];
for (int i = 0; i < nStartPos.length; i++)
{
m_nOrgStartPos[i] = input.readLong();
nStartPos[i] = input.readLong();
nEndPos[i] = input.readLong();
}
input.close();
return true;
}
catch(IOException e)
{
Utility.log(e.getMessage());
try
{
tmpFile.truncate(0);
}
catch( IOException e1 )
{
Utility.log(e1.getMessage());
}
}
return false;
}
/**
* Stop download
*
*/
public void siteStop() {
bStop = true;
for (int i = 0; i < nStartPos.length; i++)
fileSplitterFetch[i].splitterStop();
}
/**
* Restart download
*
*/
public void siteStart() {
bStop = false;
for (int i = 0; i < nStartPos.length; i++)
fileSplitterFetch[i].splitterStart();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -