You are viewing a plain text version of this content. The canonical link for it is here.
Posted to droids-commits@incubator.apache.org by bc...@apache.org on 2011/02/21 18:50:23 UTC

svn commit: r1073109 - /incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveHandler.java

Author: bchapuis
Date: Mon Feb 21 18:50:23 2011
New Revision: 1073109

URL: http://svn.apache.org/viewvc?rev=1073109&view=rev
Log:
DROIDS-125: SaveHandler - clean up. The logic has not been changed. It will make sonarsource happier :-). Patch: Eugen Paraschiv.

Modified:
    incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveHandler.java

Modified: incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveHandler.java
URL: http://svn.apache.org/viewvc/incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveHandler.java?rev=1073109&r1=1073108&r2=1073109&view=diff
==============================================================================
--- incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveHandler.java (original)
+++ incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveHandler.java Mon Feb 21 18:50:23 2011
@@ -18,6 +18,7 @@ package org.apache.droids.handle;
 
 import java.io.BufferedOutputStream;
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -39,12 +40,15 @@ import org.apache.droids.helper.FileUtil
  * 
  */
 public class SaveHandler extends WriterHandler implements Handler {
-  private String outputDir = null;
+  private String outputDir;
 
-  private URI uri = null;
+  private URI uri;
 
-  private boolean includeHost = false;
-  protected int bufferSize = 8192;
+  private boolean includeHost;
+
+  public SaveHandler() {
+    super();
+  }
 
   public void handle(URI uri, ContentEntity entity) throws IOException {
     this.uri = uri;
@@ -58,36 +62,43 @@ public class SaveHandler extends WriterH
 
   private void writeOutput(InputStream stream) throws IOException {
     if (!uri.getPath().endsWith("/")) {
-      byte[] buffer = new byte[bufferSize];
-      int length = -1;
+      String filePath = calculateFilePath();
+      log.info("Trying to save " + uri + " to " + filePath);
+      File cache = new File(filePath);
+      FileUtil.createFile(cache);
+      
+      writeContentToFile(stream, cache);
+    }
+  }
 
-      String file = outputDir;
-      if (includeHost) {
-        file += uri.getHost() + uri.getPath();
-      } else {
-        file += uri.getPath().substring(1);
+  private void writeContentToFile(InputStream stream, File cache) throws FileNotFoundException,
+      IOException {
+    OutputStream output = null;
+    final int bufferSize = 8192;
+    byte[] buffer = new byte[bufferSize];
+    int length = -1;
+    try {
+      output = new BufferedOutputStream(new FileOutputStream(cache));
+      while ((length = stream.read(buffer)) > -1) {
+        output.write(buffer, 0, length);
       }
-      log.info("Trying to save " + uri + " to " + file);
-      File cache = new File(file);
-      OutputStream output = null;
-      try {
-        FileUtil.createFile(cache);
-        output = new BufferedOutputStream(new FileOutputStream(cache));
-
-        while ((length = stream.read(buffer)) > -1) {
-          output.write(buffer, 0, length);
-        }
-
-      } catch (IOException e) {
-        throw e;
-      } finally {
-        if (null != output) {
-          output.flush();
-          output.close();
-        }
+    } finally {
+      if (null != output) {
+        output.flush();
+        output.close();
       }
     }
   }
+  
+  private String calculateFilePath() {
+    String filePath = outputDir;
+    if (includeHost) {
+      filePath += uri.getHost() + uri.getPath();
+    } else {
+      filePath += uri.getPath().substring(1);
+    }
+    return filePath;
+  }
 
   /**
    * Get the directory where we want to save the stream.