数组,transferFrom,ByteBuffer数据传输对比
来源:互联网 发布:ubuntu关闭网卡 编辑:程序博客网 时间:2024/05/29 02:50
package cache;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.OutputStream;import java.io.RandomAccessFile;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;import java.util.Random;public class CacheTest {public static void main(String args[]) throws IOException {File inFile = new File("f:\\test\\CopyOfBlockCache.java");File outFile = new File("f:\\test\\channel\\"+ (new Random().nextInt()));/* * for (int i = 0; i < 1000; i++) { getOutStream(file); } */InputStream blockIn = getBlockInputStream(inFile);FileChannel fileCh = ((FileInputStream) blockIn).getChannel();if (!outFile.getParentFile().exists())outFile.getParentFile().mkdirs();OutputStream out = getOutStream(outFile);long dataLength = 0;long size = inFile.length();long everLong = 4 * 1024 * 1024;long tmp;long start = System.currentTimeMillis();while (dataLength < size) {tmp = ((FileOutputStream) out).getChannel().transferFrom(fileCh,dataLength, everLong);dataLength += tmp;// System.out.println("dataLength="+dataLength);// System.out.println("everLong="+everLong);// System.out.println("fileSize="+size+",dataLength="+dataLength);}long end = System.currentTimeMillis();// System.out.println("fileSize="+size+",dataLength="+dataLength+",outFile1="+outFile1.length());System.out.println("channel tranfer:" + (end - start));long start1 = System.currentTimeMillis();File outFile1 = new File("f:\\test\\array\\" + (new Random().nextInt()));if (!outFile1.getParentFile().exists())outFile1.getParentFile().mkdirs();read(inFile, outFile1);long end1 = System.currentTimeMillis();System.out.println("array tranfer:" + (end1 - start1));long start2 = System.currentTimeMillis();File outFile2 = new File("f:\\test\\bytebuffer\\"+ (new Random().nextInt()));if (!outFile2.getParentFile().exists())outFile2.getParentFile().mkdirs();readByteBuffer(inFile, outFile2);long end2 = System.currentTimeMillis();System.out.println("readByteBuffer tranfer:" + (end2 - start2));}public static InputStream getBlockInputStream(File srcFile)throws IOException {RandomAccessFile blockInFile;try {blockInFile = new RandomAccessFile(srcFile, "r");} catch (FileNotFoundException fnfe) {throw new IOException("Expected block file at " + srcFile+ " does not exist.");}return new FileInputStream(blockInFile.getFD());}public static OutputStream getOutStream(File targetFile) {FileOutputStream blockOut = null;try {blockOut = new FileOutputStream(new RandomAccessFile(targetFile,"rw").getFD());} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return blockOut;}public static void read(File srcFile, File desFile) throws IOException {OutputStream out = getOutStream(desFile);InputStream in = getBlockInputStream(srcFile);byte buf[] = new byte[4 * 1024 * 1024];int dataLen = 0;int totalSize = 0;dataLen = in.read(buf, 0, buf.length);while (dataLen != -1) {totalSize += dataLen;out.write(buf, 0, dataLen);dataLen = in.read(buf, 0, buf.length);}}public static void readByteBuffer(File srcFile, File desFile)throws IOException {FileChannel out = ((FileOutputStream) getOutStream(desFile)).getChannel();FileChannel in = ((FileInputStream) getBlockInputStream(srcFile)).getChannel();ByteBuffer buf = ByteBuffer.allocate(4 * 1024 * 1024);while (in.read(buf) != -1) {buf.flip(); // 准备写out.write(buf);buf.clear(); // 准备读}}}
读取4KB小文件:
channel transfer:1
array transfer:9
readByteBuffer transfer:8
读取206MB的大文件:
channel transfer:7118
array transfer:6298
readByteBuffer transfer:12148
- 数组,transferFrom,ByteBuffer数据传输对比
- bytebuffer 与 byte数组 转化
- ByteBuffer写入数组BufferUnderflowException异常
- FileChannel、ByteBuffer对文件操作过程对比
- (Mina的ByteBuffer和java.nio.ByteBuffer性能对比)Performance comparision of Mina's ByteBuffer VS java.nio.ByteBuffer use.
- ByteBuffer
- ByteBuffer
- ByteBuffer
- ByteBuffer
- ByteBuffer
- ByteBuffer
- ByteBuffer
- ByteBuffer
- java ByteBuffer和byte 数组相互转换
- 虚拟网卡与物理网卡TCP协议数据传输对比
- Volley框架中JSON对象数据传输(string对比)
- 数据传输
- 数据传输
- 写自己想出来的东西
- IOS plist文件的增删操作
- ArcPad 10 的安装部署
- 2013年1季度中国自主A0级车品牌口碑研究报告
- eclipse 修改设置Ctrl+Shift+F长度
- 数组,transferFrom,ByteBuffer数据传输对比
- UIViewController _loadViewFromNibNamed:bundle:] loaded the "loc" nib but the view outlet was not set
- CentOS6.2安装配置tomcat
- 录音 - 静音检测(转)
- Hibernate进行单向Many-to-Many映射时如果只删除一方及中间表,切忌莫加入Cascade
- 程序中提醒用户进去App Store 评分 跳转 代码
- sqlserver,oracle,mysql等的driver驱动,url怎么写
- android 读取doc文档
- UVa:490 - Rotating Sentences