View Javadoc
1   /*
2    * Copyright 2014 The Codehaus Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.plexus.components.io.resources;
17  
18  import org.apache.commons.io.IOUtils;
19  import org.apache.commons.io.output.DeferredFileOutputStream;
20  import org.codehaus.plexus.components.io.functions.ContentSupplier;
21  import org.codehaus.plexus.components.io.functions.NameSupplier;
22  import org.codehaus.plexus.components.io.functions.SizeSupplier;
23  import org.codehaus.plexus.components.io.resources.proxy.ProxyFactory;
24  
25  import javax.annotation.Nonnull;
26  import java.io.ByteArrayInputStream;
27  import java.io.FileInputStream;
28  import java.io.IOException;
29  import java.io.InputStream;
30  
31  class Deferred implements ContentSupplier, NameSupplier, SizeSupplier
32  {
33      final DeferredFileOutputStream dfos;
34  
35      final PlexusIoResource resource;
36  
37      final PlexusIoResourceCollection owner;
38  
39      public Deferred( final PlexusIoResource resource, PlexusIoResourceCollection owner, boolean hasTransformer )
40          throws IOException
41      {
42          this.resource = resource;
43          this.owner = owner;
44          dfos = hasTransformer ? new DeferredFileOutputStream( 5000000, "p-archiver", null, null ) : null;
45          if ( dfos != null )
46          {
47              InputStream inputStream = owner.getInputStream( resource );
48              IOUtils.copy( inputStream, dfos );
49              IOUtils.closeQuietly( inputStream );
50          }
51  
52      }
53  
54      @Nonnull
55      public InputStream getContents()
56          throws IOException
57      {
58          if ( dfos == null )
59          {
60              return resource.getContents();
61          }
62          if ( dfos.isInMemory() )
63          {
64              return new ByteArrayInputStream( dfos.getData() );
65          }
66          else
67          {
68              return new FileInputStream( dfos.getFile() )
69              {
70                  @Override
71                  public void close()
72                      throws IOException
73                  {
74                      super.close();
75                      dfos.getFile().delete();
76                  }
77              };
78          }
79      }
80  
81      public long getSize()
82      {
83          if ( dfos == null )
84          {
85              return resource.getSize();
86          }
87          if ( dfos.isInMemory() )
88          {
89              return dfos.getByteCount();
90          }
91          else
92          {
93              return dfos.getFile().length();
94          }
95      }
96  
97      public String getName()
98      {
99          return owner.getName( resource );
100     }
101 
102     public PlexusIoResource asResource()
103     {
104         return ProxyFactory.createProxy( resource, Deferred.this );
105     }
106 
107 }