    001/*
    002 *  Licensed to the Apache Software Foundation (ASF) under one or more
    003 *  contributor license agreements.  See the NOTICE file distributed with
    004 *  this work for additional information regarding copyright ownership.
    005 *  The ASF licenses this file to You under the Apache License, Version 2.0
    006 *  (the "License"); you may not use this file except in compliance with
    007 *  the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 *
    017 */
    018package org.apache.commons.compress.archivers.zip;
    019
    020import java.io.InputStream;
    021
    022import org.apache.commons.compress.parallel.InputStreamSupplier;
    023
    024/**
    025 * A Thread-safe representation of a ZipArchiveEntry that is used to add entries to parallel archives.
    026 *
    027 * @since 1.10
    028 */
    029public class ZipArchiveEntryRequest {
    030
    031    /**
    032     * Creates a ZipArchiveEntryRequest
    033     * @param zipArchiveEntry The entry to use
    034     * @param payloadSupplier The payload that will be added to the ZIP entry.
    035     * @return The newly created request
    036     */
    037    public static ZipArchiveEntryRequest createZipArchiveEntryRequest(final ZipArchiveEntry zipArchiveEntry, final InputStreamSupplier payloadSupplier) {
    038        return new ZipArchiveEntryRequest(zipArchiveEntry, payloadSupplier);
    039    }
    040
    041    /**
    042     * 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
    043     * of this class and also after the thread pools have been shut down.
    044     */
    045    private final ZipArchiveEntry zipArchiveEntry;
    046    private final InputStreamSupplier payloadSupplier;
    047
    048
    049    private final int method;
    050
    051    private ZipArchiveEntryRequest(final ZipArchiveEntry zipArchiveEntry, final InputStreamSupplier payloadSupplier) {
    052        // this constructor has "safe" access to all member variables on zipArchiveEntry
    053        this.zipArchiveEntry = zipArchiveEntry;
    054        this.payloadSupplier = payloadSupplier;
    055        this.method = zipArchiveEntry.getMethod();
    056    }
    057
    058    /**
    059     * Gets the compression method to use
    060     * @return The compression method to use
    061     */
    062    public int getMethod(){
    063       return method;
    064    }
    065
    066    /**
    067     * Gets the payload that will be added to this ZIP entry
    068     * @return The input stream.
    069     */
    070    public InputStream getPayloadStream() {
    071        return payloadSupplier.get();
    072    }
    073
    074
    075    /**
    076     * Gets the underlying entry. Do not use this methods from threads that did not create the instance itself !
    077     * @return the zipArchiveEntry that is basis for this request
    078     */
    079    ZipArchiveEntry getZipArchiveEntry() {
    080        return zipArchiveEntry;
    081    }
    082}