1 21 22 package org.apache.derby.impl.io; 23 24 import org.apache.derby.io.StorageFile; 25 import org.apache.derby.io.StorageRandomAccessFile; 26 27 import org.apache.derby.iapi.services.sanity.SanityManager; 28 29 import java.io.File ; 30 import java.io.InputStream ; 31 import java.io.OutputStream ; 32 import java.io.FileOutputStream ; 33 import java.io.FileInputStream ; 34 import java.io.IOException ; 35 import java.io.FileNotFoundException ; 36 import java.io.RandomAccessFile ; 37 import java.net.MalformedURLException ; 38 import java.net.URL ; 39 40 44 class DirFile extends File implements StorageFile 45 { 46 47 52 DirFile( String path) 53 { 54 super( path); 55 } 56 57 63 DirFile( String directoryName, String fileName) 64 { 65 super( directoryName, fileName); 66 } 67 68 74 DirFile( DirFile directoryName, String fileName) 75 { 76 super( (File) directoryName, fileName); 77 } 78 79 85 public StorageFile getParentDir() 86 { 87 String parent = getParent(); 88 if( parent == null) 89 return null; 90 return new DirFile( parent); 91 } 92 93 98 static StorageFile getTempDir() throws IOException 99 { 100 File temp = File.createTempFile("derby", "tmp"); 101 StorageFile parent = new DirFile( temp.getParent()); 102 temp.delete(); 103 104 return parent; 105 } 107 116 public OutputStream getOutputStream( ) throws FileNotFoundException 117 { 118 return new FileOutputStream ( (File) this); 119 } 120 121 134 public OutputStream getOutputStream( final boolean append) throws FileNotFoundException 135 { 136 return new FileOutputStream ( getPath(), append); 137 } 138 139 146 public InputStream getInputStream( ) throws FileNotFoundException 147 { 148 return new FileInputStream ( (File) this); 149 } 150 151 160 public synchronized int getExclusiveFileLock() 161 { 162 if (exists()) 163 { 164 delete(); 165 } 166 try 167 { 168 RandomAccessFile lockFileOpen = new RandomAccessFile( (File) this, "rw"); 170 lockFileOpen.getFD().sync( ); 171 lockFileOpen.close(); 172 }catch(IOException ioe) 173 { 174 if (SanityManager.DEBUG) 177 { 178 SanityManager.THROWASSERT("Unable to create Exclusive Lock File " + getPath()); 179 } 180 } 181 182 return NO_FILE_LOCK_SUPPORT; 183 } 185 190 public synchronized void releaseExclusiveFileLock() 191 { 192 if( exists()) 193 { 194 delete(); 195 } 196 } 198 218 public StorageRandomAccessFile getRandomAccessFile( String mode) throws FileNotFoundException 219 { 220 if( "rws".equals( mode) || "rwd".equals( mode)) 222 mode = "rw"; 223 return new DirRandomAccessFile( (File) this, mode); 224 } 226 238 public boolean renameTo( StorageFile newName) 239 { 240 return super.renameTo( (File) newName); 241 } 242 243 248 public boolean deleteAll() 249 { 250 if( !exists()) 251 return false; 252 if( isDirectory()) 253 { 254 String [] childList = super.list(); 255 String parentName = getPath(); 256 for( int i = 0; i < childList.length; i++) 257 { 258 if( childList[i].equals( ".") || childList[i].equals( "..")) 259 continue; 260 DirFile child = new DirFile( parentName, childList[i]); 261 if( ! child.deleteAll()) 262 return false; 263 } 264 } 265 return delete(); 266 } 268 271 public URL getURL() throws MalformedURLException { 272 273 return toURL(); 274 } 275 } 276 | Popular Tags |