    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.IOException;
    021import java.nio.file.Files;
    022import java.nio.file.Path;
    023import java.util.concurrent.atomic.AtomicInteger;
    024
    025import org.apache.commons.compress.parallel.FileBasedScatterGatherBackingStore;
    026import org.apache.commons.compress.parallel.ScatterGatherBackingStore;
    027import org.apache.commons.compress.parallel.ScatterGatherBackingStoreSupplier;
    028
    029/**
    030 * Implements {@link ScatterGatherBackingStoreSupplier} using a temporary folder.
    031 * <p>
    032 * For example:
    033 * </p>
    034 * <pre>
    035 * final Path dir = Paths.get("target/custom-temp-dir");
    036 * Files.createDirectories(dir);
    037 * final ParallelScatterZipCreator zipCreator = new ParallelScatterZipCreator(
    038 *     Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()),
    039 *     new DefaultBackingStoreSupplier(dir));
    040 * </pre>
    041 *
    042 * @since 1.23
    043 */
    044public class DefaultBackingStoreSupplier implements ScatterGatherBackingStoreSupplier {
    045
    046    private static final String PREFIX = "parallelscatter";
    047
    048    private final AtomicInteger storeNum = new AtomicInteger();
    049
    050    private final Path dir;
    051    /**
    052     * Constructs a new instance. If {@code dir} is null, then use the default temporary-file directory.
    053     *
    054     * @param dir temporary folder, may be null, must exist if non-null.
    055     */
    056    public DefaultBackingStoreSupplier(final Path dir) {
    057        this.dir = dir;
    058    }
    059
    060    @Override
    061    public ScatterGatherBackingStore get() throws IOException {
    062        final String suffix = "n" + storeNum.incrementAndGet();
    063        final Path tempFile = dir == null ? Files.createTempFile(PREFIX, suffix) : Files.createTempFile(dir, PREFIX, suffix);
    064        return new FileBasedScatterGatherBackingStore(tempFile);
    065    }
    066}