KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openedit > repository > filesystem > FileItem


1 /*
2  * Created on Jul 30, 2004
3  */

4 package org.openedit.repository.filesystem;
5
6 import java.io.ByteArrayInputStream JavaDoc;
7 import java.io.File JavaDoc;
8 import java.io.FileInputStream JavaDoc;
9 import java.io.FileNotFoundException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.util.Date JavaDoc;
12
13 import org.openedit.repository.ContentItem;
14 import org.openedit.repository.RepositoryException;
15
16 import com.openedit.util.PathUtilities;
17
18 /**
19  * @author Matthew Avery, mavery@einnovation.com
20  */

21 public class FileItem extends ContentItem
22 {
23     protected File JavaDoc fieldFile;
24     /**
25      *
26      */

27     public FileItem()
28     {
29     }
30     protected String JavaDoc getParentPath()
31     {
32         return PathUtilities.extractDirectoryPath( getPath() );
33     }
34
35     public Date JavaDoc lastModified()
36     {
37         return new Date JavaDoc( getFile().lastModified() );
38     }
39
40     public InputStream JavaDoc getInputStream() throws RepositoryException
41     {
42         if ( isFolder() )
43         {
44             return createFileListingStream();
45         }
46         try
47         {
48             if (getFile().exists() )
49             {
50                 return new FileInputStream JavaDoc( getFile() );
51             }
52         }
53         catch( FileNotFoundException JavaDoc e )
54         {
55             throw new RepositoryException( e );
56         }
57         return null;
58     }
59
60     protected InputStream JavaDoc createFileListingStream()
61     {
62         File JavaDoc[] files = getFile().listFiles();
63         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
64         for ( int i = 0; i < files.length; i++ )
65         {
66             if ( !files[i].getName().equals(".versions") )
67             {
68                 sb.append( files[i].getName() + "\n" );
69             }
70         }
71         return new ByteArrayInputStream JavaDoc( sb.toString().getBytes() );
72     }
73     public boolean exists()
74     {
75         return getFile().exists();
76     }
77
78     public boolean isFolder()
79     {
80         return getFile().isDirectory();
81     }
82
83     public boolean isWritable()
84     {
85         return true;
86     }
87
88     public File JavaDoc getFile()
89     {
90         return fieldFile;
91     }
92     public void setFile( File JavaDoc file )
93     {
94         fieldFile = file;
95     }
96     /* (non-javadoc)
97      * @see com.einnovation.repository.ContentItem#getLength()
98      */

99     public long getLength()
100     {
101         return getFile().length();
102     }
103 }
104
Popular Tags