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