1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.millscript.commons.vfs.protocols.file;
22
23 import org.millscript.commons.alert.Alert;
24 import org.millscript.commons.vfs.AbstractVFile;
25 import org.millscript.commons.vfs.VFolder;
26 import org.millscript.commons.vfs.alerts.VCannotOpenEntryAlert;
27
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.FileOutputStream;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34
35 /**
36 * This class implements a virtual filesystem file for a local filesystem file.
37 */
38 public class LocalFile extends AbstractVFile< LocalVolume > {
39
40 /**
41 * Constructs a new virtual filesystem file with the specified name,
42 * parent folder and volume.
43 *
44 * @param vol the volume this entry is held on
45 * @param parent the parent folder for this file
46 * @param name the name of the file
47 */
48 LocalFile( final LocalVolume vol, final VFolder parent, final String name ) {
49 super( vol, parent, name );
50 }
51
52 /**
53 * Constructs a new virtual filesystem file with the specified name,
54 * parent folder. This file will belong to the same volume as the specified
55 * parent folder.
56 *
57 * @param parent the parent folder for this file
58 * @param name the name of the file
59 */
60 LocalFile( final LocalFolder parent, final String name ) {
61 super( parent.getVolume(), parent, name );
62 }
63
64 /**
65 * @see org.millscript.commons.vfs.VEntry#exists()
66 */
67 public boolean exists() {
68 return this.getFile().isFile();
69 }
70
71 /**
72 * @see org.millscript.commons.vfs.VFile#getAppendOutputStream()
73 */
74 public OutputStream getAppendOutputStream() {
75 try {
76 return new FileOutputStream( this.getFile(), true );
77 } catch ( FileNotFoundException ex ) {
78
79 throw(
80 this.handleException( ex ).remishap()
81 );
82 }
83 }
84
85 /**
86 * Returns the File for this local virtual file.
87 *
88 * @return a File for this local virtual file
89 */
90 public File getFile() {
91 return new File( this.getURI().getPath() );
92 }
93
94 /**
95 * @see org.millscript.commons.vfs.VFile#getInputStream()
96 */
97 public InputStream getInputStream() {
98 try {
99 return new FileInputStream( this.getFile() );
100 } catch ( FileNotFoundException ex ) {
101
102 throw(
103 this.handleException( ex ).remishap()
104 );
105 }
106 }
107
108 /**
109 * @see org.millscript.commons.vfs.VFile#getOutputStream()
110 */
111 public OutputStream getOutputStream() {
112 try {
113 return new FileOutputStream( this.getFile() );
114 } catch ( FileNotFoundException ex ) {
115
116 throw(
117 this.handleException( ex ).remishap()
118 );
119 }
120 }
121
122 private Alert handleException( final FileNotFoundException ex ) {
123 File f = this.getFile();
124 if ( f.isDirectory() ) {
125
126 return VCannotOpenEntryAlert.entryIsNotAFile( this ).setParentThrowable( ex ).mishap();
127 }
128
129 return VCannotOpenEntryAlert.fileNotFound( this ).setParentThrowable( ex ).mishap();
130 }
131
132 /**
133 * @see org.millscript.commons.vfs.AbstractVFile#toLocal()
134 */
135 @Override
136 public LocalFile toLocal() {
137 return this;
138 }
139
140 }