数组,transferFrom,ByteBuffer数据传输对比

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 block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.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


你可能感兴趣的:(java)