View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.commons.compress.archivers.zip;
18  
19  import java.io.InputStream;
20  
21  import org.apache.commons.compress.parallel.InputStreamSupplier;
22  
23  /**
24   * A Thread-safe representation of a ZipArchiveEntry that is used to add entries to parallel archives.
25   *
26   * @since 1.10
27   */
28  public class ZipArchiveEntryRequest {
29  
30      /**
31       * Creates a ZipArchiveEntryRequest
32       *
33       * @param zipArchiveEntry The entry to use
34       * @param payloadSupplier The payload that will be added to the ZIP entry.
35       * @return The newly created request
36       */
37      public static ZipArchiveEntryRequest createZipArchiveEntryRequest(final ZipArchiveEntry zipArchiveEntry, final InputStreamSupplier payloadSupplier) {
38          return new ZipArchiveEntryRequest(zipArchiveEntry, payloadSupplier);
39      }
40  
41      /**
42       * The ZIPArchiveEntry is not thread safe, and cannot be safely accessed by the getters of this class. It is safely accessible during the construction part
43       * of this class and also after the thread pools have been shut down.
44       */
45      private final ZipArchiveEntry zipArchiveEntry;
46      private final InputStreamSupplier payloadSupplier;
47  
48      private final int method;
49  
50      private ZipArchiveEntryRequest(final ZipArchiveEntry zipArchiveEntry, final InputStreamSupplier payloadSupplier) {
51          // this constructor has "safe" access to all member variables on zipArchiveEntry
52          this.zipArchiveEntry = zipArchiveEntry;
53          this.payloadSupplier = payloadSupplier;
54          this.method = zipArchiveEntry.getMethod();
55      }
56  
57      /**
58       * Gets the compression method to use
59       *
60       * @return The compression method to use
61       */
62      public int getMethod() {
63          return method;
64      }
65  
66      /**
67       * Gets the payload that will be added to this ZIP entry
68       *
69       * @return The input stream.
70       */
71      public InputStream getPayloadStream() {
72          return payloadSupplier.get();
73      }
74  
75      /**
76       * Gets the underlying entry. Do not use this method from threads that did not create the instance itself !
77       *
78       * @return the zipArchiveEntry that is basis for this request
79       */
80      ZipArchiveEntry getZipArchiveEntry() {
81          return zipArchiveEntry;
82      }
83  }