You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ta...@apache.org on 2015/09/22 15:32:18 UTC
svn commit: r1704620 - in /tika/trunk/tika-core/src:
main/java/org/apache/tika/io/TemporaryResources.java
test/java/org/apache/tika/io/TemporaryResourcesTest.java
Author: tallison
Date: Tue Sep 22 13:32:15 2015
New Revision: 1704620
URL: http://svn.apache.org/viewvc?rev=1704620&view=rev
Log:
TIKA-1734 via Yaniv Kunda -- use java.nio.file.Path in TemporaryResources
Added:
tika/trunk/tika-core/src/test/java/org/apache/tika/io/TemporaryResourcesTest.java
Modified:
tika/trunk/tika-core/src/main/java/org/apache/tika/io/TemporaryResources.java
Modified: tika/trunk/tika-core/src/main/java/org/apache/tika/io/TemporaryResources.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/io/TemporaryResources.java?rev=1704620&r1=1704619&r2=1704620&view=diff
==============================================================================
--- tika/trunk/tika-core/src/main/java/org/apache/tika/io/TemporaryResources.java (original)
+++ tika/trunk/tika-core/src/main/java/org/apache/tika/io/TemporaryResources.java Tue Sep 22 13:32:15 2015
@@ -19,6 +19,8 @@ package org.apache.tika.io;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.LinkedList;
import org.apache.tika.exception.TikaException;
@@ -36,43 +38,65 @@ public class TemporaryResources implemen
/**
* Tracked resources in LIFO order.
*/
- private final LinkedList<Closeable> resources = new LinkedList<Closeable>();
+ private final LinkedList<Closeable> resources = new LinkedList<>();
/**
* Directory for temporary files, <code>null</code> for the system default.
*/
- private File tmp = null;
+ private Path tempFileDir = null;
/**
* Sets the directory to be used for the temporary files created by
- * the {@link #createTemporaryFile()} method.
+ * the {@link #createTempFile()} method.
*
- * @param tmp temporary file directory,
- * or <code>null</code> for the system default
+ * @param tempFileDir temporary file directory,
+ * or <code>null</code> for the system default
*/
- public void setTemporaryFileDirectory(File tmp) {
- this.tmp = tmp;
+ public void setTemporaryFileDirectory(Path tempFileDir) {
+ this.tempFileDir = tempFileDir;
}
/**
- * Creates and returns a temporary file that will automatically be
- * deleted when the {@link #close()} method is called.
+ * Sets the directory to be used for the temporary files created by
+ * the {@link #createTempFile()} method.
*
- * @return Created temporary file that'll be deleted after closing
+ * @param tempFileDir temporary file directory,
+ * or <code>null</code> for the system default
+ * @see #setTemporaryFileDirectory(Path)
+ */
+ public void setTemporaryFileDirectory(File tempFileDir) {
+ this.tempFileDir = tempFileDir == null ? null : tempFileDir.toPath();
+ }
+
+ /**
+ * Creates a temporary file that will automatically be deleted when
+ * the {@link #close()} method is called, returning its path.
+ *
+ * @return Path to created temporary file that will be deleted after closing
* @throws IOException
*/
- public File createTemporaryFile() throws IOException {
- final File file = File.createTempFile("apache-tika-", ".tmp", tmp);
+ public Path createTempFile() throws IOException {
+ final Path path = tempFileDir == null
+ ? Files.createTempFile("apache-tika-", ".tmp")
+ : Files.createTempFile(tempFileDir, "apache-tika-", ".tmp");
addResource(new Closeable() {
public void close() throws IOException {
- if (!file.delete()) {
- throw new IOException(
- "Could not delete temporary file "
- + file.getPath());
- }
+ Files.delete(path);
}
});
- return file;
+ return path;
+ }
+
+ /**
+ * Creates and returns a temporary file that will automatically be
+ * deleted when the {@link #close()} method is called.
+ *
+ * @return Created temporary file that'll be deleted after closing
+ * @throws IOException
+ * @see #createTempFile()
+ */
+ public File createTemporaryFile() throws IOException {
+ return createTempFile().toFile();
}
/**
Added: tika/trunk/tika-core/src/test/java/org/apache/tika/io/TemporaryResourcesTest.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-core/src/test/java/org/apache/tika/io/TemporaryResourcesTest.java?rev=1704620&view=auto
==============================================================================
--- tika/trunk/tika-core/src/test/java/org/apache/tika/io/TemporaryResourcesTest.java (added)
+++ tika/trunk/tika-core/src/test/java/org/apache/tika/io/TemporaryResourcesTest.java Tue Sep 22 13:32:15 2015
@@ -0,0 +1,39 @@
+/*
+ * 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.tika.io;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.junit.Test;
+
+public class TemporaryResourcesTest {
+
+ @Test
+ public void testFileDeletion() throws IOException {
+ Path tempFile;
+ try (TemporaryResources tempResources = new TemporaryResources()) {
+ tempFile = tempResources.createTempFile();
+ assertTrue("Temp file should exist while TempResources is used", Files.exists(tempFile));
+ }
+ assertTrue("Temp file should not exist after TempResources is closed", Files.notExists(tempFile));
+ }
+
+}