
org.apache.commons.compress.archivers.zip.ParallelScatterZipCreator Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.commons.compress.archivers.zip;
import org.apache.commons.compress.parallel.FileBasedScatterGatherBackingStore;
import org.apache.commons.compress.parallel.InputStreamSupplier;
import org.apache.commons.compress.parallel.ScatterGatherBackingStore;
import org.apache.commons.compress.parallel.ScatterGatherBackingStoreSupplier;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.Deflater;
import static java.util.Collections.synchronizedList;
import static org.apache.commons.compress.archivers.zip.ZipArchiveEntryRequest.createZipArchiveEntryRequest;
/**
* Creates a zip in parallel by using multiple threadlocal {@link ScatterZipOutputStream} instances.
*
* Note that this class generally makes no guarantees about the order of things written to
* the output file. Things that need to come in a specific order (manifests, directories)
* must be handled by the client of this class, usually by writing these things to the
* {@link ZipArchiveOutputStream} before calling {@link #writeTo writeTo} on this class.
*
* The client can supply an {@link java.util.concurrent.ExecutorService}, but for reasons of
* memory model consistency, this will be shut down by this class prior to completion.
*
* @since 1.10
*/
public class ParallelScatterZipCreator {
private final List streams = synchronizedList(new ArrayList());
private final ExecutorService es;
private final ScatterGatherBackingStoreSupplier backingStoreSupplier;
private final List> futures = new ArrayList<>();
private final long startedAt = System.currentTimeMillis();
private long compressionDoneAt = 0;
private long scatterDoneAt;
private static class DefaultBackingStoreSupplier implements ScatterGatherBackingStoreSupplier {
final AtomicInteger storeNum = new AtomicInteger(0);
@Override
public ScatterGatherBackingStore get() throws IOException {
final File tempFile = File.createTempFile("parallelscatter", "n" + storeNum.incrementAndGet());
return new FileBasedScatterGatherBackingStore(tempFile);
}
}
private ScatterZipOutputStream createDeferred(final ScatterGatherBackingStoreSupplier scatterGatherBackingStoreSupplier)
throws IOException {
final ScatterGatherBackingStore bs = scatterGatherBackingStoreSupplier.get();
// lifecycle is bound to the ScatterZipOutputStream returned
final StreamCompressor sc = StreamCompressor.create(Deflater.DEFAULT_COMPRESSION, bs); //NOSONAR
return new ScatterZipOutputStream(bs, sc);
}
private final ThreadLocal tlScatterStreams = new ThreadLocal() {
@Override
protected ScatterZipOutputStream initialValue() {
try {
final ScatterZipOutputStream scatterStream = createDeferred(backingStoreSupplier);
streams.add(scatterStream);
return scatterStream;
} catch (final IOException e) {
throw new RuntimeException(e); //NOSONAR
}
}
};
/**
* Create a ParallelScatterZipCreator with default threads, which is set to the number of available
* processors, as defined by {@link java.lang.Runtime#availableProcessors}
*/
public ParallelScatterZipCreator() {
this(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));
}
/**
* Create a ParallelScatterZipCreator
*
* @param executorService The executorService to use for parallel scheduling. For technical reasons,
* this will be shut down by this class.
*/
public ParallelScatterZipCreator(final ExecutorService executorService) {
this(executorService, new DefaultBackingStoreSupplier());
}
/**
* Create a ParallelScatterZipCreator
*
* @param executorService The executorService to use. For technical reasons, this will be shut down
* by this class.
* @param backingStoreSupplier The supplier of backing store which shall be used
*/
public ParallelScatterZipCreator(final ExecutorService executorService,
final ScatterGatherBackingStoreSupplier backingStoreSupplier) {
this.backingStoreSupplier = backingStoreSupplier;
es = executorService;
}
/**
* Adds an archive entry to this archive.
*
* This method is expected to be called from a single client thread
*
*
* @param zipArchiveEntry The entry to add.
* @param source The source input stream supplier
*/
public void addArchiveEntry(final ZipArchiveEntry zipArchiveEntry, final InputStreamSupplier source) {
submit(createCallable(zipArchiveEntry, source));
}
/**
* Adds an archive entry to this archive.
*
* This method is expected to be called from a single client thread
*
*
* @param zipArchiveEntryRequestSupplier Should supply the entry to be added.
* @since 1.13
*/
public void addArchiveEntry(final ZipArchiveEntryRequestSupplier zipArchiveEntryRequestSupplier) {
submit(createCallable(zipArchiveEntryRequestSupplier));
}
/**
* Submit a callable for compression.
*
* @see ParallelScatterZipCreator#createCallable for details of if/when to use this.
*
* @param callable The callable to run, created by {@link #createCallable createCallable}, possibly wrapped by caller.
*/
public final void submit(final Callable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy