incubator-droids-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jav...@apache.org
Subject svn commit: r985466 - in /incubator/droids/trunk/droids-core/src/main/java/org/apache/droids: handle/SaveHandler.java helper/FileUtil.java
Date Sat, 14 Aug 2010 11:29:08 GMT
Author: javier
Date: Sat Aug 14 11:29:07 2010
New Revision: 985466

URL: http://svn.apache.org/viewvc?rev=985466&view=rev
Log:
Extract static method to helper class. DROIDS-90

Added:
    incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/helper/FileUtil.java
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=985466&r1=985465&r2=985466&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
Sat Aug 14 11:29:07 2010
@@ -26,6 +26,7 @@ import java.net.URI;
 
 import org.apache.droids.api.ContentEntity;
 import org.apache.droids.api.Handler;
+import org.apache.droids.helper.FileUtil;
 
 /**
  * Handler which is writing the stream to the file system.
@@ -38,107 +39,92 @@ import org.apache.droids.api.Handler;
  * 
  */
 public class SaveHandler extends WriterHandler implements Handler {
+  private String outputDir = null;
 
-    private String outputDir = null;
+  private URI uri = null;
 
-    private URI uri = null;
+  private boolean includeHost = false;
+  protected int bufferSize = 8192;
 
-    private boolean includeHost = false;
-    protected int bufferSize = 8192;
+  public void handle(URI uri, ContentEntity entity) throws IOException {
+    this.uri = uri;
+    InputStream instream = entity.obtainContent();
+    try {
+      writeOutput(instream);
+    } finally {
+      instream.close();
+    }
+  }
+
+  private void writeOutput(InputStream stream) throws IOException {
+    if (!uri.getPath().endsWith("/")) {
+      byte[] buffer = new byte[bufferSize];
+      int length = -1;
+
+      String file = outputDir;
+      if (includeHost) {
+        file += uri.getHost() + uri.getPath();
+      } else {
+        file += uri.getPath().substring(1);
+      }
+      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));
 
-    public void handle(URI uri, ContentEntity entity) throws IOException {
-        this.uri = uri;
-        InputStream instream = entity.obtainContent();
-        try {
-            writeOutput(instream);
-        } finally {
-            instream.close();
+        while ((length = stream.read(buffer)) > -1) {
+          output.write(buffer, 0, length);
         }
-    }
-
-    private void writeOutput(InputStream stream) throws IOException {
-        if (!uri.getPath().endsWith("/")) {
-            byte[] buffer = new byte[bufferSize];
-            int length = -1;
-
-            String file = outputDir;
-            if (includeHost) {
-                file += uri.getHost() + uri.getPath();
-            } else {
-                file += uri.getPath().substring(1);
-            }
-            log.info("Trying to save " + uri + " to " + file);
-            File cache = new File(file);
-            OutputStream output = null;
-            try {
-                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();
-                }
-            }
-        }
-    }
 
-    public static void createFile(File cache) throws IOException {
-        if (!cache.isDirectory() && !cache.getAbsolutePath().endsWith("/")) {
-            try {
-                cache.createNewFile();
-            } catch (IOException e) {
-                // if we cannot create a file that means that the parent path
-                // does not exists
-                File path = new File(cache.getParent());
-                path.mkdirs();
-                cache.createNewFile();
-            }
+      } catch (IOException e) {
+        throw e;
+      } finally {
+        if (null != output) {
+          output.flush();
+          output.close();
         }
+      }
     }
+  }
 
-    /**
-     * Get the directory where we want to save the stream.
-     * 
-     * @return directory where we want to save the stream.
-     */
-    public String getOutputDir() {
-        return outputDir;
-    }
-
-    /**
-     * Set the directory where we want to save the stream.
-     * 
-     * @param outputDir
-     *            the directory where we want to save the stream.
-     */
-    public void setOutputDir(String outputDir) {
-        this.outputDir = outputDir;
-    }
-
-    /**
-     * Do we want to prefix the export dir with the host name.
-     * 
-     * @return true if we want to use the prefix; false otherwise
-     */
-    public boolean isIncludeHost() {
-        return includeHost;
-    }
-
-    /**
-     * Do we want to prefix the export dir with the host name.
-     * 
-     * @param includeHost
-     *            true if we want to use the prefix; false otherwise
-     */
-    public void setIncludeHost(boolean includeHost) {
-        this.includeHost = includeHost;
-    }
+  /**
+   * Get the directory where we want to save the stream.
+   * 
+   * @return directory where we want to save the stream.
+   */
+  public String getOutputDir() {
+    return outputDir;
+  }
+
+  /**
+   * Set the directory where we want to save the stream.
+   * 
+   * @param outputDir
+   *          the directory where we want to save the stream.
+   */
+  public void setOutputDir(String outputDir) {
+    this.outputDir = outputDir;
+  }
+
+  /**
+   * Do we want to prefix the export dir with the host name.
+   * 
+   * @return true if we want to use the prefix; false otherwise
+   */
+  public boolean isIncludeHost() {
+    return includeHost;
+  }
+
+  /**
+   * Do we want to prefix the export dir with the host name.
+   * 
+   * @param includeHost
+   *          true if we want to use the prefix; false otherwise
+   */
+  public void setIncludeHost(boolean includeHost) {
+    this.includeHost = includeHost;
+  }
 
 }

Added: incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/helper/FileUtil.java
URL: http://svn.apache.org/viewvc/incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/helper/FileUtil.java?rev=985466&view=auto
==============================================================================
--- incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/helper/FileUtil.java
(added)
+++ incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/helper/FileUtil.java
Sat Aug 14 11:29:07 2010
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.droids.helper;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * This is an utility class for file related operations. 
+ */
+public final class FileUtil {
+
+  private FileUtil() {
+    throw new AssertionError();
+  }
+
+  public static void createFile(File cache) throws IOException {
+    if (!cache.isDirectory() && !cache.getAbsolutePath().endsWith("/")) {
+      try {
+        cache.createNewFile();
+      } catch (IOException e) {
+        // if we cannot create a file that means that the parent path
+        // does not exists
+        final File path = new File(cache.getParent());
+        path.mkdirs();
+        cache.createNewFile();
+      }
+    }
+  }
+
+}



Mime
View raw message