KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > vfs > VFile


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.vfs;
9
10 import java.io.InputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12
13 /**
14  * File on virtual file system.
15  *
16  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
17  */

18 public final class VFile
19 {
20     ///Complete Path to file
21
private final String JavaDoc m_path;
22
23     ///Underlying resource that if VFileAccessor specific
24
private Object JavaDoc m_resource;
25
26     ///VFileAccessor via which file is accessed
27
private VFileAccessor m_accessor;
28
29     ///Virtual file system in which this VFile is mounted
30
private VFileSystem m_filesystem;
31
32     /**
33      * Constructor for creating a directory entry.
34      *
35      * @param path the path to directory
36      * @param filesystem the filesystem VFile belongs to
37      */

38     protected VFile( final String JavaDoc path, final VFileSystem filesystem )
39     {
40         this( path, filesystem, null, null );
41     }
42
43     /**
44      * Constructor for creating a vfile.
45      *
46      * @param path the path to directory
47      * @param filesystem the filesystem VFile belongs to
48      * @param resource resource represented by vfile. Must be non-null if accessor non-null.
49      * @param accessor accessor to access vfile. Must be non-null if resource non-null.
50      * @exception IllegalArgumentException if an error occurs
51      */

52     protected VFile( final String JavaDoc path,
53                      final VFileSystem filesystem,
54                      final Object JavaDoc resource,
55                      final VFileAccessor accessor )
56         throws IllegalArgumentException JavaDoc
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 JavaDoc( "Resource and accessor must " +
68                                                     "be both null or non-null" );
69             }
70         }
71     }
72
73     /**
74      * Get path to file.
75      *
76      * @return the path to file
77      */

78     public String JavaDoc getPath()
79     {
80         return m_path;
81     }
82
83     /**
84      * Get name of file.
85      *
86      * @return the name of file
87      */

88     public String JavaDoc 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 JavaDoc
103     {
104         checkValid();
105         return m_filesystem.get( getParentsName() );
106     }
107
108     public VFile[] list()
109         throws IOException JavaDoc
110     {
111         checkValid();
112         return m_filesystem.list( this );
113     }
114
115     public VFile[] list( final VFileFilter filter )
116         throws IOException JavaDoc
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 JavaDoc
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 JavaDoc getInputStream()
144         throws IOException JavaDoc
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     /**
160      * Invalidate VFile and free all resources.
161      */

162     protected void invalidate()
163     {
164         m_filesystem = null;
165         m_resource = null;
166         m_accessor = null;
167     }
168
169     /**
170      * Helper to make sure VFile is valid before proceeding.
171      * It will throw an IOException if VFile invalid.
172      *
173      * @exception IOException if an error occurs
174      */

175     private void checkValid()
176         throws IOException JavaDoc
177     {
178         if( !isValid() )
179         {
180             throw new IOException JavaDoc( "Invalid VFile" );
181         }
182     }
183
184     /**
185      * Helper method to get parents filename.
186      *
187      * @return the parents filename.
188      */

189     private String JavaDoc 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