public class PrivilegedFileOpsForTests
extends java.lang.Object
File
that wraps the
operations in privileged block of code. This class is intended to provide
these methods for testcases to reduce the hassle of having to wrap file
operations in privileged code blocks.
Derby needs to use privileged blocks in some places to avoid
SecurityException
s being thrown, as the required privileges are
often granted to Derby itself, but not the higher level application code.
Constructor and Description |
---|
PrivilegedFileOpsForTests() |
Modifier and Type | Method and Description |
---|---|
static void |
copy(java.io.File source,
java.io.File target)
In a priv block, do a recursive copy from source to target.
|
private static void |
copySingleFile(java.io.File source,
java.io.File target,
byte[] buf)
Copy a single file from source to target.
|
static java.io.File |
createTempFile(java.lang.String prefix,
java.lang.String suffix,
java.io.File directory)
Create a temporary file.
|
static boolean |
delete(java.io.File file)
Delete a file
|
private static boolean |
deleteRecursively(java.io.File dir,
java.util.List<java.io.File> failedDeletes)
Deletes the specified directory and all its files and subdirectories.
|
static boolean |
exists(java.io.File file)
Check if the file exists.
|
static java.lang.String |
getAbsolutePath(java.io.File file)
Get the absolute path
|
static java.lang.String |
getFileInfo(java.io.File f)
Obtains information about the specified file.
|
static java.io.FileInputStream |
getFileInputStream(java.io.File file)
Returns a input stream for the specified file.
|
static java.io.FileOutputStream |
getFileOutputStream(java.io.File file)
Returns a file output stream for the specified file.
|
static java.io.FileOutputStream |
getFileOutputStream(java.io.File file,
boolean append)
Returns a file output stream for the specified file.
|
static java.io.FileReader |
getFileReader(java.io.File file)
Obtains a reader for the specified file.
|
static java.io.FileWriter |
getFileWriter(java.io.File file)
Obtains a writer for the specified file.
|
private static boolean |
internalDelete(java.io.File f,
java.util.List<java.io.File> failedDeletes)
Attempts to delete the specified file, will add it to the passed in list
if the delete fails.
|
static long |
length(java.io.File file)
Get the file length.
|
static java.io.File[] |
listFiles(java.io.File dir)
In a priv block, get an array with all the files in a directory.
|
static boolean |
mkdir(java.io.File dir)
Create a directory.
|
static java.io.File[] |
persistentRecursiveDelete(java.io.File dir)
Tries to delete all the files, including the specified directory, in the
directory tree with the specified root.
|
private static void |
recursiveCopy(java.io.File source,
java.io.File target)
Do a recursive copy from source to target.
|
static boolean |
setReadable(java.io.File file,
boolean readable,
boolean ownerOnly)
Set the read permission for a file.
|
static boolean |
setReadOnly(java.io.File file)
Make a file or directory read-only.
|
static java.net.URI |
toURI(java.io.File file)
Get the URI for a file.
|
public static long length(java.io.File file) throws java.lang.SecurityException
java.lang.SecurityException
- if the required permissions to read the file,
or the path it is in, are missingFile.length()
public static java.lang.String getAbsolutePath(java.io.File file) throws java.lang.SecurityException
file
- File for absolute pathjava.lang.SecurityException
- if the required permissions to access the file,File.getAbsolutePath()
public static java.net.URI toURI(java.io.File file) throws java.lang.SecurityException
file
- File to be queriedjava.lang.SecurityException
- if the test lacks the required permissions to access the file,File.getAbsolutePath()
public static java.io.File createTempFile(java.lang.String prefix, java.lang.String suffix, java.io.File directory) throws java.io.IOException
prefix
- file name prefix, at least three characterssuffix
- file name suffix, defaults to ".tmp" if null
directory
- where to create the file, or null
for the
default temporary file directoryjava.io.IOException
- if the file cannot be createdFile.createTempFile(String, String, File)
public static java.io.FileInputStream getFileInputStream(java.io.File file) throws java.io.FileNotFoundException
file
- the file to open a stream forjava.lang.SecurityException
- if the required permissions to read the file,
or the path it is in, are missingjava.io.FileNotFoundException
- if the specified file does not existpublic static boolean exists(java.io.File file) throws java.lang.SecurityException
true
if file exists, false
otherwisejava.lang.SecurityException
- if the required permissions to read the file,
or the path it is in, are missingFile.exists()
public static boolean delete(java.io.File file) throws java.lang.SecurityException
true
if file was deleted, false
otherwisejava.lang.SecurityException
- if the required permissions to read the file,
or the path it is in, are missingFile.delete()
public static boolean mkdir(java.io.File dir)
dir
- the directory to createtrue
if and only if the directory was createdpublic static boolean setReadable(java.io.File file, boolean readable, boolean ownerOnly)
file
- the file to set the read permission forreadable
- true
to allow read, false
to deny itownerOnly
- true
if it should be set for the owner only,
false
if it should be set for everyonetrue
if the operation succeeded, false
otherwisepublic static boolean setReadOnly(java.io.File file)
file
- the file to make read-onlytrue
if successful, false
otherwisepublic static java.io.FileReader getFileReader(java.io.File file) throws java.io.FileNotFoundException
file
- the file to obtain a reader forjava.io.FileNotFoundException
- if the specified file does not existjava.lang.SecurityException
- if the required permissions to read the file,
or the path it is in, are missingpublic static java.io.FileWriter getFileWriter(java.io.File file) throws java.io.IOException
file
- the file to obtain a writer forjava.io.IOException
- if the file cannot be openedjava.lang.SecurityException
- if the required permissions to write to the file,
or the path it is in, are missingpublic static java.io.File[] listFiles(java.io.File dir)
dir
- the directory to scanFile.listFiles()
public static void copy(java.io.File source, java.io.File target) throws java.io.IOException
source
- Source file or directory to copytarget
- Target file or directory to copy tojava.io.IOException
java.lang.SecurityException
private static void recursiveCopy(java.io.File source, java.io.File target) throws java.io.IOException
source
- Source file or directory to copytarget
- Target file or directory to copyjava.io.IOException
java.io.FileNotFoundException
private static void copySingleFile(java.io.File source, java.io.File target, byte[] buf) throws java.io.IOException
source
- Source file to copytarget
- Destination file for copybuf
- buffer used for copy (may be null
)java.io.IOException
- if accessing the specified files failjava.io.FileNotFoundException
- if a specified file doesn't existpublic static java.io.FileOutputStream getFileOutputStream(java.io.File file) throws java.io.FileNotFoundException
If the file already exists and is writable, it will be overwritten.
file
- the file to create a stream forjava.io.FileNotFoundException
- if the specified file does not existjava.lang.SecurityException
- if the required permissions to write the file,
or the path it is in, are missingpublic static java.io.FileOutputStream getFileOutputStream(java.io.File file, boolean append) throws java.io.FileNotFoundException
file
- the file to create a stream forappend
- whether to append or overwrite an existing filejava.io.FileNotFoundException
- if the specified file does not existjava.lang.SecurityException
- if the required permissions to write the file,
or the path it is in, are missingpublic static java.io.File[] persistentRecursiveDelete(java.io.File dir) throws java.io.FileNotFoundException
If deleting one of the files fail, it will be recorded and the method will move on to the remaining files and try to delete them.
dir
- the directory to delete (including subdirectories)java.io.FileNotFoundException
BaseTestCase.assertDirectoryDeleted(java.io.File)
private static boolean deleteRecursively(java.io.File dir, java.util.List<java.io.File> failedDeletes)
An attempt is made to delete all files, even if one of the delete operations fail.
dir
- the root directory to start deleting fromfailedDeletes
- a list of failed deletes (if any)true
is all delete operations succeeded, false
otherwise.private static boolean internalDelete(java.io.File f, java.util.List<java.io.File> failedDeletes)
f
- file to deletefailedDeletes
- list keeping track of failed deletestrue
if the delete succeeded, false
otherwise.public static java.lang.String getFileInfo(java.io.File f)
f
- the fileApache Derby V10.13 Internals - Copyright © 2004,2016 The Apache Software Foundation. All Rights Reserved.