KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.io.InputStream JavaDoc;
13
14 /**
15  * Accessor for vanilla directorys VFS.
16  *
17  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
18  */

19 public final class DirectoryVFileAccessor
20     implements VFileAccessor
21 {
22     /**
23      * Get size of resource in bytes.
24      *
25      * @param resource the resource designator
26      * @return the size of resource in bytes
27      */

28     public long getSize( final VFile file, final Object JavaDoc resource )
29     {
30         try
31         {
32             final File JavaDoc realFile = getFile( file, resource );
33             return realFile.length();
34         }
35         catch( final Exception JavaDoc e )
36         {
37             return -1;
38         }
39     }
40
41     /**
42      * Get InputStream for resource.
43      *
44      * @param resource the resource designator
45      * @return the input stream
46      * @exception IOException if an error occurs
47      */

48     public InputStream JavaDoc getInputStream( final VFile file, final Object JavaDoc resource )
49     {
50         try
51         {
52             final File JavaDoc realFile = getFile( file, resource );
53             return new FileInputStream JavaDoc( realFile );
54         }
55         catch( final Exception JavaDoc e )
56         {
57             return null;
58         }
59     }
60
61     /**
62      * Retrieve underlying file for resource entry.
63      *
64      * @param file the VFile representing resource entry
65      * @param resource the resource
66      * @return the File
67      */

68     private File JavaDoc getFile( final VFile file, final Object JavaDoc resource )
69     {
70         final File JavaDoc baseFile = (File JavaDoc)resource;
71         final String JavaDoc name = file.toString().substring( 1 ).replace( '/', File.separatorChar );
72         return new File JavaDoc( baseFile, name );
73     }
74 }
75
Popular Tags