日期:2014-05-20 浏览次数:20800 次
java.io.IOException: Map failed
at sun.nio.ch.FileChannelImpl.map(Unknown Source)
at iotest.ByteBufferMap.main(ByteBufferMap.java:38)
Caused by: java.lang.OutOfMemoryError: Map failed
at sun.nio.ch.FileChannelImpl.map0(Native Method)
... 2 more
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
public class ByteBufferMap {
public static void main(String[] args) {
FileChannel fcIn = null;
FileChannel fcOut = null;
try {
String filePath = "F:\\Anime\\Gundam00\\S2\\25.mkv";//1.3G
fcIn = new FileInputStream(new File(filePath)).getChannel();
fcOut = new RandomAccessFile(new File("G:\\1.mkv"), "rw").getChannel();
IntBuffer bufOut;
IntBuffer bufIn;
long readSize = 128 * 1024 * 1024;//每次映射的大小
long s;//映射开始处
long e;//映射结束处
int i = 0;
long allSize = fcIn.size();//文件的大小
long start = System.currentTimeMillis();
do {
s = i++ * readSize;
e = s + readSize > allSize ? s + allSize % readSize : s + readSize;
bufIn = fcIn.map(FileChannel.MapMode.READ_ONLY, s, e).asIntBuffer();
bufOut = fcOut.map(FileChannel.MapMode.READ_WRITE, s, e).asIntBuffer();
while(bufIn.hasRemaining()) {
bufOut.put(bufIn.get());
}
} while(e < allSize);
long end = System.currentTimeMillis();
System.out.println("Time:" + ((double) ((end - start) / 1000)) + "s");
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
fcIn.close();
fcOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}