1 16 package org.apache.commons.vfs.provider.local; 17 18 import org.apache.commons.vfs.FileName; 19 import org.apache.commons.vfs.FileObject; 20 import org.apache.commons.vfs.FileSystemException; 21 import org.apache.commons.vfs.FileType; 22 import org.apache.commons.vfs.RandomAccessContent; 23 import org.apache.commons.vfs.provider.AbstractFileObject; 24 import org.apache.commons.vfs.provider.UriParser; 25 import org.apache.commons.vfs.util.RandomAccessMode; 26 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.FileOutputStream ; 30 import java.io.InputStream ; 31 import java.io.OutputStream ; 32 33 39 public class LocalFile 40 extends AbstractFileObject 41 implements FileObject 42 { 43 private File file; 44 private final String fileName; 45 46 49 protected LocalFile(final LocalFileSystem fileSystem, 50 final String fileName, 51 final FileName name) throws FileSystemException 52 { 53 super(name, fileSystem); 54 this.fileName = UriParser.decode(fileName); 55 } 56 57 60 protected File getLocalFile() 61 { 62 return file; 63 } 64 65 68 protected void doAttach() 69 throws Exception 70 { 71 if (file == null) 72 { 73 file = new File (fileName); 74 } 75 } 76 77 80 protected FileType doGetType() 81 throws Exception 82 { 83 if (!file.exists()) 84 { 85 return FileType.IMAGINARY; 86 } 87 else if (file.isDirectory()) 88 { 89 return FileType.FOLDER; 90 } 91 else if (file.isFile()) 92 { 93 return FileType.FILE; 94 } 95 96 throw new FileSystemException("vfs.provider.local/get-type.error", file); 97 } 98 99 102 protected String [] doListChildren() 103 throws Exception 104 { 105 return UriParser.encode(file.list()); 106 } 107 108 111 protected void doDelete() 112 throws Exception 113 { 114 if (!file.delete()) 115 { 116 throw new FileSystemException("vfs.provider.local/delete-file.error", file); 117 } 118 } 119 120 123 protected void doRename(FileObject newfile) throws Exception 124 { 125 if (!file.renameTo(((LocalFile) newfile).getLocalFile())) 126 { 127 throw new FileSystemException("vfs.provider.local/rename-file.error", 128 new String []{file.toString(), newfile.toString()}); 129 } 130 } 131 132 135 protected void doCreateFolder() 136 throws Exception 137 { 138 if (!file.mkdirs()) 139 { 140 throw new FileSystemException("vfs.provider.local/create-folder.error", file); 141 } 142 } 143 144 147 protected boolean doIsWriteable() throws FileSystemException 148 { 149 return file.canWrite(); 150 } 151 152 155 protected boolean doIsHidden() 156 { 157 return file.isHidden(); 158 } 159 160 163 protected boolean doIsReadable() throws FileSystemException 164 { 165 return file.canRead(); 166 } 167 168 171 protected long doGetLastModifiedTime() throws FileSystemException 172 { 173 return file.lastModified(); 174 } 175 176 179 protected void doSetLastModifiedTime(final long modtime) 180 throws FileSystemException 181 { 182 file.setLastModified(modtime); 183 } 184 185 188 protected InputStream doGetInputStream() 189 throws Exception 190 { 191 return new FileInputStream (file); 192 } 193 194 197 protected OutputStream doGetOutputStream(boolean bAppend) 198 throws Exception 199 { 200 return new FileOutputStream (file.getPath(), bAppend); 201 } 202 203 206 protected long doGetContentSize() 207 throws Exception 208 { 209 return file.length(); 210 } 211 212 protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception 213 { 214 return new LocalFileRandomAccessContent(file, mode); 215 } 216 } 217 | Popular Tags |