1 21 22 package org.apache.derby.impl.io; 23 24 import org.apache.derby.io.StorageFactory; 25 import org.apache.derby.io.StorageFile; 26 import org.apache.derby.io.StorageRandomAccessFile; 27 28 import org.apache.derby.iapi.services.sanity.SanityManager; 29 30 import java.io.File ; 31 import java.io.InputStream ; 32 import java.io.OutputStream ; 33 import java.io.IOException ; 34 import java.io.FileNotFoundException ; 35 import java.net.MalformedURLException ; 36 import java.net.URL ; 37 38 42 abstract class InputStreamFile implements StorageFile 43 { 44 45 final String path; 46 final int nameStart; final BaseStorageFactory storageFactory; 48 49 InputStreamFile( BaseStorageFactory storageFactory, String path) 50 { 51 this.storageFactory = storageFactory; 52 if( path == null || path.length() == 0) 53 { 54 this.path = storageFactory.dataDirectory; 55 nameStart = -1; 56 } 57 else 58 { 59 StringBuffer sb = new StringBuffer ( storageFactory.separatedDataDirectory); 60 if( File.separatorChar != '/') 61 sb.append( path.replace( File.separatorChar, '/')); 62 else 63 sb.append( path); 64 this.path = sb.toString(); 65 nameStart = this.path.lastIndexOf( '/') + 1; 66 } 67 } 68 69 InputStreamFile( BaseStorageFactory storageFactory, String parent, String name) 70 { 71 this.storageFactory = storageFactory; 72 StringBuffer sb = new StringBuffer ( storageFactory.separatedDataDirectory); 73 if( File.separatorChar != '/') 74 { 75 sb.append( parent.replace( File.separatorChar, '/')); 76 sb.append( '/'); 77 sb.append( name.replace( File.separatorChar, '/')); 78 } 79 else 80 { 81 sb.append( parent); 82 sb.append( '/'); 83 sb.append( name); 84 } 85 path = sb.toString(); 86 nameStart = this.path.lastIndexOf( '/') + 1; 87 } 88 89 InputStreamFile( InputStreamFile dir, String name) 90 { 91 this.storageFactory = dir.storageFactory; 92 StringBuffer sb = new StringBuffer ( dir.path); 93 sb.append( '/'); 94 if( File.separatorChar != '/') 95 sb.append( name.replace( File.separatorChar, '/')); 96 else 97 sb.append( name); 98 path = sb.toString(); 99 nameStart = this.path.lastIndexOf( '/') + 1; 100 } 101 102 InputStreamFile( BaseStorageFactory storageFactory, String child, int pathLen) 103 { 104 this.storageFactory = storageFactory; 105 path = child.substring( 0, pathLen); 106 nameStart = this.path.lastIndexOf( '/') + 1; 107 } 108 109 public boolean equals( Object other) 110 { 111 if( other == null || ! getClass().equals( other.getClass())) 112 return false; 113 InputStreamFile otherFile = (InputStreamFile) other; 114 return path.equals( otherFile.path); 115 } 116 117 public int hashCode() 118 { 119 return path.hashCode(); 120 } 121 122 130 public String [] list() 131 { 132 return null; 133 } 134 135 140 public boolean canWrite() 141 { 142 return false; 143 } 144 145 150 public abstract boolean exists(); 151 152 158 public boolean isDirectory() 159 { 160 return false; 161 } 162 163 168 public boolean delete() 169 { 170 return false; 171 } 172 173 178 public boolean deleteAll() 179 { 180 return false; 181 } 182 183 195 public String getPath() 196 { 197 if( File.separatorChar != '/') 198 return path.replace( '/', File.separatorChar); 199 return path; 200 } 202 public String getCanonicalPath() throws IOException 203 { 204 return storageFactory.getCanonicalName() + "/" + path; 205 } 206 207 210 public String getName() 211 { 212 return (nameStart < 0) ? "" : path.substring( nameStart); 213 } 214 215 228 public boolean createNewFile() throws IOException 229 { 230 throw new IOException ( "createNewFile called in a read-only file system."); 231 } 232 233 245 public boolean renameTo( StorageFile newName) 246 { 247 return false; 248 } 249 250 255 public boolean mkdir() 256 { 257 return false; 258 } 259 260 265 public boolean mkdirs() 266 { 267 return false; 268 } 269 270 277 public long length() 278 { 279 try 280 { 281 InputStream is = getInputStream(); 282 if( is == null) 283 return 0; 284 long len = is.available(); 285 is.close(); 286 return len; 287 } 288 catch( IOException e){ return 0;} 289 } 291 297 public StorageFile getParentDir() 298 { 299 if( path.length() <= storageFactory.separatedDataDirectory.length()) 300 return null; 301 return getParentDir( path.lastIndexOf( '/')); 302 } 303 304 309 abstract StorageFile getParentDir( int pathLen); 310 311 318 public boolean setReadOnly() 319 { 320 return true; 321 } 322 323 333 public OutputStream getOutputStream( ) throws FileNotFoundException 334 { 335 throw new FileNotFoundException ( "Attempt to write into a read only file system."); 336 } 337 338 339 349 public OutputStream getOutputStream( boolean append) throws FileNotFoundException 350 { 351 throw new FileNotFoundException ( "Attempt to write into a read only file system."); 352 } 353 354 355 362 abstract public InputStream getInputStream( ) throws FileNotFoundException ; 363 364 372 public int getExclusiveFileLock() 373 { 374 return NO_FILE_LOCK_SUPPORT; 375 } 376 377 382 public void releaseExclusiveFileLock() 383 {} 384 385 407 public StorageRandomAccessFile getRandomAccessFile( String mode) throws FileNotFoundException 408 { 409 if( SanityManager.DEBUG) 410 SanityManager.NOTREACHED(); 411 return null; 412 } 413 414 419 public String toString() 420 { 421 return path; 422 } 423 424 427 public URL getURL() throws MalformedURLException { 428 throw new MalformedURLException (toString()); 429 } 430 } 431 | Popular Tags |