⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 downloadthread.java

📁 基于J2ME
💻 JAVA
字号:
package com.yxw.util;

import com.yxw.interfaces.Cancelable;
import com.yxw.interfaces.HttpListener;
import com.yxw.interfaces.PlayListener;

public class DownloadThread extends Thread implements HttpListener,Cancelable{//用于下载音频文件,完成各段数据的下载任务以及组装成完整文件

	
	private boolean isProxy;
	private String url;
	private boolean isCancel=false;
	//private HttpListener listener=null;
	//private int dataType;
	private boolean isFinish=false;
	//private long startRange,endRange;//如果是读取音频数据必须设置这两个值
	
	private byte[] inb=null;
	private long fileSize;//单位是B
	
	private long fragmentSize=200*1024;//单位是B
	private boolean[] fragmentStatus;//存储文件每段的下载状态
	private long fragmentNum;//共有多少段
	private long currentStartRange,currentEndRange;
	private int currentFragment=0;
	private Connection singleFragment=null;//正在执行下载的段
	private boolean nextDownload=true;//第一段可以下载
	
	private long allHaveDown=0;
	private long currentFragmentDown=0;
	private long currentFragmentSize=0;
	
	private PlayThread player;
	public DownloadThread(String url,long fileSize,PlayThread player,int cacheSize){
		this.player=player;
		if(fileSize==-1){
			this.fileSize=Connection.getFileSize(url);
			
			System.out.println(this.fileSize);
		}else{
			this.fileSize=fileSize;
		}
		isProxy=true;//默认使用代理连接网络
		this.url=url;
		this.fragmentSize=cacheSize*1024;
		if(this.fragmentSize>this.fileSize){
			this.fragmentSize=this.fileSize;
			this.fragmentNum=1;
		}else{
			this.fragmentNum=this.fileSize/this.fragmentSize+1;//总段数
			
		}
		this.fragmentStatus=new boolean[(int)this.fragmentNum];
		//inb=new byte[(int)this.fragmentSize];
		
		for(int i=0;i<this.fragmentNum;i++){
			this.fragmentStatus[i]=false;//初始为未下载完毕
		}
		
		this.player.cacheByte[0]=new byte[(int)this.fragmentSize];
		this.player.cacheByte[1]=new byte[(int)this.fragmentSize];
		this.isCancel=false;
		System.out.println("fragmentSize:"+this.fragmentSize+ " fileSize:"+this.fileSize+"fragmentNum:"+this.fragmentNum);
	
	}
	public void close(){
		if(this.singleFragment!=null){
			this.singleFragment.cancel();
		}
		this.isCancel=true;
		synchronized(this){
			this.notify();
		}
		
		
		
	}
	private void nextFragment(){
		if(this.currentFragment==0){
			
			this.currentStartRange=1;
			this.currentEndRange=this.fragmentSize;
			
			this.currentFragment=1;
			if(this.fragmentNum==this.currentFragment){
				this.isFinish=true;
				//inb=new byte[(int)(this.currentEndRange-this.currentStartRange)];
				//this.player.cacheByte[0]=null;this.player.cacheByte[1]=null;
				System.gc();
				//this.player.cacheByte[0]=new byte[(int)(this.currentEndRange-this.currentStartRange)];
				//this.player.cacheByte[1]=new byte[(int)(this.currentEndRange-this.currentStartRange)];
			}
		}
		else{
			
			this.currentFragment=this.currentFragment+1;
			this.currentStartRange=this.currentEndRange+1;
			this.currentEndRange=this.currentStartRange+this.fragmentSize;
			if(this.currentEndRange>this.fileSize){
				this.currentEndRange=this.fileSize;
			}
			if(this.fragmentNum==this.currentFragment){
				this.isFinish=true;
				//inb=new byte[(int)(this.currentEndRange-this.currentStartRange)];
				//this.player.cacheByte[0]=null;this.player.cacheByte[1]=null;
				System.gc();
				//this.player.cacheByte[0]=new byte[(int)(this.currentEndRange-this.currentStartRange)];
				//this.player.cacheByte[1]=new byte[(int)(this.currentEndRange-this.currentStartRange)];
			}
			
			//inb=null;
			//System.gc();
			//inb=new byte[(int)(this.currentEndRange-this.currentStartRange)];
		}
	}
	public void run(){
		
		while(!this.isCancel&&!this.isFinish){
			
			if(!this.nextDownload){
				synchronized(this){
					try {
						System.out.println("download is waiting");
						wait();
					} catch (InterruptedException e) {
						// TODO 自动生成 catch 块
						e.printStackTrace();
					}
				}
			}
			if(this.isFinish||this.isCancel){
				System.out.println("download is stop");
				return;
			}
			this.nextFragment();
			int id=0;
			if(this.player.change){
				this.inb=this.player.cacheByte[0];
				id=0;
			}else{
				this.inb=this.player.cacheByte[1];
				id=1;
			}
			System.out.println("正在下载段 "+this.currentFragment+"cacheId:"+id);
			this.singleFragment=new Connection(this.isProxy,this.url,this.isCancel,Constant.YINPIN,this.currentStartRange,this.currentEndRange,inb,this);
            this.singleFragment.start();
            
			this.nextDownload=false;
		}
		System.out.println("download is stop");
	}
	public void onError(int code, String message) {
		// TODO 自动生成方法存根
		System.out.println("code:"+code+"-- "+message);
		PlayListener playlistener=this.player.getPlayListener();
		if(playlistener!=null){
			playlistener.onError(code, "连接失败或数据不存在!");
		}
	}

