ZipArchiveEntryRequest.java

  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. import java.io.InputStream;

  19. import org.apache.commons.compress.parallel.InputStreamSupplier;

  20. /**
  21.  * A Thread-safe representation of a ZipArchiveEntry that is used to add entries to parallel archives.
  22.  *
  23.  * @since 1.10
  24.  */
  25. public class ZipArchiveEntryRequest {

  26.     /**
  27.      * Creates a ZipArchiveEntryRequest
  28.      *
  29.      * @param zipArchiveEntry The entry to use
  30.      * @param payloadSupplier The payload that will be added to the ZIP entry.
  31.      * @return The newly created request
  32.      */
  33.     public static ZipArchiveEntryRequest createZipArchiveEntryRequest(final ZipArchiveEntry zipArchiveEntry, final InputStreamSupplier payloadSupplier) {
  34.         return new ZipArchiveEntryRequest(zipArchiveEntry, payloadSupplier);
  35.     }

  36.     /**
  37.      * 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
  38.      * of this class and also after the thread pools have been shut down.
  39.      */
  40.     private final ZipArchiveEntry zipArchiveEntry;
  41.     private final InputStreamSupplier payloadSupplier;

  42.     private final int method;

  43.     private ZipArchiveEntryRequest(final ZipArchiveEntry zipArchiveEntry, final InputStreamSupplier payloadSupplier) {
  44.         // this constructor has "safe" access to all member variables on zipArchiveEntry
  45.         this.zipArchiveEntry = zipArchiveEntry;
  46.         this.payloadSupplier = payloadSupplier;
  47.         this.method = zipArchiveEntry.getMethod();
  48.     }

  49.     /**
  50.      * Gets the compression method to use
  51.      *
  52.      * @return The compression method to use
  53.      */
  54.     public int getMethod() {
  55.         return method;
  56.     }

  57.     /**
  58.      * Gets the payload that will be added to this ZIP entry
  59.      *
  60.      * @return The input stream.
  61.      */
  62.     public InputStream getPayloadStream() {
  63.         return payloadSupplier.get();
  64.     }

  65.     /**
  66.      * Gets the underlying entry. Do not use this method from threads that did not create the instance itself !
  67.      *
  68.      * @return the zipArchiveEntry that is basis for this request
  69.      */
  70.     ZipArchiveEntry getZipArchiveEntry() {
  71.         return zipArchiveEntry;
  72.     }
  73. }