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 javax.annotation.Nonnull;
19  
20  import java.io.ByteArrayInputStream;
21  import java.io.FileInputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  
25  import org.apache.commons.io.IOUtils;
26  import org.apache.commons.io.output.DeferredFileOutputStream;
27  import org.codehaus.plexus.components.io.functions.ContentSupplier;
28  import org.codehaus.plexus.components.io.functions.HardLinkIdentitySupplier;
29  import org.codehaus.plexus.components.io.functions.NameSupplier;
30  import org.codehaus.plexus.components.io.functions.SizeSupplier;
31  import org.codehaus.plexus.components.io.resources.proxy.ProxyFactory;
32  
33  class Deferred implements ContentSupplier, NameSupplier, SizeSupplier, HardLinkIdentitySupplier {
34      final DeferredFileOutputStream dfos;
35  
36      final PlexusIoResource resource;
37  
38      final PlexusIoResourceCollection owner;
39  
40      public Deferred(final PlexusIoResource resource, PlexusIoResourceCollection owner, boolean hasTransformer)
41              throws IOException {
42          this.resource = resource;
43          this.owner = owner;
44          dfos = hasTransformer
45                  ? DeferredFileOutputStream.builder()
46                          .setThreshold(5000000)
47                          .setPrefix("p-archiver")
48                          .get()
49                  : null;
50          if (dfos != null) {
51              try (InputStream inputStream = owner.getInputStream(resource);
52                      DeferredFileOutputStream closeable = dfos) {
53                  IOUtils.copy(inputStream, dfos);
54              }
55          }
56      }
57  
58      @Nonnull
59      public InputStream getContents() throws IOException {
60          if (dfos == null) {
61              return resource.getContents();
62          }
63          if (dfos.isInMemory()) {
64              return new ByteArrayInputStream(dfos.getData());
65          } else {
66              return new FileInputStream(dfos.getFile()) {
67                  @Override
68                  public void close() throws IOException {
69                      super.close();
70                      dfos.getFile().delete();
71                  }
72              };
73          }
74      }
75  
76      public long getSize() {
77          if (dfos == null) {
78              return resource.getSize();
79          }
80          if (dfos.isInMemory()) {
81              return dfos.getByteCount();
82          } else {
83              return dfos.getFile().length();
84          }
85      }
86  
87      public String getName() {
88          return owner.getName(resource);
89      }
90  
91      /**
92       * Preserves source identity only when this wrapper has not transformed the contents.
93       *
94       * @return the source identity, or null after transformation
95       * @throws IOException if the source identity cannot be read
96       */
97      @Override
98      public Object getHardLinkIdentity() throws IOException {
99          return dfos == null && resource instanceof HardLinkIdentitySupplier
100                 ? ((HardLinkIdentitySupplier) resource).getHardLinkIdentity()
101                 : null;
102     }
103 
104     public PlexusIoResource asResource() {
105         return ProxyFactory.createProxy(resource, Deferred.this);
106     }
107 }