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