hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1480298 - /httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/NFileEntity.java
Date Wed, 08 May 2013 14:35:28 GMT
Author: olegk
Date: Wed May  8 14:35:28 2013
New Revision: 1480298

URL: http://svn.apache.org/r1480298
Log:
Improved resource management by File backed non-blocking entities

Modified:
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/NFileEntity.java

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/NFileEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/NFileEntity.java?rev=1480298&r1=1480297&r2=1480298&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/NFileEntity.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/NFileEntity.java
Wed May  8 14:35:28 2013
@@ -32,6 +32,7 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.RandomAccessFile;
 import java.nio.channels.FileChannel;
 
 import org.apache.http.annotation.NotThreadSafe;
@@ -57,6 +58,7 @@ public class NFileEntity extends Abstrac
                          implements HttpAsyncContentProducer, ProducingNHttpEntity {
 
     private final File file;
+    private RandomAccessFile accessfile;
     private FileChannel fileChannel;
     private long idx = -1;
     private boolean useFileChannels;
@@ -128,11 +130,11 @@ public class NFileEntity extends Abstrac
      * @since 4.2
      */
     public void close() throws IOException {
-        final FileChannel local = fileChannel;
-        fileChannel = null;
-        if (local != null) {
-            local.close();
+        if (accessfile != null) {
+            accessfile.close();
         }
+        accessfile = null;
+        fileChannel = null;
     }
 
     /**
@@ -155,9 +157,11 @@ public class NFileEntity extends Abstrac
 
     public void produceContent(final ContentEncoder encoder, final IOControl ioctrl)
             throws IOException {
+        if (accessfile == null) {
+            accessfile = new RandomAccessFile(this.file, "r");
+        }
         if (fileChannel == null) {
-            final FileInputStream in = new FileInputStream(file);
-            fileChannel = in.getChannel();
+            fileChannel = accessfile.getChannel();
             idx = 0;
         }
 



Mime
View raw message