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                      (int) entry.getLongUserId(),
47                      entry.getUserName(),
48                      (int) entry.getLongGroupId(),
49                      entry.getGroupName(),
50                      entry.getMode());
51          }
52  
53          return attributes;
54      }
55  
56      public synchronized void setAttributes(PlexusIoResourceAttributes attributes) {
57          this.attributes = attributes;
58      }
59  
60      @Override
61      public URL getURL() throws IOException {
62          return null;
63      }
64  
65      @Nonnull
66      @Override
67      public InputStream getContents() throws IOException {
68          return tarFile.getInputStream(entry);
69      }
70  }