1 18 package org.apache.tools.ant.types.resources; 19 20 import java.io.File ; 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 import java.io.IOException ; 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 36 public class TarResource extends ArchiveResource { 37 38 private String userName = ""; 39 private String groupName = ""; 40 private int uid; 41 private int gid; 42 43 46 public TarResource() { 47 } 48 49 55 public TarResource(File a, TarEntry e) { 56 super(a, true); 57 setEntry(e); 58 } 59 60 66 public TarResource(Resource a, TarEntry e) { 67 super(a, true); 68 setEntry(e); 69 } 70 71 77 public InputStream getInputStream() throws IOException { 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 103 public OutputStream getOutputStream() throws IOException { 104 if (isReference()) { 105 return ((Resource) getCheckedRef()).getOutputStream(); 106 } 107 throw new UnsupportedOperationException ( 108 "Use the tar task for tar output."); 109 } 110 111 114 public String getUserName() { 115 if (isReference()) { 116 return ((TarResource) getCheckedRef()).getUserName(); 117 } 118 return userName; 119 } 120 121 124 public String getGroup() { 125 if (isReference()) { 126 return ((TarResource) getCheckedRef()).getGroup(); 127 } 128 return groupName; 129 } 130 131 134 public int getUid() { 135 if (isReference()) { 136 return ((TarResource) getCheckedRef()).getUid(); 137 } 138 return uid; 139 } 140 141 144 public int getGid() { 145 if (isReference()) { 146 return ((TarResource) getCheckedRef()).getGid(); 147 } 148 return uid; 149 } 150 151 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 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 |