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 org.apache.commons.compress.parallel.InputStreamSupplier;
021
022import java.io.InputStream;
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     The zipArchiveEntry is not thread safe, and cannot be safely accessed by the getters of this class.
032     It is safely accessible during the construction part of this class and also after the
033     thread pools have been shut down.
034     */
035    private final ZipArchiveEntry zipArchiveEntry;
036    private final InputStreamSupplier payloadSupplier;
037    private final int method;
038
039
040    private ZipArchiveEntryRequest(final ZipArchiveEntry zipArchiveEntry, final InputStreamSupplier payloadSupplier) {
041        // this constructor has "safe" access to all member variables on zipArchiveEntry
042        this.zipArchiveEntry = zipArchiveEntry;
043        this.payloadSupplier = payloadSupplier;
044        this.method = zipArchiveEntry.getMethod();
045    }
046
047    /**
048     * Create a ZipArchiveEntryRequest
049     * @param zipArchiveEntry The entry to use
050     * @param payloadSupplier The payload that will be added to the zip entry.
051     * @return The newly created request
052     */
053    public static ZipArchiveEntryRequest createZipArchiveEntryRequest(final ZipArchiveEntry zipArchiveEntry, final InputStreamSupplier payloadSupplier) {
054        return new ZipArchiveEntryRequest(zipArchiveEntry, payloadSupplier);
055    }
056
057    /**
058     * The payload that will be added to this zip entry
059     * @return The input stream.
060     */
061    public InputStream getPayloadStream() {
062        return payloadSupplier.get();
063    }
064
065    /**
066     * The compression method to use
067     * @return The compression method to use
068     */
069    public int getMethod(){
070       return method;
071    }
072
073
074    /**
075     * Gets the underlying entry. Do not use this methods from threads that did not create the instance itself !
076     * @return the zipArchiveEntry that is basis for this request
077     */
078    ZipArchiveEntry getZipArchiveEntry() {
079        return zipArchiveEntry;
080    }
081}