1 8 package org.apache.avalon.excalibur.vfs; 9 10 import java.io.InputStream ; 11 import java.io.IOException ; 12 13 18 public final class VFile 19 { 20 private final String m_path; 22 23 private Object m_resource; 25 26 private VFileAccessor m_accessor; 28 29 private VFileSystem m_filesystem; 31 32 38 protected VFile( final String path, final VFileSystem filesystem ) 39 { 40 this( path, filesystem, null, null ); 41 } 42 43 52 protected VFile( final String path, 53 final VFileSystem filesystem, 54 final Object resource, 55 final VFileAccessor accessor ) 56 throws IllegalArgumentException 57 { 58 m_path = path; 59 m_filesystem = filesystem; 60 m_resource = resource; 61 m_accessor = accessor; 62 63 if( null == resource || null == accessor ) 64 { 65 if( null != resource && null != accessor ) 66 { 67 throw new IllegalArgumentException ( "Resource and accessor must " + 68 "be both null or non-null" ); 69 } 70 } 71 } 72 73 78 public String getPath() 79 { 80 return m_path; 81 } 82 83 88 public String getName() 89 { 90 final int index = m_path.lastIndexOf( '/' ); 91 if( -1 == index ) 92 { 93 return m_path; 94 } 95 else 96 { 97 return m_path.substring( index ); 98 } 99 } 100 101 public VFile getParent() 102 throws IOException 103 { 104 checkValid(); 105 return m_filesystem.get( getParentsName() ); 106 } 107 108 public VFile[] list() 109 throws IOException 110 { 111 checkValid(); 112 return m_filesystem.list( this ); 113 } 114 115 public VFile[] list( final VFileFilter filter ) 116 throws IOException 117 { 118 checkValid(); 119 return m_filesystem.list( this, filter ); 120 } 121 122 public boolean isDirectory() 123 { 124 return ( null == m_resource ); 125 } 126 127 public boolean isFile() 128 { 129 return ( null != m_resource ); 130 } 131 132 public long getSize() 133 throws IOException 134 { 135 checkValid(); 136 if( isDirectory() ) return 0; 137 else 138 { 139 return m_accessor.getSize( this, m_resource ); 140 } 141 } 142 143 public InputStream getInputStream() 144 throws IOException 145 { 146 checkValid(); 147 if( isDirectory() ) return null; 148 else 149 { 150 return m_accessor.getInputStream( this, m_resource ); 151 } 152 } 153 154 public boolean isValid() 155 { 156 return ( null != m_filesystem ); 157 } 158 159 162 protected void invalidate() 163 { 164 m_filesystem = null; 165 m_resource = null; 166 m_accessor = null; 167 } 168 169 175 private void checkValid() 176 throws IOException 177 { 178 if( !isValid() ) 179 { 180 throw new IOException ( "Invalid VFile" ); 181 } 182 } 183 184 189 private String getParentsName() 190 { 191 final int index = m_path.lastIndexOf( '/' ); 192 if( -1 == index ) 193 { 194 return m_path; 195 } 196 else 197 { 198 return m_path.substring( 0, index ); 199 } 200 } 201 } 202 | Popular Tags |