hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1480278 - in /httpcomponents/httpasyncclient/trunk/httpasyncclient/src: main/java/org/apache/http/nio/client/methods/ test/java/org/apache/http/nio/client/methods/
Date Wed, 08 May 2013 13:55:43 GMT
Author: olegk
Date: Wed May  8 13:55:43 2013
New Revision: 1480278

URL: http://svn.apache.org/r1480278
Log:
Improved resource management by File backed async consumers / producers

Modified:
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseZeroCopyRequestProducer.java
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/HttpAsyncMethods.java
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyConsumer.java
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyPost.java
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyPut.java
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/nio/client/methods/TestZeroCopy.java

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseZeroCopyRequestProducer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseZeroCopyRequestProducer.java?rev=1480278&r1=1480277&r2=1480278&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseZeroCopyRequestProducer.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/BaseZeroCopyRequestProducer.java
Wed May  8 13:55:43 2013
@@ -27,8 +27,9 @@
 package org.apache.http.nio.client.methods;
 
 import java.io.File;
-import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.RandomAccessFile;
 import java.net.URI;
 import java.nio.channels.FileChannel;
 
@@ -52,18 +53,21 @@ abstract class BaseZeroCopyRequestProduc
 
     private final URI requestURI;
     private final File file;
+    private final RandomAccessFile accessfile;
     private final ContentType contentType;
 
     private FileChannel fileChannel;
     private long idx = -1;
 
     protected BaseZeroCopyRequestProducer(
-            final URI requestURI, final File file, final ContentType contentType) {
+            final URI requestURI,
+            final File file, final ContentType contentType) throws FileNotFoundException
{
         super();
         Args.notNull(requestURI, "Request URI");
         Args.notNull(file, "Source file");
         this.requestURI = requestURI;
         this.file = file;
+        this.accessfile = new RandomAccessFile(file, "r");
         this.contentType = contentType;
     }
 
@@ -93,8 +97,7 @@ abstract class BaseZeroCopyRequestProduc
     public synchronized void produceContent(
             final ContentEncoder encoder, final IOControl ioctrl) throws IOException {
         if (this.fileChannel == null) {
-            final FileInputStream in = new FileInputStream(this.file);
-            this.fileChannel = in.getChannel();
+            this.fileChannel = this.accessfile.getChannel();
             this.idx = 0;
         }
         long transferred;
@@ -130,7 +133,10 @@ abstract class BaseZeroCopyRequestProduc
     }
 
     public synchronized void close() throws IOException {
-        closeChannel();
+        try {
+            this.accessfile.close();
+        } catch (final IOException ignore) {
+        }
     }
 
 }

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/HttpAsyncMethods.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/HttpAsyncMethods.java?rev=1480278&r1=1480277&r2=1480278&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/HttpAsyncMethods.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/HttpAsyncMethods.java
Wed May  8 13:55:43 2013
@@ -27,6 +27,7 @@
 package org.apache.http.nio.client.methods;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.UnsupportedEncodingException;
 import java.net.URI;
 
