KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resources > TarResource


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

18 package org.apache.tools.ant.types.resources;
19
20 import java.io.File JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import org.apache.tools.ant.Project;
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.types.Resource;
28 import org.apache.tools.ant.util.FileUtils;
29 import org.apache.tools.tar.TarEntry;
30 import org.apache.tools.tar.TarInputStream;
31
32 /**
33  * A Resource representation of an entry in a tar archive.
34  * @since Ant 1.7
35  */

36 public class TarResource extends ArchiveResource {
37
38     private String JavaDoc userName = "";
39     private String JavaDoc groupName = "";
40     private int uid;
41     private int gid;
42
43     /**
44      * Default constructor.
45      */

46     public TarResource() {
47     }
48
49     /**
50      * Construct a TarResource representing the specified
51      * entry in the specified archive.
52      * @param a the archive as File.
53      * @param e the TarEntry.
54      */

55     public TarResource(File JavaDoc a, TarEntry e) {
56         super(a, true);
57         setEntry(e);
58     }
59
60     /**
61      * Construct a TarResource representing the specified
62      * entry in the specified archive.
63      * @param a the archive as Resource.
64      * @param e the TarEntry.
65      */

66     public TarResource(Resource a, TarEntry e) {
67         super(a, true);
68         setEntry(e);
69     }
70
71     /**
72      * Return an InputStream for reading the contents of this Resource.
73      * @return an InputStream object.
74      * @throws IOException if the tar file cannot be opened,
75      * or the entry cannot be read.
76      */

77     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
78         if (isReference()) {
79             return ((Resource) getCheckedRef()).getInputStream();
80         }
81         Resource archive = getArchive();
82         final TarInputStream i = new TarInputStream(archive.getInputStream());
83         TarEntry te = null;
84         while ((te = i.getNextEntry()) != null) {
85             if (te.getName().equals(getName())) {
86                 return i;
87             }
88         }
89
90         FileUtils.close(i);
91         throw new BuildException("no entry " + getName() + " in "
92                                  + getArchive());
93     }
94
95     /**
96      * Get an OutputStream for the Resource.
97      * @return an OutputStream to which content can be written.
98      * @throws IOException if unable to provide the content of this
99      * Resource as a stream.
100      * @throws UnsupportedOperationException if OutputStreams are not
101      * supported for this Resource type.
102      */

103     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
104         if (isReference()) {
105             return ((Resource) getCheckedRef()).getOutputStream();
106         }
107         throw new UnsupportedOperationException JavaDoc(
108             "Use the tar task for tar output.");
109     }
110
111     /**
112      * @return the user name for the tar entry
113      */

114     public String JavaDoc getUserName() {
115         if (isReference()) {
116             return ((TarResource) getCheckedRef()).getUserName();
117         }
118         return userName;
119     }
120
121     /**
122      * @return the group name for the tar entry
123      */

124     public String JavaDoc getGroup() {
125         if (isReference()) {
126             return ((TarResource) getCheckedRef()).getGroup();
127         }
128         return groupName;
129     }
130
131     /**
132      * @return the uid for the tar entry
133      */

134     public int getUid() {
135         if (isReference()) {
136             return ((TarResource) getCheckedRef()).getUid();
137         }
138         return uid;
139     }
140
141     /**
142      * @return the uid for the tar entry
143      */

144     public int getGid() {
145         if (isReference()) {
146             return ((TarResource) getCheckedRef()).getGid();
147         }
148         return uid;
149     }
150
151     /**
152      * fetches information from the named entry inside the archive.
153      */

154     protected void fetchEntry() {
155         Resource archive = getArchive();
156         TarInputStream i = null;
157         try {
158             i = new TarInputStream(archive.getInputStream());
159             TarEntry te = null;
160             while ((te = i.getNextEntry()) != null) {
161                 if (te.getName().equals(getName())) {
162                     setEntry(te);
163                     return;
164                 }
165             }
166         } catch (IOException JavaDoc e) {
167             log(e.getMessage(), Project.MSG_DEBUG);
168             throw new BuildException(e);
169         } finally {
170             if (i != null) {
171                 FileUtils.close(i);
172             }
173         }
174         setEntry(null);
175     }
176
177     private void setEntry(TarEntry e) {
178         if (e == null) {
179             setExists(false);
180             return;
181         }
182         setName(e.getName());
183         setExists(true);
184         setLastModified(e.getModTime().getTime());
185         setDirectory(e.isDirectory());
186         setSize(e.getSize());
187         setMode(e.getMode());
188         userName = e.getUserName();
189         groupName = e.getGroupName();
190         uid = e.getUserId();
191         gid = e.getGroupId();
192     }
193
194 }
195
Popular Tags