[Udt-java-commits] SF.net SVN: udt-java:[38] udt-java/trunk/src/main/java/udt/util/SendFile. java
Status: Alpha
Brought to you by:
bschuller
From: <bsc...@us...> - 2010-05-24 20:48:56
|
Revision: 38 http://udt-java.svn.sourceforge.net/udt-java/?rev=38&view=rev Author: bschuller Date: 2010-05-24 20:48:49 +0000 (Mon, 24 May 2010) Log Message: ----------- experiment with memory-mapped file Modified Paths: -------------- udt-java/trunk/src/main/java/udt/util/SendFile.java Modified: udt-java/trunk/src/main/java/udt/util/SendFile.java =================================================================== --- udt-java/trunk/src/main/java/udt/util/SendFile.java 2010-05-24 19:45:19 UTC (rev 37) +++ udt-java/trunk/src/main/java/udt/util/SendFile.java 2010-05-24 20:48:49 UTC (rev 38) @@ -34,8 +34,13 @@ import java.io.File; import java.io.FileInputStream; +import java.io.OutputStream; +import java.io.RandomAccessFile; import java.net.InetAddress; import java.nio.ByteBuffer; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.channels.FileChannel.MapMode; import java.text.NumberFormat; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -120,9 +125,11 @@ private final NumberFormat format=NumberFormat.getNumberInstance(); + private final boolean memMapped; public RequestRunner(UDTSocket socket){ this.socket=socket; format.setMaximumFractionDigits(3); + memMapped=true; } public void run(){ @@ -144,7 +151,7 @@ File file=new File(new String(fileName)); System.out.println("[SendFile] File requested: '"+file.getPath()+"'"); - FileInputStream fis=new FileInputStream(file); + FileInputStream fis=null; try{ long size=file.length(); System.out.println("[SendFile] File size: "+size); @@ -152,7 +159,12 @@ out.write(PacketUtil.encode(size)); long start=System.currentTimeMillis(); //and send the file - Util.copy(fis, out, size, false); + if(memMapped){ + copyFile(file,out); + }else{ + fis=new FileInputStream(file); + Util.copy(fis, out, size, false); + } long end=System.currentTimeMillis(); System.out.println(socket.getSession().getStatistics().toString()); double rate=1000.0*size/1024/1024/(end-start); @@ -162,7 +174,7 @@ } }finally{ socket.getSender().stop(); - fis.close(); + if(fis!=null)fis.close(); } logger.info("Finished request from "+socket.getSession().getDestination()); }catch(Exception ex){ @@ -173,4 +185,17 @@ } + private static void copyFile(File file, OutputStream os)throws Exception{ + FileChannel c=new RandomAccessFile(file,"r").getChannel(); + MappedByteBuffer b=c.map(MapMode.READ_ONLY, 0, file.length()); + byte[]buf=new byte[1024*1024]; + int len=0; + while(true){ + len=Math.min(buf.length, b.remaining()); + b.get(buf, 0, len); + os.write(buf, 0, len); + if(b.remaining()==0)break; + } + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |