KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > io > ArchiveEntryMetaData


1 /*
2  * ArchiveEntryMetaData.java
3  *
4  * Created on 26. Februar 2006, 22:03
5  */

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

21
22 package de.schlichtherle.io;
23
24 import de.schlichtherle.io.archive.Archive;
25 import de.schlichtherle.io.archive.spi.ArchiveDriver;
26 import de.schlichtherle.io.archive.spi.ArchiveEntry;
27
28 import java.io.FileFilter JavaDoc;
29 import java.io.FilenameFilter JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Set JavaDoc;
35
36 import javax.swing.Icon JavaDoc;
37
38 /**
39  * <em>This class is <b>not</b> intended for public use!</em>
40  * It's only public for technical reasons and may get renamed or entirely
41  * disappear without notice.
42  * <p>
43  * This class annotates an {@link ArchiveEntry} with the fields and methods
44  * required to implement the concept of a directory.
45  *
46  * @author Christian Schlichtherle
47  * @version @version@
48  * @since TrueZIP 6.0
49  */

50 public class ArchiveEntryMetaData {
51
52     /**
53      * This thread local variable returns an {@link ArrayList} which is used
54      * as a temporary buffer to implement filtered list methods.
55      */

56     private static final ThreadLocal JavaDoc threadLocal = new ThreadLocal JavaDoc() {
57         protected Object JavaDoc initialValue() {
58             return new ArrayList JavaDoc(64);
59         }
60     };
61
62     /**
63      * If the entry from which this object has been created represents a
64      * directory, then this is a valid reference to a set of Strings,
65      * representing the children names.
66      * Otherwise this field is initialized with <code>null</code>.
67      */

68     final Set JavaDoc children;
69
70     /**
71      * A package private constructor.
72      * Used by the factory in this package only.
73      */

74     ArchiveEntryMetaData(final ArchiveEntry entry) {
75         this.children = entry.isDirectory() ? new HashSet JavaDoc() : null;
76     }
77
78     /**
79      * Returns the names of the members in this directory in a newly
80      * created array. The returned array is <em>not</em> sorted.
81      * This is the most efficient list method.
82      *
83      * @throws NullPointerException If the entry from which this object has
84      * been created is not a directory.
85      */

86     String JavaDoc[] list() {
87         final String JavaDoc[] list = new String JavaDoc[children.size()];
88         children.toArray(list);
89
90         return list;
91     }
92
93     /**
94      * Returns the names of the members in this directory which are
95      * accepted by <code>filenameFilter</code> in a newly created array.
96      * <code>dir</code> is used as the directory argument for the
97      * <code>filenameFilter</code>. The returned array is <em>not</em> sorted.
98      *
99      * @param filenameFilter a valid object - must not be <code>null</code>.
100      * @param dir the directory represented as a File object.
101      *
102      * @throws NullPointerException If the entry from which this object has
103      * been created is not a directory.
104      */

105     String JavaDoc[] list(
106             final FilenameFilter JavaDoc filenameFilter,
107             final File dir) {
108         final List JavaDoc filteredList = (List JavaDoc) threadLocal.get();
109         assert filteredList.isEmpty();
110         try {
111             for (final Iterator JavaDoc i = children.iterator(); i.hasNext(); ) {
112                 final String JavaDoc child = (String JavaDoc) i.next();
113                 if (filenameFilter.accept(dir, child))
114                     filteredList.add(child);
115             }
116             final String JavaDoc[] list = new String JavaDoc[filteredList.size()];
117             filteredList.toArray(list);
118
119             return list;
120         } finally {
121             filteredList.clear(); // support garbage collection of zip controllers!
122
}
123     }
124
125     /**
126      * Returns <code>File</code> objects for the members in this directory
127      * which are accepted by <code>filenameFilter</code> in a newly created
128      * array.
129      * <code>dir</code> is used as the directory argument for the
130      * <code>filenameFilter</code>. The returned array is <em>not</em> sorted.
131      *
132      * @param filenameFilter may be <code>null</code> to accept all members.
133      * @param dir the directory represented as a File object.
134      *
135      * @throws NullPointerException If the entry from which this object has
136      * been created is not a directory.
137      */

138     File[] listFiles(
139             FilenameFilter JavaDoc filenameFilter,
140             final File dir,
141             final FileFactory factory) {
142         final List JavaDoc filteredList = (List JavaDoc) threadLocal.get();
143         assert filteredList.isEmpty();
144         try {
145             for (final Iterator JavaDoc i = children.iterator(); i.hasNext(); ) {
146                 final String JavaDoc child = (String JavaDoc) i.next();
147                 if (filenameFilter == null
148                     || filenameFilter.accept(dir, child))
149                     filteredList.add(factory.createFile(dir, child));
150             }
151             final File[] list = new File[filteredList.size()];
152             filteredList.toArray(list);
153
154             return list;
155         } finally {
156             filteredList.clear(); // support garbage collection of zip controllers!
157
}
158     }
159
160     /**
161      * Returns <code>File</code> objects for the members in this directory
162      * which are accepted by <code>filenameFilter</code> in a newly created
163      * array.
164      * <code>dir</code> is used as the directory argument for the
165      * <code>filenameFilter</code>. The returned array is <em>not</em> sorted.
166      *
167      * @param fileFilter may be <code>null</code> to accept all members.
168      * @param dir the directory represented as a File object.
169      *
170      * @throws NullPointerException If the entry from which this object has
171      * been created is not a directory.
172      */

173     File[] listFiles(
174             final FileFilter JavaDoc fileFilter,
175             final File dir,
176             final FileFactory factory) {
177         final List JavaDoc filteredList = (List JavaDoc) threadLocal.get();
178         assert filteredList.isEmpty();
179         try {
180             for (final Iterator JavaDoc i = children.iterator(); i.hasNext(); ) {
181                 final String JavaDoc child = (String JavaDoc) i.next();
182                 final File file = factory.createFile(dir, child);
183                 if (fileFilter == null || fileFilter.accept(file))
184                     filteredList.add(file);
185             }
186             final File[] list = new File[filteredList.size()];
187             filteredList.toArray(list);
188
189             return list;
190         } finally {
191             filteredList.clear(); // support garbage collection of zip controllers!
192
}
193     }
194 }
195
Popular Tags