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 */
017package org.apache.commons.compress.archivers.zip;
018
019import java.io.InputStream;
020
021import org.apache.commons.compress.parallel.InputStreamSupplier;
022
023/**
024 * A Thread-safe representation of a ZipArchiveEntry that is used to add entries to parallel archives.
025 *
026 * @since 1.10
027 */
028public class ZipArchiveEntryRequest {
029
030    /**
031     * Creates a ZipArchiveEntryRequest
032     *
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    private final int method;
049
050    private ZipArchiveEntryRequest(final ZipArchiveEntry zipArchiveEntry, final InputStreamSupplier payloadSupplier) {
051        // this constructor has "safe" access to all member variables on zipArchiveEntry
052        this.zipArchiveEntry = zipArchiveEntry;
053        this.payloadSupplier = payloadSupplier;
054        this.method = zipArchiveEntry.getMethod();
055    }
056
057    /**
058     * Gets the compression method to use
059     *
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     *
069     * @return The input stream.
070     */
071    public InputStream getPayloadStream() {
072        return payloadSupplier.get();
073    }
074
075    /**
076     * Gets the underlying entry. Do not use this method from threads that did not create the instance itself !
077     *
078     * @return the zipArchiveEntry that is basis for this request
079     */
080    ZipArchiveEntry getZipArchiveEntry() {
081        return zipArchiveEntry;
082    }
083}