@@ -179,28 +180,28 @@ public final class HttpAsyncMethods {
     public static HttpAsyncRequestProducer createZeroCopyPost(
             final URI requestURI,
             final File content,
-            final ContentType contentType) {
+            final ContentType contentType) throws FileNotFoundException {
         return new ZeroCopyPost(requestURI, content, contentType);
     }
 
     public static HttpAsyncRequestProducer createZeroCopyPost(
             final String requestURI,
             final File content,
-            final ContentType contentType) {
+            final ContentType contentType) throws FileNotFoundException {
         return new ZeroCopyPost(URI.create(requestURI), content, contentType);
     }
 
     public static HttpAsyncRequestProducer createZeroCopyPut(
             final URI requestURI,
             final File content,
-            final ContentType contentType) {
+            final ContentType contentType) throws FileNotFoundException {
         return new ZeroCopyPut(requestURI, content, contentType);
     }
 
     public static HttpAsyncRequestProducer createZeroCopyPut(
             final String requestURI,
             final File content,
-            final ContentType contentType) {
+            final ContentType contentType) throws FileNotFoundException {
         return new ZeroCopyPut(URI.create(requestURI), content, contentType);
     }
 
@@ -208,7 +209,8 @@ public final class HttpAsyncMethods {
         return new BasicAsyncResponseConsumer();
     }
 
-    public static HttpAsyncResponseConsumer<HttpResponse> createZeroCopyConsumer(final
File file) {
+    public static HttpAsyncResponseConsumer<HttpResponse> createZeroCopyConsumer(
+            final File file) throws FileNotFoundException {
         return new ZeroCopyConsumer<HttpResponse>(file) {
 
             @Override

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyConsumer.java?rev=1480278&r1=1480277&r2=1480278&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyConsumer.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyConsumer.java
Wed May  8 13:55:43 2013
@@ -27,8 +27,9 @@
 package org.apache.http.nio.client.methods;
 
 import java.io.File;
-import java.io.FileOutputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.RandomAccessFile;
 import java.nio.channels.FileChannel;
 
 import org.apache.http.HttpEntity;
@@ -47,18 +48,20 @@ import org.apache.http.util.Asserts;
 public abstract class ZeroCopyConsumer<T> extends AbstractAsyncResponseConsumer<T>
{
 
     private final File file;
+    private final RandomAccessFile accessfile;
 
     private HttpResponse response;
     private ContentType contentType;
     private FileChannel fileChannel;
     private long idx = -1;
 
-    public ZeroCopyConsumer(final File file) {
+    public ZeroCopyConsumer(final File file) throws FileNotFoundException {
         super();
         if (file == null) {
             throw new IllegalArgumentException("File may nor be null");
         }
         this.file = file;
+        this.accessfile = new RandomAccessFile(this.file, "rw");
     }
 
     @Override
@@ -70,7 +73,7 @@ public abstract class ZeroCopyConsumer<T
     protected void onEntityEnclosed(
             final HttpEntity entity, final ContentType contentType) throws IOException {
         this.contentType = contentType;
-        this.fileChannel = new FileOutputStream(this.file).getChannel();
+        this.fileChannel = this.accessfile.getChannel();
         this.idx = 0;
     }
 
@@ -91,7 +94,6 @@ public abstract class ZeroCopyConsumer<T
         }
         if (decoder.isCompleted()) {
             this.fileChannel.close();
-            this.fileChannel = null;
         }
     }
 
@@ -108,12 +110,9 @@ public abstract class ZeroCopyConsumer<T
 
     @Override
     protected void releaseResources() {
-        if (this.fileChannel != null) {
-            try {
-                this.fileChannel.close();
-            } catch (final IOException ignore) {
-            }
-            this.fileChannel = null;
+        try {
+            this.accessfile.close();
+        } catch (final IOException ignore) {
         }
     }
 

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyPost.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyPost.java?rev=1480278&r1=1480277&r2=1480278&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyPost.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyPost.java
Wed May  8 13:55:43 2013
@@ -27,6 +27,7 @@
 package org.apache.http.nio.client.methods;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.net.URI;
 
 import org.apache.http.HttpEntity;
@@ -36,11 +37,17 @@ import org.apache.http.entity.ContentTyp
 
 public class ZeroCopyPost extends BaseZeroCopyRequestProducer {
 
-    public ZeroCopyPost(final URI requestURI, final File content, final ContentType contentType)
{
+    public ZeroCopyPost(
+            final URI requestURI,
+            final File content,
+            final ContentType contentType) throws FileNotFoundException {
         super(requestURI, content, contentType);
     }
 
-    public ZeroCopyPost(final String requestURI, final File content, final ContentType contentType)
{
+    public ZeroCopyPost(
+            final String requestURI,
+            final File content,
+            final ContentType contentType) throws FileNotFoundException {
         super(URI.create(requestURI), content, contentType);
     }
 

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyPut.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyPut.java?rev=1480278&r1=1480277&r2=1480278&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyPut.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/ZeroCopyPut.java
Wed May  8 13:55:43 2013
@@ -27,6 +27,7 @@
 package org.apache.http.nio.client.methods;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.net.URI;
 
 import org.apache.http.HttpEntity;
@@ -36,11 +37,17 @@ import org.apache.http.entity.ContentTyp
 
 public class ZeroCopyPut extends BaseZeroCopyRequestProducer {
 
-    public ZeroCopyPut(final URI requestURI, final File content, final ContentType contentType)
{
+    public ZeroCopyPut(
+            final URI requestURI,
+            final File content,
+            final ContentType contentType) throws FileNotFoundException {
         super(requestURI, content, contentType);
     }
 
-    public ZeroCopyPut(final String requestURI, final File content, final ContentType contentType)
{
+    public ZeroCopyPut(
+            final String requestURI,
+            final File content,
+            final ContentType contentType) throws FileNotFoundException {
         super(URI.create(requestURI), content, contentType);
     }
 

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/nio/client/methods/TestZeroCopy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/nio/client/methods/TestZeroCopy.java?rev=1480278&r1=1480277&r2=1480278&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/nio/client/methods/TestZeroCopy.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/nio/client/methods/TestZeroCopy.java
Wed May  8 13:55:43 2013
@@ -28,6 +28,7 @@ package org.apache.http.nio.client.metho
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.InetSocketAddress;
@@ -172,7 +173,9 @@ public class TestZeroCopy extends HttpAs
 
         private final boolean forceChunking;
 
-        protected TestZeroCopyPost(final String requestURI, final boolean forceChunking)
{
+        protected TestZeroCopyPost(
+                final String requestURI,
+                final boolean forceChunking) throws FileNotFoundException {
             super(URI.create(requestURI), TEST_FILE, ContentType.create("text/plain"));
             this.forceChunking = forceChunking;
         }
@@ -194,7 +197,7 @@ public class TestZeroCopy extends HttpAs
 
     static class TestZeroCopyConsumer extends ZeroCopyConsumer<Integer> {
 
-        public TestZeroCopyConsumer(final File file) {
+        public TestZeroCopyConsumer(final File file) throws FileNotFoundException {
             super(file);
         }
 



Mime
View raw message