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              InputStream inputStream = owner.getInputStream(resource);
51              IOUtils.copy(inputStream, dfos);
52              IOUtils.closeQuietly(inputStream);
53          }
54      }
55  
56      @Nonnull
57      public InputStream getContents() throws IOException {
58          if (dfos == null) {
59              return resource.getContents();
60          }
61          if (dfos.isInMemory()) {
62              return new ByteArrayInputStream(dfos.getData());
63          } else {
64              return new FileInputStream(dfos.getFile()) {
65                  @Override
66                  public void close() throws IOException {
67                      super.close();
68                      dfos.getFile().delete();
69                  }
70              };
71          }
72      }
73  
74      public long getSize() {
75          if (dfos == null) {
76              return resource.getSize();
77          }
78          if (dfos.isInMemory()) {
79              return dfos.getByteCount();
80          } else {
81              return dfos.getFile().length();
82          }
83      }
84  
85      public String getName() {
86          return owner.getName(resource);
87      }
88  
89      public PlexusIoResource asResource() {
90          return ProxyFactory.createProxy(resource, Deferred.this);
91      }
92  }