From 1978b0f42f9c185d09fd23e1476accea398a72d9 Mon Sep 17 00:00:00 2001
From: Jukka Zitting <jukka@apache.org>
Date: Wed, 5 Oct 2011 18:35:36 +0200
Subject: [PATCH] TIKA-605: Tika GDAL parser

Allow a default suffix to be defined for a temporary file created by TikaInputStream.getFile()
---
 .../org/apache/tika/io/TemporaryResources.java     |   55 ++++++++++++++++++-
 .../java/org/apache/tika/io/TikaInputStream.java   |   39 +++++++++++---
 2 files changed, 83 insertions(+), 11 deletions(-)

diff --git a/tika-core/src/main/java/org/apache/tika/io/TemporaryResources.java b/tika-core/src/main/java/org/apache/tika/io/TemporaryResources.java
index e03cb12..709c700 100644
--- a/tika-core/src/main/java/org/apache/tika/io/TemporaryResources.java
+++ b/tika-core/src/main/java/org/apache/tika/io/TemporaryResources.java
@@ -18,7 +18,10 @@ package org.apache.tika.io;
 
 import java.io.Closeable;
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -59,11 +62,47 @@ public class TemporaryResources implements Closeable {
      * Creates and returns a temporary file that will automatically be
      * deleted when the {@link #close()} method is called.
      *
-     * @return
-     * @throws IOException
+     * @return temporary file
+     * @throws IOException if a temporary file could not be created
      */
     public File createTemporaryFile() throws IOException {
-        final File file = File.createTempFile("apache-tika-", ".tmp", tmp);
+        return createTemporaryFile(null);
+    }
+
+    /**
+     * Creates and returns a temporary file with the given contents.
+     * The temporary file will automatically be deleted when the
+     * {@link #close()} method is called. The given input stream is
+     * <em>not</em> closed.
+     *
+     * @since Apache Tika 1.0
+     * @param input initial content of the temporary file, or <code>null</code>
+     * @return temporary file
+     * @throws IOException if a temporary file could not be created
+     */
+    public File createTemporaryFile(InputStream input) throws IOException {
+        return createTemporaryFile(input, null);
+    }
+
+    /**
+     * Creates and returns a temporary file with the given contents and
+     * the given file name suffix. The temporary file will automatically
+     * be deleted when the {@link #close()} method is called. The given
+     * input stream is <em>not</em> closed.
+     *
+     * @since Apache Tika 1.0
+     * @param input initial content of the temporary file, or <code>null</code>
+     * @param suffix file name suffix, or <code>null</code> for the default
+     * @return temporary file
+     * @throws IOException if a temporary file could not be created
+     */
+    public File createTemporaryFile(InputStream input, String suffix)
+            throws IOException {
+        if (suffix == null) {
+            suffix = ".tmp";
+        }
+
+        final File file = File.createTempFile("apache-tika-", suffix, tmp);
         addResource(new Closeable() {
             public void close() throws IOException {
                 if (!file.delete()) {
@@ -73,6 +112,16 @@ public class TemporaryResources implements Closeable {
                 }
             }
         });
+
+        if (input != null) {
+            OutputStream output = new FileOutputStream(file);
+            try {
+                IOUtils.copy(input, output);
+            } finally {
+                output.close();
+            }
+        }
+
         return file;
     }
 
diff --git a/tika-core/src/main/java/org/apache/tika/io/TikaInputStream.java b/tika-core/src/main/java/org/apache/tika/io/TikaInputStream.java
index e89bdf2..ca53210 100644
--- a/tika-core/src/main/java/org/apache/tika/io/TikaInputStream.java
+++ b/tika-core/src/main/java/org/apache/tika/io/TikaInputStream.java
@@ -22,10 +22,8 @@ import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
@@ -529,18 +527,43 @@ public class TikaInputStream extends TaggedInputStream {
     }
 
     public File getFile() throws IOException {
+        return getFile(null);
+    }
+
+    /**
+     * Returns the contents of this stream as a file. If this stream is
+     * originally backed by a file, then that file is simply returned.
+     * Otherwise a temporary file is created), the entire stream is
+     * spooled into that file, and this stream is re-set to read from
+     * that file. The temporary file is automatically removed when this
+     * stream or the associated {@link TemporaryResources} object is closed.
+     * <p>
+     * If a new temporary file gets created by this method, then the
+     * suffix of the temporary file is taken from the given file name pattern
+     * (like "name.pdf"). This helps tools that rely on file name suffixes
+     * for correct processing of files.
+     *
+     * @since Apache Tika 1.0
+     * @param name file name pattern, or <code>null</code> for the default
+     * @return the contents of this stream as a file
+     * @throws IOException if the file could not be created
+     */
+    public File getFile(String name) throws IOException {
         if (file == null) {
             if (position > 0) {
                 throw new IOException("Stream is already being read");
             } else {
                 // Spool the entire stream into a temporary file
-                file = tmp.createTemporaryFile();
-                OutputStream out = new FileOutputStream(file);
-                try {
-                    IOUtils.copy(in, out);
-                } finally {
-                    out.close();
+                String suffix = null;
+                if (name != null) {
+                    name = name.substring(name.lastIndexOf('/') + 1);
+                    name = name.substring(name.lastIndexOf('\\') + 1);
+                    int dot = name.indexOf('.', 1);
+                    if (dot != -1) {
+                        suffix = name.substring(dot);
+                    }
                 }
+                file = tmp.createTemporaryFile(in, suffix);
 
                 // Create a new input stream and make sure it'll get closed
                 FileInputStream newStream = new FileInputStream(file);
-- 
1.7.4.4

