破解各大视频播放器视频播放的秘密,————文件的分割与利用Vector字节合并流来保证边缓存边看保证流畅度,来直接上代码,代码中有注释

package com.vince;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;

public class FileDivisionMergeDemo {

	/**
	 * 文件的分割
	 * 
	 * tarfetFIle要分割的目标文件
	 * cutSize每个文件大小
	 * @param args
	 */
	private static void division(File targetFile,long cutSize) {//类似火车分货,文件分割
		if(targetFile==null)
			return;
		//num计算总分割的文件数,相当于把货物计划分到几个仓库
		int num = targetFile.length()%cutSize==0?
				(int)(targetFile.length()/cutSize):(int)(targetFile.length()/cutSize+1);

				try {
				BufferedInputStream in = new BufferedInputStream(new FileInputStream(targetFile));
				BufferedOutputStream out = null;
				byte[] bytes = null;//每次要读的字节数,每次转移 几箱货物
				int len = -1;//每次实际读取的长度,工人每次搬运的货物
				int count = 0;//每一个文件要读的次数,一车货物需要转移几次
				//循环次数为生成文件的个数
				for(int i=0;i<num;i++) {
					out = new BufferedOutputStream(
							new FileOutputStream(new File("c:\\test\\my\\"+(i+1)+"-temp-av-"+targetFile.getName())));
					if(cutSize<=1024) {//如果剩余不足一次工人搬运
						bytes = new byte[(int)cutSize];
						count = 1;
					}else {
						bytes = new byte[1024];
						count = (int)cutSize/1024;
					}
					while(count>0 &&(len=in.read(bytes))!=-1) {
						out.write(bytes,0,len);
						out.flush();
						count--;
					}
					//计算每个文件大小除于1024的余数,决定是否要再读一次
					if(cutSize%1024!=0) {
						bytes = new byte[(int)cutSize%1024];
						len = in.read(bytes);
						out.write(bytes,0,len);
						out.flush();
						out.close();
					}
				}
				in.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	/**
	 * 文件的合并
	 * @param args
	 */
	private static void merge(Enumeration<InputStream> es) {
		//构造一个合并流
		//SequenceInputStream可以查一下API大概意思是可以保障输入流的逻辑串联,
		//从第一个开始读取到最后一个结束,而sis接受的就是Vector整理好的视频地址流es
		SequenceInputStream sis = new SequenceInputStream(es);
		try {
			BufferedOutputStream bos = new BufferedOutputStream(
					new FileOutputStream("c:\\test\\my\\avtest.mp4"));
			byte[] bytes = new byte[1024];
			int len = -1;
			while((len=sis.read(bytes))!=-1) {
				bos.write(bytes,0,len);
				bos.flush();
			}
			bos.close();//关闭流
			sis.close();//关闭流
			System.out.println("合并完成啦~");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
//		File file = new File("c:\\test\\av.mp4");
//		division(file,1024*1024);
		try {
			//new FileInputStream相当于把文件地址传入到InputStream的 in 中
			InputStream in1 = new FileInputStream(new File("c:\\test\\my\\1-temp-av-av.mp4"));
			InputStream in2 = new FileInputStream(new File("c:\\test\\my\\2-temp-av-av.mp4"));
			InputStream in3 = new FileInputStream(new File("c:\\test\\my\\3-temp-av-av.mp4"));
			InputStream in4 = new FileInputStream(new File("c:\\test\\my\\4-temp-av-av.mp4"));
			InputStream in5 = new FileInputStream(new File("c:\\test\\my\\5-temp-av-av.mp4"));
			
			//集合工具类,把五个仓库的地址add到一个数组中,以数组的形式保存
			//Vector类实现了可扩展的对象数组。 像数组一样,它包含可以使用整数索引访问的组件。
			Vector<InputStream> v = new Vector<InputStream>();
			v.add(in1);
			v.add(in2);
			v.add(in3);
			v.add(in4);
			v.add(in5);
			//选用Vector使用为Vector集合中有一个方法v.elements()可以得到集合中的所有项。
			Enumeration<InputStream> es = v.elements();
			merge(es);//向构造方法中传入es
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	private static void meige(Enumeration<InputStream> es) {
		// TODO Auto-generated method stub
		
	}

}





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的程序员!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值