From 1350823035597f784f9cf871aa487f896f3d1840 Mon Sep 17 00:00:00 2001
From: Sven Gothel
java.io.tmpdir
is not allowed within the current security context
*
* @see PropertyAccess#getProperty(String, boolean)
* @see Context#getDir(String, int)
*/
public static File getTempDir(final boolean executable)
- throws SecurityException, RuntimeException
+ throws SecurityException, IOException
{
if(!tempRootSet) { // volatile: ok
synchronized(IOUtil.class) {
@@ -1089,7 +1089,7 @@ public class IOUtil {
final File r = executable ? tempRootExec : tempRootNoexec ;
if(null == r) {
final String exe_s = executable ? "executable " : "";
- throw new RuntimeException("Could not determine a temporary "+exe_s+"directory");
+ throw new IOException("Could not determine a temporary "+exe_s+"directory");
}
final FilePermission fp = new FilePermission(r.getAbsolutePath(), "read,write,delete");
SecurityUtil.checkPermission(fp);
@@ -1113,7 +1113,7 @@ public class IOUtil {
* @param executable true if the temporary root folder needs to hold executable files, otherwise false.
* @return
* @throws IllegalArgumentException
- * @throws IOException
+ * @throws IOException if no temporary directory could be determined or temp file could not be created
* @throws SecurityException
*/
public static File createTempFile(final String prefix, final String suffix, final boolean executable)
@@ -1136,4 +1136,29 @@ public class IOUtil {
}
}
}
+
+ /**
+ * Helper to simplify closing {@link Closeable}s.
+ *
+ * @param stream the {@link Closeable} instance to close
+ * @param saveOneIfFree cache for one {@link IOException} to store, if not already used (excess)
+ * @param dumpExcess dump the excess {@link IOException} on this {@link PrintStream}
+ * @return the excess {@link IOException} or {@code null}.
+ */
+ public static IOException close(final Closeable stream, final IOException[] saveOneIfFree, final PrintStream dumpExcess) {
+ try {
+ stream.close();
+ } catch(final IOException e) {
+ if( null == saveOneIfFree[0] ) {
+ saveOneIfFree[0] = e;
+ } else {
+ if( null != dumpExcess ) {
+ dumpExcess.println("Caught "+e.getClass().getSimpleName()+": "+e.getMessage());
+ e.printStackTrace(dumpExcess);
+ }
+ return e;
+ }
+ }
+ return null;
+ }
}
--
cgit v1.2.3