KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > tar > TarFileObject


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.provider.tar;
17
18 import java.io.InputStream JavaDoc;
19 import java.util.HashSet JavaDoc;
20
21 import org.apache.commons.compress.archivers.tar.TarEntry;
22 import org.apache.commons.vfs.FileName;
23 import org.apache.commons.vfs.FileObject;
24 import org.apache.commons.vfs.FileSystemException;
25 import org.apache.commons.vfs.FileType;
26 import org.apache.commons.vfs.provider.AbstractFileObject;
27
28 /**
29  * A file in a Tar file system.
30  */

31 public class TarFileObject
32     extends AbstractFileObject
33     implements FileObject
34 {
35     private final HashSet JavaDoc children = new HashSet JavaDoc();
36     private final TarFileSystem fs;
37     protected TarEntry entry;
38     private FileType type;
39
40     protected TarFileObject(FileName name,
41                             TarEntry entry,
42                             TarFileSystem fs,
43                             boolean tarExists) throws FileSystemException
44     {
45         super(name, fs);
46         this.fs = fs;
47         setTarEntry(entry);
48         if (!tarExists)
49         {
50             type = FileType.IMAGINARY;
51         }
52     }
53
54     /**
55      * Sets the details for this file object.
56      */

57     protected void setTarEntry(final TarEntry entry)
58     {
59         if (this.entry != null)
60         {
61             return;
62         }
63
64         if ((entry == null) || (entry.isDirectory()))
65         {
66             type = FileType.FOLDER;
67         }
68         else
69         {
70             type = FileType.FILE;
71         }
72
73         this.entry = entry;
74     }
75
76     /**
77      * Attaches a child
78      */

79     protected void attachChild(FileName childName)
80     {
81         children.add(childName.getBaseName());
82     }
83
84     /**
85      * Returns true if this file is read-only.
86      */

87     public boolean isWriteable()
88     {
89         return false;
90     }
91
92     /**
93      * Returns the file's type.
94      */

95     protected FileType doGetType()
96     {
97         return type;
98     }
99
100     /**
101      * Lists the children of the file.
102      */

103     protected String JavaDoc[] doListChildren()
104     {
105         return (String JavaDoc[]) children.toArray(new String JavaDoc[children.size()]);
106     }
107
108     /**
109      * Returns the size of the file content (in bytes). Is only called if
110      * {@link #doGetType} returns {@link FileType#FILE}.
111      */

112     protected long doGetContentSize()
113     {
114         if (entry == null)
115         {
116             return 0;
117         }
118
119         return entry.getSize();
120     }
121
122     /**
123      * Returns the last modified time of this file.
124      */

125     protected long doGetLastModifiedTime() throws Exception JavaDoc
126     {
127         if (entry == null)
128         {
129             return 0;
130         }
131
132         return entry.getModTime().getTime();
133     }
134
135     /**
136      * Creates an input stream to read the file content from. Is only called
137      * if {@link #doGetType} returns {@link FileType#FILE}. The input stream
138      * returned by this method is guaranteed to be closed before this
139      * method is called again.
140      */

141     protected InputStream JavaDoc doGetInputStream() throws Exception JavaDoc
142     {
143         return fs.getInputStream(entry);
144     }
145 }
146
Popular Tags