	public void onFinish(byte[] data, int size) {
		// TODO 自动生成方法存根
		//this.nextDownload=true;
		PlayListener playlistener=this.player.getPlayListener();
		if(playlistener!=null){
			playlistener.onFinish(null, size);
		}
		this.singleFragment=null;
		System.gc();
	//	System.out.println("onfinish"+size);
		this.allHaveDown+=(size+1);
		this.fragmentStatus[this.currentFragment-1]=true;//该段下载完毕
		
		if(this.player.isCurrentFinish()&&this.fragmentStatus[this.player.getCurrentFragNum()-1]){
			synchronized(this.player){
				this.player.setCurrentFinish(false);
				this.player.notify();
			}
		}
		
		if(this.player.getCurrentFragNum()==this.currentFragment&&this.currentFragment<this.fragmentNum){
			this.nextDownload=true;
			this.player.change=!this.player.change;
			synchronized(this){
				this.notify();
			}
		}
		//System.out.println("onProgress总下载进度:"+(this.allHaveDown*100)/this.fileSize+" %");

	}

	public void onProgress(int haveDown) {
		// TODO 自动生成方法存根
	//	System.out.println("onProgress该段已下载:"+(haveDown*100)/this.currentFragmentSize+" %");
		PlayListener playlistener=this.player.getPlayListener();
		if(playlistener!=null){
			playlistener.onProgress(haveDown);
		}
	}

	public void onSetSize(int size,long fileSize) {
		// TODO 自动生成方法存根

		currentFragmentSize=size;
		PlayListener playlistener=this.player.getPlayListener();
		if(playlistener!=null){
			playlistener.onSetSize(size,fileSize);//返回该段大小
			}
		System.out.println("onSetSize"+size);
	}
	public boolean isProxy() {
		return isProxy;
	}
	public void setProxy(boolean isProxy) {
		this.isProxy = isProxy;
	}
	public void cancel() {//退出播放器,需要停止下载
		// TODO 自动生成方法存根
		this.isCancel=true;
	}
	public boolean[] getFragmentStatus() {
		return fragmentStatus;
	}
	public void setFragmentStatus(boolean[] fragmentStatus) {
		this.fragmentStatus = fragmentStatus;
	}
	public long getFragmentSize() {
		return fragmentSize;
	}
	public void setFragmentSize(long fragmentSize) {
		this.fragmentSize = fragmentSize;
	}
	public byte[] getInb() {
		return inb;
	}
	public void setInb(byte[] inb) {
		this.inb = inb;
	}
	public long getFragmentNum() {
		return fragmentNum;
	}
	public void setFragmentNum(long fragmentNum) {
		this.fragmentNum = fragmentNum;
	}
	public int getCurrentFragment() {
		return currentFragment;
	}
	public void setCurrentFragment(int currentFragment) {
		this.currentFragment = currentFragment;
	}
	public boolean isNextDownload() {
		return nextDownload;
	}
	public void setNextDownload(boolean nextDownload) {
		this.nextDownload = nextDownload;
	}
	public long getCurrentEndRange() {
		return currentEndRange;
	}
	public void setCurrentEndRange(long currentEndRange) {
		this.currentEndRange = currentEndRange;
	}
	public long getCurrentStartRange() {
		return currentStartRange;
	}
	public void setCurrentStartRange(long currentStartRange) {
		this.currentStartRange = currentStartRange;
	}


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -