View Javadoc
1   package org.codehaus.plexus.archiver.tar;
2   
3   import javax.annotation.Nonnull;
4   
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.net.URL;
8   
9   import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
10  import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
11  import org.codehaus.plexus.components.io.attributes.SimpleResourceAttributes;
12  import org.codehaus.plexus.components.io.functions.ResourceAttributeSupplier;
13  import org.codehaus.plexus.components.io.resources.AbstractPlexusIoResource;
14  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
15  
16  public class TarResource extends AbstractPlexusIoResource implements ResourceAttributeSupplier {
17  
18      private final TarFile tarFile;
19  
20      private final TarArchiveEntry entry;
21  
22      private PlexusIoResourceAttributes attributes;
23  
24      public TarResource(TarFile tarFile, TarArchiveEntry entry) {
25          super(
26                  entry.getName(),
27                  getLastModifiedTime(entry),
28                  entry.isDirectory() ? PlexusIoResource.UNKNOWN_RESOURCE_SIZE : entry.getSize(),
29                  !entry.isDirectory(),
30                  entry.isDirectory(),
31                  true);
32  
33          this.tarFile = tarFile;
34          this.entry = entry;
35      }
36  
37      private static long getLastModifiedTime(TarArchiveEntry entry) {
38          long l = entry.getModTime().getTime();
39          return l == -1 ? PlexusIoResource.UNKNOWN_MODIFICATION_DATE : l;
40      }
41  
42      @Override
43      public synchronized PlexusIoResourceAttributes getAttributes() {
44          if (attributes == null) {
45              attributes = new SimpleResourceAttributes(
46                      entry.getUserId(), entry.getUserName(), entry.getGroupId(), entry.getGroupName(), entry.getMode());
47          }
48  
49          return attributes;
50      }
51  
52      public synchronized void setAttributes(PlexusIoResourceAttributes attributes) {
53          this.attributes = attributes;
54      }
55  
56      @Override
57      public URL getURL() throws IOException {
58          return null;
59      }
60  
61      @Nonnull
62      @Override
63      public InputStream getContents() throws IOException {
64          return tarFile.getInputStream(entry);
65      }
66  }