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.NameSupplier;
29  import org.codehaus.plexus.components.io.functions.SizeSupplier;
30  import org.codehaus.plexus.components.io.resources.proxy.ProxyFactory;
31  
32  class Deferred implements ContentSupplier, NameSupplier, SizeSupplier {
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          this.resource = resource;
42          this.owner = owner;
43          dfos = hasTransformer
44                  ? DeferredFileOutputStream.builder()
45                          .setThreshold(5000000)
46                          .setPrefix("p-archiver")
47                          .get()
48                  : null;
49          if (dfos != null) {
50              try (InputStream inputStream = owner.getInputStream(resource);
51                      DeferredFileOutputStream closeable = dfos) {
52                  IOUtils.copy(inputStream, dfos);
53              }
54          }
55      }
56  
57      @Nonnull
58      public InputStream getContents() throws IOException {
59          if (dfos == null) {
60              return resource.getContents();
61          }
62          if (dfos.isInMemory()) {
63              return new ByteArrayInputStream(dfos.getData());
64          } else {
65              return new FileInputStream(dfos.getFile()) {
66                  @Override
67                  public void close() throws IOException {
68                      super.close();
69                      dfos.getFile().delete();
70                  }
71              };
72          }
73      }
74  
75      public long getSize() {
76          if (dfos == null) {
77              return resource.getSize();
78          }
79          if (dfos.isInMemory()) {
80              return dfos.getByteCount();
81          } else {
82              return dfos.getFile().length();
83          }
84      }
85  
86      public String getName() {
87          return owner.getName(resource);
88      }
89  
90      public PlexusIoResource asResource() {
91          return ProxyFactory.createProxy(resource, Deferred.this);
92      }
93  }