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