22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.security.action;
27
28 import java.io.*;
29
30 import java.security.PrivilegedExceptionAction;
31
32 /**
33 * A convenience class for opening a FileInputStream as a privileged action.
34 *
35 * @author Andreas Sterbenz
36 */
37 public class OpenFileInputStreamAction
38 implements PrivilegedExceptionAction<FileInputStream> {
39
40 private final File file;
41
42 public OpenFileInputStreamAction(File file) {
43 this.file = file;
44 }
45
46 public OpenFileInputStreamAction(String filename) {
47 this.file = new File(filename);
48 }
49
50 public FileInputStream run() throws Exception {
51 return new FileInputStream(file);
52 }
53 }
|
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.security.action;
27
28 import java.io.*;
29
30 import java.security.PrivilegedExceptionAction;
31
32 /**
33 * A convenience class for opening a FileInputStream as a privileged action.
34 *
35 * @author Andreas Sterbenz
36 */
37 public class OpenFileInputStreamAction
38 implements PrivilegedExceptionAction<FileInputStream> {
39
40 private final File file;
41
42 // Return null if the file doesn't exist
43 private final boolean graceful;
44
45 public OpenFileInputStreamAction(File file) {
46 this(file, false);
47 }
48
49 public OpenFileInputStreamAction(File file, boolean graceful) {
50 this.file = file;
51 this.graceful = graceful;
52 }
53
54 public OpenFileInputStreamAction(String filename) {
55 this.file = new File(filename);
56 this.graceful = false;
57 }
58
59 public FileInputStream run() throws Exception {
60 if (graceful) {
61 try {
62 if (file.exists()) {
63 return new FileInputStream(file);
64 } else {
65 return null;
66 }
67 } catch (FileNotFoundException e) {
68 // couldn't find it, oh well.
69 return null;
70 }
71 } else {
72 return new FileInputStream(file);
73 }
74 }
75 }
|